annotate main.c @ 7:52cb08a01171

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