Menu

NANO, SR04 Untrasonic TX/RC and LCD 1602

Help
lhatch
2015-05-26
2015-05-27
  • lhatch

    lhatch - 2015-05-26

    The LCD is fine, the trigger I can see on the scope, but do not get echo. I put the sonar code on a PIC and it works fine. Any ideas? A lite pullup. It is wired right and code it test on non AVR chip.
    ~~~~

    chip mega328p, 16

    config mclr_on

    include <UNO_mega328p.h>

    define TRIG DIGITAL_4

    define ECHO DIGITAL_5

    degine LED DIGITAL_13 'Blink LED in showit to see count

    Dir TRIG out
    Dir ECHO in
    DIM distance as word
    DIM pulsetime as word
    DIM ii as word

    HI2CMode Master

    define HI2C_DATA

    define LCD_IO 10

    define LCD_I2C_Address 0x4E 'Change to 0xFE for SainSmart

    define LCD_Backlight_On_State 1 'These may need to be reversed

    define LCD_Backlight_Off_State 0

    do
    InitTimer1 Osc, PS1_2 'Init timer
    ClearTimer 1 'Clear to value to zero
    cls : locate 0,0 : Print "Updating..."
    wait 500 ms
    gosub PINGIT
    gosub SHOWIT
    wait 2 s
    loop
    end

    PINGIT:
    wait 100 msec 'Send TRIGGER pulse
    set TRIG on
    wait 100 msec
    set TRIG off
    Pulse_In ECHO, pulsetime 'Wait/counter for ECHO
    ' pulsein ECHO, pulsetime, 1 usec
    distance=pulsetime / 148 'Divide for INCHES
    return

    SHOWIT:
    for ii = 1 to distance 'blink distance count to The on-board LED
    set LED on
    wait 200 ms
    set LED off
    wait 200 ms
    next
    CLS : Locate 0,0 : PRINT "Distance "
    locate 0,9 : print asc(distance)
    return

    macro Pulse_In (Pin, Variable)
    ;Make sure the pin is not already high
    Wait Until Pin = OFF
    ;Start timer on signal high
    Wait Until Pin = ON
    StartTimer 1
    ;Stop timer on signal low
    Wait Until Pin = Off
    StopTimer 1
    variable = Timer1
    ClearTimer 1
    end macro

     

    Last edit: lhatch 2015-05-26
  • William Roth

    William Roth - 2015-05-26

    Hi,

    Timer Prescaler "PS1_2" is not a valid option for AVR.

    Per the Help File ...
    On an AVR chip, the following options are available for prescaler:
    · PS_1
    · PS_8
    · PS_64
    · PS_256

    · PS_1024

    "define" is misspelled in one place

    #degine LED DIGITAL_13
    

    While Gosub/Label/Return is not expressly forbidden, a better practice is to use sub/ end sub. You will never see a GCB demo written with Gosub/Label/Return. It is not recommended.

    You may want try the code below as a starting point. Get the scope out and check the trig and echo pins for proper signals.

    #chip mega328p, 16
    #config mclr_on
    #include <UNO_mega328p.h>
    
    #define TRIG DIGITAL_4
    #define ECHO DIGITAL_5
    #define LED DIGITAL_13  'Blink LED in showit to see count
    
    Dir TRIG out
    Dir ECHO in
    
    DIM distance as word
    DIM pulsetime as word
    DIM ii as word
    
    ; LCD Stuff
    HI2CMode Master
    #define HI2C_DATA
    #define LCD_IO 10
    #define LCD_I2C_Address 0x4E      'Change for SainSmart
    #define LCD_Backlight_On_State 1  'These may need reversing
    #define LCD_Backlight_Off_State 0
    
    ' Main Program Loop
    Do
        InitTimer1 Osc, PS_1 'Init timer
        ClearTimer 1 'Clear to value to zero
        cls : locate 0,0 : Print "Updating..."
        wait 500 ms
        PINGIT
        SHOWIT
        wait 2 s
    Loop
    
    Sub PINGIT
        wait 100 ms 'Send TRIGGER pulse
        set TRIG on
        wait 100 ms
        set TRIG off
        Pulse_In ECHO, pulsetime 'Wait/counter for ECHO
        ' pulsein ECHO, pulsetime, 1 usec
    
       'Divide for INCHES   (change as needed)
       distance = pulsetime / 148
    
    End Sub
    
    Sub SHOWIT
        for ii = 1 to distance 'blink distance count
            set LED on
            wait 200 ms
            set LED off
            wait 200 ms
        next
    
        CLS : Locate 0,0 : PRINT "Distance "
        locate 0,9 : print asc(distance)
        locate 1,0 : PRINT "Distance "   ' added 
        locate 1,9 : print distance      ' added         
    End Sub
    
    macro Pulse_In (Pin, Variable)
        ;Make sure the pin is not already high
        Wait Until Pin = OFF
        ;Start timer on signal high
        Wait Until Pin = ON
        StartTimer 1
        ;Stop timer on signal low
        Wait Until Pin = Off
        StopTimer 1
        variable = Timer1
        ClearTimer 1
    end macro
    
     

    Last edit: William Roth 2015-05-29
  • lhatch

    lhatch - 2015-05-27

    Thanks again William.

    The problem was the prescaler. There is an error in the sample code you posted, can you fix and I will remove this part of my post (can you change PS1_1 to PS_1).

    Here in the final code (gosub gone), I removed the LED (and typo), fixed prescaller. If someone does not have the display but has the SR04, then can look at first code and add LED to blink on the inches with a distance / 300 (close)

    Final code:

    ~~~~#chip mega328p, 16

    include <UNO_mega328p.h>

    'SR04 Ultra sonic Stuff

    define TRIG DIGITAL_4

    define ECHO DIGITAL_5

    Dir TRIG out
    Dir ECHO in

    '1602 LCD with TWI Stuff
    HI2CMode Master

    define HI2C_DATA

    define LCD_IO 10

    define LCD_I2C_Address 0x4E 'Change for SainSmart

    define LCD_Backlight_On_State 1 'These may need reversing

    define LCD_Backlight_Off_State 0

    'Variables
    DIM distance as word
    DIM pulsetime as word
    DIM ii as word

    'Set prescale (my main error)
    InitTimer1 Osc, PS_1 'Init timer

    ' Main Loop
    Do
    ClearTimer 1 'Clear to value to zero
    PINGIT
    SHOWIT
    wait 2 s
    Loop

    'Send the Pulse out and get Return count
    Sub PINGIT
    wait 100 ms 'Send TRIGGER pulse
    set TRIG on
    wait 100 ms
    set TRIG off
    Pulse_In ECHO, pulsetime 'Wait and count ECHO size
    ' pulsein ECHO, pulsetime, 1 usec OLD CODE
    distance = pulsetime /270 'Divide for resolution you want
    End Sub

    'Display distance count
    Sub SHOWIT
    CLS : Locate 0,0 : PRINT "Distance "
    locate 0,9 : print distance
    End Sub

    macro Pulse_In (Pin, Variable)
    ;Make sure the pin is not already high
    Wait Until Pin = OFF
    ;Start timer on signal high
    Wait Until Pin = ON
    StartTimer 1
    ;Stop timer on signal low
    Wait Until Pin = Off
    StopTimer 1
    variable = Timer1
    ClearTimer 1
    end macro

     

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.