Challenge

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

#include <avr/io.h>
#define F_CPU 16000000ul
#include <util/delay.h>
#include <avr/interrupt.h>
#include<string.h>
#include<stdlib.h>

#define lcd_data_dir DDRD /Define LCD data port direction/
#define lcd_command_dir DDRC /Define LCD command port direction register/
#define lcd_data_port PORTD /Define LCD data port/
#define lcd_command_port PORTC /Define LCD data port/
#define EN PC2 /Define Enable signal pin/
#define RW PC3 /Define Read/Write signal pin/
#define RS PC4 /Define Register Select(data reg./command reg.)signal pin/

void lcd_command(char); /LCD command write function/
void lcd_char(char); /LCD data write function/
void lcd_init(void); /LCD Initialize function/
void lcd_string(char*); /Send string to LCD function/
void lcd_string_xy (char,char,char*); /Send row,position and string to LCD function/

void lcd_command (char cmd) /LCD command write function/
{
lcd_data_port = cmd; /Write command data to LCD data port/
lcd_command_port&=~((1<<RS)|(1<<RW)); /*Make RS LOW(command reg.),RW LOW(Write)*/
lcd_command_port|= (1<<EN); /*High to Low transition on EN(Enable)*/
_delay_ms(1);
lcd_command_port&=~(1<<EN);
_delay_ms(2); /Wait little bit/

}
void lcd_char(char char_data) /LCD data write function/
{
lcd_data_port= char_data; /Write data to LCD data port/
lcd_command_port&=~(1<<RW); /*Make RW LOW(Write)*/
lcd_command_port|=((1<<EN)|(1<<RS)); /*Make RS HIGH(data reg.)and High to Low transition on EN(Enable)*/
_delay_ms(1);
lcd_command_port&=~(1<<EN);
_delay_ms(1);
}
void lcd_init(void) /LCD Initialize function/
{
lcd_command_dir|= (1<<RS)|(1<<RW)|(1<<EN); /Make LCD command port direction as o/p/
lcd_data_dir = 0xff; /Make LCD data port direction as o/p/
_delay_ms(20); /LCD power up time to get things ready,it should always>15ms/
lcd_command(0x38); /Initialize 16X2 LCD in 8bit mode/
lcd_command(0x0C); /Display ON,Cursor OFF command/
lcd_command(0x06); /Auto Increment cursor/
lcd_command(0x01); /Clear LCD command/
lcd_command(0x80); /8 is for first line and0 is for 0th position/
}
void lcd_string(char str) /*Send string to LCD function/
{
int i;
for(i=0;str[i]!=0;i++) /Send each char of string till the NULL/
{
lcd_char (str[i]); /Call LCD data write/
}

}
void lcd_string_xy(char row,char pos,char str) /*Send string to LCD function/
{
if (row == 1)
lcd_command((pos & 0x0f)|0x80); /*Command of first row */
else if (row == 2)
lcd_command((pos & 0x0f)|(0xC0)); /*Command of Second row */
lcd_string(str); /Call LCD string function/
}
#define trigger_pin PA0 /Trigger pin/
int TimerOverFlow = 0;
ISR(TIMER1_OVF_vect)
{
TimerOverFlow++; /Increment Timer Overflow count/
}
int main (void)
{
char string[10];
long count;
double distance;

DDRA = 0x01; /Make trigger pin as output/
PORTB = 0xff; /Turn on Pull-up/

lcd_init();
lcd_string_xy(1,0,"ultrasonic");

sei(); /Enable global interrupt/
TIMSK = (1<< TOIE1); /Enable Timerl overflow interrupts/
TCCR1A = 0; /Set all bit to zero Normal operation/

while(1)
{
/Give 10us trigger pulse on trig.pin to HC-SR04/
PORTA |= (1<< trigger_pin);
_delay_ms(10);
PORTA &=(~(1<< trigger_pin));

TCNT1 = 0; /Clear Timer counter/
TCCR1B = 0x41; /Capture on rising edge,No prescaler/
TIFR = 1<<ICF1; /*Clear ICP flag(Input Capture flag)*/
TIFR = 1<<TOV1; /Clear Timer Overflow flag/
/*Calculate 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/
TimerOverFlow = 0; /Clear Timer overflow count/

while ((TIFR & (1<< ICF1))== 0); /Wait for falling edge/
count = ICR1 +(65535 * TimerOverFlow); /Take count/
distance = (double)count/466.47;
/8MHz Timer freq,sound speed=343m/s/
dtostrf(distance,2,2,string); /distance to string/
strcat(string,"cm"); /Concat unit Le.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.