comparison main.c @ 307:e50091063890

Some counter2 bits
author Matt Johnston <matt@ucc.asn.au>
date Mon, 07 May 2012 00:14:53 +0800
parents 848b0fe159d2
children ab0e30c4b344
comparison
equal deleted inserted replaced
306:848b0fe159d2 307:e50091063890
2 * Author: <insert your name here> 2 * Author: <insert your name here>
3 * Copyright: <insert your copyright message here> 3 * Copyright: <insert your copyright message here>
4 * License: <insert your license reference here> 4 * License: <insert your license reference here>
5 */ 5 */
6 6
7 #include <avr/io.h>
8 #include <stdio.h> 7 #include <stdio.h>
9 #include <string.h> 8 #include <string.h>
9 #include <avr/io.h>
10 #include <avr/interrupt.h>
11 #include <avr/sleep.h>
10 #include <util/crc16.h> 12 #include <util/crc16.h>
11 13
12 // configuration params 14 // configuration params
13 // - measurement interval 15 // - measurement interval
14 // - transmit interval 16 // - transmit interval
15 // - bluetooth params 17 // - bluetooth params
16 // - number of sensors (and range?) 18 // - number of sensors (and range?)
17 19
20 // 1 second. we have 1024 prescaler, 32768 crystal.
21 static const uint8_t CNT2_COMPARE = 32;
22 static const int SECONDS_WAKE = 60;
23
18 static int uart_putchar(char c, FILE *stream); 24 static int uart_putchar(char c, FILE *stream);
19 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, 25 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL,
20 _FDEV_SETUP_WRITE); 26 _FDEV_SETUP_WRITE);
21 27
22 static uint8_t n_measurements; 28 static uint8_t n_measurements;
23 static uint8_t measurements[500]; 29 static uint8_t measurements[500];
24 30
31 static uint8_t need_measurement;
32
25 static uint8_t readpos; 33 static uint8_t readpos;
26 static char readbuf[30]; 34 static char readbuf[30];
35
36 static uint8_t sec_count;
27 37
28 static void 38 static void
29 uart_init(unsigned int baud) 39 uart_init(unsigned int baud)
30 { 40 {
31 // baud rate 41 // baud rate
52 int i; 62 int i;
53 printf("%d measurements\n", n_measurements); 63 printf("%d measurements\n", n_measurements);
54 for (i = 0; i < n_measurements; i++) 64 for (i = 0; i < n_measurements; i++)
55 { 65 {
56 printf("%3d : %d\n", i, measurements[i]); 66 printf("%3d : %d\n", i, measurements[i]);
57 crc = _crc_ccitt_updatec(crc, measurements[i]); 67 crc = _crc_ccitt_update(crc, measurements[i]);
58 } 68 }
59 printf("CRC : %d\n", i, measurements[i]); 69 printf("CRC : %d\n", crc);
60 } 70 }
61 71
62 static void 72 static void
63 cmd_clear() 73 cmd_clear()
64 { 74 {
88 { 98 {
89 printf("Bad command\n"); 99 printf("Bad command\n");
90 } 100 }
91 } 101 }
92 102
93 ISR(UART_RX_vect) 103 ISR(USART_RXC_vect)
94 { 104 {
95 char c = UDR; 105 char c = UDR;
96 if (c == '\n') 106 if (c == '\n')
97 { 107 {
98 readbuf[readpos] = '\0'; 108 readbuf[readpos] = '\0';
108 readpos = 0; 118 readpos = 0;
109 } 119 }
110 } 120 }
111 } 121 }
112 122
123 ISR(TIMER2_COMP_vect)
124 {
125 sec_count ++;
126 if (sec_count == SECONDS_WAKE)
127 {
128 sec_count = 0;
129 need_measurement = 1;
130 }
131 }
132
133 static void
134 deep_sleep()
135 {
136 // p119 of manual
137 OCR2 = CNT2_COMPARE;
138 loop_until_bit_is_clear(ASSR, OCR2UB);
139
140 set_sleep_mode(SLEEP_MODE_PWR_SAVE);
141 sleep_mode();
142 }
143
144 static void
145 do_measurement()
146 {
147 need_measurement = 0;
148 }
149
113 int main(void) 150 int main(void)
114 { 151 {
115 uart_init(9600); 152 uart_init(9600);
116 153
117 fprintf(&mystdout, "hello %d\n", 12); 154 fprintf(&mystdout, "hello %d\n", 12);
118 155
119 // get current time 156 // set up counter2.
157 // COM21 COM20 Set OC2 on Compare Match (p116)
158 // WGM21 Clear counter on compare
159 // CS22 CS21 CS20 clk/1024
160 TCCR2 = _BV(COM21) | _BV(COM20) | _BV(WGM21) | _BV(CS22) | _BV(CS21) | _BV(CS20);
161 // set async mode
162 ASSR |= _BV(AS2);
120 163
121 for(;;){ 164 for(;;){
122 /* insert your main loop code here */ 165 /* insert your main loop code here */
166 if (need_measurement)
167 {
168 do_measurement();
169 }
123 } 170 }
124 return 0; /* never reached */ 171 return 0; /* never reached */
125 } 172 }