Challenge

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

/*
* DistanceMeasurment.c
*
* Created: 24/12/2022
* Author : Noorul Ameen
*/

#define F_CPU 8000000UL /* Define CPU Frequency e.g. here 8MHz */
#include <avr/io.h> /* Include AVR std. library file */
#include <util/delay.h> /* Include inbuilt defined Delay Header file */
#include <avr/interrupt.h> /* Include inbuilt defined Interrupt Header file */
#include <string.h> /* Include String Header file */
#include <stdlib.h> /* Include Standard Library Header file */

#define Trigger_pin PC5 /* PORTC 5th PIN as Trigger Input signal to HC-SR04 */
#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 RS PC0 /* Define Register Select (data/command reg.)pin */
#define EN PC2 /* Define Enable signal pin of LCD*/

/* LCD Command write Function */
void LCD_Command(unsigned char cmnd)
{
LCD_Data_Port= cmnd;
LCD_Command_Port &= ~(1<<RS); /* RS=0 command reg. */
LCD_Command_Port |= (1<<EN); /* Enable pulse */
_delay_us(1);
LCD_Command_Port &= ~(1<<EN);
_delay_ms(3);
}

/* LCD data write function */
void LCD_Char (unsigned char char_data)
{
LCD_Data_Port= char_data;
LCD_Command_Port |= (1<<RS); /* RS=1 Data reg. */
LCD_Command_Port |= (1<<EN); /* Enable Pulse */
_delay_us(1);
LCD_Command_Port &= ~(1<<EN);
_delay_ms(1);
}

/* LCD Initialize function */
void LCD_Init (void)
{
LCD_Command_Dir = 0xFF; /* 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 ON delay always >15ms */

LCD_Command (0x38); /* Initialization of 16X2 LCD in 8bit mode */
LCD_Command (0x0C); /* Display ON Cursor OFF */
LCD_Command (0x06); /* Auto Increment cursor */
LCD_Command (0x01); /* Clear display */
LCD_Command (0x80); /* Cursor at home position */
}

/* Send string to LCD function */
void LCD_String (char *str)
{
int i;
for(i=0;str[i]!=0;i++) /* Send each char of string till the NULL */
{
LCD_Char (str[i]);
}
}

/* Send string to LCD with xy position */
void LCD_String_xy (char row, char pos, char *str)
{
if (row == 0 && pos<16)
LCD_Command((pos & 0x0F)|0x80); /* Command of first row and required position<16 */
else if (row == 1 && pos<16)
LCD_Command((pos & 0x0F)|0xC0); /* Command of first row and required position<16 */
LCD_String(str); /* Call LCD string function */
}

/* LCD clear Function */
void LCD_Clear()
{
LCD_Command (0x01); /* clear display */
LCD_Command (0x80); /* cursor at home position */
}

int TimerOverflow = 0;

/* Interrupt Timer OverFlow Vector */
ISR(TIMER1_OVF_vect)
{
TimerOverflow++; /* Increment Timer Overflow count */
}

/* Main Function */
int main(void)
{
char string[10];
long count;
double distance;

DDRC = (1<<5); /* Make trigger pin as output - PORTC 5th PIN */
PORTB = 0xFF; /* Turn on Pull-up Resistor of PORTB - For PB0 Input Capture */

LCD_Init();
LCD_String_xy(0, 0, " HC-SR04 Sensor");

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

while(1)
{
/* Give 20us trigger pulse on trig. pin to HC-SR04 */
PORTC |= (1 << Trigger_pin);
_delay_us(20);
PORTC &= (~(1 << Trigger_pin));

TCNT1 = 0; /* Clear Timer counter */
TCCR1B = 0x41; /* Capture on rising edge, No prescaler*/
TIFR1 = 1<<ICF1; /* Clear ICP flag (Input Capture flag) */
TIFR1 = 1<<TOV1; /* Clear Timer Overflow flag */

/*Calculate width of Echo by Input Capture (ICP) */

while ((TIFR1 & (1 << ICF1)) == 0);/* Wait for rising edge */

TCNT1 = 0; /* Clear Timer counter */
TCCR1B = 0x01; /* Capture on falling edge, No prescaler */
TIFR1 = 1<<ICF1; /* Clear ICP flag (Input Capture flag) */
TIFR1 = 1<<TOV1; /* Clear Timer Overflow flag */
TimerOverflow = 0;/* Clear Timer overflow count */

while ((TIFR1 & (1 << ICF1)) == 0);/* Wait for falling edge */

count = ICR1 + (65535 * TimerOverflow); /* 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 of Output with unit cm */
LCD_String_xy(1, 0, "Dist = "); /* Print String */
LCD_String_xy(1, 7, string); /* Print Measured 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.