News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Serial communication between 2 Microcontrollers

Started by basparky, Feb 09, 2026, 01:01 PM

Previous topic - Next topic

basparky

Hi All,

I have a dsPic (a) which communicaties with a Pic(b) over UART. The Dspic is the master and the Pic the slave. The slave(b) can be a unique device with his own data packet output.
In my current deisgn i have the slave sending his datapacket like following:

Proc SendMessage(Nr As Byte)
'Str = 255-3-200-0-100-100-233 wChecksum = 891 bCheckSUm = 123 
    StrToMain[0]    = 255           'Fixed ID
    StrToMain[1]    = Modus   
    StrToMain[2]    = Command     
    StrToMain[3]    = Throttle
    StrToMain[4]    = Dir
    StrToMain[5]    = SafetySwitch
    StrToMain[6]    = 233 'dummy           
    wCheckSum = StrToMain[0] + StrToMain[1] + StrToMain[2] + StrToMain[3] + StrToMain[4] + StrToMain[5]+ StrToMain[6]
    bCheckSum = wCheckSum.LowByte   
    StrToMain[7] = bCheckSum
   
    TMR0ON = 0
    TMR0IE = 0 
   
    For bIndex = 0 To 7
      HRSOut StrToMain [bIndex]
    Next
   
    Interval = 0             
    TMR0ON = 1
    TMR0IE = 1         
EndProc

The master is receiving like:
Isr U2RXInterrupt   
    ID = 255'121 'Id from joystick
       
    If U2STAbits_OERR = 1 Then
        Clear U2STAbits_OERR
        SOM = 0
        Index2 = 0
        GoTo ExitInterrupt2
    EndIf
       
    USART2bRx = U2RXREG   
   
    'catch start off message
    If USART2bRx = ID And SOM = 0 And Index2 = 0 Then
        SOM = 1
        Index2 = 0
    EndIf

    '   
    If SOM = 1 Then
        StrJoyStick[Index2] = USART2bRx
        Inc Index2
        If Index2 > 7 Then
            Index2 = 0
            ReceivedFromJoystick = 1
            SOM = 0
            Toggle LedRd
        EndIf
    EndIf
   
    ExitInterrupt2:
    Clear IFS1bits_U2RXIF
EndIsr


So the slave is sending his ID and a data packet in a fixed length. As shown the receiver waits for the ID 255 and then expects the payload before waiting for the next startbyte (ID).

I would like to make this communication more robuust and create somehow a ID based receive routine.
At this moment i have 3 types of slave. each with their own different datapacket and ID. Does anyone have some suggestions how to build this robuust?
I had issues with sending the SLaves ID because the value 255 van also be the value of one of the bytes in the payload. After knowing this i made a catch start off message as shown in the ISP but i think its too basic to have this working for 3 slave ID's.

I think i'm asking a direction and some help make this work in practise. I have no experience with dynamic data packages but believe this is what it should be on the receiver side.

Any hints or information how to do this would help me a lot. Thanks in advance!






John Lawton

You could use ninth bit addressing to indicate the packet start?

John
-----------------------------------------------------------------------------------------
Amicus 8 and 16A/16B dev boards
Especially created for Positron development
https://www.easy-driver.co.uk/Amicus

trastikata

Is it possible to include a MAC address to each packet?

TimB

I would recommend that you use the 3.5T end of packet system. It's pretty simple to implement you start a timer after the RX of every byte.  In your RX routine when you get a new byte just check if the timer has zeroed out. If that is the case then clear pointers etc and presume this is the address byte.

Check the address against your own and if it matches set your RX state machine to load the rest of the packet.

Just to clarify the 3.5T is the time to send 3.5t bytes





basparky

Thanks! I like the simplicity of the use of 9th bit usage to detect start.
Just wondering which register present this bit in my dspic33FJ.
i will dive into the datasheet :)


John Lawton

-----------------------------------------------------------------------------------------
Amicus 8 and 16A/16B dev boards
Especially created for Positron development
https://www.easy-driver.co.uk/Amicus