Project 2 - Measuring distance of an object using ultrasonic sensor (HC-SR04)
Code:
#include <avr/io.h>//AVR preprocessor section
#include <util/delay.h>//delay library
#define F_CPU 16000000UL//frequency of the AVR
#include <avr/interrupt.h>//interrupt service routine library
#include <string.h>//library for strings
#include <stdlib.h>// library for the mathematical operations
#define lcd_data_dir DDRC// LCD data direction port as port d
#define lcd_command_dir DDRD//LCD command direction port as port c
#define lcd_data_port PORTC//LCD data port as port d
#define lcd_command_port PORTD////LCD command port as port c
#define EN PD7//enable signal set to port c pin 2
#define RS PD6//register select set to port c pin 4
void lcd_cmd(char);//command write function for LCD
void lcd_char(char);//data write function for LCD
void lcd_init(void);//LCD initialization function
void lcd_string(char*);//send string to LCD function
void lcd_out(char,char,char*);//sending the row, position and string to LCD function
void lcd_command(char cmd)
{
lcd_data_port=cmd;//command data to LCD data port
lcd_command_port&=~(1<<RS);//making RS and RW low
lcd_command_port|=(1<<EN);//making the enable pin high
_delay_ms(1);// wait for 1 millisecond
lcd_command_port&=~(1<<EN);//change to low of enable pin
_delay_ms(2);// wait for 1 milliseconds
}/* LCD commands purpose of this function*/
void lcd_char(char char_data)
{
lcd_data_port=char_data;//LCD data write function
lcd_command_port|=((1<<EN)|(1<<RS));//high of EN and RS pins
_delay_ms(1);// wait for 1 millisecond
lcd_command_port&=~(1<<EN);//low of EN pin
_delay_ms(1);// wait for 1 millisecond
}/* LCD data write function*/
void lcd_init(void)
{
lcd_command_dir|=((1<<RS)|(1<<EN));// making the RS,RW and EN high
lcd_data_dir=0Xff;// LCD command port direction as output
_delay_ms(20);// wait for 20 milliseconds
lcd_command(0x38);//2 line and 5x7 matrix
_delay_ms(2);
lcd_command(0x01);//Clear LCD screen
_delay_ms(2);
lcd_command(0x0C);//display ON,cursor OFF
_delay_ms(2);
lcd_command(0x06);//increment cursor
_delay_ms(2);
lcd_command(0x80);//force the cursor to begin from the 1st line
_delay_ms(2);
}/* LCD initialization function*/
void lcd_string(char*str)
{
int i;// declaring the i as int data type
for(i=0;str[i]!=0;i++)
{
lcd_char(str[i]);
}//for string will be sent till it reaches NULL
}// for the purpose of LCD string increment
void lcd_out(char row,char pos,char *str)
{
if(row==1)
lcd_command((pos&0X0f)|(0x80));//first row and position<16
else if(row==2)
lcd_command((pos&0x0f)|(0xC0));//second row and position<16
lcd_string(str);//LCD string function called
}/* LCD string display row and position declaration function*/
#define trigger_pin PA0//setting trigger pin as port A pin 0
int TimeOverFlow=0;
ISR(TIMER1_OVF_vect)
{
TimeOverFlow++;//increment timeoverflow
}/* Interrupt service routine function*/
int main(void)
{
char string[10];
long count;
double dist;
DDRA=0X01;//Port A as output
PORTB=0Xff;//for turning ON internal pull_resistor
lcd_init();//LCD_init function called
lcd_out(1,0,"ULTRASONIC");//displays at 1 st row 0th position
sei();//enable the global interrupt
TIMSK=(1<<TOIE1);//enable timer 1
TCCR1A=0;//timer counter control register as low
while (1)
{
PORTA|=(1<<trigger_pin);// making the trigger pin 0 at Port A high
_delay_ms(10);
PORTA&=(~(1<<trigger_pin));// making the trigger pin 0 at Port A low
TCNT1=0;//clear the timer counter
TCCR1B=0X41;//no pre scalar, capture on rising
TIFR=1<<ICF1;//clear Interrupt capture flag
TIFR=1<<TOV1;//clear timer overflow flag
while((TIFR &(1<<ICF1))==0);//wait for raising edge
TCNT1=0;//clear the timer counter
TCCR1B=0X01;//capture on falling edge,no prescalar
TIFR=1<<ICF1;//clear the interrupt capture flag
TIFR=1<<TOV1;//clear the timeoverflow flag
TimeOverFlow=0;//clear timeoverflagflow count
while((TIFR &(1<<ICF1))==0);//wait for falling edge
count=ICR1+(65536 * TimeOverFlow);//tkae count fromt the 16MHz timer frequency, sound frequency=343m/s
dist=(double)count/466.47;//calcualting the distance
dtostrf(dist,2,2,string);//distance to string
strcat(string,"cm");//concatenated unit of cm
lcd_out(2,1,"dist= ");//displays at 2nd row and 0th position
lcd_out(2,7,string);//display the string at 2nd row ,7 th position
_delay_ms(200);//wait for 200 milliseconds
}
}
Circuit:

Output:
