Finally worked out deciseconds

This commit is contained in:
Neale Pickett 2013-06-30 14:19:41 -06:00
parent 27a9eb281a
commit a5936d5590
2 changed files with 40 additions and 37 deletions

71
blink.c
View File

@ -1,39 +1,42 @@
#include <avr/io.h> #include <avr/io.h>
#include <util/delay.h> #include <avr/interrupt.h>
/* This only works out for a 16MHz or 8MHz clock */
#define CLOCK_HZ 16000000
#define TICKS_PER_SECOND (CLOCK_HZ / 256)
#define TICKS_PER_JIFFY (TICKS_PER_SECOND / 10)
int main (void) volatile uint32_t jiffies = 0;
ISR(TIM1_COMPA_vect)
{ {
unsigned char counter; jiffies += 1;
/* set PORTB for output*/ }
DDRB = 0xFF;
int
while (1) main(void)
{ {
/* set PORTB.2 high */ unsigned char counter;
PORTB = 0xFF;
/*
/* wait (10 * 120000) cycles = wait 1200000 cycles */ * set PORTB for output
counter = 0; */
while (counter != 10) DDRB = 0xFF;
{ PORTB = 0x00;
/* wait (30000 x 4) cycles = wait 120000 cycles */
_delay_loop_2(30000); TCCR1A = 0;
counter++; TCCR1B = 0;
} TCNT1 = 0;
OCR1A = TICKS_PER_JIFFY - 1;
/* set PORTB.2 low */ TCCR1B |= (1 << WGM12);
PORTB = 0x00; TCCR1B |= (1 << CS12);
TIMSK1 |= (1 << OCIE1A);
/* wait (10 * 120000) cycles = wait 1200000 cycles */
counter = 0; sei();
while (counter != 10)
{ while (1) {
/* wait (30000 x 4) cycles = wait 120000 cycles */ PORTB = 0xFF * ((jiffies/ 10) % 2);
_delay_loop_2(30000); }
counter++;
} return 0;
}
return 1;
} }