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

    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.

  2. #2
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,170


    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

  3. #3
    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.

  4. #4
    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.

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


    Did you find this post helpful? Yes | No

    Default

    Dave, by 1 he means that the first bit of the first byte is "1" and the rest are first bit "0".

    So wait cannot be used just like that.

    unifoxz:
    What is the repeat rate of the bytes? 8 bytes at 4800 and then a pause of some seconds or milliseconds?

    You might just wait for the current transmission to finish, as you may have missed the begining of it, then wait for the pause state and grab the next 8 bytes.

    But wait, you now say it is 5 bytes? At your first post I noticed you tried to get 8 bytes of data, right?

    Ioannis

  6. #6
    Join Date
    Jun 2009
    Posts
    12


    Did you find this post helpful? Yes | No

    Default

    It's repeating 5 bytes. 5 bytes continued by another 5 bytes without pause, all are sent out continuously. In order to get the SpO2 byte and pulse rate byte which is at the 4th and 5th byte from the header byte (the byte with synchronous bit = 1), I need to read in 9 bytes as if the first byte I get is the 2nd byte of the 5 byte. So the header byte will be at the 5th byte and the SpO2 byte and pulse rate byte will be at the 8th byte and the 9th byte.

    It's like this:
    2, 3, 4, 5, 1 (header byte), 2, 3, 4 (pulse rate byte), 5 (SpO2 byte)

    All the data is in hex format, no specific 0-9-A-Z.

  7. #7
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,170


    Did you find this post helpful? Yes | No

    Default

    I cannot imagine a device keep sending a blast of bytes without any synchronization!

    Please give us a link for the data sheet or if small post it here.

    Ioannis

Similar Threads

  1. Replies: 6
    Last Post: - 31st August 2007, 10: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, 09:12
  3. 16F876 Usart Receive
    By syscoder in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 21st March 2007, 16:43
  4. Replies: 1
    Last Post: - 6th September 2005, 17:32
  5. Serial Pic to Pic using HSER
    By Chadhammer in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 12th March 2005, 00: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