View previous topic :: View next topic |
Author |
Message |
RShankar
Joined: 01 Aug 2008 Posts: 2
|
Problem with serial communication |
Posted: Fri Aug 01, 2008 1:04 pm |
|
|
Hi everyone, I'm new at ccs compiler and now I've got a big problem with rs-232 communication. I can transmit from pic to pc but when i want to recieve from pc to pic I dónt know what I´m doing wrong. This is my code:
Code: | #include <16f877a.h>
#include "programa.h"
#fuses XT,NOWDT,NOPUT,NOBROWNOUT,NOPROTECT
#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
int value=0;
void main()
{
while(1)
{
if(kbhit())
{
putc('a');
delay_ms(1000);
}
}
} |
Pls I need help with this, what am I doing wrong? The result of this is that the PIC always sends de character "a" every second even if I don´t send something from the pc |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Aug 01, 2008 1:12 pm |
|
|
You have to actually remove the character from the hardware UART's
receive buffer. Otherwise it will stay there, in the buffer. kbhit()
does not remove the character. kbhit() only tells you if there is a
character in the buffer. Use getc() to read the character and remove
it from the buffer.
Two more things:
1. Add the ERRORS parameter to your #use rs232() statement.
Always do that. This will prevent the hardware UART receiver from
locking up if you don't read the incoming characters.
2. Add NOLVP to your #fuses statement. On recent versions of the
compiler it's done automatically for you. But if you have a slightly
older version, you probably need to add it. NOLVP will prevent your
PIC from locking up if the PGM pin (pin B3) goes to a high level.
NOLVP disables the "low voltage programming" feature of the 16F877A. |
|
|
RShankar
Joined: 01 Aug 2008 Posts: 2
|
|
Posted: Fri Aug 01, 2008 1:23 pm |
|
|
lol ok, first of all thanx for ur help. I finally made it work and noticed that I have to use getc to remove the character from the buffer as u said. The main problem was that I forgot to set PIN C7 as an input so this pin was full of noise and kbhit was always high. Thank u very much |
|
|
|