Project 2 - Measuring distance of an object using ultrasonic sensor (HC-SR04)
1)
/*
* AVR PROJECT 2(FINAL).c
*
* Created: 9/11/2023 11:50:23 AM
* Author : adaik
*/
#include
#define F_CPU 16000000L
#include
#include "avr/interrupt.h"
#define trigger_pin PB1
//#include
unsigned int a,b,c;
ISR(TIMER1_CAPT_vect)
{
if((TCCR1B &(1<
{ //Check if capture for is active for rising mode
a = ICR1; //store the capture value into variable a
TCCR1B &= ~(1<
}
else if((TCCR1B&(0x00)) == 0){ //Check if capture for is active for falling mode
b = ICR1; //store the capture value into variable b
TCCR1B |= (1<
c = b-a; // Subtract the a register from b
// Serial.println(c);
}
}
void Capture_Init(void){
//Enable the required timer function
TCCR1A = 0x00; //Normal mode operation
//Set the mode of the timer1 Input capture
TIMSK1 |= (1<
//Set the prescaler for the timer
TCCR1B |= (1<
//Set the edge detection bit
TCCR1B |= (1<
//Set Timer initial value
ICR1 = 0; // ICR1 register is 0;
}
int main()
{
DDRB|=(1<<1>
DDRB&=~(1<<0>
//Serial.begin(9600);
while(1)
{
PORTB|=(1<<1>
_delay_us(15);
PORTB&=~(1<<1>
}
Capture_Init();
}