Hi S,Makes no sense at all. If RCIF=1, then it can never be cleared until something reads RCREG, so it will never be =0.Code:If PIR1.RCIF = 1 Then nxt_rxin: WaitMs 10 '??????????????????????????????????????????????? If PIR1.RCIF = 0 Then Goto nxt_rxin
Also, waiting 10mS means you will miss 10 characters at 9600 baud, and get an overflow error right away. Never use a "wait" in a UART receive loop, just check the RCIF flag. You do not have time to waste with "WaitMS" commands, characters will be coming in every 1mS at 9600 baud.
I would write it something like:
This will read one (possibly two) characters and then exit the loop. It will not read the entire string in one shot, because RCIF =0 for most of the time when the program is running. You may have to loop back to the first IF forever, and only exit this loop when you get the EOL. However, this may "block" if you get an OERR, since you do not check it within this read loop. Hence, you may have to loop back to the OERR if statement at the top and loop forever until you get your EOL.Code:If PIR1.RCIF = 1 Then 'Means there is a character ready to be read nxt_rxin: Hserin char If char = "?" Or char = 0x0a Then Goto msg_eol If char = "$" Then str1(0) = "$" 'CHAR = $ rxi = 0 Endif If str1(0) = "$" Then str1(rxi) = char rxi = rxi + 1 'Goto nxt_rxin 'No, not correct, you should not go back unless RCIF=1. Remove this line If rxi > 79 Then Hserout " OVER STR1 LIMIT", CrLf 'Over STR1() limit rxi = 0 Endif Endif If PIR1.RCIF = 1 Then Goto nxt_rxin 'Change to =1 !!! to see if there is any more characters 'If not, you fall through and exit IF loop, as there are no more characters to read Endif
What happens when you get a "?", and branch to msg_eol, you may need to still check RCIF flag to make sure the receive buffer (RCREG) is emptied, though this may not be too critical at this point if yoiu assume you have an entire message. Any further characters will trigger an overflow most likely, and you will pick that up at the start where you check OERR
When you say "makes no sense" note, that the CODE #118 is showing results, in the SIM.
Here is a Screen shot at the point of RCIF = 0, where your CODE is checking for a [1 ] and goes round the full MAIN program LOOP.. At this point my CODE waits at HSERIN CHAR until the RCIF changes to [ 1 ] for a fraction of a second, in the mini LOOP around HSERIN CHAR. All this is inside an [ If PIR1.RCIF = 1 ] LOOP.
EDIT: The WAIT in my CODE has ????????????? following it. I add odd marks, when I try temporary tests, to remind me to remove them.
C
Attachments
-
992.9 KB Views: 2
Last edited: