annotate main.c @ 312:ef64a178092f

- Some basic ADC code - Turn on -mcall-prologues - Put strings in program space, saves RAM
author Matt Johnston <matt@ucc.asn.au>
date Mon, 14 May 2012 00:22:57 +0800
parents fc96a51bdce6
children 52cb08a01171
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 /* Name: main.c
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 * Author: <insert your name here>
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 * Copyright: <insert your copyright message here>
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 * License: <insert your license reference here>
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 */
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 #include <stdio.h>
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 #include <string.h>
307
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
9 #include <avr/io.h>
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
10 #include <avr/interrupt.h>
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
11 #include <avr/sleep.h>
312
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
12 #include <avr/pgmspace.h>
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 #include <util/crc16.h>
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14
309
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents: 308
diff changeset
15 #include "integer.h"
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents: 308
diff changeset
16
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 // configuration params
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 // - measurement interval
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 // - transmit interval
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 // - bluetooth params
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 // - number of sensors (and range?)
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22
307
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
23 // 1 second. we have 1024 prescaler, 32768 crystal.
308
e36ee3e156c1 switch to atmega328
Matt Johnston <matt@ucc.asn.au>
parents: 307
diff changeset
24 #define SLEEP_COMPARE 32
310
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
25 #define MEASURE_WAKE 60
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
26
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
27 #define COMMS_WAKE 3600
308
e36ee3e156c1 switch to atmega328
Matt Johnston <matt@ucc.asn.au>
parents: 307
diff changeset
28
e36ee3e156c1 switch to atmega328
Matt Johnston <matt@ucc.asn.au>
parents: 307
diff changeset
29 #define BAUD 9600
e36ee3e156c1 switch to atmega328
Matt Johnston <matt@ucc.asn.au>
parents: 307
diff changeset
30 #define UBRR ((F_CPU)/16/(BAUD)-1)
307
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
31
312
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
32 #define NUM_MEASUREMENTS 300
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
33
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 static int uart_putchar(char c, FILE *stream);
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL,
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 _FDEV_SETUP_WRITE);
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 static uint8_t n_measurements;
312
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
39 // stored as 1/5 degree above 0
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
40 static uint8_t measurements[NUM_MEASUREMENTS];
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
41 static uint8_t internal_measurements[NUM_MEASUREMENTS];
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42
310
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
43 // boolean flags
307
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
44 static uint8_t need_measurement;
310
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
45 static uint8_t need_comms;
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
46 static uint8_t comms_done;
307
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
47
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 static uint8_t readpos;
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 static char readbuf[30];
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50
311
fc96a51bdce6 Fix build
Matt Johnston <matt@ucc.asn.au>
parents: 310
diff changeset
51 static uint8_t measure_count;
fc96a51bdce6 Fix build
Matt Johnston <matt@ucc.asn.au>
parents: 310
diff changeset
52 static uint16_t comms_count;
310
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
53
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
54 static void deep_sleep();
307
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
55
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 static void
312
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
57 uart_on(unsigned int ubrr)
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 {
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 // baud rate
308
e36ee3e156c1 switch to atmega328
Matt Johnston <matt@ucc.asn.au>
parents: 307
diff changeset
60 UBRR0H = (unsigned char)(ubrr >> 8);
e36ee3e156c1 switch to atmega328
Matt Johnston <matt@ucc.asn.au>
parents: 307
diff changeset
61 UBRR0L = (unsigned char)ubrr;
312
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
62 UCSR0B = _BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0);
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 //8N1
312
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
64 UCSR0C = _BV(UMSEL00) | _BV(UCSZ00);
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
65
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
66 // Power reduction register
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
67 PRR &= ~_BV(PRUSART0);
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
68 }
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
69
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
70 static void
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
71 uart_off()
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
72 {
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
73 // Turn of interrupts and disable tx/rx
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
74 UCSR0B = 0;
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
75
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
76 // Power reduction register
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
77 PRR |= _BV(PRUSART0);
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 }
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 static int
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 uart_putchar(char c, FILE *stream)
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 {
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 // XXX should sleep in the loop for power.
308
e36ee3e156c1 switch to atmega328
Matt Johnston <matt@ucc.asn.au>
parents: 307
diff changeset
84 loop_until_bit_is_set(UCSR0A, UDRE0);
e36ee3e156c1 switch to atmega328
Matt Johnston <matt@ucc.asn.au>
parents: 307
diff changeset
85 UDR0 = c;
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 return 0;
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 }
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 static void
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 cmd_fetch()
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 {
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 uint16_t crc = 0;
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 int i;
312
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
94 printf_P(PSTR("%d measurements\n"), n_measurements);
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 for (i = 0; i < n_measurements; i++)
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 {
312
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
97 printf_P(PSTR("%3d : %d\n"), i, measurements[i]);
307
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
98 crc = _crc_ccitt_update(crc, measurements[i]);
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99 }
312
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
100 printf_P(PSTR("CRC : %d\n"), crc);
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 }
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103 static void
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104 cmd_clear()
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105 {
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
106 }
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108 static void
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109 cmd_btoff()
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110 {
310
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
111 comms_done = 1;
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112 }
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
113
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
114 static void
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
115 read_handler()
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116 {
312
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
117 if (strcmp_P(readbuf, PSTR("fetch")) == 0)
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
118 {
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
119 cmd_fetch();
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
120 }
312
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
121 else if (strcmp_P(readbuf, PSTR("clear")) == 0)
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
122 {
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
123 cmd_clear();
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
124 }
312
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
125 else if (strcmp_P(readbuf, PSTR("btoff")) == 0)
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
126 {
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
127 cmd_btoff();
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
128 }
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
129 else
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
130 {
312
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
131 printf_P(PSTR("Bad command\n"));
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
132 }
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
133 }
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
134
308
e36ee3e156c1 switch to atmega328
Matt Johnston <matt@ucc.asn.au>
parents: 307
diff changeset
135 ISR(USART_RX_vect)
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
136 {
308
e36ee3e156c1 switch to atmega328
Matt Johnston <matt@ucc.asn.au>
parents: 307
diff changeset
137 char c = UDR0;
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
138 if (c == '\n')
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
139 {
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
140 readbuf[readpos] = '\0';
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
141 read_handler();
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
142 readpos = 0;
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
143 }
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
144 else
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
145 {
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
146 readbuf[readpos] = c;
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
147 readpos++;
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
148 if (readpos >= sizeof(readbuf))
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
149 {
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
150 readpos = 0;
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
151 }
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
152 }
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
153 }
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
154
308
e36ee3e156c1 switch to atmega328
Matt Johnston <matt@ucc.asn.au>
parents: 307
diff changeset
155 ISR(TIMER2_COMPA_vect)
307
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
156 {
310
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
157 measure_count ++;
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
158 comms_count ++;
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
159 if (measure_count == MEASURE_WAKE)
307
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
160 {
310
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
161 measure_count = 0;
307
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
162 need_measurement = 1;
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
163 }
310
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
164
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
165 if (comms_count == COMMS_WAKE)
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
166 {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
167 comms_count = 0;
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
168 need_comms = 1;
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
169 }
307
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
170 }
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
171
309
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents: 308
diff changeset
172 DWORD get_fattime (void)
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents: 308
diff changeset
173 {
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents: 308
diff changeset
174 return 0;
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents: 308
diff changeset
175 }
49e83333e546 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents: 308
diff changeset
176
307
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
177 static void
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
178 deep_sleep()
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
179 {
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
180 // p119 of manual
308
e36ee3e156c1 switch to atmega328
Matt Johnston <matt@ucc.asn.au>
parents: 307
diff changeset
181 OCR2A = SLEEP_COMPARE;
e36ee3e156c1 switch to atmega328
Matt Johnston <matt@ucc.asn.au>
parents: 307
diff changeset
182 loop_until_bit_is_clear(ASSR, OCR2AUB);
307
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
183
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
184 set_sleep_mode(SLEEP_MODE_PWR_SAVE);
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
185 sleep_mode();
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
186 }
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
187
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
188 static void
310
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
189 idle_sleep()
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
190 {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
191 set_sleep_mode(SLEEP_MODE_IDLE);
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
192 sleep_mode();
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
193 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
194
312
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
195 static void
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
196 do_adc_335()
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
197 {
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
198 PRR &= ~_BV(PRADC);
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
199
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
200 ADMUX = _BV(ADLAR);
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
201
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
202 // ADPS2 = /16 prescaler, 62khz at 1mhz clock
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
203 ADCSRA = _BV(ADEN) | _BV(ADPS2);
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
204
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
205 // measure value
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
206 ADCSRA |= _BV(ADSC);
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
207 loop_until_bit_is_clear(ADCSRA, ADSC);
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
208 uint8_t low = ADCL;
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
209 uint8_t high = ADCH;
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
210 uint16_t f_measure = low + (high << 8);
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
211
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
212 // set to measure 1.1 reference
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
213 ADMUX = _BV(ADLAR) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
214 ADCSRA |= _BV(ADSC);
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
215 loop_until_bit_is_clear(ADCSRA, ADSC);
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
216 uint8_t low_11 = ADCL;
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
217 uint8_t high_11 = ADCH;
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
218 uint16_t f_11 = low_11 + (high_11 << 8);
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
219
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
220 float res_volts = 1.1 * f_measure / f_11;
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
221
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
222 // 10mV/degree
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
223 // scale to 1/5 degree units above 0C
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
224 int temp = (res_volts - 2.73) * 500;
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
225 measurements[n_measurements] = temp;
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
226 // XXX something if it hits the limit
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
227
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
228 // measure AVR internal temperature against 1.1 ref.
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
229 ADMUX = _BV(ADLAR) | _BV(MUX3) | _BV(REFS1) | _BV(REFS0);
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
230 ADCSRA |= _BV(ADSC);
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
231 loop_until_bit_is_clear(ADCSRA, ADSC);
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
232 uint16_t res_internal = ADCL;
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
233 res_internal |= ADCH << 8;
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
234
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
235 float internal_volts = res_internal * (1.1 / 1024.0);
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
236
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
237 // 1mV/degree
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
238 int internal_temp = (internal_volts - 2.73) * 5000;
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
239 internal_measurements[n_measurements] = internal_temp;
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
240
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
241 printf_P("measure %d: external %d, internal %d, 1.1 %d\n",
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
242 n_measurements, temp, internal_temp, f_11);
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
243
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
244 n_measurements++;
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
245 PRR |= _BV(PRADC);
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
246 }
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
247
310
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
248 static void
307
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
249 do_measurement()
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
250 {
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
251 need_measurement = 0;
312
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
252
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
253 do_adc_335();
307
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
254 }
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
255
310
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
256 static void
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
257 do_comms()
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
258 {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
259 need_comms = 0;
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
260
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
261 // turn on bluetooth
312
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
262 uart_on(UBRR);
310
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
263
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
264 // write sd card here? same 3.3v regulator...
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
265
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
266 comms_done = 0;
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
267 for (;;)
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
268 {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
269 if (comms_done)
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
270 {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
271 break;
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
272 }
312
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
273
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
274 if (need_measurement)
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
275 {
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
276 do_measurement();
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
277 }
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
278
310
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
279 idle_sleep();
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
280 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
281
312
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
282 uart_off();
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
283
310
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
284 // turn off bluetooth
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
285 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
286
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
287 int main(void)
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
288 {
312
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
289 stdout = &mystdout;
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
290 uart_on(UBRR);
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
291 fprintf_P(&mystdout, PSTR("hello %d\n"), 12);
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
292 uart_off();
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
293
312
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
294 // turn off everything except timer2
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
295 PRR = _BV(PRTWI) | _BV(PRTIM0) | _BV(PRTIM1) | _BV(PRSPI) | _BV(PRUSART0) | _BV(PRADC);
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
296
307
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
297 // set up counter2.
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
298 // COM21 COM20 Set OC2 on Compare Match (p116)
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
299 // WGM21 Clear counter on compare
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
300 // CS22 CS21 CS20 clk/1024
308
e36ee3e156c1 switch to atmega328
Matt Johnston <matt@ucc.asn.au>
parents: 307
diff changeset
301 TCCR2A = _BV(COM2A1) | _BV(COM2A0) | _BV(WGM21);
e36ee3e156c1 switch to atmega328
Matt Johnston <matt@ucc.asn.au>
parents: 307
diff changeset
302 TCCR2B = _BV(CS22) | _BV(CS21) | _BV(CS20);
307
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
303 // set async mode
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
304 ASSR |= _BV(AS2);
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
305
312
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
306 #ifdef TEST_MODE
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
307 for (;;)
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
308 {
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
309 do_comms()
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
310 }
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
311 #else
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
312 for(;;){
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
313 /* insert your main loop code here */
307
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
314 if (need_measurement)
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
315 {
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
316 do_measurement();
310
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
317 continue;
307
e50091063890 Some counter2 bits
Matt Johnston <matt@ucc.asn.au>
parents: 306
diff changeset
318 }
310
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
319
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
320 if (need_comms)
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
321 {
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
322 do_comms();
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
323 continue;
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
324 }
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
325
0a64532c1de1 Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents: 309
diff changeset
326 deep_sleep();
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
327 }
312
ef64a178092f - Some basic ADC code
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
328 #endif
306
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
329 return 0; /* never reached */
848b0fe159d2 Some basic bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
330 }