I have put together an interrupt based serial buffer that allows the HW serial port to be buffered automatically. This way the "normal" processing can continue and the SW serial port is not affected.
This piece of code works on the 18F[24]5x as far as I have tested and does serial buffering. It operates in a circular array way.
When more then 65 characters are received, the first one will be overwritten and all 64 previous characters will be lost! So regular checking of the input buffer is still required.
It should be possible to implement this processing with buffers of sizes which are a power of 2 (2-4-8-16-32-64-128-256) without any problems. When using other sizes, makes sure that the generated code does not use the internal variables!!
Code is constructed in such way that no compiler variables are altered when in the interrupt routine. This way the code should not interfere with the "normal" run of the program.
Example Code
'Hardware serial port buffer
DIM BUFFER[64] AS BYTE
DIM BUFH AS BYTE
DIM BUFT AS BYTE
DIM DUM AS BYTE
ON_INTERRUPT goto INTERRUPT_ROUTINE 'hardware interrupt
GOTO OVER_INTERRUPT
INTERRUPT_ROUTINE:
CONTEXT SAVE 'Only needed for 16F devices
' INTERRUPT HANDLING WHEN HW USART RECEIVES A CHARACTER
IF PIR1.5 = 1 THEN
CLEAR PIR1.5
BUFFER[BUFT] = RCREG
INC BUFT
BUFT = BUFT & 63
ENDIF
CONTEXT RESTORE
OVER_INTERRUPT:
Main_Loop:
GOSUB INIT
' Other things to do...
' -------------------------------------------------------------
INIT:
HRSIN {1,main_loop}, DUM 'initiate the Hardware Serial Port
'if I don't do this, it does not work
PIE1.5=1 'settings for 18F458
INTCON=$C0
RETURN
ReadChar:
WHILE BUFT = BUFH:WEND
DUM= BUFFER[BUFH]
INC BUFH
BUFH = BUFH & 63
RETURN
DIM BUFFER[64] AS BYTE
DIM BUFH AS BYTE
DIM BUFT AS BYTE
DIM DUM AS BYTE
ON_INTERRUPT goto INTERRUPT_ROUTINE 'hardware interrupt
GOTO OVER_INTERRUPT
INTERRUPT_ROUTINE:
CONTEXT SAVE 'Only needed for 16F devices
' INTERRUPT HANDLING WHEN HW USART RECEIVES A CHARACTER
IF PIR1.5 = 1 THEN
CLEAR PIR1.5
BUFFER[BUFT] = RCREG
INC BUFT
BUFT = BUFT & 63
ENDIF
CONTEXT RESTORE
OVER_INTERRUPT:
Main_Loop:
GOSUB INIT
' Other things to do...
' -------------------------------------------------------------
INIT:
HRSIN {1,main_loop}, DUM 'initiate the Hardware Serial Port
'if I don't do this, it does not work
PIE1.5=1 'settings for 18F458
INTCON=$C0
RETURN
ReadChar:
WHILE BUFT = BUFH:WEND
DUM= BUFFER[BUFH]
INC BUFH
BUFH = BUFH & 63
RETURN


Menu
Recent Articles


Using PDS with SPI GLCD based on ST7565R Controller
Graphic LCDs based on the ST7565 are cheaper then GLCDs with other controllers. SPI requires only four pins. If the circuit