I need to write a program for controlling a car or robot with remote control, but I don´t know how to mesure the pulse width of ultrasonic beam from remote, Can you help me?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
PulseLength = 0
do while PORTB.0 ON
PulseLength = PulseLength + 1
IF PulseLength = 0 then goto PulseTooLong
Wait 1 ms
loop
PulseTooLong:
You will need to change PORTB.0 to the port that you are using, and it might also be necessary to change the wait command depending on the width of the pulse. The code above can measure pulses of 0 - 255 ms with 1 ms accuracy.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi! GCBasic is great.
I have an issue with pulsin command.
I use an ultrasonic wave detector (distance sensor) to detect the distance of an object and display it on LCD module.
The device has 4 pinouts, 1 for power, 1 for ground, 1 for trigger input and the last for echo. I connect trigger pin to portB.3 of pic and echo to portB.4. I use 4MHz oscillator
the code in pic basic pro is as follows:
main:
distance var word
distinch var word
xxxx
xxxx
sonar:
pulsout portB.2, 1 '10 micro-second
puslin portB.3, 1, distance
distinch = distance / 15
Then i want to display distinch on LCD module.
it works fine with picbasic pro,
But the main program has other codes that exceeds 50 lines so i cant possibly use picbasic pro free version.
i am just a student so cant afford to buy the software
i tried GCBasic graphical version and loved it.
i convert this program to gcbasic as follows:
sonar:
PulseOut portB.2, 1 10us
PulseIn portB.3, distance, 10 us
but it did not work.
I have tried with a do loop as follows:
distance = 0
do while portB.3 = on
wait 10 us
distance = distance +1
if distance = 0 then exit do
loop
it did not work either
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Is the PulseIn pin direction set as an input? dir PortB.3 in
Here is a thought instead of using PulseIn, just use an IF with a manual setup of Timer 1. The idea being there is less overhead to account for. Should be good for up to 70+ feet at 4 MHz if my math is right. Otherwise the timer 1 prescaler would need adjusting for faster clock.
#define echo PortB.3
dir PortB.2 out
dir PortB.3 in
Dim Timer1Value As Word alias TMR1H, TMR1L
'initialize TMR1, 1:1 prescale, Osc On, Fosc/4, TMR1 stopped
T1CON = b'00001000'
Start:
PulseOut portB.2, 1 10us
Timer1Value = 0 'clear timer 1
Set TMR1ON On 'start TMR1
waitecho:
If echo = 1 Then goto waitecho
Set TMR1ON Off 'stop TMR1
'Do something with Timer1Value
goto Start
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I need to write a program for controlling a car or robot with remote control, but I don´t know how to mesure the pulse width of ultrasonic beam from remote, Can you help me?
Here is some code that you can use:
PulseLength = 0
do while PORTB.0 ON
PulseLength = PulseLength + 1
IF PulseLength = 0 then goto PulseTooLong
Wait 1 ms
loop
PulseTooLong:
You will need to change PORTB.0 to the port that you are using, and it might also be necessary to change the wait command depending on the width of the pulse. The code above can measure pulses of 0 - 255 ms with 1 ms accuracy.
Hi! GCBasic is great.
I have an issue with pulsin command.
I use an ultrasonic wave detector (distance sensor) to detect the distance of an object and display it on LCD module.
The device has 4 pinouts, 1 for power, 1 for ground, 1 for trigger input and the last for echo. I connect trigger pin to portB.3 of pic and echo to portB.4. I use 4MHz oscillator
the code in pic basic pro is as follows:
main:
distance var word
distinch var word
xxxx
xxxx
sonar:
pulsout portB.2, 1 '10 micro-second
puslin portB.3, 1, distance
distinch = distance / 15
Then i want to display distinch on LCD module.
it works fine with picbasic pro,
But the main program has other codes that exceeds 50 lines so i cant possibly use picbasic pro free version.
i am just a student so cant afford to buy the software
i tried GCBasic graphical version and loved it.
i convert this program to gcbasic as follows:
sonar:
PulseOut portB.2, 1 10us
PulseIn portB.3, distance, 10 us
but it did not work.
I have tried with a do loop as follows:
distance = 0
do while portB.3 = on
wait 10 us
distance = distance +1
if distance = 0 then exit do
loop
it did not work either
Is the PulseIn pin direction set as an input? dir PortB.3 in
Here is a thought instead of using PulseIn, just use an IF with a manual setup of Timer 1. The idea being there is less overhead to account for. Should be good for up to 70+ feet at 4 MHz if my math is right. Otherwise the timer 1 prescaler would need adjusting for faster clock.
Ooops, that would be out, then back on Timer 1. So, halve Timer1Value for around 35+ feet range.