/* * ultrasonicSensor.c * * Created: 17-3-2021 09:47:12 * Author : Sem interrupt op rising edge in echo, dan timer1 aanzetten -> timer1 want 16 bits en willen nauwkeurig afstand kunnen meten, en afstand kan van 2 cm tot 4 m, dus willen zeker zijn dat het past en interrupt zetten op falling edge in echo als falling edge interrupt geeft -> waarde uit timer1 uitlezen en formule gebruiken high level time * velocity (340M/S) / 2 timer1 uitzetten interrupt weer op rising edge van echo zetten */ #define F_CPU 20e6 #include #include #include #include "lcd_control.h" enum interrupt_status {INTERRUPT_FALLING, INTERRUPT_RISING}; static enum interrupt_status int_stat = INTERRUPT_RISING; void wait_us(unsigned int us) { for(int i = 0; i < us; i++) { _delay_us(1); } } void wait_ms(unsigned int ms) { for(int i = 0; i < ms; i++) { _delay_ms(1); } } void ultrasonic_send_pulse() { PORTG = 0x00; // 10 us low pulse wait_us(10); PORTG = 0x01; } ISR(INT0_vect) { // if the interrupt was generated on a rising edge (start sending echo) if (int_stat == INTERRUPT_RISING) { // set interrupt pin 0 on PORTD to falling edge EICRA = 0x02; // set interrupt status int_stat = INTERRUPT_FALLING; } else // else if it was generated on a falling edge (end sending echo) { // set interrupt pin 0 on PORTD to rising edge EICRA = 0x03; // set interrupt status int_stat = INTERRUPT_RISING; } } int main(void) { DDRG = 0xFF; // port g all output. pin 0 is trig, the rest is for debug DDRD = 0x00; // port D pin 0 on input. 0 is echo and also interrupt DDRA = 0xFF; EICRA |= 0x03; // interrupt PORTD on pin 0, rising edge EIMSK |= 0x01; // enable interrupt on pin 0 (INT0) sei(); // turn on interrupt system /* Replace with your application code */ while (1) { ultrasonic_send_pulse(); if (int_stat == INTERRUPT_FALLING) { PORTA = 0xFF; } else { PORTA = 0x00; } wait_ms(100); } }