How to receive stream of bytes using PIC USART


Results 1 to 35 of 35

Threaded View

  1. #4
    Join Date
    Jun 2009
    Posts
    12


    Did you find this post helpful? Yes | No

    Default

    No, I am reading data from a SpO2 module. The module sends out 5 bytes in sequence continuously. Among the bytes sent out from module, I only need to extract two of them which provide the information of the SpO2 and pulse rate. I used software UART to communicate with the SpO2 module while hardware UART to communicate with PC UART. When I connect the SpO2 directly to the PC, the data is correct. However, when I connect the SpO2 module and PC through PIC16F877, the data all mess up. I think it's probably because of the insufficient buffer in the PIC. I never handle such problem and I failed to solve the problem after trying for the whole day. What can I do to solve this problem?

    Here is the PIC code written in PICBasic Pro:
    Code:
    INCLUDE "modedefs.bas"
    DEFINE LOADER_USED 1        
    DEFINE OSC 20 
    B0 VAR BYTE
    B1 VAR BYTE
    B2 VAR BYTE
    B3 VAR BYTE
    B4 VAR BYTE
    B5 VAR BYTE
    B6 VAR BYTE
    B7 VAR BYTE
    B8 VAR BYTE
    PR VAR BYTE
    SPO2 VAR BYTE
    ID_2 VAR BYTE
    ID_1 VAR BYTE
    ID_0 VAR BYTE
    
    'PB.7 = Rx
    TRISB = %10000000 
    
    'data acquisition ID
    ID_2 = "0"
    ID_1 = "1"
    ID_0 = "1"
    
    'request from PC, 9600 baud rate
    standby:
    SerIn PORTC.7,6,["P",ID_2, ID_1, ID_0]
    
    loop:
    '4800 baud rate, 
    SerIn2 PORTB.7,16572,[B0, B1, B2, B3, B4, B5, B6, B7, B8]
    
    ' detect synchorous bit (the header byte in 5 bytes data)
    IF B0 > 127 Then  
    GoTo b0_true
    EndIF
    
    IF B1 > 127 Then 
    GoTo b1_true
    EndIF
    
    IF B2 > 127 Then 
    GoTo b2_true
    EndIF
    
    IF B3 > 127 Then 
    GoTo b3_true
    EndIF
    
    IF B4 > 127 Then 
    GoTo b4_true
    EndIF
    
    GoTo loop
    
    ' extract PR and SpO2 data
    b0_true:
    IF B2 > 63 Then 
        B2=64
    Else
        B2=0
    EndIF
    B3=B3+B2
    PR=B3
    SPO2=B4
    GoTo send_data
    
    b1_true:
    IF B3 > 63 Then 
        B3=64
    Else
        B3=0
    EndIF
    B4=B4+B3
    PR=B4
    SPO2=B5
    GoTo send_data
    
    b2_true:
    IF B4 > 63 Then 
        B4=64
    Else
        B4=0
    EndIF
    B5=B5+B4
    PR=B5
    SPO2=B6
    GoTo send_data
    
    b3_true:
    IF B5 > 63 Then 
        B5=64
    Else
        B5=0
    EndIF
    B6=B6+B5
    PR=B6
    SPO2=B7
    GoTo send_data
    
    b4_true:
    IF B6 > 63 Then 
        B6=64
    Else
        B6=0
    EndIF
    B7=B7+B6
    PR=B7
    SPO2=B8
    GoTo send_data
    
    'send to PC, 9600 baud rate
    send_data:
    SerOut PORTC.6,6,[ID_2, ID_1, ID_0, "^P^", #PR, "^", #SPO2]
    
    GoTo standby
    
    End
    Last edited by ScaleRobotics; - 28th November 2010 at 13:27. Reason: added code tags

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