Project 2 - Measuring distance of an object using ultrasonic sensor (HC-SR04)
#include <avr/io.h> //preprocessor with header file
#define F_CPU 16000000ul //cpu clock cycle
#include <util/delay.h> //preprocesor with the delay header file
#include <avr/interrupt.h> //preprocessor with the interrupt header file
#include <string.h> //string header file
#include <stdlib.h> //standard library header file
#define lcd_data DDRD //Data direction register for port D
#define lcd_command_dir DDRC //Data direction register for port C
#define lcd_data_port PORTD //lcd port for port D
#define lcd_command_port PORTC //lcd port PORTC
#define EN PC2 //pin for enable
#define RW PC3 //pin for read write in LCD
#define RS PC4 //pin for register select
void lcd_command(char); //function for command mode
void lcd_char(char); //function for lcd characters
void lcd_init(void); //function for lcd intialization
void lcd_string(char*); //function for lcd string printing
void lcd_string_xy (char,char,char*); //lcd for string x, y
void lcd_command (char cmd) //called function
{
lcd_data_port = cmd; //cmd in data port
lcd_command_port&=~((1<<RS)|(1<<RW)); //making low the pins
lcd_command_port|= (1<<EN);
_delay_ms(1);
lcd_command_port&=~(1<<EN);
_delay_ms(2);
}
void lcd_char(char char_data) //called function
{
lcd_data_port= char_data; //char data in data port
lcd_command_port&=~(1<<RW); //making pin low
lcd_command_port|=((1<<EN)|(1<<RS));
_delay_ms(1);
lcd_command_port&=~(1<<EN);
_delay_ms(1);
}
void lcd_init(void) //called function fot initialization
{
lcd_command_dir|= (1<<RS)|(1<<RW)|(1<<EN); //pins related for the lcd cmd
lcd_data = 0xff;
_delay_ms(20);
lcd_command(0x38);//commands related to lcd
lcd_command(0x0C);
lcd_command(0x06);
lcd_command(0x01);
lcd_command(0x80);
}
void lcd_string(char *str) //string function
{
int i;
for(i=0;str[i]!=0;i++)
{
lcd_char (str[i]);
}
}
void lcd_string_xy(char row,char pos,char *str) //cursor position related function
{
if (row == 1)
lcd_command((pos & 0x0f)|0x80);
else if (row == 2)
lcd_command((pos & 0x0f)|(0xC0));
lcd_string(str);
}
#define trigger_pin PA0 //another pin for trigger pin
int TimerOverFlow = 0;
ISR(TIMER1_OVF_vect) //interrupt
{
TimerOverFlow++;
}
int main (void)
{
char string[10];
long count;
double distance;
DDRA = 0x01; //data direction register pin as some adddress
PORTB = 0xff;
lcd_init();
lcd_string_xy(1,0,"ultrasonic"); //printing in lcd
sei(); //global interrupt
TIMSK = (1<< TOIE1); //timsk
TCCR1A = 0;
while(1) //infinite loop
{
PORTA |= (1<< trigger_pin);
_delay_ms(10);
PORTA &=(~(1<< trigger_pin));
TCNT1 = 0; //for init timer count
TCCR1B = 0x41; //for controlling the timer counter and controller
TIFR = 1<<ICF1; //timer flow
TIFR = 1<<TOV1;
while ((TIFR & (1<<ICF1)) == 0); //iterating till tifr and icf1 is 0
TCNT1 = 0;
TCCR1B = 0x01;
TIFR = 1<<ICF1;
TIFR = 1<<TOV1;
TimerOverFlow = 0;
while ((TIFR & (1<< ICF1))== 0);
count = ICR1 +(65535 * TimerOverFlow); //storing the value max*timeroverflow
distance = (double)count/466.47; //getting distance by making division count by 466.67
dtostrf(distance,2,2,string); //for lcd
strcat(string,"cm");//cat the string
lcd_string_xy(2,0,"distan="); //printing statement
lcd_string_xy(2,7,string); //printing statement
_delay_ms(200);
}
}