Project 2 - Measuring distance of an object using ultrasonic sensor (HC-SR04)
The implementation of code for “Measuring distance of an object using ultrasonic distance sensor (HC-SR04)
/*
* project-2.c
*
* Created: 11-12-2022 23:13:36
* Author : lavanyajarpula715
*/
#include
#define F_CPU 8000000UL // define the crystal frequency
#include "avr/interrupt.h" // we are using external interrupt pin
#include
#include
#include
#define LCD_Data_Dir DDRD /* DEFINE LCD data port direction*/
#define LCD_Commamd_Dir DDRC /*define lcD command port direction*/
#define LCD_Data_Port PORTD /*define lcD Data port */
#define LCD_Commamd_Port PORTC /*define lcD Data port */
#define EN PC2 /* defiine enable signal pin*/
#define RW PC3 /* defiine read and write signal pin*/
#define RS PC4 /* defiine register select signal pin*/
void LCD_Command (char);//command write function
void LCD_Char (char);//lcd data write function
void LCD_init (void);//lcd initialaise fun
void LCD_String (char*);//send string 2 lcd fun
void LCD_String_XY (char,char, char*);//send row position,string to lcd fun
void LCD_Command(char cmd) //command for lcd display
{
LCD_Data_Port = cmd;//write cammand data to lcd port
LCD_Commamd_Port &= ~((1<
LCD_Commamd_Port &= ~(1<
}
void LCD_Char (char char_data) //lcd data write function
{
LCD_Data_Port = char_data;
LCD_Commamd_Port &= ~(1<
LCD_Commamd_Port &= ~(1<
}
void LCD_init(void)//lcd intialization
{
LCD_Commamd_Dir |= (1<
_delay_ms(20);
LCD_Command (0X38);
LCD_Command (0X0C);
LCD_Command (0X06);
LCD_Command (0X01);
LCD_Command (0X80);
}
void LCD_String(char *str)//send string to lcd
{
int i;
for(i=0;str[i]!=0;i++) //send each char of string till null
{
LCD_Char(str[i]); //call lcd data write
}
}
void LCD_String_XY(char row, char pos, char *str) //send strng to lcd fun
{
if (row == 1)
LCD_Command((pos & 0x0F)|0X80);//command of first row requried position<16
else if(row == 2)
LCD_Command((pos & 0x0F)|0XC0);//com to 2nd row required pos<16
LCD_String(str);
}
#define Trigger_pin PA0 //trigger pin
int Timeroverflow = 0;
ISR(TIMER1_OVF_vect)
{
Timeroverflow ++;//increment timer over flow count
}
int main(void)
{
char string[10];
long count;
double distance;
DDRA = 0x01;//make trigger pin as out
PORTB = 0xFF;//turn on pull up
LCD_init();
LCD_String_XY(1,0,"Ultrasonic");
sei(); //Enable global interrupt
TIMSK = (1 << TOIE1);
TCCR1A = 0;
while (1)
{
//give 10us trigger pulse on trig pin to HC-SR04
PORTA |= (1 << Trigger_pin);
_delay_us(10);
PORTA &= (~(1 << Trigger_pin));
TCNT1 = 0;// CLEAR TIMER COUNTER
TCCR1B = 0X41;//Capture on rising edge,no prescalar value
TIFR = 1<
//cal width eco by inpt capture
while((TIFR & (1 << ICF1)) == 0);//wait for raising edge
TCNT1 = 0;//claer timer counter
TCCR1B = 0X01;//cap falling edge,no prescalar
TIFR = 1<
while((TIFR & (1 << ICF1)) == 0);//wait for falling edge
count = ICR1 + (65535 * Timeroverflow);//take count
distance = (double)count / 356.47;//8mhz timer frq ,sound speed=343 m/s
dtostrf(distance,2,2,string);//dis to string
strcat(string,"cm");
LCD_String_XY(2,0,"Dist=");
LCD_String_XY(2,7,string);//print distance
_delay_ms(200);
}
}

COMPONENTS USED IN SIMULATION:
ATmega32:1
sensor:(HC-SR04)
LCD DISPLAY:1:Hd44780
OUTPUT:
