diff options
author | Lars-Dominik Braun <lars@6xq.net> | 2014-02-24 15:32:57 +0100 |
---|---|---|
committer | Lars-Dominik Braun <lars@6xq.net> | 2014-02-24 15:32:57 +0100 |
commit | b89a708807c72c67b128aa3d684afdbd694f66bc (patch) | |
tree | 9aa15abee88bd19f898217725974824606b495bf | |
parent | e9c1df9237c210ebe1fa6d7578bc7b61e2ab33f0 (diff) | |
download | hourglass-b89a708807c72c67b128aa3d684afdbd694f66bc.tar.gz hourglass-b89a708807c72c67b128aa3d684afdbd694f66bc.tar.bz2 hourglass-b89a708807c72c67b128aa3d684afdbd694f66bc.zip |
Add simple timer that counts one second
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | main.c | 36 | ||||
-rw-r--r-- | timer.c | 56 | ||||
-rw-r--r-- | timer.h | 11 |
4 files changed, 80 insertions, 25 deletions
@@ -2,7 +2,7 @@ MCU = atmega88 all: sanduhr.hex -sanduhr.elf: main.c i2c.c i2c.h uart.c uart.h +sanduhr.elf: main.c i2c.c i2c.h uart.c uart.h timer.c timer.h avr-gcc -std=gnu99 -mmcu=$(MCU) -Os -o $@ $^ sanduhr.hex: sanduhr.elf @@ -10,6 +10,7 @@ #include "i2c.h" #include "uart.h" +#include "timer.h" static void ledInit () { /* set led1,led2 to output */ @@ -31,13 +32,6 @@ static void cpuInit () { CLKPR = 0b00000011; } -volatile unsigned char count; - -ISR(TIMER0_OVF_vect) { - ++count; - //printf ("timer interrupt hit, count now %u\n", count); -} - #define sleepwhile(cond) \ sleep_enable (); \ while (cond) { sleep_cpu (); } \ @@ -61,30 +55,24 @@ int main () { sleepwhile (twr.status == TWST_WAIT); printf ("final twi status was %i\n", twr.status); + timerStart (); + unsigned char seconds = 0; while (1) { uint8_t val[6]; + + sleepwhile (!timerHit ()); + ++seconds; + printf ("running for %u seconds\n", seconds); + if (!twReadMulti (LIS302DL, 0x28, val, 6)) { printf ("cannot start read\n"); } sleepwhile (twr.status == TWST_WAIT); - printf ("%i/%i/%i\n", (int8_t) val[1], (int8_t) val[3], (int8_t) val[5]); - - count = 0; - /* set normal mode timer0 */ - TCCR0A = 0; - /* io clock with 1024 prescaler */ - TCCR0B = (TCCR0B & ~((1 << CS01)) | (1 << CS02) | (1 << CS00)); - /* enable overflow interrupt */ - TIMSK0 = (1 << TOIE0); - - sleepwhile (count < 3); - - /* stop timer (zero clock source) */ - TCCR0B = 0; - - /* XXX: why do we need the delay here? */ - //_delay_ms (250); + printf ("%i/%i/%i\n", (int8_t) val[1], (int8_t) val[3], + (int8_t) val[5]); } + timerStop (); + /* global interrupt disable */ cli (); @@ -0,0 +1,56 @@ +#include <avr/io.h> +#include <avr/interrupt.h> + +#include "timer.h" + +volatile unsigned char count = 0; +volatile unsigned char hit = 0; + +ISR(TIMER0_OVF_vect) { + /* there seems to be no mode of operation which disconnects pin OC0A _and_ + * clears the value */ + ++count; + switch (count) { + case 1: + case 2: + TCNT0 = 0; + break; + + /* three overflows happened, next one is a little shorter: + * F_CPU/prescaler-3*256=208.5625 -> 256-208=48 -> zero-based=47 */ + case 3: + TCNT0 = 47; + break; + + /* one second elapsed */ + case 4: + TCNT0 = 0; + count = 0; + ++hit; + break; + } +} + +bool timerHit () { + bool ret = hit != 0; + hit = 0; + return ret; +} + +void timerStart () { + count = 0; + /* reset timer value */ + TCNT0 = 0; + /* set normal mode timer0 */ + TCCR0A = 0; + /* enable overflow interrupt */ + TIMSK0 = (1 << TOIE0); + /* io clock with 1024 prescaler */ + TCCR0B = (TCCR0B & ~((1 << CS01)) | (1 << CS02) | (1 << CS00)); +} + +void timerStop () { + /* zero clock source */ + TCCR0B = 0; +} + @@ -0,0 +1,11 @@ +#ifndef TIMER_H +#define TIMER_H + +#include <stdbool.h> + +void timerStart (); +bool timerHit (); +void timerStop (); + +#endif /* TIMER_H */ + |