View previous topic :: View next topic |
Author |
Message |
Rumple
Joined: 18 Aug 2008 Posts: 2
|
How to check incoming data in a software UART |
Posted: Mon Sep 29, 2008 9:22 am |
|
|
Hello,
I am using a 16F877A and have configured 2 I/O pins to work as UART send and receive. I was wondering how to check whether any data is coming in or not.
I wondered whether I could use kbhit() for this purpose, but i am not sure.
Any suggestions?
Thanks
R |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Mon Sep 29, 2008 10:20 am |
|
|
That is what kbhit() is usually used for. Remember the software UART has NO buffering, not even a bit. So when a serial byte comes you must either be waiting in getc(), or poll with kbhit() often enough that you catch the early part of the Start bit. If the Start bit is missed the rest of the byte will be garbled. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Wed Oct 01, 2008 10:38 am |
|
|
In order to capture the chars arriving at the software Rx pin, it is necesary to poll that pin looking for the first edge of START bit.
As SherpaDoug said, that´s is what kbhit() is usually used for. Trying to capture the very first edge waiting in a loop (polling) is a senseless and overhead task. Polling is not the best way to use the microcontroller's resources, because at 9600 bauds, the START bit will long 104 usec and the polling should arrive at least once to sense this change, otherwise the 'char' is lost. This require a tight loop doing almost nothing but wasting time expecting a level change in the Rx pin.
A trick used in order to avoid polling is assigning the Rx pin of the software UART at the dedicated pin used as external interrupt (mostly RB0 in middle-range uC). Used in this way, the microcontroller generate an EXTernal INTerrupt with the START bit of the incoming characters. This way, it is more effective from the CPU use point of view because it is not necesary to stay in a loop waiting for an edge. Instead, once the external interrupt was fired, all you need to capture the char inside the interrumpt routine with the help of getc.
Humberto |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Wed Oct 01, 2008 12:36 pm |
|
|
A lot of the time my PICs are sitting waiting for a command and checking a few fault inputs something like this: Code: | while (!kbhit(){
if(input_pin(alarm_1))
State = 1, break;
if(input_pin(alarm_2))
State - 2, break;
}
If(State != 0)
Handle_Alarm(State);
else
Get_Command();
|
The PIC has nothing else to do so the while loop spins very fast. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
|