Created
September 26, 2013 20:59
-
-
Save dagon666/6720470 to your computer and use it in GitHub Desktop.
Arduino Timer Delay example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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