From a5936d559009b762d3a7aa0bf23aa2757179f07e Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Sun, 30 Jun 2013 14:19:41 -0600 Subject: [PATCH] Finally worked out deciseconds --- Makefile.avr | 2 +- blink.c | 75 +++++++++++++++++++++++++++------------------------- 2 files changed, 40 insertions(+), 37 deletions(-) diff --git a/Makefile.avr b/Makefile.avr index a9011df..a4d0aef 100644 --- a/Makefile.avr +++ b/Makefile.avr @@ -39,4 +39,4 @@ clean: - \ No newline at end of file + diff --git a/blink.c b/blink.c index 02fb07d..745985b 100644 --- a/blink.c +++ b/blink.c @@ -1,39 +1,42 @@ #include -#include - - -int main (void) +#include + +/* 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) + +volatile uint32_t jiffies = 0; + +ISR(TIM1_COMPA_vect) { - unsigned char counter; - /* set PORTB for output*/ - DDRB = 0xFF; - - while (1) - { - /* set PORTB.2 high */ - PORTB = 0xFF; - - /* wait (10 * 120000) cycles = wait 1200000 cycles */ - counter = 0; - while (counter != 10) - { - /* wait (30000 x 4) cycles = wait 120000 cycles */ - _delay_loop_2(30000); - counter++; + jiffies += 1; +} + +int +main(void) +{ + unsigned char counter; + + /* + * set PORTB for output + */ + DDRB = 0xFF; + PORTB = 0x00; + + TCCR1A = 0; + TCCR1B = 0; + TCNT1 = 0; + OCR1A = TICKS_PER_JIFFY - 1; + TCCR1B |= (1 << WGM12); + TCCR1B |= (1 << CS12); + TIMSK1 |= (1 << OCIE1A); + + sei(); + + while (1) { + PORTB = 0xFF * ((jiffies/ 10) % 2); } - - /* set PORTB.2 low */ - PORTB = 0x00; - - /* wait (10 * 120000) cycles = wait 1200000 cycles */ - counter = 0; - while (counter != 10) - { - /* wait (30000 x 4) cycles = wait 120000 cycles */ - _delay_loop_2(30000); - counter++; - } - } - - return 1; -} \ No newline at end of file + + return 0; +}