Menu

Using uart data protocol

Ben Omlor
2024-11-25
2025-01-20
  • Ben Omlor

    Ben Omlor - 2024-11-25

    I am working on an altimeter program.
    The altimeter module uses uart data protocol.
    The goal is to use the module with one of our gear alert boxes to check gear position and give a reminder if the plane gets to a set distance above the ground and the gears not down.
    Right now, I have the module ran to a test board with a LCD display with hopes of establishing communication with the module and getting raw hex readings on the display. Once I get that I can work on decoding the info.
    I am using a PIC16F18345 as the micro.
    The uart protocol specs for the module are.
    Baud Rate: 115200b/s
    Data Bit: 8
    Parity Bit: N
    Stop Bit: 1
    voltage level: 3.3

    I am getting hex numbers on the display, but they do not seem to change with the movement above the module.
    Program I have started is attached. I searched the forum for ideas and this format seemed to get me the closest.

    Thanks for any help or ideas!
    Ben

     
  • Anobium

    Anobium - 2024-11-25

    Can you post the USART protocol and the datasheet for the module.

     
  • mmotte

    mmotte - 2024-11-25

    Ben,
    You should be looking for a "sync" character and then receive the packet of 7 characters.

    Or is it a break for a long time?

    Mike

     
    • Anobium

      Anobium - 2024-11-25

      @mmotte. My thoughts are the same.

       
  • Ben Omlor

    Ben Omlor - 2024-11-26

    Hi,

    Data sheet for module is attached. Page 14 has the data protocol.
    This is my first attempt at something using this type of data transfer.
    I am assuming I am missing some kind of pulse "sync" to tell it to respond.
    The goal would be for it to continuously update the data to determine real time height.

    Thanks!
    Ben

     
  • mmotte

    mmotte - 2024-11-26

    Ben, the altimeter is sending out packets continuously.
    The beginning of the packet is 0xFE , so this is the "sync" character in this system.
    Your program , to get into sync, will use HSerReceive to read and check if it is the beginning of a packet.
    If not then read again.

    if it is the beginning, 0xFE is the character, then continue by reading and filling the variables and checksum. They will be in the right places.

         SData(2) = HSerReceive
          SData(3) = HSerReceive
          SData(4) = HSerReceive
          SData(5) = HSerReceive
          SData(6) = HSerReceive
    

    ver
    altH
    altL
    snr
    chksum

    do checksum and see if valid

    check snr and see if it is not valid

    display altitude

    go back and to short sync loop and wait for next packet


    Your variables are bytes , not strings.

    dim version as string
    dim altitude as string
    dim altitude2 as string
    dim snr as string
    dim checksum as string
    

    So this part is wrong. It should be bytes and your array is bytes already

    Also you could name the altitude as a word variable

    dim  altitude as word
    

    So then you only have to print one variable to the LCD or comparison. It will be handled as a 16bit number
    altitude_H is the higher 8 bits
    altitude is the lower 8 bits

    I don't want to overload you, so I will quit there.

    Mike

     
  • Ben Omlor

    Ben Omlor - 2024-11-26

    Thanks Mike!

    I think I'm getting the jist.
    A sync loop could look something like this?

    do
    wait 10 ms
    SData(1) = HSerReceive
    if sdata(1) = 0xFE then
    SData(2) = HSerReceive
    SData(3) = HSerReceive
    SData(4) = HSerReceive
    SData(5) = HSerReceive
    SData(6) = HSerReceive
    gosub display
    end if

    loop

    Thanks,
    Ben

     
  • Anobium

    Anobium - 2024-11-26

    I would implement a ring buffer then you wont loss any incoming bytes. See the HELP for the ring buffer code.

     
  • mmotte

    mmotte - 2024-11-26

    Ben,
    Your program looks right for capturing the packet directly.

    The data sheet doesn't tell the conversion from 16 bit altitude to the the reading you will output on the display. The overall range is 0.5M to 50 meter. But somehow you will have to combine the 2 bytes to make one number.
    Method 1

    Dim altitude as word
    
    altitude_H = SData(4)
    altitude   = SData(3)
    

    the other way is :
    altitude = SData(4) * 256 + SData(3)

    With altitude being a word, the LCD routine will recognize that and print it as a number

    Mike

     
  • Ben Omlor

    Ben Omlor - 2025-01-20

    Hi Guys,
    Got slammed over the holidays so just getting back into this.
    I believe with the following program I am syncing and getting data, but the display reads 255 for all values other than the Packet (sync) of 254.
    I know its supposedly sending hex values, do I have to do any conversions to have it display properly?
    Thanks for any help or ideas!

    Ben

     
    • Anobium

      Anobium - 2025-01-20

      You still need to implement blocking else the serial will crash through.

      I still recommend a buffer ring and then you know each character arriving and you can sync in the buffer ring. The help has details of the buffer ring.

       
  • Ben Omlor

    Ben Omlor - 2025-01-20

    Now we're getting somewhere!
    added #define USART_BLOCKING
    now it returns varying values, but it seems to be alternating between a good value and 255 every time it reads.
    I looked over the ring buffer help and I gotta admit it has me a bit intimidated. If you're willing to help answer some probably very dumb questions I'd like to take a go at it.
    I see that it DIM's a sync byte but I do not see where you tell it what the syncbyte is supposed to be. And then how do I pull out the bytes that I need to use?

    Thanks,
    Ben

     
  • mmotte

    mmotte - 2025-01-20

    Ben,
    I agree ,you need blocking.

    'USART settings
        #define USART_BAUD_RATE 115200
        #define USART_BLOCKING
    

    I don't agree to needing a buffer. In this case it is being sent all the time. one after another. You don't want all of them you want the most recent. So you ask for it and at most you have to wait for the next packet.

    sub get_val   'gets one packet
            index = 0
    Look:
            HSerReceive temp           'Look for  start of message in tight loop
        If temp <> 0xFE then  goto Look
    
              HSerReceive Version
              HSerReceive Altitude
              HSerReceive altitude2
              HSerReceive snr
              HSerReceive Checksum
      end sub
    

    So the main loop you just call the things to do

    do Forever
    
    get_val
    ;do you want to check checksum?
    GroundAlt = altitude2 *256 + Altitude
    Display
    wait long enough to see the display
    
    loop
    

    Good luck
    Mike

     
    👍
    1
  • Ben Omlor

    Ben Omlor - 2025-01-20

    Thanks Mike!
    That's got it! I was still getting an alternating good number and 254 on the display.
    I added a checksum to filter out the bad read and now she's working great.
    Now for the math to display it in feet for us Mericans. :-)

    Thanks again,

    Ben

     
    • Anobium

      Anobium - 2025-01-20

      Excellent.

       

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.