hello ,this is the code i made for interfacing an ultrasonic sensor to atmega8 board, it worked fine...
//*atmega8*// #include<avr/io.h> #include<util/delay.h> #include "lcd118010.h"
#define US_PORT PORTC #define US_PIN PINC #define US_DDR DDRC
#define US_TRIG PC5 //PORTA0= trigger #define US_ECHO PC0 // port a1 = echo
/********************************************************************
This function measusers the width of high pulse in micro second.
********************************************************************/
#define US_ERROR 0xffff #define US_NO_OBSTACLE 0xfffe // while using macro place the replacing thing "before" the replaced thing with a white space int d; //global variable=distance; 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 TCCR1A=0X00; TCCR1B=(1<<CS11); //Prescaler = Fcpu/8 TCNT1=0x00; //Init counter
//Now wait for the falling edge for(i=0;i<600000;i++) { if(US_PIN & (1<<US_ECHO)) { if(TCNT1 > 60000) break; else continue; } else break; }
//Falling edge found
result=TCNT1;
//Stop Timer TCCR1B=0x00;
if(result > 60000) return 0xfffe; //No obstacle else return (result>>1); }
void move_robo() { if(d<20) { PORTD=0x10; _delay_ms(100); } else if(d>20) { PORTD=0xa0; _delay_ms(100); } else { PORTD=0x90; /// spin }
}
void Wait() { uint8_t i; for(i=0;i<10;i++) _delay_loop_2(0); } int main() { DDRD=0xf0; PORTD=0x00; uint16_t r;
Wait();
//Initialize the LCD Module lcd_init();
lcd_clear(); lcd_gotoxy(0,0); lcd_string("HC SR04 TEST");
Wait(); Wait(); Wait(); Wait(); Wait(); Wait();
lcd_clear();
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_PORT|=(1<<US_TRIG); //High
_delay_us(15);
US_PORT&=(~(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); if(d>20) { PORTD=0xa0; _delay_ms(200); } else { move_robo(); }
Wait(); } } }
BUT,WHEN I CONVERTED THIS CODE INTO ARM
/*********************************************************************************
microcontroller: LPC1768-Cortex M3 clock: 12mhz compiler Keil uVision 4 ******************************************************************************** ***********************************************************************/ #include "LPC17xx.h" #include "delay.h" #include "type.h" #include "lcd.h" #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;
//Setup Timer1 LPC_SC->PCONP |= 1 << 1; //Power up Timer 0 LPC_SC->PCLKSEL0 |= 1 << 2; // Clock for timer = CCLK
LPC_TIM0->TCR |= 1 << 1; 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; }
result=(LPC_TIM0->TC); //STORE COUNT IN RESULT
//Stop Timer LPC_TIM0->TCR |= 0 << 0;
int main (void) { uint16_t r; SystemInit(); lcd_init(); delay_ms(50);
lcd_clear(); lcd_gotoxy(0,0); lcd_string("HC SR04 TEST"); delay_ms(50);
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); } } }
IT IS NOT DOING, LCD IS DISPLAYING USELESS VALUES.. PLEASE HELP ME CORRECTING THE CODE FOR ARM.. ULTRASONIC MODULE I USED IS HC SR04
i thing i may have missed some timer concepts; as i'm a beginner THANKYOU