Menu

Serial Communication

Help
Kaka
2016-01-28
2016-01-29
  • Kaka

    Kaka - 2016-01-28

    Hi Guys,
    I am new user of Great Cow. I am trying to communicate 2 MCUs. The process is first MCU will send data to second MCU; this will do some calculations on received data and send back to first MCU, it will verify this data. That’s it.
    The problem is when first MCU receive calculated data from second MCU it’s not the data sent by second MCU. Please tell me what i am doing wrong.

    First MCU Code

    ;Chip Settings
    #chip 16F887,8
    
    ;Defines (Constants)
    #define LCD_IO 4
    #define LCD_RS PORTB.2
    #define LCD_NO_RW 1
    #define LCD_Enable PORTB.3
    #define LCD_DB4 PORTB.4
    #define LCD_DB5 PORTB.5
    #define LCD_DB6 PORTB.6
    #define LCD_DB7 PORTB.7
    
    #define SerialOut       PORTC.6 'Pin 25
    #define SerialIn        PORTC.7 'Pin 26
    
    #define USART_BLOCKING
    #define USART_BAUD_RATE 9600
    #define USART_DELAY 1 ms
    
    ;Variables
    Dim Temp As Byte
    Dim cTemp1 As word
    Dim cTemp2 As word
    Dim String as word
    
    
    cTemp1 = 45
    
    Cls
    Print "Sending:"
    Print cTemp1
    Print " "
    cTemp2 = cTemp1 + 5
    cTemp2 = cTemp2 / 2
    cTemp2 = cTemp2 + 2
    
    HSerPrint Chr(cTemp1)
    Wait Until USARTHasData
    Temp = HSerReceive
    Locate 1,0
    Print "Reciving:"
    Print Temp
    

    Second MCU Code

    ;Chip Settings
    #chip 16F887,8
    
    ;Defines (Constants)
    #define LCD_IO 4
    #define LCD_RS PORTB.2
    #define LCD_NO_RW 1
    #define LCD_Enable PORTB.3
    #define LCD_DB4 PORTB.4
    #define LCD_DB5 PORTB.5
    #define LCD_DB6 PORTB.6
    #define LCD_DB7 PORTB.7
    
    #define SerialOut       PORTC.6 'Pin 25
    #define SerialIn        PORTC.7 'Pin 26
    
    #define USART_BLOCKING
    #define USART_BAUD_RATE 9600
    #define USART_DELAY 1 ms
    
    ;Variables
    Dim Temp As Byte
    
    Wait Until USARTHasData
      Temp = HSerReceive
    
      Print "Reciving:"
      Print Temp
      HSerPrint Temp
    
      Temp = Temp + 5
      Temp = Temp / 2
      Temp = Temp + 2
    
      HSerPrint Chr(Temp)
      Locate 1,0
      Print "Sending:"
      Print Temp
    
     

    Last edit: Kaka 2016-01-28
  • mmotte

    mmotte - 2016-01-28

    I tried your "calculator" chip program and had trouble with the CHR().in your line"HSerPrint Chr(Temp)". Just use" HSerPrint Temp" because you don't need to convert to ascii.using the chr command.

    Also "Wait Until USARTHasData" is redundant because you have defined "#define USART_BLOCKING" above and that takes care of waiting for the next received byte.You don't need the wait statement.

    GL
    Mike

     
  • Kaka

    Kaka - 2016-01-29

    Thanks for reply mmotte, as you said i tried the following code. but no success. after removing chr command second MCU is getting 52 instead of 45.

    MCU1

    ;Chip Settings
    #chip 16F887,8
    
    ;Defines (Constants)
    #define LCD_IO 4
    #define LCD_RS PORTB.2
    #define LCD_NO_RW 1
    #define LCD_Enable PORTB.3
    #define LCD_DB4 PORTB.4
    #define LCD_DB5 PORTB.5
    #define LCD_DB6 PORTB.6
    #define LCD_DB7 PORTB.7
    
    #define SerialOut       PORTC.6 'Pin 25
    #define SerialIn        PORTC.7 'Pin 26
    
    #define USART_BLOCKING
    #define USART_BAUD_RATE 9600
    #define USART_DELAY 1 ms
    
    ;Variables
    Dim Temp As Byte
    Dim cTemp1 As word
    Dim cTemp2 As word
    Dim String as word
    
    cTemp1 = 45
    
    Cls
    Print "Sending:"
    Print cTemp1
    Print " "
    cTemp2 = cTemp1 + 5
    cTemp2 = cTemp2 / 2
    cTemp2 = cTemp2 + 2
    
    HSerPrint cTemp1
    Temp = HSerGetString
    Locate 1,0
    Print "Reciving:"
    Print Temp
    
    If cTemp1 = cTemp2 Then
            Print "OK"
    Else
            Print "NOT OK"
    End If
    

    MCU2

    ;Chip Settings
    #chip 16F887,8
    
    ;Defines (Constants)
    #define LCD_IO 4
    #define LCD_RS PORTB.2
    #define LCD_NO_RW 1
    #define LCD_Enable PORTB.3
    #define LCD_DB4 PORTB.4
    #define LCD_DB5 PORTB.5
    #define LCD_DB6 PORTB.6
    #define LCD_DB7 PORTB.7
    
    #define SerialOut       PORTC.6 'Pin 25
    #define SerialIn        PORTC.7 'Pin 26
    
    #define USART_BLOCKING
    #define USART_BAUD_RATE 9600
    #define USART_DELAY 1 ms
    
    ;Variables
    Dim Temp As Byte
    
    Temp = HSerReceive
    
    Print "Reciving:"
    Print Temp
    ;HSerPrint Temp
    
    Temp = Temp + 5
    Temp = Temp / 2
    Temp = Temp + 2
    
    HSerPrint Temp
    Locate 1,0
    Print "Sending:"
    Print Temp
    

    i am attaching Proteus file so you can test it your self. Thank you so much.

     

    Last edit: Kaka 2016-01-29
  • William Roth

    William Roth - 2016-01-29

    Hi,

    When sending serial data between MCU's it is probably a good idea to send/receive the data in its raw format. What I mean is NOT in Ascii format. This means using Hserout and HserReceive rather than Hserprint, Hsergetstring, etal

    When troubleshooting code, I generally reduce the code to its most simple terms as a starting point.

    In testing, I had trouble using the sub < HserReceive1 data > This did not return a correct value. However the function < data = HserReceive1 > Works OK. We need to look into why the sub does not work correctly.

    Here is code that tested to work well. You will need to change Pins, etc as required by your chips

    Sending Code:

    #chip 18F25K22, 16
    #config OSC = INTIO67
    
       'Using SWI2C for Display
    #define I2C_MODE Master
    #define I2C_DATA PORTB.4
    #define I2C_CLOCK PORTB.5
    #define I2C_DISABLE_INTERRUPTS ON
    #define LCD_IO 10
    #define LCD_I2C_Address_1 0x4E
    
    '-----------------------------
    #define USART_BAUD_RATE 9600
    #define USART_BlOCKING
    DIR PortC.6 out    '// TX1 Pin 17
    DIR PORTC.7 IN     '// PIN 18
    '-----------------------------
    
    CLS
    Wait 1 s
    Locate 0,0
    Print "Hardware Serial Test"
    Wait 1 s : Cls
    main:
    
    Do
      For send_data = 0 to 255
        Locate 0,0: Print "Sent: "
        Locate 0,6: Print "   "
        Locate 0,6: Print Send_data
    
        Hsersend Send_data, 1  '//send data from USART1
    
       'Hserreceive1 Rec_data    '// ** Does not work ! **
        Rec_data = HserReceive1  '//wait for return byte
    
        Locate 2,0: Print "Received: "
        Locate 2,10: Print "   "
        Locate 2,10:  Print Rec_data
        wait  1 s   '//time to see data
      next
    loop
    

    Receiver Code

    #chip 16F1829, 16
    
    #define USART_BAUD_RATE 9600
    #Define USART_BLOCKING
    
    DIR PORTB.7  OUT  '// Tx Pin 10
    DIR PORTB.5  IN   '// Rx Pin 12
    
    Do
       'Hserreceive1 Rec_byte   '// THIS DOES NOT WORK !
    
       Rec_byte = hserreceive1   '// Use function !
       Wait 5 ms
       HserSend Rec_byte, 1
    Loop
    
     

    Last edit: William Roth 2016-01-29

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.