Challenge

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

OFFLINE 
 
 
#include
#include
#define F_CPU 8000000
#include
#include
#include
 
#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<RW));             //Making RS and RW low
LCD_cmd_port|=(1<
_delay_ms(1);
LCD_cmd_port&=~(1<
_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<
LCD_cmd_port|=(1<                //high to low transition on EN, RS high
_delay_ms(1);
LCD_cmd_port&=~(1<
_delay_ms(1);
}
 
 
void LCD_init(void)
{
LCD_cmd_dir|=(1<1<
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,"Ultrsonic");
 
sei();                                         //set global interrupt
TIMSK=(1<
TCCR1A=0;                          
 
    while (1) 
    {
    PORTA|=(1<
_delay_ms(10);
PORTA&=(~(1<
TCNT1=0;                                        //clear timer counter
TCCR1B=0x41;                                    //no prescalar,capture on rising edge
TIFR=1<
TIFR=1<
 
/*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<
TIFR = 1<
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);
}
}
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.