Project 2 - Measuring distance of an object using ultrasonic sensor (HC-SR04)
OFFLINE BATCH-1
#include <avr/io.h>
#include <avr/interrupt.h>
#define F_CPU 8000000
#include <util/delay.h>
#include <string.h>
#include <stdlib.h>
#define LCD_data_dir DDRD //LCD data port direction
#define LCD_cmd_dir DDRC //LCD command port direction register
#define LCD_data_port PORTD //LCD data port
#define LCD_cmd_port PORTC //LCD command port
#define EN PC2 //enable signal pin
#define RW PC3 //read/write signal pin
#define RS PC4 //register select signal pin
void LCD_cmd(char); //command write fn declaration
void LCD_char(char); //LCD data write fn
void LCD_init(void); //LCD initialise function
void LCD_string(char*); //send string to LCD fn
void LCD_string_xy(char,char,char*) ; //sending row,position and string to LCD fn
void LCD_cmd(char cmd)
{
LCD_data_port=cmd; //command data to LCD data port
LCD_cmd_port &=~((1<<RS)|(1<<RW)); //Making RS and RW low
LCD_cmd_port|=(1<<EN); //high to low transition on EN
_delay_ms(1);
LCD_cmd_port&=~(1<<EN);
_delay_ms(3);
}
void LCD_char(char char_data) //LCD data write function
{
LCD_data_port=char_data; //writing data to LCD data port
LCD_cmd_port&=~(1<<RW); //Making RS and RW low
LCD_cmd_port|=(1<<EN)|(1<<RS); //high to low transition on EN, RS high
_delay_ms(1);
LCD_cmd_port&=~(1<<EN);
_delay_ms(1);
}
void LCD_init(void)
{
LCD_cmd_dir|=(1<<RS)|(1<<RW)|( 1<<EN); //LCD cmd port direction as output
LCD_data_dir=0xff; //LCD data port direction as output
_delay_ms(20);
LCD_cmd(0X38);
_delay_ms(5); //2line and 5X7 matrix
LCD_cmd(0X02);
_delay_ms(5); //
LCD_cmd(0X01);
_delay_ms(5); //clear LCD command
LCD_cmd(0X0C);
_delay_ms(5); //display on, cursor off command
LCD_cmd(0X06);
_delay_ms(5); //increment cursor automatically
LCD_cmd(0X80);
_delay_ms(5); //8is for first line&0 denotes the zero position
}
void LCD_string(char* str)
{
int i;
for(i=0;str[i]!=0;i++) //string will be sent till it reaches NULL
{
LCD_char(str[i]);
}
}
void LCD_string_xy(char row,char pos,char *str)
{
if(row==1)
LCD_cmd((pos&0x0f)|0x80); //first row and position<16
else if(row==2)
LCD_cmd((pos&0x0f)|0xC0); //second row and position<16
LCD_string(str); //LCD string function called
}
#define Trigger_pin PA0 //trigger pin
int TimeOverflow=0;
ISR(TIMER0_OVF_vect)
{
TimeOverflow++; //increment the timer overflow
}
int main(void)
{
char string[10];
long count;
double distance;
DDRA=0X01; //Trigger point as output
PORTB=0xff; //turn on pull up
LCD_init();
LCD_string_xy(1,0,"Ultrasonic" );
sei(); //set global interrupt
TIMSK=(1<<TOIE1); //enable timer1
TCCR1A=0;
while (1)
{
PORTA|=(1<<Trigger_pin);
_delay_ms(10);
PORTA&=(~(1<<Trigger_pin));
TCNT1=0; //clear timer counter
TCCR1B=0x41; //no prescalar,capture on rising edge
TIFR=1<<ICF1; //clear ICP flag
TIFR=1<<TOV1; //clear timer overflow flag /*calculating width of echo by input capture(ICP)*/
while ((TIFR & (1 << ICF1)) == 0); // Wait for rising edge
TCNT1 = 0; // Clear Timer counter
TCCR1B = 0x01; // Capture on falling edge, No prescaler
TIFR = 1<<ICF1; // Clear ICP flag (Input Capture flag
TIFR = 1<<TOV1; // Clear Timer Overflow flag
TimeOverflow = 0; // Clear Timer overflow count
while ((TIFR & (1 << ICF1)) == 0); // Wait for falling edge
count = ICR1 + (65535 * TimeOverflow); // Take count /* 8MHz Timer freq, sound speed =343 m/s */
distance = (double)count / 466.47;
dtostrf(distance,2,2,string); // distance to string
strcat(string,"cm"); // Concatenation unit i.e.cm
LCD_string_xy(2,0,"Dist = ");
LCD_string_xy(2,7,string); // Print distance
_delay_ms(200);
}
}
#include <avr/interrupt.h>
#define F_CPU 8000000
#include <util/delay.h>
#include <string.h>
#include <stdlib.h>
#define LCD_data_dir DDRD //LCD data port direction
#define LCD_cmd_dir DDRC //LCD command port direction register
#define LCD_data_port PORTD //LCD data port
#define LCD_cmd_port PORTC //LCD command port
#define EN PC2 //enable signal pin
#define RW PC3 //read/write signal pin
#define RS PC4 //register select signal pin
void LCD_cmd(char); //command write fn declaration
void LCD_char(char); //LCD data write fn
void LCD_init(void); //LCD initialise function
void LCD_string(char*); //send string to LCD fn
void LCD_string_xy(char,char,char*)
void LCD_cmd(char cmd)
{
LCD_data_port=cmd; //command data to LCD data port
LCD_cmd_port &=~((1<<RS)|(1<<RW)); //Making RS and RW low
LCD_cmd_port|=(1<<EN); //high to low transition on EN
_delay_ms(1);
LCD_cmd_port&=~(1<<EN);
_delay_ms(3);
}
void LCD_char(char char_data) //LCD data write function
{
LCD_data_port=char_data; //writing data to LCD data port
LCD_cmd_port&=~(1<<RW); //Making RS and RW low
LCD_cmd_port|=(1<<EN)|(1<<RS); //high to low transition on EN, RS high
_delay_ms(1);
LCD_cmd_port&=~(1<<EN);
_delay_ms(1);
}
void LCD_init(void)
{
LCD_cmd_dir|=(1<<RS)|(1<<RW)|(
LCD_data_dir=0xff; //LCD data port direction as output
_delay_ms(20);
LCD_cmd(0X38);
_delay_ms(5); //2line and 5X7 matrix
LCD_cmd(0X02);
_delay_ms(5); //
LCD_cmd(0X01);
_delay_ms(5); //clear LCD command
LCD_cmd(0X0C);
_delay_ms(5); //display on, cursor off command
LCD_cmd(0X06);
_delay_ms(5); //increment cursor automatically
LCD_cmd(0X80);
_delay_ms(5); //8is for first line&0 denotes the zero position
}
void LCD_string(char* str)
{
int i;
for(i=0;str[i]!=0;i++) //string will be sent till it reaches NULL
{
LCD_char(str[i]);
}
}
void LCD_string_xy(char row,char pos,char *str)
{
if(row==1)
LCD_cmd((pos&0x0f)|0x80); //first row and position<16
else if(row==2)
LCD_cmd((pos&0x0f)|0xC0); //second row and position<16
LCD_string(str); //LCD string function called
}
#define Trigger_pin PA0 //trigger pin
int TimeOverflow=0;
ISR(TIMER0_OVF_vect)
{
TimeOverflow++; //increment the timer overflow
}
int main(void)
{
char string[10];
long count;
double distance;
DDRA=0X01; //Trigger point as output
PORTB=0xff; //turn on pull up
LCD_init();
LCD_string_xy(1,0,"Ultrasonic"
sei(); //set global interrupt
TIMSK=(1<<TOIE1); //enable timer1
TCCR1A=0;
while (1)
{
PORTA|=(1<<Trigger_pin);
_delay_ms(10);
PORTA&=(~(1<<Trigger_pin));
TCNT1=0; //clear timer counter
TCCR1B=0x41; //no prescalar,capture on rising edge
TIFR=1<<ICF1; //clear ICP flag
TIFR=1<<TOV1; //clear timer overflow flag /*calculating width of echo by input capture(ICP)*/
while ((TIFR & (1 << ICF1)) == 0); // Wait for rising edge
TCNT1 = 0; // Clear Timer counter
TCCR1B = 0x01; // Capture on falling edge, No prescaler
TIFR = 1<<ICF1; // Clear ICP flag (Input Capture flag
TIFR = 1<<TOV1; // Clear Timer Overflow flag
TimeOverflow = 0; // Clear Timer overflow count
while ((TIFR & (1 << ICF1)) == 0); // Wait for falling edge
count = ICR1 + (65535 * TimeOverflow); // Take count /* 8MHz Timer freq, sound speed =343 m/s */
distance = (double)count / 466.47;
dtostrf(distance,2,2,string); // distance to string
strcat(string,"cm"); // Concatenation unit i.e.cm
LCD_string_xy(2,0,"Dist = ");
LCD_string_xy(2,7,string); // Print distance
_delay_ms(200);
}
}