Challenge

Project 2 - Measuring distance of an object using ultrasonic sensor (HC-SR04)

/*
* HC-SR04.c
*
* Created: 03-02-2023 07:21:10
* Author : Ajith
*/
#include <avr/io.h>//standard input output library
#define F_CPU 16000000L//micro controller frequency
#include <avr/interrupt.h>//library for interrupt
#include <util/delay.h>//library for delay function
#include <string.h>//library for string function
#define lcdport PORTD//for display
#define en_high PORTC |=(1<<PC4);//for enable
#define en_low PORTC &=~(1<<PC4);//for disable
#define rs_high PORTC |=(1<<PC5);//for register select (data)
#define rs_low PORTC &=~(1<<PC5);//for register select(command)
volatile unsigned int a,b,c;//global variable for ICR1 capture
ISR (TIMER1_CAPT_vect)
{
if((TCCR1B &(1<<ICES1)) == (1<<ICES1))
{ //Check if capture for is active for rising mode
a = ICR1; //store the capture value into variable a
TCCR1B &= ~(1<<ICES1); //Change into falling edge mode
}
else if((TCCR1B&(0x00)) == 0)
{ //Check if capture for is active for falling mode
b = ICR1; //store the capture value into variable b
TCCR1B |= (1<<ICES1); //Change into falling edge mode
c = b-a; // Subtract the a register from b to get the difference to store in c variable.
}
}
void trigger()//function for giving trigger pulse to the ultrasonic sensor
{
PORTC |=(1<<6);//making a high pulse
_delay_ms(15);
PORTC &=~(1<<6);//making a low pulse
}
void lcd_cmd(unsigned char cmd)//function for lcd command
{
rs_low;
lcdport=cmd;//writting the command on lcd
en_high;
_delay_ms(1);
en_low;
}
void lcd_data(unsigned char data)//function for writting data for lcd
{
rs_high;
lcdport=data;
en_high;
_delay_ms(1);
en_low;
}
void lcd_init()//function for initialising lcd
{
lcd_cmd(0x38);_delay_ms(1);
lcd_cmd(0x02);_delay_ms(1);
lcd_cmd(0x01);_delay_ms(1);
lcd_cmd(0x0c);_delay_ms(1);
lcd_cmd(0x06);_delay_ms(1);
lcd_cmd(0x80);_delay_ms(1);
}
void cursor_pos(unsigned int x,unsigned int y)//setting the cursor position
{
if((x>2||x<1) || (y>16||y<1))
{
x=1;
y=1;
}
if(x==1)
lcd_cmd(0x7F+y);
else
lcd_cmd(0xBF+y);
}
void lcd_print(unsigned int x,unsigned int y,unsigned char*str)//function for printing string data on lcd
//with row and coloumn as argument
{
unsigned int i=0;
if(x==0||y==0)
{
cursor_pos(1,1);
}
else
{
cursor_pos(x,y);
}
while(str[i]!=0)
{
lcd_data(str[i]);
i++;
}
}
void convert_to_display(char x,char y,int value ,int digits)//for displaying digits on screen
{
unsigned int temp;
unsigned int unit;
unsigned int tens;
unsigned int hundreds;
unsigned int thousands;
unsigned int millions;
unsigned int flag=0;
if((x==0) || (y==0))//realocating cursor position in (1,1)
cursor_pos(1,1);
else//realocating cursor position in (row,coloum)
cursor_pos(x,y);
if(digits==5 ||flag==1)
{
millions=value/10000+48;//digital value is converted into ascii value
lcd_data(millions);
flag=1;
}
if(digits==4 ||flag==1)
{
temp=value/1000;//digital value is converted into ascii value
thousands=temp%10+48;
lcd_data(thousands);
flag=1;
}
if(digits==3 ||flag==1)
{
temp=value/100;//digital value is converted into ascii value
hundreds=temp%10+48;
lcd_data(hundreds);
flag=1;
}
if(digits==2 ||flag==1)
{
temp=value/10;//digital value is converted into ascii value
tens=temp%10+48;
lcd_data(tens);
flag=1;
}
if(digits==1 ||flag==1)
{
unit=value%10+48;//digital value is converted into ascii value
lcd_data(unit);
}
if(digits>5)
{
lcd_print(1,1,"error");
}
}
// //////////////////////////
int main(void)
{
unsigned int period=0;
DDRD=0xff;//making portd as output
DDRC |=(1<<5)|(1<<4)|(1<<6);
DDRB=(0<<0);//making pb0 as input
TCCR1A=0;//configuring the timers
TCCR1B=0x41;
TIMSK1=0b00100000;//configuring TIMSK register
lcd_init();//function call for initializing the lcd
lcd_print(1,2,"DISTANCE IN CM ");//printing the distance
while (1)
{
TCNT1=0;//making tcnt1 to zero
trigger();//function call to trigger the ultrasonic sensor
sei();//enabling all interrupt functionality
period=(c/447);c//onverting to distance
convert_to_display(2,5,period,3);//displaying the distance in "cm"
}
}
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.