How to receive stream of bytes using PIC USART


Closed Thread
Results 1 to 35 of 35

Hybrid View

  1. #1
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Here are several examples of serial in and out.
    http://www.melabs.com/resources/samples/pbp/ser2mod.bas

    Now for the
    What should I do to avoid the data from crashing in Rx buffer of PIC UART, provided I cannot control over the stream of bytes received?
    You are using software for serial so the only time anything gets into the PIC is when you ask for it. If you were using hardware then the buffers would need to be cleared.

    'send byte to PC
    SerOut2 PORTB.6,16572,[B0]
    Earlier you said the PC was running the port at 9600? Did you change it for the above test to 4800?

    And modifiers. The examples on the above link talks about DEC and such VS ASC||. You might need one.
    Dave
    Always wear safety glasses while programming.

  2. #2
    Join Date
    Jun 2009
    Posts
    12


    Did you find this post helpful? Yes | No

    Default

    Yes, I tried to set up a simple test to check what is actually happening on the bytes I receive. I use 4800 baud rate for both module and PC this time.

    You are using software for serial so the only time anything gets into the PIC is when you ask for it. If you were using hardware then the buffers would need to be cleared.
    I tried with PIC hardware UART to receive the bytes from module too but the result is the same. The data is not the one it should be.

    May I know how to clear hardware UART buffer? I tried to reset CREN and read in the data after the reset, it is not working as well. I am not sure whether it is the way to clear the buffer.

  3. #3
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    This automatically clears it, but then you can't read what it stuck on. But seeing as this data gets resent over and over, it really should be a problem. You should catch it on the next round.

    Code:
    DEFINE HSER_CLROERR 1                  'Auto Reset Buffer Overrun Errors
    Last edited by ScaleRobotics; - 14th June 2009 at 07:14.
    http://www.scalerobotics.com

  4. #4
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    If you do not want to setup all of the defines and want to change things around on the fly here is an example that I use for testing on a particular chip. Displays the serial input on an LCD. The RCSTA.4 part clears any overruns.

    Did you have a MAX232 or equivalent when you tried the hardware serial?
    Code:
    '******************************************
    '18F6680   02/14/09  INFEED PARSE TEST BAUD 9600
        DEFINE OSC 20
        @ __CONFIG    _CONFIG1H, _OSC_HS_1H
        @ __CONFIG    _CONFIG2H, _WDT_ON_2H & _WDTPS_128_2H
        @ __CONFIG    _CONFIG4L, _LVP_OFF_4L
        DEFINE LCD_DREG     PORTG 
        define LCD_DBIT     0
        DEFINE LCD_RSREG    PORTE 
        DEFINE LCD_RSBIT    0
        DEFINE LCD_EREG     PORTE 
        DEFINE LCD_EBIT     1
        DEFINE LCD_BITS     4 
        DEFINE LCD_LINES    4
        DEFINE LCD_COMMANDUS    3000 
        DEFINE LCD_DATAUS   150
      '###############################################
        PAUSE 100 : LCDOUT $FE,1,"TEST"
        N1 VAR LONG:N2 VAR LONG
        START: N1 = 0 : N2 = 0
        HIGH PORTG.4 :PAUSE 250:LOW PORTG.4
        RCSTA.4 = 0 : RCSTA.4 = 1
        'CHANGE LINE FEED AND CARRIAGE RETURN AS REQUIRED 
        RCSTA=$90:TXSTA=$24:SPBRG=129:HSERIN[WAIT($a),WAIT($d),DEC N1,WAIT(","),DEC N2]  
        LCDOUT $FE,1,DEC N1 : lcdout $FE,$C0,DEC N2 : GOTO START
    Dave
    Always wear safety glasses while programming.

  5. #5
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,132


    Did you find this post helpful? Yes | No

    Default

    What is the exact data sent from the SpO2 module? Is there any header that can be identified befor getting data?

    I mean does the module send out "Hey! Data coming" and then the measurements each time?

    If the measurements are sent out continusly without any header, then your PIC is reading at random times, confusing the data frames between them.

    You have to find a way to just wait for the start of each frame and then grab the next data.

    Ioannis

  6. #6
    Join Date
    Jun 2009
    Posts
    12


    Did you find this post helpful? Yes | No

    Default

    mackrackit: Did you have a MAX232 or equivalent when you tried the hardware serial?
    I don't use MAX232 for this circuit. I use a 22k resistor at Rx pin and 1k resistor at Tx pin. May I know is there any effect for not using MAX232? There is another question that I would like to know: can MAX3232 be used to replace MAX232?

    Ioannis: What is the exact data sent from the SpO2 module? Is there any header that can be identified befor getting data?
    The first byte of the 5 bytes data block starts with the synchronous bit of '1' while the other bytes start with bit '0'. This is the only identifier I could use to detect the the data bytes (SpO2 and pulse rate) I need.

    I tried to check the bytes sent from the module since the starting of the module operation. The data might be correct for the first few bytes but the continuous bytes are all wrong.

    My problem is similar to retransmitting a big file (in MB) through PIC. The file sent out from the PIC is not the same as the file before it was sent to the PIC. The PIC cannot handle a stream of continuous bytes and thus causing data went wrong.

  7. #7
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    I don't use MAX232 for this circuit. I use a 22k resistor at Rx pin and 1k resistor at Tx pin. May I know is there any effect for not using MAX232? There is another question that I would like to know: can MAX3232 be used to replace MAX232?
    Yes, I used a 232. From the PIC Hardware Serial to a PC some kind of level inverter is needed. The PICs sends in true mode while PCs use inverted. It is a hardware thing that can not be fixed in soft ware . That is why we have SERIN/2, bit banging a "virtual serial poer".

    I think the 3232 will also work but I have not used one to be sure about it.

    If the module is always sending a 1,then use that in the WAIT part, grab what you need after that. What are the possibilities for the remaining data? 0-9-A-Z?
    Dave
    Always wear safety glasses while programming.

Similar Threads

  1. Replies: 6
    Last Post: - 31st August 2007, 09:31
  2. USART Problem , but don't know where, in pc? or in PIC?
    By precision in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 15th July 2007, 08:12
  3. 16F876 Usart Receive
    By syscoder in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 21st March 2007, 15:43
  4. Replies: 1
    Last Post: - 6th September 2005, 16:32
  5. Serial Pic to Pic using HSER
    By Chadhammer in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 11th March 2005, 23:14

Members who have read this thread : 1

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