annotate main.c @ 12:3c27bfbd7f3a

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