2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
</#include "lpc17xx.h"
#include "type.h"
#include "delay.h"
#include "lcd.h" //header file to program LCD
#define US_PORTCLR LPC_GPIO0->FIOCLR
#define US_PORTSET LPC_GPIO0->FIOSET
#define US_DDR LPC_GPIO0->FIODIR
#define US_PIN LPC_GPIO0->FIOPIN
#define US_ECHO 5 // pot 0 5th pin for echo
#define US_TRIG 6 // p0.6 for trigger
#define US_ERROR 0xffff
#define US_NO_OBSTACLE 0xfffe
uint32_t d;
uint16_t getPulseWidth()
{
uint32_t i,result;
//Wait for the rising edge
for(i=0;i<600000;i++)
{
if(!(US_PIN & (1<<US_ECHO))) continue; else break;
}
if(i==600000)
return 0xffff; //Indicates time out
//High Edge Found
//Setup Timer1
LPC_SC->PCONP |= 1 << 1; //Power up Timer 0
LPC_SC->PCLKSEL0 |= ( 1 << 2)|(1<<3); // Clock for timer = CCLK /8 -->12000000/8hz
LPC_TIM0->TC=0x00000000; // init counter
LPC_TIM0->TCR |= 1 << 1; // reset timer
LPC_TIM0->TCR |= 1 << 0; // Start timer
//Now wait for the falling edge
for(i=0;i<600000;i++)
{
if(US_PIN & (1<<US_ECHO))
{
if((LPC_TIM0->TC) > 60000) break; else continue;
}
else
break;
}
if(i==600000)
return 0xffff; //Indicates time out
//Falling edge found
result=(LPC_TIM0->TC); //STORE COUNT IN RESULT
//Stop Timer
LPC_TIM0->TCR |= 0 << 0;
if(result > 60000)
return 0xfffe; //No obstacle
else
return (result>>1);
}
int main(void)
{
uint16_t r;
LPC_PINCON->PINSEL0|=(0<<10)|(0<<11)|(0<<12)|(0<<13);// MAKING P0.5,6 AS GPIO
SystemInit();
lcd_init(); //initialization of LCD
delay(500);
lcd_gotoxy(0,0); //function to move the cursor position to (0th row & 5th column )
lcd_string("Hc sr04 test"); //function to display a string
delay(500);
while(1)
{
//Set Ultra Sonic Port as out
US_DDR|=(1<<US_TRIG);
delay_us(10);
//Give the US pin a 15us High Pulse
US_PORTSET|=(1<<US_TRIG); //High
delay_us(15);
US_PORTCLR|=(1<<US_TRIG);//Low
delay_us(20);
//Now make the pin input
US_DDR&=(~(1<<US_ECHO));
//Measure the width of pulse
r=getPulseWidth();
//Handle Errors
if(r==US_ERROR)
{
lcd_clear();
lcd_gotoxy(0,0);
delay_us(100);
lcd_string("error");
}
else if(r==US_NO_OBSTACLE)
{
lcd_gotoxy(0,0);
\
delay_us(100);
lcd_string("clear");
}
else
{
d=(r/58.0); //Convert to cm
lcd_gotoxy(1,0);
lcd_string("d=");
delay_us(100);
lcd_gotoxy(1,4);
lcd_showvalue(d);
delay_ms(50);
lcd_gotoxy(1,7);
lcd_string("cm");
delay_ms(100);
}
}
}
Bold Text Here
hi
I was working with interfacing of hc sr04 ultrasonic sensor with lpc1768 so as to display the distance from an obstacle to the sensor on an LCD display in centimeters ..
there were no errors in the code, but distance is not being varied its showing always as 0 cm..
i could not find the problem.. i'm sure that sensor is in good condition