It doesn't really matter if you're using HSERIN or SERIN2 with the WAIT option. Both are
going to stall and WAIT for the character you're WAITing for before moving on.
The hardware UART on most PICs has a 2-byte FIFO first-in, first-out buffer that will
buffer up to 2-bytes. This doesn't require any software over-head. These 2-bytes of data
are automatically stashed in the buffer, in the background, by the hardware UART.
Just enable the UART for continuous receive, test RCIF for new characters, check for over-
runs, etc.
Below is a simple example.
With HSERIN or SERIN2 and the WAIT modifier, you would be stuck in a continuous loopCode:' ** // PIC16F877A, 20MHz crystal ' ** // Buffers up to 80 characters using the hardware ' ** // USART. Buffering ends when the EOM "~" character ' ** // is received. ' ** // Uses RCIF bit polling for new characters DEFINE OSC 20 SPBRG = 129 ' 20MHz=129, 4MHz=25 to set baud rate at 9600 RCSTA = $90 ' Enable USART receive TXSTA = $24 ' Set USART parameters GP VAR byte ' GP variable BytesIn VAR byte[80] ' Up to 80 bytes ByteCnt VAR byte ' Indicates number of bytes in buffer EOM CON "~" ' EOM = "End Of Message" marker OERR VAR RCSTA.1 ' Alias USART over-run bit CREN VAR RCSTA.4 ' Alias USART continuous receive enable bit RCIF VAR PIR1.5 ' Alias USART received character interrupt flag bit INTCON = 0 ' Disable interrupts ByteCnt = 0 ' Zero counter ADCON1 = 7 ' A/D off, all digital Main: IF RCIF THEN ' If RCIF=1 there's a new character in RCREG BytesIn[ByteCnt]=RCREG ' Yes. Store new char in array IF BytesIn[ByteCnt]=EOM THEN OutMsg ' End of message? ByteCnt=ByteCnt+1 ' No. Increment array index pointer IF ByteCnt >=80 THEN OutMsg ' Test for software buffer over-run ENDIF IF !OERR THEN Main ' If OERR=0 no over-run CREN = 0 ' Over-run so disable receive CREN = 1 ' Re-enable & clear OERR "over-run" flag GOTO Main OutMsg: HSEROUT [13,10,dec ByteCnt," bytes received",13,10] HSEROUT [str BytesIn\ByteCnt,13,10] ' Echo characters received FOR GP=0 to ByteCnt ' Clear array bytes 0 to ByteCnt BytesIn[GP]=0 ' NEXT ' ByteCnt=0 ' Reset index pointer back to first element WHILE RCIF ' Trash anything received after the EOM marker GP=RCREG ' by reading RCREG until RCIF is cleared. WEND GOTO Main END
waiting for your special character to show up. Of course, you can also use the TIMEOUT
LABEL options to exit HSERIN or SERIN2, but if there's noise on the RX pin, the timeout
period gets reset, and you're stuck again in a nasty continuous loop.
Even if the timout/label option works, you're still wasting a boat-load of instruction cycles
doing absolutely nothing.
By just monitoring flag bits, and doing a little house-keeping, you have tons of time to do
other things while the hardware UART buffers inbound data for you.
That's the big difference. Even a command like HSERIN, that uses a hardware peripheral
like the UART, can slow things down. Learning how to use the hardware by itself opens up
a lot of options you don't have when using a software command to control the hardware.
Another example is HPWM. You have limitations with the HPWM command that you don't
when you learn how to setup/use the hardware yourself.





Bookmarks