Project 2 - Measuring distance of an object using ultrasonic sensor (HC-SR04)
(This project was taught by Abinaya mam)
Aim:Instead of having any obstacle we have a potentiometer if we vary potentiometer we would get output on display.
oscillpscope used to visualize duty cycle
in this program we are using timer as interrupt
16 bit timer is used.
we are giving input to potentiometer by capturing those input we are displaying it on LCD.
Project-2 Programming
#include <avr/io.h> //header file
#define F_CPU 16000000l //CPU frequency
#include <util/delay.h> // for generating delay in program
#include "avr/interrupt.h" //header for special register
#define lcdport PORTB
#define rs_high PORTD|=(1<<0);
#define rs_low PORTD&=~(1<<0);
#define en_high PORTD|=(1<<1);
#define en_low PORTD&=~(1<<1);
unsigned int tov=0,count,distance;
void lcd_command (unsigned char cmd);
void lcd_data (unsigned char data);
void lcd_init();
void cursorpos(char x,char y);
void lcd_out(char x,char y,char *str);
void lcd_print(char row,char column,unsigned int value,int digits);
int main(void)
{
DDRB=0XFF; //Port B as output
DDRD|=(0<<6); //Port D pin 6 as input from echo pin of sensor
DDRC|=(1<<0); //Port c 0 as output
sei(); // enable global interrupt
lcd_init();
TCCR1A=0X00; //Normal mode
TIMSK|=(1<<2); //we are using timer as interrupt
while(1)
{
PORTC|=(1<<0); // Trigor sensor
_delay_ms(10);
PORTC&=~(1<<0);
TCCR1B=0X41; //ICF Enable no prescaling
TCNT1=0;
TIFR=(1<<ICF1); //enabled input captured flag
TIFR=(1<<TOV1); //overflow flag enabled
tov=0;
while((TIFR&(1<<ICF1))==0); //Checking overflow condition
TCNT1=0;
TCCR1B=0X01;
TIFR=(1<<ICF1);
TIFR=(1<<TOV1);
tov=0;
while((TIFR &(1<<ICF1))==0); //till the overflow comes this process has to be repeated
count=ICR1+(65535*tov); // calculate count for time taken for movement of potentiometer
distance=(unsigned int )count/932.87; // sensor data sheet formula
lcd_print(1,1,distance,4);
lcd_out(1,6,"cm");
}
}
ISR(TIMER1_VI_vect)
{
tov++;
}
void lcd_cmd(unsigned char cmd)
{
lcdport=cmd;
rs_low;
en_low;
_delay_ms(1);
en_high;
}
void lcd_data(unsigned char data)
{
lcdport=data;
rs_high;
en_low;
_delay_ms(1);
en_high;
}
void lcd_init()
{
lcd_cmd(0x38);_delay_ms(5);
lcd_cmd(0x02);_delay_ms(5);
lcd_cmd(0x01);_delay_ms(5);
lcd_cmd(0x0c);_delay_ms(5);
lcd_cmd(0x06);_delay_ms(5);
lcd_cmd(0x80);_delay_ms(5);
lcd_cmd(0xc0);_delay_ms(5);
}
void cursorpos(char x,char y)
{
if((x<1||x>2)||(y<1||y>16))
{
x=1;
y=1;
}
if(x==1)
lcd_cmd(0x7f+y);
else
lcd_cmd(0xbf+y);
}
void lcd_out(char x,char y,char *str)
{
cursorpos(x,y);
while(*str)
{
lcd_data(*str);
str++;
}
}
void lcd_print(char row,char column,unsigned int value,int digits)
{
unsigned int temp,unit,tens,hundred,thousand,million;
unsigned char flag=0;
if (row==0||column==0)
{
cursorpos(1,1);
}
else
{
cursorpos(row,column);
}
if(digits==5||flag==1)
{
million=value/10000+48;
lcd_data(million);
flag=1;
}
if (digits==4||flag==1)
{
temp=value/1000;
thousand=temp%10+48;
lcd_data(thousand);
flag=1;
}
if(digits==3||flag==1)
{
temp=value/100;
hundred=temp%10+48;
lcd_data(hundred);
flag=1;
}
if(digits==2||flag==1)
{
temp=value/10;
tens=temp%10+48;
lcd_data(tens);
flag=1;
}
if (digits==1||flag==1)
{
unit=value%10+48;
lcd_data(unit);
}
if(digits>5)
{
lcd_data('e');
}
}

