PDA

View Full Version : receive ASCII with usart



harryweb
- 6th February 2004, 14:58
HI,

I'm trying to use the USART of a 16f876. I have a problem

I need to receive more than one character...
By example: I can receive "B 01234" or "F 65495" or " F +"....

I have no problem to receive the 1st character (with the sample melabs prog given) but I can't find any example for 3 to 7 characters.

I can't use the serin2 command !

If someone can help !

Regards

Darrel Taylor
- 7th February 2004, 08:26
Hi harryweb,

There can be any number of things to consider when receiving data with the USART. But if the first 2 examples you presented were the normal sequence of bytes being received, something like this might work.


B1 var byte ' First character
B2 var byte ' space
W1 var word ' Value received

loop:
HserIn [B1]
if (B1 <> "B") and (B1 <> "F") then loop
HserIn [B2, DEC5 W1]
Now B1 holds the first char, and W1 has the value (0-65535)

Of course, this assumes that the data stream is always in the same format. If it differs from packet to packet, you'll need to think harder about how to capture all the variations. And then there's timeouts you can add, in case something goes wrong, the program can still go on working, instead of just sitting and waiting for data that will never come.


Darrel

harryweb
- 9th February 2004, 09:16
Thank you DARREL
The problem is also:

I don't know when something could be received and my program can't wait for a character. (It has something else to do)
If some characters arrive when my prog is not ready to receive, (out of a timeout) these characters are lost ?

Regards

HERVE

Darrel Taylor
- 9th February 2004, 09:57
Well, then it sounds like it's time for an interrupt driven serial receive buffer.

Here's one example from Tim Box, using INSTANT INTERRUPTS. It might be a good start for you. Check out the TXT files in the zip for a description of how to use them.

http://www.pbpgroup.com/files/INT_CTRL.zip