#include <avr/io.h> // Including AVR input/output library
#define F_CPU 16000000ul // define CPU frequency
#include <util/delay.h> // Including AVR time (delay) Library
#include <avr/interrupt.h> // Including AVR interrupt library
#include <string.h> // Including string Library
#include <stdlib.h> // Including Standard Library
#define lcd_data_dir DDRD //define lcd_data_dir as PORT-D data register
#define lcd_command_dir DDRC //define lcd_command_dir as PORT-C data register
#define lcd_data_port PORTD //define lcd_data_port as write PORT-D
#define lcd_command_port PORTC //define lcd_command_port as write PORT-C
#define EN PC2 //define enable pin
#define RW PC3 //define read/write pin
#define RS PC4 //define register select pin
void lcd_command(char); //This function is for using LCD in command mode
void lcd_char(char); //This function is to use LCD in data mode
void lcd_init(void); //LCD initialize function
void lcd_string(char*); //This function is for sending string to LCD
void lcd_string_xy (char,char,char*); //This function is for selection of row position
void lcd_command (char cmd)
{
lcd_data_port = cmd;
lcd_command_port&=~((1<<RS)|(1<<RW)); //make RS low and RW low
lcd_command_port|= (1<<EN); //high to low transition on EN PIN of LCD to use it in Command mode
_delay_ms(1); //delay
lcd_command_port&=~(1<<EN); //clear bit
_delay_ms(2); //delay
}
void lcd_char(char char_data)
{
lcd_data_port= char_data; // char_data is coming from string function
lcd_command_port&=~(1<<RW); //make RW low(write)
lcd_command_port|=((1<<EN)|(1<<RS)); //make Rs high to use LCD in data mode and EN high
_delay_ms(1); //delay
lcd_command_port&=~(1<<EN); //clear bit --> EN
_delay_ms(1); //delay
}
void lcd_init(void)
{
lcd_command_dir|= (1<<RS)|(1<<RW)|(1<<EN);
lcd_data_dir = 0xff; //Making PORT-D as output
_delay_ms(20); //wait
lcd_command(0x38); //16x2 LCD 8 bit
lcd_command(0x0C); //display On
lcd_command(0x06); //auto increment cursor
lcd_command(0x01); //clear LCD screen
lcd_command(0x80); //Force cursor to begin 1st line
}
void lcd_string(char *str)
{
int i; //variable declaration
for(i=0;str[i]!=0;i++) //sending each char string till null
{
lcd_char (str[i]); //sending full string to lcd_char function
}
}
void lcd_string_xy(char row,char pos,char *str)
{
if (row == 1)
lcd_command((pos & 0x0f)|0x80); //command of first row and required position <16
else if (row == 2)
lcd_command((pos & 0x0f)|(0xC0)); //command of second row and required position<16
lcd_string(str); //calling lcd_string function
}
#define trigger_pin PA0 //trigger pin --> A0
int TimerOverFlow = 0; //variable declaration
ISR(TIMER1_OVF_vect)
{
TimerOverFlow++; //increment timer overflow count
}
int main (void) //main function
{
char string[10]; //string declaration
long count; //variable declaration
double distance; //variable declaration
DDRA = 0x01; //make trigger pin as output
PORTB = 0xff; //pull-up resister turn on
lcd_init(); //function called for LCD initialization
lcd_string_xy(1,0,"ultrasonic"); //function called
sei(); //enabling interrupts
TIMSK = (1<< TOIE1); //enable timer 1 overflow interrupt
TCCR1A = 0; //set all bit to zero normal position (Control Register A)
while(1)
{
PORTA |= (1<< trigger_pin); // sending a plus to trigger pin
_delay_ms(10); // for 10msec.
PORTA &=(~(1<< trigger_pin)); //clear bit
TCNT1 = 0; //clear timer counter
TCCR1B = 0x41; //capture on rising edge,no pre scaler
TIFR = 1<<ICF1; //Input capture bit of TIFR register
TIFR = 1<<TOV1; //timer overflow flag bit of TIFR register
while ((TIFR & (1<<ICF1)) == 0); //checking ICF1 status till it get 0
TCNT1 = 0; //clear timer counter
TCCR1B = 0x01; //no prescaler
TIFR = 1<<ICF1; //clear ICP flag
TIFR = 1<<TOV1; //clear timer overflow flag
TimerOverFlow = 0; //clear timer overflow count
while ((TIFR & (1<< ICF1))== 0); //checking ICF1 status till it get 0
count = ICR1 +(65535 * TimerOverFlow); //take count
distance = (double)count/466.47; //calculating distance
dtostrf(distance,2,2,string); //distance to string
strcat(string,"cm"); //string concatenate after string 1 get null this will execute
lcd_string_xy(2,0,"dist= "); // Printing
lcd_string_xy(2,7,string); //Print distance
_delay_ms(200); //delay
}
}
