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
i'm attaching the code
/******************************************************************************** Microcontroller: LPC1768-Cortex M3 Clock: 12mhz Compiler Keil uVision 4 ********************************************************************************/ #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); } } }
"So can u plz provide me the source code for the same."
The OP has already posted his non-working code for a 17xx.
If you really want to hand in "stolen" code - why not then rewrite the code from a 17xx to a 21xx and at the same time fix the bug? Or did you expect the OP or someone else to rewrite the code and hand you, so you have something to hand in to your teacher?