Serial communication problem with p16f877a

General discussion on mikroC.
Post Reply
Author
Message
vaggelis_86
Posts: 6
Joined: 24 Oct 2010 00:39

Serial communication problem with p16f877a

#1 Post by vaggelis_86 » 24 Oct 2010 15:45

Hi i'am in the begining of a project and i am new in pic programming and i am having some trouble in serial communication.I try to write a simple code to see how it works but even the code is so simple when i open the USART terminal it don't work right. The idea is, when i send to the pic (whits it's on the Easypic 5) the letter 'A', send me back the letter 'A' and the word "HELLO", otherwise just the letter i have just send. Here is the code in MicroC.. Sorry about my english..

//variable declaration

unsigned short record;

//main function

void main()
{
TRISC=0x80; // pin 7 of PORTC input (RX) pin 6 of PORTC output (TX)
TXSTA=0x24; // TX9=0 (8 data bits) , TXEN=1 (activate) ,BRGH=1(High speed)
RCSTA=0x90; // SREN=1 (activate) , CREN=1 (continius receive)
SPBRG=0x19; // baude rate 9600 , crystall 4MHz

while(1)
{
if (PIR.F5=1) // if something received from the serial port
{
record=RCREG;
TXREG=record;
delay_ms(500);
if(record=='A')
{
TXREG='H';
delay_ms(500);
TXREG='E';
delay_ms(500);
TXREG='L';
delay_ms(500);
TXREG='L';
delay_ms(500);
TXREG='O';
delay_ms(500);
}
} end of 2nd if
} //end of 1st if
}//end of main

It dont work right, it sends me back wrong letters..CAN anyone HELP me??
For beggining i don't use interupts, as i read that the RCIF,TXIF flag change their condition by it shilf..

Thanks by advance!!

KaranSoin
Posts: 130
Joined: 07 May 2010 22:27
Location: Melbourne, Australia

Re: Serial communication problem with p16f877a

#2 Post by KaranSoin » 25 Oct 2010 10:43

Change

Code: Select all

if (PIR.F5=1) // if something received from the serial port
to

Code: Select all

if (PIR.F5 == 1)

Quite a common mistake


cheers

Post Reply

Return to “mikroC General”