uart communication with pic16f886

uart communication with pic16f886

Postby ajitnayak87 » Tue Nov 28, 2017 1:46 pm

I am using PIC16F886 with 11.059Mhz crystal. I have tested below code with PIC18F24K40 its working fine with 8Mhz internal oscillator.
When i test below code with 11.059 crystal oscilator i am getting garbage value.
Since both pin configration are equal i dont know why it give garbage value

Code: Select all
#include <htc.h>
#include <stdio.h>
#include<pic.h>
#include<stdint.h>
#include "delay.h"
//#define _XTAL_FREQ 11.059e6
unsigned int i=0;


#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(TRMT==0);

}


void Send_string_uart1(const unsigned char *string)
{
    unsigned char i = 0;

    do {
        TXREG = string[i++];
        while(TRMT==0);
    } while (string[i] != '\0');
}

char Serial_Receive_byte()
{
   
   while(0==PIR1bits.RCIF);
    return RCREG;
   
}

char usart_RxString()
{

   while(*str !='\0')
   { // 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 Timer1_Interrupt()
{
               
 INTCON = 0b00000000;
 PIE1=0b00000001;
 PIR1=0x01;
 TMR1H=0x0B;
 TMR1L=0xDC;
T1CON=0x31;
                // T1CON=0x01;
}


void Init_Controller()
 {
  cnt=100;
   TRISC = 0x80; //RX port configured as input
    PORTC = 0x80;
    TRISB=0X00;
    PORTB = 0X00;
   TRISA=0X00;
    PORTA=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)
    {
     TMR1H=0xF8;
     TMR1L=0xE8;
    TMR1IF=0;
     Dgt++;
   {
    LED =! LED;
   }
  }
                                     
  }



void Serial_1_Init()
{
                //SPBRG1=103; // Baud Rate 9600
                //SPBRG1=51; // Baud Rate 19200
                SPBRGH= 0x00; // 9600 Baud Rate for 64Mhz
                SPBRG = 0x17;
                TXSTA=0b00100010; // Low Speed mode
                RCSTA=0b10010000;
                BAUDCTL=0b01001000; //16bit baudrate has used
// RCIE=1;
// RCIF=0;
//CCP1IE=1;
}




void UART_Init(int baudRate)
{
    TRISC=0x80; // Configure Rx pin as input and Tx as output
                TXSTA=0b00100010; // Low Speed mode
                RCSTA=0b10010000;
    SPBRG = (11059200UL/(long)(64UL*baudRate))-1;
                RCIE=1;
// RCIF=0;
//CCP1IE=1;
}

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 main(void)
  {

             Init_Controller();
             Timer1_Interrupt();
            Serial_1_Init();
         // UART_Init(9600);
           GIE=1;
           PEIE=1;
           TMR1IE=1;
  unsigned char c=0;
                while(1)
                {
    Send_string_uart1("Welcome back");
// Serial_1_Send_byte(1);
//UART_TxChar(1);
DelayMs(1000);
                }


}






ajitnayak87
 
Posts: 3
Joined: Tue Aug 01, 2017 1:37 pm

Re: uart communication with pic16f886

Postby ric » Tue Nov 28, 2017 10:17 pm

Code: Select all
void Serial_1_Init()
{
                SPBRGH= 0x00; // 9600 Baud Rate for 64Mhz
                SPBRG = 0x17;
                TXSTA=0b00100010; // Low Speed mode
                RCSTA=0b10010000;
                BAUDCTL=0b01001000; //16bit baudrate has used
}

The datasheet tells you to use decimal 71 in SPBRG to get 9600 at 11.0692MHz with BRGH=0 and BRG16=1,
but you are using 0x17 which is decimal 23. That will give 28800 baud.

You seem to have two versions of each routine.
Your UART_TxChar() is better than your Serial_1_Send_byte() routine, but it is pointlessly trying to clear TXIF.
That is a read-only flag. The only way to clear it is by writing to TXREG, which is the next thing you do anyway.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 433
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products


Return to SCI/USART/EUSART

Who is online

Users browsing this forum: No registered users and 1 guest