CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

How to check incoming data in a software UART

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Rumple



Joined: 18 Aug 2008
Posts: 2

View user's profile Send private message

How to check incoming data in a software UART
PostPosted: Mon Sep 29, 2008 9:22 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Sep 29, 2008 10:20 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Oct 01, 2008 10:38 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Oct 01, 2008 12:36 pm     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group