Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Ultrasonic Range Finder with PIC16F690

Status
Not open for further replies.

emzdanow

New Member
Hi, I am new to uC and especially range finders. I purchased a SRF04 (SRF04 Technical Documentation) and am trying to interface with my PIC16F690. If I run the code listed nothing happens. If I comment out the while(RB5 == 0) the code will loop and I can at least hear the range finder pulsing. I am not sure if my uC is not sensing the high signal from the echo, or if my code is just wrong, please help! Thanks!

#include <htc.h>
void pause (unsigned short msvalue);
__CONFIG (INTIO & WDTDIS & MCLRDIS & UNPROTECT );
unsigned char ticks = 0; //timer tick storage variable can be 0 to 255
main()
{
ANSEL = 0;
CM1CON0 = 0;
CM2CON0 = 0;
PORTC = 0x00;
TRISC = 0x00;
PORTB = 0x00;
TRISB = 0b00100000;
while(1)
{
/******************Pulse Transmission************************************** ********************/
RB4 = 1; // 10 commands of RB4 = 1 is 10us
RB4 = 1; // pulse high (1 inst cycle per us
RB4 = 1;
RB4 = 1;
RB4 = 1;
RB4 = 1;
RB4 = 1;
RB4 = 1;
RB4 = 1;
RB4 = 1;
RB4 = 0;

/****************Echo Detection***************************************** ***********************/
OPTION = 0b00000101; // Make TMRO prescale 1:64, oscillator = 1 tick per 1us, 64(1us)= 64us. Range = 256(64us) = 16.3ms
T0IF = 0; // Clear timer0 interupt flag
while(RB5 == 0); // Wait for echo pulse to start (go high)
TMR0 = 0; // Start Timer0 with 0 ticks
while(RB5 == 1); // Wait for echo pulse to stop (go low)
ticks = TMR0; // Capture timer0 value and store it in 'ticks'
/****************Display Results******************************************* *********************/
if(TMR0 < 60)
{
RC0 = 1;
RC1 = 0;
RC2 = 0;
RC3 = 0;
}
if(TMR0 > 60 && TMR0 < 120 )
{
RC0 = 1;
RC1 = 1;
RC2 = 0;
RC3 = 0;
}
if(TMR0 > 120 && TMR0 < 180)
{
RC0 = 1;
RC1 = 1;
RC2 = 1;
RC3 = 0;
}
if(TMR0 > 180)
{
RC0 = 1;
RC1 = 1;
RC2 = 1;
RC3 = 1;
}
pause(10); // Pause 10 milliseconds between next measurement
}
}
 
So I have added timer for pulse input and adjusted my echo timer, my main problem is that I am not detecting an echo signal (RB5 ==1) with my PIC. Any suggestions?
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top