Log in

View Full Version : the ultrasonic SRF04 with pic16f88



tur_bot
- 16th March 2008, 17:57
hello everybody

I have few problems with my ultrasonic ranger the SRF04.
I managed to make it work with a pic 16f84 to measure distances in inches and centimeters and display the results on a serial lcd. worked really fine.

then i tried to use the pic 16f88 with an internal clock ; but the lcd displays 0cm and 0 inches all the time which means that the SRF04 is not doing the job (the internal clock is working by the way. i tested it with another code)

here is a part of my code in picbasic pro:

DEFINE OSC 4
INTRC_OSC_NOCLKOUT
INTRC_IO
OSCCON=%01101000

include "modedefs.bas"



trisa = %11111111
trisb = %00000000

lcd var portb.7
'piezo var portb.4
trigger var portb.6
echo var porta.0

baud con N2400
dist_raw var word
dist_inch var word
dist_cm var word
conv_inch con 15
conv_cm con 6

low trigger


serout lcd,baud,[254,1] 'clear lcd screen
pause 1000

serout lcd,baud,[254,128,"-sonar ranger-"]

'sound piezo,[100,10,50,5,70,10,50,2] 'make startup sound
pause 1000 'pause 1 sec

serout lcd,baud,[254,128,"inches: "] 'set up the lcd display
serout lcd,baud,[254,192,"centimeters: "]

main:

gosub sr_sonar

serout lcd,baud,[254,135,#dist_inch," "] 'display the disance in inches

serout lcd,baud,[254,204,#dist_cm," "] 'display the distance in cm

goto main

end

sr_sonar:

pulsout trigger,1 'send a 10us trigger pulse

pulsin echo,1,dist_raw 'start timing the pulse width
'on echo pin
dist_inch = (dist_raw/conv_inch)
dist_cm = (dist_raw/conv_cm)

pause 1

return



i suppose that the pins of the pic are not inputing or outputing the right datas. may be because i did not set the input and output properly.
i had been strogling for few days , please help me to make it work.
thanx
regards

Acetronics2
- 16th March 2008, 18:17
Just an idea ...

May be some comparators and adc to disable ???

Good evening

Alain

tur_bot
- 16th March 2008, 19:44
good idea but how can I do it???
how can I disable the ADC and comparators in a 16f88???

mat janssen
- 16th March 2008, 20:14
There could be a possibility to find that kind of information in the datasheet.

I did that for all types I use and it worked !!

tur_bot
- 16th March 2008, 20:36
hi
i just tested my port with few led and they work fine, inputing and outputting.
but the srf04 is still not working.
I have no idea of what is going on.

anybody with an idea????

Acetronics2
- 16th March 2008, 21:10
- Try External clock !!! ( internal Osc is "not so accurate" ...)

- Place "Low trigger" just before "Pulsout"

Alain

tur_bot
- 16th March 2008, 21:40
i just tryied to use the external clock again.
but it is still not working.

any one with an idea, please ??????

skimask
- 17th March 2008, 02:04
i just tryied to use the external clock again.
but it is still not working.
any one with an idea, please ??????

Re-read post #2 and post #4...
Look at your program...
Take a good look at PortA.0
Compare PortA.0's function from the datasheet to what you are trying to do with it...

Get back to us with the results...

tur_bot
- 17th March 2008, 03:43
what do you mean by check porta.0 , read post2 and 4 ????? do you mean that the porta.0 is analog and I shouldn't use it for digital signals input????

anyway , I moved the echo signal to differents port.b's and port.a's and nothing changed.

I tested few portB input pins with infrared detectors ( which worked fine) but when the echo input signal, is connected to the same portb that i just tested , and the trigger signal output (connected to portb output pins that i tested with LEDs) of my srf04 are connected , nothing is working

can you tell me clearly what do you have in mind please???

mackrackit
- 17th March 2008, 04:33
Have you seen this?
http://www.picbasic.co.uk/forum/showthread.php?t=561

skimask
- 17th March 2008, 05:19
can you tell me clearly what do you have in mind please???
Nope...I don't have your circuit running with your software running on your hardware programmed by your programmer.
There are numerous differences between the 16F84 and the 'F88. I'm not going to spell them all out because they're already in the datasheets.

tur_bot
- 17th March 2008, 06:49
thank you so much. you are a star
finally everything is working with internal clock and measurements well displayed on the lcd.

wasn't that easy to understand the data sheet but because of all of you guys, with all your examples and suggestions , i managed to find the right settings.

again thank you guys
God bless you all

Muaz
- 17th March 2008, 08:44
Hi, This post of mine is very knowledgable and may enhance the information of the viewers , however I would like some specific information for myself. If someone can help me then please send me a private message. Best Regards,

skimask
- 17th March 2008, 17:17
wasn't that easy to understand the data sheet but because of all of you guys, with all your examples and suggestions , i managed to find the right settings.
Just curious...which bit fixed the problem?
Invariably, it's usually that one little bit that fixes everything...

tur_bot
- 20th March 2008, 18:20
Just curious...which bit fixed the problem?
Invariably, it's usually that one little bit that fixes everything...

those 6 lines allowed me to use an accurate inside clock and to use all pins as digital input or outputs:

DEFINE OSC 4
INTRC_OSC_NOCLKOUT
INTRC_IO
OSCCON=%01101000
ANSEL=%00000000 'this comand disable all adc
CMCON=%00000111 'this bit disable all comparators


the two last commands fixed the problem to get just digital inpout and outpout on all 15 pins available, and disabled all adc and comparators.

skimask
- 20th March 2008, 18:24
the two last commands fixed the problem to get just digital inpout and outpout on all 15 pins available, and disabled all adc and comparators.
Just like posts #2, #4, #8, #10 said it would...