Project 2 - Measuring distance of an object using ultrasonic sensor (HC-SR04)
ABINAYA INBAMANI MAM (LIVE BATCH)
Overview of the Project:
In this project, the implementation of code for “Measuring distance of an object using ultrasonic distance sensor (HC-SR04)
Goals:
- Measuring the distance of an object using Ultrasonic Distance Sensor (HC-SR04)
- Usage of Input Capture Mode
Key Highlights of the Project:
- Measuring the distance travelled by the wave to the object and gets bounced back from the object to sensor.
- Measuring Pulse width using Input Capture Mode
#include <avr/io.h> #define F_CPU 8000000UL #include <util/delay.h> #include <string.h> #include <stdlib.h> #define LCD_Date_Dir DDRD //define LCD data port direction #define LCD_Command_Dir DDRC //Define LCD command port direction register #define LCD_Data_Port PORTD //Define LCD data port #define LCD_Command_Port PORTC // #define EN PC2 // define Enable signal pin #define RW PC3 //Define read/write signal pin #define RS PC4 //define register select (data reg./command reg.) signal pin void LCD_Command(char); //lcd command write funtion void LCD_Char(char); //lcd data write function void LCD_Init(void); //lcd initialize function void LCD_String(char*); //send string to lcd funtion void LCD_String_xy(char,char,char*); //send row position and string to lcd funtion void LCD_Command (char cmd) // lcd command write funtion { LCD_Data_Port=cmd; // write command daata to lcd data port.. LCD_Command_Port&=~((1<<RS)|(1<<RW)); //RS low (command reg.) RW low (write) LCD_Command_Port|=(1<<EN); // high to low transition on EN _delay_ms(1); LCD_Command_Port&=~(1<<EN); _delay_ms(2); // wait little bit } void LCD_Char(char char_data) //LCD data write function { LCD_Data_Port = char_data; //write data to lce data port LCD_Command_Port &=~(1<<RW); //RW low (write) LCD_Command_Port |=((1<<EN)|(1<<RS)); //RS high (data reg). high to low transis=tion on EN _delay_ms(1); LCD_Command_Port &=~(1<<EN); _delay_ms(1); //wait little bit } void LCD_Init(void) { LCD_Command_Dir|=(1<<RS)|(1<<RW)|(1<<EN); //LCD COmmand port direction as o/p LCD_Date_Dir =0xFF; //lcd data port direction as o/p _delay_ms(20); // lcd power up time to get things ready its always >15ms LCD_Command(0x38); //initialize 16*2 lcd in 8bit mode LCD_Command(0x0C);//display on, cursor off command LCD_Command(0x06);//auto increment cursor LCD_Command(0x01);//clear lcd command LCD_Command(0x80); //8 is for first line and 0 is for 0th position } void LCD_String(char *str) // send string to lcd fun { int i; for (i=0;str[i]!=0;i++) //send each char of string til null { LCD_Char(str[i]); } } void LCD_String_xy(char row,char pos,char *str) { if(row==1) LCD_Command((pos & 0x0F)|0x80); // command of 1st row and required position<16 else if (row==2) LCD_Command((pos &0X0F)|0XC0); // 2ND ROW AND REQUIRED POSITION<16 LCD_String(str); //LCD string fun } #define Tigger_pin PA0 // trigger pin int timeroverflow =0; ISR (TIMER1_OVF_vect) { timeroverflow++; // TIMER OVERFLOW COUNT } int main(void) { char string[10]; long count; double distance; DDRA = 0X01; //trigger pin as output PORTB =0xFF; // turn on pull up LCD_Init(); LCD_String_xy(1,0,"ultrasonic"); //sei(); // global interrupt TIMSK =(1<<TOIE1);// ENABLEE TIMER1 OVERFLOW INTERRUPTS TCCR1A = 0; // SET ALL BIT TO ZERO NORMAL OPERATION while (1) { PORTA |=(1<< Tigger_pin); _delay_ms(10); PORTA &=(~(1<<Tigger_pin)); TCNT1=0; //clear timer counter TCCR1B =0x41; //rising edge, no prescaler TIFR = 1<<ICF1; //clear icp flag //input capture flag TIFR = 1<<TOV1; //clear timer overflow flag while ((TIFR &(1<<ICF1)) == 0); // wait for rising edge TCNT1=0; //clear timer counter TCCR1B=0x01;//on falling edge,no prescaler TIFR=1<<ICF1; //clear icp flag (i/p capture flag TIFR=1<<TOV1;// clear timer overfloer flag timeroverflow =0; // clear timer overflow count while((TIFR &(1<<ICF1))==0); // Wait for fallling edge count = ICR1+(65535*timeroverflow); // take count // 8MHZ timer freq, sound speed=343m/s distance = (double) count/466.47; dtostrf(distance,2,2,string); //distance to string strcat (string,"cm"); //concat unit i e. cm LCD_String_xy(2,0,"Dist= "); LCD_String_xy(2,7,string); //print distance _delay_ms(200); } }