Capturing values in ultrasonic sensor using lpc2148
![](https://www.embeddedrelated.com/new/images/defaultavatar.jpg)
Hi everyone, I'm trying to capture the value to determine the distance of an object using ultrasonic sensor,but getting the distance as 0 on lcd, checked the status of trigger pin in oscilloscope getting the pulse in this pin but not on echo pin.
Assigned Trigger as Output & Echo as Input, below is the code:
Connected ECHO_PIN to PIN 16 & TRIGGER_PIN to PIN 12
uC: LPC2148
Compiler : Keil uvision 5.1
Pclk: 60Mhz
#include<LPC214x.h>
#include<stdio.h>
#include "lcd.h"
#define PRESCALE 60
#define TIMER_TICK_1_MS (59999) /* VPB at 60 MHz clock */
#define ECHO_PIN (1 << 16) //INPUT PIN[PIN 16]
#define TRIGGER_PIN (1 << 17) //OUTPUT PIN[PIN 12]
void initTimer(void);
void delay3(int);
unsigned char buf[16] = {0};
void delay_ms(unsigned int count);
void initTimer()
{
T1TCR = 0x00; /* ensure timer 1 is off */
/* set timer counter and prescale counter */
T1TC = 0x0;
T1PC = 0x0;
/* set prescale and match values to give 1 ms "tick" */
T1PR = 0x0;
T1MR0 = TIMER_TICK_1_MS;
/* set control to interrupt on MR0 match, and reset to 0 (i.e. "tick") */
T1MCR = 0x3;
/* go start timer 1... */
T1TCR = 0x1;
}
void delay_ms(unsigned int count)
{
unsigned int j=0,i=0;
for(j=0;j<count;j++)
{
for(i=0;i<3000;i++);
}
}
int main()
{
init_lcd();
IO1DIR &= ~(1<<16); //Echo pin
IO1DIR |= (1<<17); //trigger pin
initTimer();
lcd_clear();
while(1)
{
long time,dist;
IO1SET = 1<<17; //send high on trigger pin
delay_ms(1000); //delay for 10us
IO1CLR = 1<<17; // send low after 10us
T1TCR = 0x01; //Enable Timer1
if(T1CR0 == 0)
{
delay_ms(100);
} //wait for rising edge to occur on echo pin
delay_ms(3); //wait for debounce
if(T1CR0 == 0)
{
time = T1CR0; //read value of CR0 reg
dist = (time*340)/2; //dist in meters
IO1SET = 1<<16;
delay_ms(100);
IO1CLR = 1<<16;
sprintf((char *)buf, "dist:%d cm",dist);
lcd_putstring(1, (char *)buf);
}
else
T1TCR = 0x00; //disable Timer1
}
}
Thank you,
Akashh
![](https://www.embeddedrelated.com/new/images/defaultavatar.jpg)
Why don't you get a pulse on the echo pin?
Is the trigger pulse within specification?
Have you understood your sensor?
![](https://www.embeddedrelated.com/new/images/defaultavatar.jpg)
Thank you for the response sir, and have referred the working principle
of sensor, will work on the echo pin.
![](https://www.embeddedrelated.com/new/images/defaultavatar.jpg)
>>delay_ms(1000); //delay for 10us
That is not a 10 us delay - that would be 1 second!