Here's a simple routine I use for testing. It will buffer up to 80 bytes, and
print it after receiving the end of message ~ character.
Code:
DEFINE LOADER_USED 1
DEFINE OSC 20
DEFINE HSER_BAUD 9600
DEFINE HSER_CLROERR 1 ' Automatically clear over-run errors
DEFINE HSER_RCSTA 90h ' Enable USART receive
DEFINE HSER_TXSTA 24h ' TXSTA=%00100100. TX enable, BRGH=1 for high-speed
'DEFINE HSER_TXSTA 20h ' TXSTA=%00100000. TX enable, BRGH=0 for low-speed
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. Then store it in array
IF BytesIn[ByteCnt]=EOM THEN OutMsg
ByteCnt=ByteCnt+1 ' Increment array index pointer
ENDIF
GOTO Main
OutMsg:
HSEROUT [13,10,dec ByteCnt," bytes received",13,10]
HSEROUT [STR BytesIn\ByteCnt,13,10]
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 ' Keep reading until RCIF is clear to flush buffer
GP=RCREG ' after EOM is received
WEND
GOTO Main
END
Bookmarks