Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

how to write code to ultrasonic sensor HC-SR04

Status
Not open for further replies.

Electronics BH

Newbie level 3
Newbie level 3
Joined
Mar 2, 2013
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,308
hi ,


I want o write a code for a distance meter using Mikroc Pro , using ultrasonic sensor ( HC-SR04 ) , pic16f628a , and 16 *2 LCD .

What i understand about this sensor module is the following :

in the code :

1 - you have to end a trigger signal ( 5 V) .
2- delay_us(15),
3- set the trigger pin to be low ( 0 V) again , then the module will send a burst signal ( 40 Khz) .
3- after sending the signal, the ECHO pin will be high ( 5 v) automatically , so in the code we have to wait for this pin to be high .
4- when ECHO pin becomes high (5V) , we have to start the timer .and listening to the ECHO pin to become low (0v) again to indicate that the sent signal has been reflected from an abject and detected by the module.
5- When ECHO becomes (0V), we have to stop the timer .
6- use the time ( which is counted by the timer ) to calculate the distance and display the distance on the LCD .




can you just help me in writing this code with the following :



pic 16f628a

Trigger pin = RA5
Echo pin = RA4

Timer = Timer1

Oscillator is the internal oscillator of the same pic .



LCD can be connected to PORT B.
 

hi,


i'm stuked in the step of checking the echo pin to be 1 and then to start the timer1 ,


what statements i can use to say :

wait for echo pin to be 1 and then start timer1

then wait for the echo pin to be 0 again and stop the timer1


what is the code to state the previous statements and how to enable and disable the timer 1 ?




Thanks
 

is it a good idea to use interrupt!

i tried to use the external interrupts of pic 16f628a , pin# 6, B0 .


a trigger signal will be sent to trigger the module , wait for 10 us , stop the trigger signal , the module will send the burst signal , and then the Echo will be high "1" , the timer will be started , and ifany reflection of the burst signal, the echo will become low again" 0 " , sowe have stop the timer!


is it a right scenario, please have a look at the following code :




Code:
#define ECHO PORTB.F0
#define TRIG PORTB.F1

// LCD module connections
 sbit LCD_RS at RB2_bit;
 sbit LCD_EN at RB3_bit;
 sbit LCD_D4 at RB4_bit;
 sbit LCD_D5 at RB5_bit;
 sbit LCD_D6 at RB6_bit;
 sbit LCD_D7 at RB7_bit;

sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections


unsigned int Time;
unsigned int Distance;
char txt[14];

void interrupt()
{
if (INTCON.INTF)    // IF RB0 is occured
                  {
                  if ( ECHO == 1) {
                                   INTCON.INTF = 0;  // make the interrupt to 0 again
                                   OPTION_REG.INTEDG=0; // interrupts activated at falling edge
                                   T1CON.TMR1ON =1;       //enable timer
                                  }

                  if ( ECHO == 0) {
                                   INTCON.INTF = 0;  // make the interrupt to 0 again
                                   OPTION_REG.INTEDG=1; // interrupts activated at rising edge
                                   T1CON.TMR1ON =0;       //disable timer
                                   Time = (TMR1H<<8)+(TMR1L); // to read the value of the timer ( shift left and add )
                                   TMR1L=TMR1H=0;
                                  }
                  }

}

void main()
{

OPTION_REG.INTEDG=1;
delay_ms(500);
Lcd_Init();                 //Initialize LCD
delay_ms(500);
Lcd_Cmd(_LCD_CLEAR);        // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF);   // Cursor off
Lcd_Out(1,2,"((--Distance--))");
TRISA = 0;
TRISB.F0=1;
TRISB.F1=0;
INTCON = 0b11010000;         //  enable global interrupt, preiferal is enables , and RB0 externl interupt is enabled
T1CON =  0b00000001;         // set prescaler to 1:1 and enable timer 1.


while(1)
{
TRIG =0;
Delay_us(1000);
TRIG = 1;
delay_us(20);
TRIG = 0;
Distance = Time * 1.716 * 0.927856;

inttostr(Distance,txt);
lcd_out(2,3,txt) ;
lcd_out(2,11,"cm") ;


}


}
 

Yes. You have to use external interrupt to capture echo. Give a pulse to trig pin and start timer and when echo pin connected to INT0 changes state then stop timer. convert time to distance. If timer overflows then you should also use a counter to count time i.e., if you have a timer interrupt for every 2 us and 10+ us has elapsed when echo is detected then a counter will hold the value of 2 us * 5 = 10 us.

counter will be 5 and remaining time must be calculated from value left in TMRxH and TMRxL registers.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top