Menu

UART problem

Help
MBB
2024-07-23
2024-08-02
  • MBB

    MBB - 2024-07-23

    I'm using a 18F4550 with Great Cow BASIC (0.99.01 2022-01-27 (Windows 64 bit) : Build 1073).

    I'm working with this ultrasonic sensor https://www.adafruit.com/product/4665

    I wrote this code:

    ;Chip Settings
    #chip 18F4550,48
    #config PLLDIV=1, CPUDIV=OSC1_PLL2, OSC=HSPLL_HS, LVP=OFF, MCLRE=ON ;OFF

    ;Defines (Constants)
    #define LCD_IO 2
    #define LCD_CB PORTE.1
    #define LCD_DB PORTE.0
    

    '**********
    #define UART_Rec PORTC.7 ;Data FROM DYP-A01-V2.0
    #define UART_TX PORTC.6 ;Data TO YP-A01-V2.0
    #define USART_BAUD_RATE 9600
    #define USART_BLOCKING 'USART routines wait until data can be sent or received.

    Dir UART_Rec In
    Dir UART_TX Out

    '**********

    dim Total_Distance as Word

    wait 100 ms
    '*****

    Do_Again:

    HSerReceive Frame_Header
    HSerReceive Data_MSB
    HSerReceive Data_LSB
    HSerReceive Check_Sum

    locate 0, 0
    print Frame_Header

    locate 1, 0
    print Data_MSB
    print " "
    print Data_LSB
    
      locate 2, 0
      print Check_Sum
    
        Total_Distance = (256 * Data_MSB) + Data_LSB
    
          locate 3, 0
          print Total_Distance
          print " mm"
    

    wait 1 s
    cls

    goto Do_Again
    
    '********************************
    

    My logic analyzer reads the correct distances so the ultrasonic sensor is working correctly.

    The above code reads 10 for Data_MSB, 10 for Data_LSB, 10 for Check_Sum and 2570 for Total_Distance.

    The 10 never changes as I move the sensor around. I tried 4 different 18F4550s.

    Any idea what I'm doing wrong?

     
  • Anobium

    Anobium - 2024-07-23

    No idea, at the moment.

    Are you getting LCD display?
    Does serial sent work? this will ensure the serial functionality is working in the context of 48Mhz configuration and specified frequency.

    This info will help.

     
  • MBB

    MBB - 2024-07-24

    Are you getting LCD display?
    The 18F4550 shows the 10 for Data_MSB, 10 for Data_LSB, 10 for Check_Sum and 2570 for Total_Distance on the LCD.

    Does serial sent work?
    This ultrasonic sensor responds with out being sent any commands.

    So, I tried this UART controlled Bluetooth device https://www.sparkfun.com/products/23287

    With this device the 18F4550 sent and received data correctly i.e, both the LCD and logic analyzer read the data.
    I was able to send data from my cell phone and see it on the LC D display.
    I was able to send data from the 18F4550 and see it on my cell phone.

     
  • Anobium

    Anobium - 2024-07-24

    Good news. Serial is all good.

    So, the HSerRecieve is always receiving a value of 10, 10, 2570 (( 256*10)+10). So, something is not correct.

    Do you have the actual datasheet for the product? Sounds like a sync issue to me. And, there implementing a serial ring buffer would resolve.

     
  • MBB

    MBB - 2024-07-24

    I just added this to my program #define CHECK_USART_BAUD_RATE

    When I compile it I get this:
    Error: Big error in baudrate - percentage error = 75 @ 16800

     
  • Anobium

    Anobium - 2024-07-24

    That is good diagnostics. Also, that could be the issue.

    Check the data sheet to check that 9600 is practical at 48mHz.

    You can edit your program to force the baud rate.

     
  • MBB

    MBB - 2024-07-29

    I found a solution to the above problem.

    I changed from 48 MHz to 8 MHz.

    BUT

    I also had to change this:

    HSerReceive Frame_Header
    HSerReceive Data_MSB
    HSerReceive Data_LSB
    HSerReceive Check_Sum

    to this:

    HSerReceive xx ; xx and yy are dummy bytes
    HSerReceive yy

    HSerReceive Frame_Header
    HSerReceive Data_MSB
    HSerReceive Data_LSB
    HSerReceive Check_Sum

    I don't understand why I needed to read xx and yy to make it work. The data sheet does not require them.

     
  • Anobium

    Anobium - 2024-07-29

    Look two issue that are independent of each other.

    1. The accuracy of the baud rate at 48mHz. Resolved by using a slower frequency. The datasheet does show the error % when using different frequencies and clearly the high error % at 48mHz caused a comms issue.

    2. The dummy reads clear the buffer. A buffer ring with a sync byte would also have resolved.

    Anyway, you did a great job to resolve.

     
  • MBB

    MBB - 2024-08-02

    After I sent the above, the ultrasonic sensor stopped working. Further troubleshooting showed that I need to add this to my code:
    comport = 1
    Now everything is working.

    In the help file under HSerReceive there are the following statements which seem contradictory.

    "comport = 1 '(1|2|3|4|5)Not needed unless using multiple comports in use"
    I'm using Port C.7 for UART_Rec and PortC.6 for UART_TX (18F4550). Only one comport.
    Since it is not needed why do I need it?

    "The subroutine HSerReceive can get a byte from any comport but must set the comport number immediately before the call."
    This seems to indicate I do need it.

    "#define USART_BAUD_RATE 9600 'sets up comport 1 for 9600 baudHSerReceive(InChar) 'Receive data as Subroutine from comport 1 "
    This seems to say setting baud rate automatically sets the proper comport.

    Since I'm using one comport why do I need the "comport = 1" statement in my code?

     
  • Anobium

    Anobium - 2024-08-02

    The comport should be automatically.

    zip up the project and post. Setting comport=1 is very important and this should happen.

     

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.