EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Topics / uart communication on pic16F886A

uart communication on pic16F886A

|

Microcontroller › PIC › uart communication on pic16F886A

  • This topic has 1 reply, 2 voices, and was last updated 7 years, 3 months ago by Abhishek.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • December 1, 2017 at 10:30 am #4701
    ajit
    Participant

    I am testing UART communication using 20Mhz crystal, 16Bit.Asyn mode.9600 baudarte.

    With below setting i am getting garbage value . kindly letme know if any mistake done 

     
     
    #include <htc.h>
    #include <stdio.h>
    #include<pic.h>
    #include<stdint.h>
    #include "delay.h"
    #define _XTAL_FREQ 20000000
    unsigned int i=0;   
      unsigned char c=0;
      unsigned char ch;
     
    #define SBIT_TXEN     5
    #define SBIT_SPEN     7
    #define SBIT_CREN     4
     
     
     
     
    #define LED_RX RC7     // Pin assigned RX LED
    #define LED_TX RC6    // Pin assigned TX LED
    #define LED RC2     // Pin assigned for LED 
    #define DE RC5
    #define RECEIVE     0
    #define TRANSMIT 1
    #define READ_REG     3
    #define WRITE_REG   6
    #define ILLEGAL_DATAVALUE 0x03
    #define FALSE   0
    #define TRUE 1
    #define METER_ID        1
    unsigned int j=0;
    unsigned char* str;
    unsigned int count = 0;
    char data = 0;
    unsigned char rxbuf[50];
    unsigned char ser_data[95];
    unsigned char crc_data[95];
    unsigned char Max_scroll = 0;
    unsigned char buff[10];
    volatile uint8_t index = 0, rec_flag = 0, Delay_count = 0, Id[10], Buffer_count = 0, Cal_count = 0, Disp_count = 0, inc = 0, One_sec_update = 0, Auto_scroll_count = 0;
    char data1[10];
    unsigned char buf[20];
     
     
    unsigned short int cnt, num,Dgt=0;
     
    unsigned int j;
    #define  Notes_Index 25
     
    unsigned int Toff_counter=0;
    unsigned int POn_Flag=1;
    unsigned int Poff_Flag=0;
     
     
     
     
    void Serial_1_Send_byte(uint8_t trbuf1) 
    {
     TXREG = trbuf1;
      while(TXIF==0);    // Wait till the transmitter register becomes empty
    }
     
     
    void Send_string_uart1(const unsigned char *string) 
    {
        unsigned char i = 0;
     
        do {
            TXREG = string[i++];
            while(!TXIF);    // Wait till the transmitter register becomes empty
        } while (string != '');
    }
     
    char Serial_Receive_byte()
    {
       
       while(!RCIF);
        return RCREG;
        
    }
     
    char usart_RxString()
    {
     
       while(*str !='')
       {            // while not end of string
          while(0==PIR1bits.RCIF);
           RCREG = *str;            // read next character 
           str++;                    // increment pointer to next character
       }
           return RCREG;    // return the contents of uart
    }
     
     
    void UART_TxChar(char ch)
    {
        while(TXIF==0);    // Wait till the transmitter register becomes empty
        TXIF=0;            // Clear transmitter flag
        TXREG=ch;          // load the char to be transmitted into transmit reg
    }
     
     
    char UART_RxChar()
    {
        while(RCIF==0);    // Wait till the data is received 
        RCIF=0;            // Clear receiver flag
        return(RCREG);     // Return the received data to calling function
    }
     
     
     
    void Timer1_Interrupt()
    {
     
    INTCON = 0b00000000;
    PIE1=0b00000001;
    PIR1=0x01;
    TMR1H=0x0B;
    TMR1L=0xDC; 
    T1CON=0x31;
        TMR1IE = 1;
    TMR1IF = 0;
    //  T1CON=0x01;
    }
     
     
    void Init_Controller()
    {
      cnt=100;
        TRISC  = 0x80; //RX port configured as input
    PORTC  = 0x80;
    TRISB=0X00;
    PORTB = 0X00;
    TRISA=0X00; 
        PORTA=0X00;
        ANSELH=0x00;
    ADCON0 = 0b00000000;
    ANSEL = 0b00000000;
    Timer1_Interrupt();
        LED=0;
     
    }
     
    //Below code make Ton and Toff time in equal Interval
     
     
    void interrupt isr(void)
    {
    if(TMR1IF==1)
    {
     
                        //EA60=45ms CB20 =107ms  
               TMR1H=0xEA; //load the time value(0x0BDC) for 100ms delay
    TMR1L=0x60; //Timer1 Interrupt for 65000
    TMR1IF=0; // Clear timer interrupt flag
           Dgt++;
               if(  Dgt>=10)
                      {
                       //  LED = !LED; 
                       
                      } 
                      
              
    }      
                  
      }  
     
     
     
     
     
    void Serial_1_Init()
    { 
     
     /* BAUDCTL=0b01001000; //16bit baudrate has used
    INTCON = 0b00000000;
        RCIE=1;
    RCIF=0;
         CCP1IE=1;
        RCSTA=0b10010000;
        SPBRG = 0x81;
        SPBRGH= 0x00; // 9600 Baud Rate for 64Mhz
    TXSTA=0b00100010; // Low Speed mode 
        */
      SPBRG = 32; //9600 baud rate 8Mhz
       RCSTA=0b10010000;
     RCSTA=0b10010000;
      TXEN  = 1;
      BRGH  = 0;
      TXIE  = 0;
      SYNC=0;
      RCSTA = 0;
      SPEN  = 1;
      RX9   = 0;
      CREN  = 1;
     
       
     
    }
     
     
    void EUSART_Initialize(void)
    {
        // disable interrupts before changing states
        RCIE = 0;
        TXIE = 0;
        BAUDCTL=0X09; //16bit baudrate has used
     
        // SPEN enabled; RX9 8-bit; CREN enabled; ADDEN disabled; SREN disabled; 
        RCSTA = 0x90;
     
        // TX9 8-bit; TX9D 0; SENDB sync_break_complete; TXEN enabled; SYNC asynchronous; BRGH hi_speed; CSRC slave; 
        TXSTA = 0x22;
     
        SPBRG=81;
     
    SPBRGH=00;
    RCIE = 1;
    }
     
     
    void main(void)
      {
     
                 Init_Controller();
                Serial_1_Init();
           //UART_Init(9600);
        /* GIE=1;
     PEIE=1;
     TMR1IE=1; */
     
       
     
    while(1)
    {
     
        Send_string_uart1("hello");
     
     
     
    }
     
     
    }
     
     
     
    December 27, 2018 at 8:13 am #14989
    Abhishek
    Participant

    you need to provide delays in ISR routines, otherwise this woon't work

  • Author
    Posts
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • Voltage comparator circuit verification April 10, 2026
  • Help with a coffee machine circuitboard April 10, 2026
  • DS3231 RTC + 24C32 EEPROM I²C Module (PIC18) April 9, 2026
  • Bot checks April 9, 2026
  • Heltec V3 GPS repeater. April 9, 2026

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2026 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise