Environment
- Ubuntu 16.04
- Pololu 3pi Robot (ATmega328P)
Polling
#include <avr/io.h>
int main(void) {
DDRD = 0b00000010;
PORTD &= 0b11111101;
for(;;) {
PORTD ^= 0b00000010;
uint64_t i = 0;
for (i = 0; i < 150000; i++);
}
}
Interrupt
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdbool.h>
#include <util/atomic.h>
volatile bool update = false;
ISR(TIMER1_OVF_vect) {
update = true;
}
int main(void) {
DDRD = 0b00000010;
PORTD &= 0b11111101;
TCCR1B |= 0b00000100;
TIMSK1 |= _BV(TOIE1);
sei();
for(;;) {
if (update) {
PORTD ^= 0b00000010;
update = false;
}
}
}
Reference