Challenge

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

#include

#define F_CPU 16000000L

#include

#define RisingEdge 0

#define FallingEdge 1

unsigned int rising;

unsigned int falling;

unsigned int period;

#define trigger_pin PB1

void trigger(){

DDRB |= (1<<1>

PORTB |= (1<

_delay_ms(10);

PORTB &=~(1<

}

ISR(TIMER1_CAPT_vect)

{

if (RisingEdge)

{

rising=TCNT1;

TCCR1B = 0x01;

TIMSK1= (1<

}

else if (FallingEdge)

{

falling=TCNT1;

TIMSK1= (1<

TCCR1B = 0x41;

}

}

int main(void)

{

while (1)

{

PORTB |= 0x01;//Enabling pullup

TIMSK1= (1<

TCCR1A = 0;

TCCR1B = 0x41;

TCNT1 = 0;

trigger();

period = falling-rising;

lcd_init();

lcd_print(1,1,period,4);

}

}

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.