Challenge

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

Main program:

#include <avr/io.h> //input and output register of avr ,preprocessor
#define F_CPU 16000000ul // CPU crystal frequency
#include <util/delay.h> // delay function
#include <avr/interrupt.h> // external interrupt pin

#define lcd_data_dir DDRD //define lcd data port direction
#define lcd_command_dir DDRC //define lcd command port direction
#define lcd_data_port PORTD //lcd data port
#define lcd_command_port PORTC // lcd data port
#define EN PC2 // enable pin
#define RW PC3 //define read/write pin
#define RS PC // register select

void lcd_command(char);
void lcd_char(char);
void lcd_init(void);
void lcd_string(char*);
void lcd_string_xy (char,char,char*);
void lcd_command (char cmd)
{
lcd_data_port = cmd;
lcd_command_port&=~((1<<RS)|(1<<RW));
lcd_command_port|= (1<<EN);
_delay_ms(1);
lcd_command_port&=~(1<<EN);
_delay_ms(2);
}
void lcd_char(char char_data)
{
lcd_data_port= char_data;
lcd_command_port&=~(1<<RW);
lcd_command_port|=((1<<EN)|(1<<RS));
_delay_ms(1);
lcd_command_port&=~(1<<EN);
_delay_ms(1);
}
void lcd_init(void) //
{
lcd_command_dir|= (1<<RS)|(1<<RW)|(1<<EN);
lcd_data_dir = 0xff;
_delay_ms(20);
lcd_command(0x38);
lcd_command(0x0C);
lcd_command(0x06);
lcd_command(0x01);
lcd_command(0x80);
}
void lcd_string(char *str)
{
int i;
for(i=0;str[i]!=0;i++)
{
lcd_char (str[i]);
}
void lcd_string_xy(char row,char pos,char *str)
{
if (row == 1)
lcd_command((pos & 0x0f)|0x80);
else if (row == 2)
lcd_command((pos & 0x0f)|(0xC0));
lcd_string(str);
}
#define trigger_pin PA0
int TimerOverFlow = 0; //variable declaration
ISR(TIMER1_OVF_vect)
{
TimerOverFlow++; //increment timer overflow count
}
int main (void)
{
char string[10];
long count;
double distance;
DDRA = 0x01; //trigger pin as output
PORTB = 0xff; //pull-up resister turn on
lcd_init();
lcd_string_xy(1,0,"ultrasonic");
sei();
TIMSK = (1<< TOIE1);
TCCR1A = 0;
while(1)
{
PORTA |= (1<< trigger_pin); //set bit
_delay_ms(10);
PORTA &=(~(1<< trigger_pin)); //clear bit
TCNT1 = 0; //clear timer counter
TCCR1B = 0x41; //rising edge,no pre scaler
TIFR = 1<<ICF1;
TIFR = 1<<TOV1;
//calculate width of echo
while ((TIFR & (1<<ICF1)) == 0);
TCNT1 = 0;
TCCR1B = 0x01;
TIFR = 1<<ICF1;
TIFR = 1<<TOV1;
TimerOverFlow = 0;
while ((TIFR & (1<< ICF1))== 0); //falling edge
count = ICR1 +(65535 * TimerOverFlow);
distance = (double)count/466.47; //distance formula
dtostrf(distance,2,2,string); //distance to string
strcat(string,"CM"); //count unit or centimeter
lcd_string_xy(2,0,"DIST = "); //function displayed in the LCD
lcd_string_xy(2,7,string); //print distance of the object
_delay_ms(200); //delay function
}
}

stimulation window:

The circuit without the power supply

The working condition of the circuit and displays the distance .

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.