Skip to content

Instantly share code, notes, and snippets.

@dagon666
Created September 26, 2013 20:59
Show Gist options
  • Save dagon666/6720470 to your computer and use it in GitHub Desktop.
Save dagon666/6720470 to your computer and use it in GitHub Desktop.
Arduino Timer Delay example
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/power.h>
#define F_CPU 16000000UL
static volatile uint32_t duration = 0x00;
ISR(TIMER0_COMPA_vect) {
if (!duration) {
TIMSK0 &= _BV(OCIE0A);
TCCR0B = 0x00;
}
else {
duration--;
}
}
void tdelay_ms(uint16_t a_delay) {
sei();
power_timer0_enable();
TCCR0A = 0x02;
// prescaler = 64
TCCR0B = 0x03;
// divide frequency further on, by 125
OCR0A = 0x7c;
TCNT0 = 0x00;
duration = a_delay;
// enable interrupt
TIMSK0 |= _BV(OCIE0A);
while (duration);
}
int main(void)
{
DDRB = 0xff;
PORTB = 0x00;
while (1) {
PORTB ^= 0xff;
tdelay_ms(1000);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment