Project 2 - Measuring distance of an object using ultrasonic sensor (HC-SR04)
#define F_CPU 16000000UL//frequency of the AVR
#include //AVR preprocessor section
#include //delay library
#include //interrupt service routine library
#include //library for strings
#include // library for the mathematical operations
#define lcd_data_dir DDRD// LCD data direction port as port d
#define lcd_command_dir DDRC//LCD command direction port as port c
#define lcd_data_port PORTD//LCD data port as port d
#define lcd_command_port PORTC////LCD command port as port c
#define EN PC2//enable signal set to port c pin 2
#define RW PC3//read/write select set to port c pin 2
#define RS PC4//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_string_xy(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<
lcd_command_port|=(1<
_delay_ms(1);// wait for 1 millisecond
lcd_command_port&=~(1<
_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<
lcd_command_port|=((1<
_delay_ms(1);// wait for 1 millisecond
lcd_command_port&=~(1<
_delay_ms(1);// wait for 1 millisecond
}/* LCD data write function*/
void lcd_init(void)
{
lcd_command_dir|=((1<
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_string_xy(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 distance;
DDRA=0X01;//Port A as output
PORTB=0Xff;//for turning ON internal pull_resistor
lcd_init();//LCD_init function called
lcd_string_xy(1,0,"ULTRASONIC");//displays at 1 st row 0th position
sei();//enable the global interrupt
TIMSK=(1<
TCCR1A=0;//timer counter control register as low
while (1)
{
PORTA|=(1<
_delay_ms(10);
PORTA&=(~(1<
TCNT1=0;//clear the timer counter
TCCR1B=0X41;//no pre scalar, capture on rising
TIFR=1<
TIFR=1<
while((TIFR &(1<
TCNT1=0;//clear the timer counter
TCCR1B=0X01;//capture on falling edge,no prescalar
TIFR=1<
TIFR=1<
TimeOverFlow=0;//clear timeoverflagflow count
while((TIFR &(1<
count=ICR1+(65536 * TimeOverFlow);//tkae count fromt the 16MHz timer frequency, sound frequency=343m/s
distance=(double)count/466.47;//calcualting the distance
dtostrf(distance,2,2,string);//distance to string
strcat(string,"cm");//concatenated unit of cm
lcd_string_xy(2,0,"dist= ");//displays at 2nd row and 0th position
lcd_string_xy(2,7,string);//display the string at 2nd row ,7 th position
_delay_ms(200);//wait for 200 milliseconds
}
}
Your portfolio, nextShip work like this