Project 2 - Measuring distance of an object using ultrasonic sensor (HC-SR04)
PROJECT 2
#include
#include
#define F_CPU 16000000//this Specifies the CPU frequency for the delay functions.
#define FOSC 1843200//this Specifies the oscillator frequency.
#define Baudrate 9600//this Specifies the baud rate for serial communication.
#define UBRR ((FOSC/(16*Baudrate))-1)//this Calculates the value for USART Baud Rate Register (UBRR).
#include
#define TRIGGER_PIN PORTB && 0x02//this Defines the trigger pin as the second bit of PORTB.
#define ECHO_PIN PORTB && 0x01//this Defines the echo pin as the first bit of PORTB.
unsigned int pulse_width = 0;//this variable used to store the pulse width.
void uart_init()
{
UBRR0H = UBRR << 8;
UBRR0L = UBRR;
UCSR0B = (1 << TXEN0) | (1 << RXEN0);//here enabling the transmitter and receiver
UCSR0C = (1 << UCSZ00) | (1 << UCSZ01);//there registers sets high by which 8 bit data can operate
}
void usart_transmit(unsigned char data){
while ((UCSR0A && (1 << UDRE0))==0);//when the UDR register gets empty then the UDR register will goinng to receive th data
UDR0 = data;
}
void send_string( char* str)//this function is used for printing any sentence type data and this coding is fixed for printing any char data bit wise
{
while (*str) {
usart_transmit(*str);
str++;
}
}
void send_number(unsigned int num)//this function is used for printing the integer data by storing the data one by one bit operation
{
unsigned int n;
do {
n=num % 10 + 48;// here used 48 i.e. noting but the ascii value of 0 and that value will going to store in n variable
num= num/ 10;
usart_transmit(n);
} while (num > 0);
}
void trigger()// this function coding is fixed when we are using the ultrasonic wave for finding the distance
{
uart_init();// this is used for giving the baudrate of 9600
PORTB |=(1 << 1);// this PB1 pin used as triggered pin and this OC1A
_delay_us(25);// here we must be use delay of at least 15 micro second because if we take less then of 15us then our echo system don't works
PORTB &= ~(1 << 1);
}
void timer1_init() {
ACSR &= ~(1 << ACIC);//Analog Comparator Input Capture Enable , this register used because we are receiving the input via potentiometer
TIFR1 |= (1 << ICF1);//Timer/Counter1, Input Capture Flag, when ever we are using ICP1 pin at that time we need to set this register(PB0 pin).
TCNT1 = 0;
TCCR1A = 0;//WGM10, WGM11 sets low here by which we are going to use PWM mode as normal.
TCCR1B &= ~((1 << WGM12) | (1 << WGM13) | (1 << ICES1));//When the ICES1 bit is written to zero, a falling (negative) edge is used as trigger
TCCR1B |= (1 << 2);//for using prescaler from 256
TIMSK1 |= (1 << ICIE1);//for enabling the timer1 interrupt we need to set this register
}
ISR(TIMER1_CAPT_vect)//here we are calling for the timer interrupt for calculating the pulse width
{
pulse_width = ((ICR1 - 12) * 10) / 625;
}
void main(void) {
DDRB |= (1 << TRIGGER_PIN );// here we are making the trigger pin as output pin
DDRB &= ~(1 << ECHO_PIN );//here we are making the echo pin as a input pin
unsigned int distance= 0;
uart_init();
while (1) {
trigger();
timer1_init();
sei();// global interrupt enable
distance = (34*pulse_width)/ 2;// it is simply a formula for calculating the distance according to pulse width
send_string("Distance ");
send_number(distance);
send_string(" cm\r\n");
_delay_ms(40);
}
}