/* Sample program to run on the Arduino without using the Arduino library. Adam Sampson IO pins 0 - 13 as labelled on the board are port D 0-7, then port B 0-5. See include/avr/iom8.h for the register names. */ #include #include #ifdef __AVR_ATmega8__ #define TCCR0B TCCR0 #define TIMSK0 TIMSK #endif volatile unsigned long tick_count; SIGNAL(TIMER0_OVF_vect) { tick_count++; } unsigned long ticks(const unsigned long millis) { return millis * F_CPU / (128000UL * 64UL * 2UL); } int after(const unsigned long a, const unsigned long b) { return (b - a) < 0x8000000; } unsigned long get_ticks(void) { unsigned long n; /* disable interrupts while reading the time, so we don't get an * inconsistent result */ cli(); n = tick_count; sei(); return n; } void delay(const unsigned long t) { unsigned long m = get_ticks() + ticks(t); while (after(get_ticks(), m)) { /* do nothing */ } } int main(void) { /* enable interrupts */ sei(); tick_count = 0; /* timer 0 prescale 64 */ TCCR0B |= 3 << CS00; /* enable timer 0 overflow interrupt */ TIMSK0 |= _BV(TOIE0); /* pins to output */ DDRB = 0xFF; /* flash pins */ while (1) { PORTB = 0xFF; delay(500); PORTB = 0; delay(500); } }