All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
PROJECT-2: Aim: To write a program to measure the distance using ultasonic sensot Schematic Diagram: Steps of Programming ATmega16 microcontroller needs to transmit at least 10 us trigger pulse to the HC-SR04 Trig Pin. After getting a trigger pulse, HC-SR04 automatically sends eight 40 kHz sound waves and the microcontroller…
Chandrakumar ADEPU
updated on 24 Jun 2023
PROJECT-2:
Aim: To write a program to measure the distance using ultasonic sensot
Schematic Diagram:
Steps of Programming
Calculation (distance in cm) (H1)
Sound velocity = 343.00 m/s = 34300 cm/s
The distance of Object (in cm) = Sound velocity *Timer/2
= 32300*Timer/2
= 17150*Timer
Now, here we have selected an internal 8 MHz oscillator frequency for ATmega16, with No-presale for timer frequency. Then time to execute 1 instruction is 0.125 us.
So, the timer gets incremented after 0.125 us time elapse.
= 17150 x (TIMER value) x 0.125 x 10^-6 cm
= 0.125 x (TIMER value)/58.30 cm
= (TIMER value) / 466.47 cm
Code:
/*
* Project2_ultrasonic.c
*
* Created: 24-06-2023 08:13:41
* Author : chand
*/
#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_command_dir DDRC
#define lcd_data_port PORTD
#define lcd_command_port PORTC
#define EN PC2
#define RW PC3
#define RS PC4
void lcd_command(char);
void lcd_char(char);
void lcd_init(void);
void lcd_string(char*);
void lcd_string_xy (char,char,char*);
void lcd_command (char cmd)
{
lcd_data_port = cmd;
lcd_command_port&=~((1<<RS)|(1<<RW));
lcd_command_port|= (1<<EN);
_delay_ms(1);
lcd_command_port&=~(1<<EN);
_delay_ms(2);
}
void lcd_char(char char_data)
{
lcd_data_port= char_data;
lcd_command_port&=~(1<<RW);
lcd_command_port|=((1<<EN)|(1<<RS));
_delay_ms(1);
lcd_command_port&=~(1<<EN);
_delay_ms(1);
}
void lcd_init(void)
{
lcd_command_dir|= (1<<RS)|(1<<RW)|(1<<EN);
lcd_data_dir = 0xff;
_delay_ms(20);
lcd_command(0x38);
lcd_command(0x0C);
lcd_command(0x06);
lcd_command(0x01);
lcd_command(0x80);
}
void lcd_string(char *str)
{
int i;
for(i=0;str[i]!=0;i++)
{
lcd_char (str[i]);
}
}
void lcd_string_xy(char row,char pos,char *str)
{
if (row == 1)
lcd_command((pos & 0x0f)|0x80);
else if (row == 2)
lcd_command((pos & 0x0f)|(0xC0));
lcd_string(str);
}
#define trigger_pin PA0
int TimerOverFlow = 0;
ISR(TIMER1_OVF_vect)
{
TimerOverFlow++;
}
int main (void)
{
char string[10];
long count;
double distance;
DDRA = 0x01;
PORTB = 0xff;
lcd_init();
lcd_string_xy(1,0,"ultrasonic");
sei();
TIMSK = (1<< TOIE1);
TCCR1A = 0;
while(1)
{
PORTA |= (1<< trigger_pin);
_delay_ms(10);
PORTA &=(~(1<< trigger_pin));
TCNT1 = 0;
TCCR1B = 0x41;
TIFR = 1<<ICF1;
TIFR = 1<<TOV1;
while ((TIFR & (1<<ICF1)) == 0);
TCNT1 = 0;
TCCR1B = 0x01;
TIFR = 1<<ICF1;
TIFR = 1<<TOV1;
TimerOverFlow = 0;
while ((TIFR & (1<< ICF1))== 0);
count = ICR1 +(65535 * TimerOverFlow);
distance = (double)count/466.47;
dtostrf(distance,2,2,string);
strcat(string,"cm");
lcd_string_xy(2,0,"dist= ");
lcd_string_xy(2,7,string);
_delay_ms(200);
}
}
Output:
-----------------------------------------------********************-----------------------------------------------
Leave a comment
Thanks for choosing to leave a comment. Please keep in mind that all the comments are moderated as per our comment policy, and your email will not be published for privacy reasons. Please leave a personal & meaningful conversation.
Other comments...
Week - 4
Implement control logic of a “washing machine” using Stateflow as per given sequence: If the power supply is available, the system gets activated If the Water supply is not available, stop the process & indicate through LED Soaking time should be 200s followed by Washing time of 100s. Then rinsing…
29 Sep 2023 07:17 AM IST
Project 2 - V&V SW Analysis II
Q:- 1. Perform Static Code Review Analysis for “C:\**\LDRA_workarea\Examples\C_Testbed_examples\Testrain\Testrain.c” Generate Code review report and upload them. Ans: Aim: To gernetate the code review report and upload them. Steps: Steps for White box testing:- Source ->…
18 Aug 2023 01:16 PM IST
Project 1 - V&V SW Analysis - I
Q:- Write a Test plan to test features of a new mobile phone (Blackbox test) that needs to be implemented based on the following requirements. (The product is still under development stage and is yet to be UA (User Acceptance ) tested…
12 Jul 2023 03:17 AM IST
Project 2 - Measuring distance of an object using ultrasonic sensor (HC-SR04)
PROJECT-2: Aim: To write a program to measure the distance using ultasonic sensot Schematic Diagram: Steps of Programming ATmega16 microcontroller needs to transmit at least 10 us trigger pulse to the HC-SR04 Trig Pin. After getting a trigger pulse, HC-SR04 automatically sends eight 40 kHz sound waves and the microcontroller…
24 Jun 2023 09:50 AM IST
Related Courses
0 Hours of Content
Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.
© 2025 Skill-Lync Inc. All Rights Reserved.