I am trying to read the ultrasonic receiver using the pic12f683 I thought about using the pulse out and pulsein command in picbasic pro but how do i change the internal oscillator frequency to drive it to 10us pulse to trigger the receiver
I am trying to read the ultrasonic receiver using the pic12f683 I thought about using the pulse out and pulsein command in picbasic pro but how do i change the internal oscillator frequency to drive it to 10us pulse to trigger the receiver
I am trying to get the pic to send out a 10us pulse in order to trigger the ultrasonic receiver and then convert it inches to turn on an led or to drive motors help is appreciated
The default clock on the 12F683 is 4MHz according to the datasheet found in REG 3-1 but still better to set it and define it along with turning OFF other peripherals not used:
But if using PBP3 then:Code:@ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF & _BOD_OFF OSCCON = %01100001 ' Osc 4MHz ADCON0 = 0 ' A/D module OFF CMCON0 = 7 ' Analog comparators off GPIO = %00000000 ' Turn OFF all ports TRISIO = %00001101 ' Ports 0, 2 and 3 inputs, rest outputs (Example) DEFINE OSC 4 ' Declare FOR PAUSE and PAUSEUS statements
Code:#CONFIG __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF & _BOD_OFF #ENDCONFIG OSCCON = %01100001 ' Osc 4MHz ADCON0 = 0 ' A/D module OFF CMCON0 = 7 ' Analog comparators off GPIO = %00000000 ' Turn OFF all ports TRISIO = %00001101 ' Ports 0, 2 and 3 inputs, rest outputs (Example) DEFINE OSC 4 ' Declare FOR PAUSE and PAUSEUS statements
Louie
here is the code i may not be understanding the pulse command the sensor is an hc-sr04 thanks for the help
define osc 4
ANSEL = 0 ' set I/O pin to digital
CMCON0 = 7 ' set comparator off
osccon = $60
define osc 4
input GPIO.4 ' set GPIO.4 to input pin 3
OUtput GPIO.2 ' set GPIO.0 to output pin 5
output GPIO.1 ' set GPIO.1 to output pin 6
sens var GPIO.4
led var GPIO.2
trig var GPIO.1
dist var word
main:
gosub distance
low led
while dist < 8
high led
'pause 500
'low led
'pause 500
wend
goto main
distance:
high trig
pauseus 10
low trig
'pulsout trig,2 ' produces 10us pulse
pause 50
pulsin sens,1,dist 'reads echo pulse from sonar
dist= dist/15 'convert to inches
pause 100
return
The PULSIN and PULSOUT commands at 4MHz have a 10us resolution per count.
PULSIN will time out after 65535 counts or 655ms and return a value of 0 (zero) if it doesn't see the expected pulse.
Make sure MCLR setting is OFF or pin 4 pulled HIGH with a 10K or something to keep it out of reset.
Try this code but realize that if the PULSIN times out, the 0 (zero) will turn the LED ON too because it is <8.
If you don't want the LED ON when PULSOUT times out then I would use the SELECT CASE:Code:ANSEL = 0 ' set I/O pin to digital CMCON0 = 7 ' set comparator off osccon = $60 define osc 4 input GPIO.4 ' set GPIO.4 to input pin 3 OUtput GPIO.2 ' set GPIO.0 to output pin 5 output GPIO.1 ' set GPIO.1 to output pin 6 sens var GPIO.4 led var GPIO.2 trig var GPIO.1 dist var word main: GOSUB distance IF dist < 8 THEN HIGH led ELSE LOW led ENDIF GOTO main distance: trig = 0 ' start output in LOW state PULSOUT trig,1 ' each count = 10us PULSIN sens,1,dist ' will time out in 655ms if nothing received dist= dist/15 'convert to inches PAUSE 100 RETURN END
Code:ADCON0 = 0 ' A/D module OFF ANSEL = 0 ' set I/O pin to digital CMCON0 = 7 ' set comparator off osccon = $60 define osc 4 input GPIO.4 ' set GPIO.4 to input pin 3 OUtput GPIO.2 ' set GPIO.0 to output pin 5 output GPIO.1 ' set GPIO.1 to output pin 6 sens var GPIO.4 led var GPIO.2 trig var GPIO.1 dist var word main: GOSUB distance SELECT CASE dist CASE 0 ' PULSIN timed out and returns zero, turns LED OFF LOW led CASE IS < 8 ' dist <8 inches but not zero, turns ON LED HIGH led CASE ELSE ' dist >8 inches turns LED OFF LOW led END SELECT GOTO main distance: trig = 0 ' start output in LOW state PULSOUT trig,1 ' each count = 10us PULSIN sens,1,dist ' will time out in 655ms if nothing received dist= dist/15 'convert to inches PAUSE 100 RETURN END
Louie
thanks I will give it a try I have chuck hellebuyck's book on picbasic are there any other recommendations for it that could better benefit me .
ok I tried the case method but when the sensor detects anything within the required distance the led blinks instead of holding steady on
Chuck Hellebuyck's book is the best commercial basic-to-intermediate PBP book that I've seen. As far as other recommendations:
1. The manual that comes with PB3! There is a wealth of knowledge in there, and it is put together very well. I still find something new every time I look in it--I keep a shortcut for it on the Windows task bar.
2. The manual for whatever PIC you're using. It took me a while to learn how to understand the PIC manuals, it was a post in a precursor to this fine forum (email list) that clicked it for me. Which brings me to...
3. This forum. Use Darrel's excellent improved search tool (here). Commit to reading every new post for a few weeks and get a feel for the common troubles many have, drink in the wisdom of the regular posters (not me), and follow some of the posted code as it gets improved.
Best Regards,
Paul
The way to avoid mistakes is to gain experience. The way to gain experience is to make mistakes.
the sensor works, but the led turns off and on rapidly instead of staying on what could be the problem
Hmmmmm.... try putting the trig = 0 before the main loop:
Code:trig = 0 ' start output in LOW state main: GOSUB distance SELECT CASE dist CASE 0 ' PULSIN timed out and returns zero, turns LED OFF LOW led CASE IS < 8 ' dist <8 inches but not zero, turns ON LED HIGH led CASE ELSE ' dist >8 inches turns LED OFF LOW led END SELECT GOTO main distance: PULSOUT trig,1 ' each count = 10us PULSIN sens,1,dist ' will time out in 655ms if nothing received dist= dist/15 'convert to inches PAUSE 100 RETURN END
Louie
I put the low trig before the main but it still blinks I wonder if the sensor is sending stray numbers when it starts the distance routine, is there a way I can sim the pic in multisim 11? I may try programming another pic and make sure it is not an internal error
You may right about the stray or false triggering as mentioned in the data sheet.
Now try putting a little delay after the PULSOUT to give the device time to complete the transmit function before listening for an echo.
Code:distance: PULSOUT trig,1 ' each count = 10us PAUSEUS 120 ' Wait for transmit pulse to complete PULSIN sens,1,dist ' will time out in 655ms if nothing received dist= dist/15 'convert to inches PAUSE 100 RETURN END
Louie
ok I tried pause from 50 to 200 milliseconds but it will not respond until I comment the pause command out or remove, it I am wondering if the pic itself is bad internally if so that could explain some erroneous behavior, i have a couple of pic 16f877A coming in and may use them to see if it clears up and then try these later on new 12f683 pics
I have found when I suspect a bad PIC, it turns out that it is just doing what I programmed it to do.
Anyways, 50 to 200ms PAUSE's are way to long to wait since:ok I tried pause from 50 to 200 milliseconds but it will not respond until I comment the pause command out or remove
Max distance measured is 4M or 157.48 inches
And if I'm thinking straight, that works out to 157*15= 2362 counts
Then with PUSLIN at 10us per increment is 2362*10us = 23.62ms
So 23.62ms will be the maximum time measured for 4 meters. Unless I took a rong tern somewhere in the calculation.
Do you have a scope to see how long it actually takes to complete the pulse transmission?
Louie
I do I have two analogs and one digital I prefer the analogs , but don't have probes for them I will try to hook it up later and see what the timing is
In the code that LinkMTech provided there should be no issue with the PAUSEUS or PAUSE commands.
There is a minimum delay between the time the device reacts to the input (PULSOUT) and the time it provides an output (to be read by PULSIN). This time is defined by the 8 40kHz bursts it sends. The device does not raise its output high before the burst is sent (that is in the datasheet), so that means that you could use an approximate delay no longer than 200us between PULSOUT and PULSIN commands and still be ok. LinkMTech suggested 120us to allow for margin; that should be no problem. The other delay, the one which occurs after the PULSIN, represents how much to delay before the next time it tries the measurement again. This again should not be an issue.distance:
PULSOUT trig,1 ' each count = 10us
PAUSEUS 120 ' Wait for transmit pulse to complete
PULSIN sens,1,dist ' will time out in 655ms if nothing received
dist= dist/15 'convert to inches
PAUSE 100
RETURN
END
You certainly need a scope, but not to test your code (since your code looks fine, and it matches BASIC STAMP code and many others which work with modules like the PING, and Devantech's SRF04 modules. You need the scope to see if the module you have is doing what its datasheet claims it does. Also. maybe a pulldown (say 10-20k on the GPIO4 may help - perhaps the SR module does not pull to ground too well).
Well the problem was the breadboard I was developing on the configuration works fine on a different one so I am chalking it up to good ole conductivity. sometimes it is the simple things that get you
Ahhh, son of a garbanzo beans!
Well glad to hear you got it fixed and on your way to becoming an expert.![]()
Louie
Bookmarks