Project 2 - Measuring distance of an object using ultrasonic sensor (HC-SR04)
#include <avr/io.h> //used headre file for avr function
#define F_CPU 16000000 //cpu cycle has been definer
#include <util/delay.h> //include header file for delay function
#include <stdlib.h>
#define LCD_PORT PORTD //port d define as lcdport
#define RS_HIGH PORTB |= (1<<0); //enable rs pin as high
#define RS_LOW PORTB &= ~(1<<0); //enable rs pin as low
#define RW_HIGH PORTB |= (1<<1);
#define RW_LOW PORTB &= ~(1<<1);
#define EN_HIGH PORTB |= (1<<2); //enable pin defines as high
#define EN_LOW PORTB &= ~(1<<2);//enable pin defines as low
int distance = 0;
#define Delay 600000
//commands to run when the LCD RS bit is LOW
void LCD_COMMAND (unsigned char cmd)
{
LCD_PORT = cmd;
RS_LOW; //rs set aslow for enable the cmd
RW_HIGH; //high pulse for accpting as cmd
EN_HIGH;
_delay_ms(1);
EN_LOW;
}
void LCD_DATA (unsigned char data) //define function for lcd data
{
LCD_PORT = data;
RW_LOW;//low for accepting as data
RS_HIGH;//high for genrate pulse
EN_HIGH;
_delay_ms(1);
EN_LOW;
}
//Commands to initialize the LCD display
void LCD_INIT()
{
RW_HIGH;
RS_HIGH;
EN_HIGH;
LCD_PORT = 0xff;
LCD_COMMAND(0x38); //define 2lines and5 *7 matrix
_delay_ms(5);
LCD_COMMAND(0x02);//return home
_delay_ms(5);
LCD_COMMAND(0x01);// clear screen
_delay_ms(5);
LCD_COMMAND(0x0C);//display in cursor off
_delay_ms(5);
LCD_COMMAND(0x06);///increment cursor
_delay_ms(5);
LCD_COMMAND(0x80);// force cursor to begin with 2nd line
_delay_ms(5);
}
void CURSOR_POS(char x, char y) //function defined for set the cursor
{
if((x<1)|(x>2)||(y<1)|(y>16))
{
x = 1;
y = 1;
}
if(x == 1)
{
LCD_COMMAND(0x7F+y); //to start from 1st line
}
else
{
LCD_COMMAND(0xBF+y); //to start from 2nd line
}
}
void LCD_OUT(char x, char y, char* str) //function defined for print
{
CURSOR_POS(x, y); //function called here
while(*str)//loop will run till null condition
{
LCD_DATA(*str);
str++;
}
}
void SENSOR_Init()
{
DDRC |= (1<<0); //c0set as output
}
void SENSOR_Trigger() //making the trigger pin high to low after 15 microseconds
{
PORTC |= (1<<0); //c0 generated the high
_delay_us(15);
PORTC &= ~(1<<0);//c0 generated the low
}
uint16_t PULSE_WIDTH() //function used for calculation pulse time taken
{
uint32_t i, result;
for(i=0; i<Delay; i++) //loop will run till delay
{
if(!(PINC & (1<<1))) //if echo pin is low or not
continue;
else
break;
}
//checking if timer value is equal to delay given
if(i==Delay)
return 0;
TCCR1A = 0x00; //bit is claer
TCCR1B = (1<<CS11);//using pre scaler
TCNT1 = 0x00; //timer configure
for(i=0; i<Delay;i++)
{
if(PINC & (1<<1)) //if echo pin becomes low
{
if(TCNT1 > Delay)
break;
else
continue;
}
else
break;
}
if(i==Delay)
return 0;
result = TCNT1;
TCCR1B = 0x00;
//if timer has pass out
if(result > Delay)
return 0;
else
return (result>>1);
}
int main()
{
LCD_INIT();//lcd initialization
char numberstring[4]; //define as string
while(1)
{
uint16_t r;
_delay_ms(100);
SENSOR_Init(); //function called here
while(1)
{
SENSOR_Trigger(); //trigering will start
r = PULSE_WIDTH(); //data will stored in r
if(r==0)//no value
{
CURSOR_POS(1,1);
LCD_OUT(1,1,"ERROR!");//no value
}
else
{
distance = (r*0.034/2.0);
LCD_OUT(1,0,"Distance");
itoa(distance, numberstring, 10); //as received data in integer so converted here
LCD_OUT(2,4,numberstring);
LCD_OUT(2,10,"cm");
_delay_ms(30);
}
}
}
}