PDA

View Full Version : Serial input buffer size?



Chris DeHut
- 26th January 2006, 20:12
Hi all,

Looking through the manual it appears that there is only a 2 character input buffer for serial communications. Is there a way of checking to see if something is in that buffer? Otherwise is there a way to increase the size of the buffer?

I will be communicating from a Window PC to the Micro at hopefully around 128,000 baud (40 Mhz processor). I need to move data around as fast as possible and I also need reliability.

Any comments or suggestions are greatly appreciated!

Chris

PJALM
- 26th January 2006, 22:23
The easiest and best thing to do is create a byte array as big as you want the buffer to be and save incoming data to the byte array before processing. You will also need to use the hardware USART and USART Interrupts to capture the incoming data to save to the array.

Here is an example:

<code>
DEFINE HSER_BAUD&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;57600
DEFINE HSER_RCSTA&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;90h
DEFINE HSER_TXSTA&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;20h
DEFINE HSER_CLROERR&nbsp;&nbsp;&nbsp;&nbsp;1

Buffer_Size CON 8&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Total size for serial buffer
Data_Buffer VAR BYTE[Buffer_Size]&nbsp;&nbsp;' Buffer to hold incoming serial data
B0 VAR BYTE

INTCON.7 = 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Enable Global Interrupt Handler
PIE1.5 = 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Enable USART Receive Interrupt

Bytes_Received = 0

ON INTERRUPT GOTO InterruptHandler
ENABLE INTERRUPT

Main_Loop:
&nbsp;&nbsp;'Put code here to check the buffer and process the data when full
&nbsp;&nbsp;'Set Bytes_Received to 0 once the data has been processed and clear the buffer
&nbsp;&nbsp;GOTO Main_Loop

'------------------------------------------------------------------------------------
' Interrupt Handler
'------------------------------------------------------------------------------------
DISABLE INTERRUPT ' No interrupts past this point
InterruptHandler:

' USART Receive Interrupt
IF PIR1.5 = 1 THEN
&nbsp;&nbsp;HSERIN [B0]

&nbsp;&nbsp;Bytes_Received = Bytes_Received + 1
&nbsp;&nbsp;Data_Buffer(Bytes_Received) = B0

&nbsp;&nbsp;PIR1.5 = 0
ENDIF

RESUME&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Return to main program
ENABLE INTERRUPT

</code>


Now this is just a simple example of buffering but it can be expanded for what ever purpose you need it for. You will have to add some code to the main program loop to check when the buffer if full and process it then set the Bytes_Received back to 0.

I have never tried it beyond 57600 baud but it should work fine.

I hope this helps :)