[PIC] PIC16f877A UART char read error

Status
Not open for further replies.

go ahead

Junior Member level 1
Joined
Aug 30, 2012
Messages
15
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,421
I am using PIC16F877A with the Bluetooth module (HC-05) in UART . problem is that when i get a char from UART and send it to PORTB then its shows some value in 0 and 1.

but i wanted to check, what char it is then its not working. :x
i have tried as a char like
if RCREG=='A' but its not working and when i tried from comparing by char's ASC codes but still its not working. in ASC i compare like if RCREG==0x41 (for A).

the task i wanted to perform simple task like-
if its gets A on UART then glow led on RB7 for some time than turn it off.
if its gets B on UART then glow led on RB6 for some time than turn it off and so on.
and my code is written in MPLAB.
thanks in advance :grin:

#include<htc.h>

#define _XTAL_FREQ 8000000

void Delay500Us()
{
unsigned char cnt500Us = 165;
while(--cnt500Us != 0) //
continue;

//100 msec Delay function
void Delay100ms()
{
unsigned char cnt100ms = 200; // 200 * 500 Usec = 100 msec

do
{
Delay500Us();
}while(--cnt100ms != 0);
}

//1 sec Delay function
void Delay1s()
{
unsigned char cnt1ms = 10; //10 * 100 msec = 1 sec

do
{
Delay100ms();
}while(--cnt1ms != 0);
}

char UART_Init()
{


SPBRG = 25; //Writing SPBRG register
SYNC = 0; //Selecting Asynchronous Mode
SPEN = 1; //Enables Serial Port
TRISC7 = 1;
TRISC6 = 0;
RCIE =1;
TX9= 0;
RX9= 0;
CREN = 1; //Enables Continuous Reception
TXEN = 1;
TXEN = 1; //Enables Transmission


}
char UART_Data_Ready()
{
return RCIF;
}

char UART_Read()
{
while(!RCIF);
return RCREG;
}
void UART_Write(char data)
{
while(!TRMT);
TXREG = data;
}

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

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




void main()
{
unsigned int temp;
TRISB = 0x00; //PORTB as Output
UART_Init();
Delay100ms(1);

while(1)
{
if(UART_Data_Ready())
{
temp= UART_Read(RCREG);



if(temp=='A')
{
RB0=0;
Delay100ms(10);
RB0=1;
}
if(temp=='B')
{
RB1=0;
Delay100ms(10);
RB1=1;
}
if(temp=='C')
{
RB2=0;
Delay100ms(10);
RB2=1;
}
if(temp=='D')
{
RB3=0;
Delay100ms(10);
RB3=1;
}

//PORTB = UART_Read();
//Delay100ms(10);
}
}
}
 

hello


Code:
temp= UART_Read(RCREG);

UART_Read don't need RCREG as parameter ! it' return RCREG

and UART_Read is allready waiting for RCIF==1

so use direct test

so
Code:
while(1)
{
 temp=UART_READ();
.. etc ....

at the end of tests
clear flags Frame FERR and OVER
 

You have to clear RCIF in UART_Read_Text() function. Add RCIF = 0; as last line of UART_Read_Text().
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…