Challenge

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

ONLINE LIVE BATCH - 8

1.

/*
* AVR_PROJECT2.c
*
* Created: 28-08-2022 14:51:08
* Author : user
*/


#include <avr/io.h> //we are using I/O registers of AVR
#define F_CPU 16000000L // define the crystal frequency
#include "avr/interrupt.h" // Library for interrupt function
#include <util/delay.h> // Library for delay
#include <string.h> // library for string conversion
#include <stdlib.h> // general purpose standard library

#define lcd_port PORTC //PORTC pins are for LCD data
#define RS_high PORTD |= (1<<0) // d5 pin is used for Register Select(RS)
#define RS_low PORTD &=~ (1<<0) // d5 pin is used for RS
#define EN_high PORTD |= (1<<1) // d6 pin is used for Enable
#define EN_low PORTD &=~ (1<<1) // d6 pin is used for En.

#define Trigger_pin PA0 //Trigger pin

int TimerOverflow = 0;
char string[10];
long count;
double distance;

//command for lcd display
void lcd_cmd(char cmd)
{
lcd_port = cmd; // Send command to LCD
RS_low; // Pull down to set to command mode

// Reset the LCD display
EN_high;
_delay_ms(1);
EN_low;
}

//send the data to lcd
void lcd_data (char data)
{
lcd_port = data; // Send data to LCD
RS_high; // Pull up to set to data mode

// Reset the LCD display
EN_high;
_delay_ms(1);
EN_low;
}

// LCD initialization condition
void lcd_init()
{
lcd_cmd(0x38); // 8 bit 2 line, 5x7 dots
_delay_ms(500);
lcd_cmd(0x02); // Return home
_delay_ms(500);
lcd_cmd(0x01); // Clear display
_delay_ms(5);
lcd_cmd(0x0c); //Display ON cursor OFF
_delay_ms(5);
lcd_cmd(0x06); // Entry mode
_delay_ms(5);
lcd_cmd(0x80); //Force the cursor to start from 1st line
_delay_ms(5);
}

void cursorpos(char x,char y)
{
if((x<1 || x>2) || (y>16 || y<1 )) //condition will checks the 2 rows and 16 columns
{
x=1; //x= row
y=1; //y= column
}
if(x==1)
{
lcd_cmd(0x80 + y); //Force cursor on 1st line
}
else //(x==2)
{

lcd_cmd(0xC0 + y); //Force cursor on 2nd line
}
}


void lcd_out(char x, char y, char *str) //here we are checking which row we want to print, and we print the string
{
cursorpos(x,y); //Check the row
while(*str) // get the string address
{
lcd_data(*str); // send the data to LCD
str++;
}
}

int main(void)
{
DDRC = 0xFF; //Declaring all 8-pins as output for LCD.
DDRA = 0x01; //Make trigger pin as output
sei(); /* Enable global interrupt */
TIMSK = (1 << TOIE1); /* Enable Timer1 overflow interrupts */
TCCR1A = 0; /* Set all bit to zero Normal operation */
lcd_init(); //Initialize lcd
lcd_out(1,1,"ULTRASONIC");
while (1)
{
//Give 10us trigger pulse on trig. pin to HC-SR04
PORTA |= (1 << Trigger_pin);
_delay_us(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
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

// 16MHz Timer freq, sound speed =343 m/s
distance = (double)count / 932.94;

dtostrf(distance, 2, 2, string); // convert float to string
strcat(string, " cm "); // Concatenate the result with CM

//Display the result
lcd_out(2,1,"Dist= ");
lcd_out(2,7,string);
}
}

ISR(TIMER1_OVF_vect)
{
TimerOverflow++; // Increment Timer Overflow count when ICR1 goes beyond 65535
}

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.