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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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 countDirTRIGoutDirECHOinDIMdistanceaswordDIMpulsetimeaswordDIMiiasword;LCDStuffHI2CModeMaster#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'MainProgramLoopDoInitTimer1Osc,PS_1'InittimerClearTimer1'Cleartovaluetozerocls:locate0,0:Print"Updating..."wait500msPINGITSHOWITwait2sLoopSubPINGITwait100ms'SendTRIGGERpulsesetTRIGonwait100mssetTRIGoffPulse_InECHO,pulsetime'Wait/counterforECHO'pulseinECHO,pulsetime,1usec'DivideforINCHES(changeasneeded)distance=pulsetime/148EndSubSubSHOWITforii=1todistance'blinkdistancecountsetLEDonwait200mssetLEDoffwait200msnextCLS:Locate0,0:PRINT"Distance "locate0,9:printasc(distance)locate1,0:PRINT"Distance "'addedlocate1,9:printdistance'addedEndSubmacroPulse_In(Pin,Variable);MakesurethepinisnotalreadyhighWaitUntilPin=OFF;StarttimeronsignalhighWaitUntilPin=ONStartTimer1;StoptimeronsignallowWaitUntilPin=OffStopTimer1variable=Timer1ClearTimer1endmacro
Last edit: William Roth 2015-05-29
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
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
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.
Last edit: William Roth 2015-05-29
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