Serial input buffer size?


Closed Thread
Results 1 to 2 of 2
  1. #1
    Chris DeHut's Avatar
    Chris DeHut Guest

    Default Serial input buffer size?

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Toronto
    Posts
    109


    Did you find this post helpful? Yes | No

    Default

    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;&nb sp;&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;&nb sp;&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;&nb sp;&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;&n bsp;&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

Similar Threads

  1. EasyHID max usb buffer size
    By jizyjuice in forum USB
    Replies: 4
    Last Post: - 29th November 2007, 05:12
  2. Interrupt driven Buffered Serial Input
    By johnmiller in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 13th May 2007, 01:42
  3. Student Project
    By aggie in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 8th July 2006, 08:56
  4. Serial Keypad Input
    By OKST8 in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 28th April 2005, 23:49
  5. serial input buffer
    By vsingh in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 14th June 2004, 05:55

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts