Mercurial > templog
diff main.c @ 1:1d1897d66b03
Some counter2 bits
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Mon, 07 May 2012 00:14:53 +0800 |
parents | c8b14b2950b9 |
children | ab0e30c4b344 |
line wrap: on
line diff
--- a/main.c Sun May 06 23:13:14 2012 +0800 +++ b/main.c Mon May 07 00:14:53 2012 +0800 @@ -4,9 +4,11 @@ * License: <insert your license reference here> */ -#include <avr/io.h> #include <stdio.h> #include <string.h> +#include <avr/io.h> +#include <avr/interrupt.h> +#include <avr/sleep.h> #include <util/crc16.h> // configuration params @@ -15,6 +17,10 @@ // - bluetooth params // - number of sensors (and range?) +// 1 second. we have 1024 prescaler, 32768 crystal. +static const uint8_t CNT2_COMPARE = 32; +static const int SECONDS_WAKE = 60; + static int uart_putchar(char c, FILE *stream); static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE); @@ -22,9 +28,13 @@ static uint8_t n_measurements; static uint8_t measurements[500]; +static uint8_t need_measurement; + static uint8_t readpos; static char readbuf[30]; +static uint8_t sec_count; + static void uart_init(unsigned int baud) { @@ -54,9 +64,9 @@ for (i = 0; i < n_measurements; i++) { printf("%3d : %d\n", i, measurements[i]); - crc = _crc_ccitt_updatec(crc, measurements[i]); + crc = _crc_ccitt_update(crc, measurements[i]); } - printf("CRC : %d\n", i, measurements[i]); + printf("CRC : %d\n", crc); } static void @@ -90,7 +100,7 @@ } } -ISR(UART_RX_vect) +ISR(USART_RXC_vect) { char c = UDR; if (c == '\n') @@ -110,16 +120,53 @@ } } +ISR(TIMER2_COMP_vect) +{ + sec_count ++; + if (sec_count == SECONDS_WAKE) + { + sec_count = 0; + need_measurement = 1; + } +} + +static void +deep_sleep() +{ + // p119 of manual + OCR2 = CNT2_COMPARE; + loop_until_bit_is_clear(ASSR, OCR2UB); + + set_sleep_mode(SLEEP_MODE_PWR_SAVE); + sleep_mode(); +} + +static void +do_measurement() +{ + need_measurement = 0; +} + int main(void) { uart_init(9600); fprintf(&mystdout, "hello %d\n", 12); - // get current time + // set up counter2. + // COM21 COM20 Set OC2 on Compare Match (p116) + // WGM21 Clear counter on compare + // CS22 CS21 CS20 clk/1024 + TCCR2 = _BV(COM21) | _BV(COM20) | _BV(WGM21) | _BV(CS22) | _BV(CS21) | _BV(CS20); + // set async mode + ASSR |= _BV(AS2); for(;;){ /* insert your main loop code here */ + if (need_measurement) + { + do_measurement(); + } } return 0; /* never reached */ }