Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] pic16f877a uart not working

Status
Not open for further replies.

navintiwari08

Newbie level 2
Joined
Jul 27, 2013
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
20
hello there!
I'm trying to communicate with PC using pic16f877a's uart.. I've written a code in HITECH C.. but it doesn't seem to work.. pls help.. I'm using 20 MHz crystal and 9600 bauds..
the code is given below..


Code:
#include <htc.h>
#define _XTAL_FREQ 20000000
__CONFIG(FOSC_HS&LVP_OFF&CP_OFF&BOREN_OFF&PWRTE_OFF&WRT_OFF&WDTE_OFF);
unsigned char x;
void main()
{
    TRISC6 = 1;
    TRISC7 = 1;
    BRGH = 1;
    SYNC = 0;

    SPEN = 1;
    SPBRG = 129; //9600 baud with 20MHz
    TXIE = 1;
    RCIE = 1;
    GIE = 1;
    PEIE = 1;
       CREN = 1;
    TXEN = 1;
    while(1);
    
}

void interrupt ISR(void)
{
    if(RCIE && RCIF)
    {    
        x = RCREG;
        if(TXIF)
            TXREG = x;
            __delay_ms(100);
    }
}
 

Remove delay instruction from ISR. RCIF needs to be cleared in ISR.
 

On most PICs the RCIF is cleared by reading RCREG. Agreed - any delay in an ISR is bad news, it serves no useful purpose so take it out.

Brian.
 

I am not starting a new thread. My question is also related to this. here is my UART code
Code:
#ifndef _XTAL_FREQ
 #define _XTAL_FREQ 4000000
#endif


char UART_Init(const long int baudrate)
{
	unsigned int x;
	x = (_XTAL_FREQ - baudrate*64)/(baudrate*64);
	if(x>255)
	{
		x = (_XTAL_FREQ - baudrate*16)/(baudrate*16);
		BRGH = 1;
		SPBRG = x;
	 	SYNC = 0;
	    SPEN = 1;
        TRISC7 = 1;
        TRISC6 = 1;
        CREN = 1;
        TXEN = 1;
	    return 1;
	}
	if(x<256)
	{
	  BRGH=0;
	  SPBRG = x;
	  SYNC = 0;
	  SPEN = 1;
      TRISC7 = 1;
      TRISC6 = 1;
      CREN = 1;
      TXEN = 1;
	  return 1;
	}
	return 0;
}

char UART_TX_Empty()
{
  return TRMT;
}

char UART_Data_Ready()
{
   return RCIF;
}
char UART_Read()
{
 
  while(!RCIF);
  return RCREG;
}

void UART_Read_Text(char *Output, unsigned int length)
{
	int i;
	for(int i=0;i<length;i++)
		Output[i] = UART_Read();
}

void UART_Write(char data)
{
  while(!TRMT);
  TXREG = data;
}

void UART_Write_Text(char *text)
{
  int i;
  for(i=0;text[i]!='\0';i++)
	  TXREG=text[i];
}


I am just calling UART_Write_text function and passing a char string but it doesnt seems to work. Controller does send something but it is sending some random character. What is the possible error here.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top