[ADD] timer, but I don't think it works yet

This commit is contained in:
Sem van der Hoeven
2021-03-18 20:11:44 +01:00
parent 7a3180019e
commit 9f96c762fc
4 changed files with 1410 additions and 197 deletions

View File

@@ -28,6 +28,8 @@ enum interrupt_status {INTERRUPT_FALLING, INTERRUPT_RISING};
static enum interrupt_status int_stat = INTERRUPT_RISING;
uint16_t timer_dist = 0; // time measured by timer;
void wait_us(unsigned int us)
{
for(int i = 0; i < us; i++)
@@ -61,6 +63,9 @@ ISR(INT0_vect)
// set interrupt pin 0 on PORTD to falling edge
EICRA = 0x02;
// reset the time in timer1
TCNT1 = 0x00;
// set interrupt status
int_stat = INTERRUPT_FALLING;
} else
@@ -69,6 +74,9 @@ ISR(INT0_vect)
// set interrupt pin 0 on PORTD to rising edge
EICRA = 0x03;
// read timer1 into time_dist
timer_dist = TCNT1;
// set interrupt status
int_stat = INTERRUPT_RISING;
}
@@ -81,26 +89,34 @@ 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)
TCCR1A = 0b00000000; // initialize timer1, prescaler=256
TCCR1B = 0b00001100; // CTC compare A, RUN
sei(); // turn on interrupt system
init_4bits_mode();
_delay_ms(10);
lcd_clear();
/* Replace with your application code */
while (1)
{
ultrasonic_send_pulse();
if (int_stat == INTERRUPT_FALLING)
{
PORTA = 0xFF;
} else {
PORTA = 0x00;
}
wait_ms(100);
int distance = timer_dist * 340 / 2;
lcd_clear();
lcd_write_int(distance);
wait_ms(1000);
}
}