comparison 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
comparison
equal deleted inserted replaced
6:9d538f674ff0 7:52cb08a01171
7 #include <stdio.h> 7 #include <stdio.h>
8 #include <string.h> 8 #include <string.h>
9 #include <avr/io.h> 9 #include <avr/io.h>
10 #include <avr/interrupt.h> 10 #include <avr/interrupt.h>
11 #include <avr/sleep.h> 11 #include <avr/sleep.h>
12 #include <util/delay.h>
12 #include <avr/pgmspace.h> 13 #include <avr/pgmspace.h>
13 #include <util/crc16.h> 14 #include <util/crc16.h>
14 15
15 #include "integer.h" 16 #include "integer.h"
16 17
25 #define MEASURE_WAKE 60 26 #define MEASURE_WAKE 60
26 27
27 #define COMMS_WAKE 3600 28 #define COMMS_WAKE 3600
28 29
29 #define BAUD 9600 30 #define BAUD 9600
30 #define UBRR ((F_CPU)/16/(BAUD)-1) 31 #define UBRR ((F_CPU)/8/(BAUD)-1)
32
33 #define PORT_LED PORTC
34 #define DDR_LED DDRC
35 #define PIN_LED PC4
31 36
32 #define NUM_MEASUREMENTS 300 37 #define NUM_MEASUREMENTS 300
33 38
34 static int uart_putchar(char c, FILE *stream); 39 static int uart_putchar(char c, FILE *stream);
35 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, 40 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL,
36 _FDEV_SETUP_WRITE); 41 _FDEV_SETUP_WRITE);
37 42
38 static uint8_t n_measurements; 43 static uint8_t n_measurements = 0;
39 // stored as 1/5 degree above 0 44 // stored as 1/5 degree above 0
40 static uint8_t measurements[NUM_MEASUREMENTS]; 45 static uint8_t measurements[NUM_MEASUREMENTS];
41 static uint8_t internal_measurements[NUM_MEASUREMENTS]; 46 static uint8_t internal_measurements[NUM_MEASUREMENTS];
42 47
43 // boolean flags 48 // boolean flags
44 static uint8_t need_measurement; 49 static uint8_t need_measurement = 0;
45 static uint8_t need_comms; 50 static uint8_t need_comms = 0;
46 static uint8_t comms_done; 51 static uint8_t comms_done = 0;
47 52
48 static uint8_t readpos; 53 static uint8_t readpos = 0;
49 static char readbuf[30]; 54 static char readbuf[30];
50 55
51 static uint8_t measure_count; 56 static uint8_t measure_count = 0;
52 static uint16_t comms_count; 57 static uint16_t comms_count = 0;
53 58
54 static void deep_sleep(); 59 static void deep_sleep();
55 60
56 static void 61 static void
57 uart_on(unsigned int ubrr) 62 uart_on()
58 { 63 {
59 // baud rate 64 // baud rate
60 UBRR0H = (unsigned char)(ubrr >> 8); 65 UBRR0H = (unsigned char)(UBRR >> 8);
61 UBRR0L = (unsigned char)ubrr; 66 UBRR0L = (unsigned char)UBRR;
67 // set 2x clock, improves accuracy of UBRR
68 UCSR0A |= _BV(U2X0);
62 UCSR0B = _BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0); 69 UCSR0B = _BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0);
63 //8N1 70 //8N1
64 UCSR0C = _BV(UMSEL00) | _BV(UCSZ00); 71 UCSR0C = _BV(UCSZ01) | _BV(UCSZ00);
65 72
66 // Power reduction register 73 // Power reduction register
67 PRR &= ~_BV(PRUSART0); 74 //PRR &= ~_BV(PRUSART0);
75 }
76
77 static void
78 uart_test()
79 {
80
81 UDR0 = 'n';
82 _delay_ms(50);
83 UDR0 = 'f';
84 _delay_ms(50);
85 while ( !( UCSR0A & (1<<UDRE0)) );
86 UDR0 = 'x';
87 loop_until_bit_is_set(UCSR0A, UDRE0);
88 UDR0 = 'a';
89 loop_until_bit_is_set(UCSR0A, UDRE0);
90 UDR0 = 'b';
91 loop_until_bit_is_set(UCSR0A, UDRE0);
92 UDR0 = 'c';
93 loop_until_bit_is_set(UCSR0A, UDRE0);
94 UDR0 = '\n';
68 } 95 }
69 96
70 static void 97 static void
71 uart_off() 98 uart_off()
72 { 99 {
101 } 128 }
102 129
103 static void 130 static void
104 cmd_clear() 131 cmd_clear()
105 { 132 {
133 n_measurements = 0;
134 printf_P(PSTR("Cleared\n"));
106 } 135 }
107 136
108 static void 137 static void
109 cmd_btoff() 138 cmd_btoff()
110 { 139 {
140 printf_P(PSTR("Turning off\n"));
141 _delay_ms(50);
111 comms_done = 1; 142 comms_done = 1;
112 } 143 }
113 144
114 static void 145 static void
115 read_handler() 146 read_handler()
165 if (comms_count == COMMS_WAKE) 196 if (comms_count == COMMS_WAKE)
166 { 197 {
167 comms_count = 0; 198 comms_count = 0;
168 need_comms = 1; 199 need_comms = 1;
169 } 200 }
201
202 PORT_LED ^= _BV(PIN_LED);
170 } 203 }
171 204
172 DWORD get_fattime (void) 205 DWORD get_fattime (void)
173 { 206 {
174 return 0; 207 return 0;
257 do_comms() 290 do_comms()
258 { 291 {
259 need_comms = 0; 292 need_comms = 0;
260 293
261 // turn on bluetooth 294 // turn on bluetooth
262 uart_on(UBRR); 295 uart_on();
263 296
264 // write sd card here? same 3.3v regulator... 297 // write sd card here? same 3.3v regulator...
265 298
266 comms_done = 0; 299 comms_done = 0;
267 for (;;) 300 for (;;)
282 uart_off(); 315 uart_off();
283 316
284 // turn off bluetooth 317 // turn off bluetooth
285 } 318 }
286 319
320 static void
321 blink()
322 {
323 PORT_LED &= ~_BV(PIN_LED);
324 _delay_ms(100);
325 PORT_LED |= _BV(PIN_LED);
326 }
327
328 static void
329 long_delay(int ms)
330 {
331 int iter = ms / 100;
332
333 for (int i = 0; i < iter; i++)
334 {
335 _delay_ms(100);
336 }
337 }
338
287 int main(void) 339 int main(void)
288 { 340 {
341 DDR_LED |= _BV(PIN_LED);
342
343 blink();
344 long_delay(1000);
345 blink();
346 long_delay(500);
289 stdout = &mystdout; 347 stdout = &mystdout;
290 uart_on(UBRR); 348 uart_on();
349 blink();
350 long_delay(200);
351 blink();
352 long_delay(200);
353 uart_test();
354
355 PORT_LED &= ~_BV(PIN_LED);
356 for (int i = 0; i < 10; i++)
357 _delay_ms(100);
358 PORT_LED |= _BV(PIN_LED);
359
291 fprintf_P(&mystdout, PSTR("hello %d\n"), 12); 360 fprintf_P(&mystdout, PSTR("hello %d\n"), 12);
292 uart_off(); 361 //uart_off();
293 362
294 // turn off everything except timer2 363 // turn off everything except timer2
295 PRR = _BV(PRTWI) | _BV(PRTIM0) | _BV(PRTIM1) | _BV(PRSPI) | _BV(PRUSART0) | _BV(PRADC); 364 PRR = _BV(PRTWI) | _BV(PRTIM0) | _BV(PRTIM1) | _BV(PRSPI) | _BV(PRUSART0) | _BV(PRADC);
296 365
297 // set up counter2. 366 // set up counter2.
301 TCCR2A = _BV(COM2A1) | _BV(COM2A0) | _BV(WGM21); 370 TCCR2A = _BV(COM2A1) | _BV(COM2A0) | _BV(WGM21);
302 TCCR2B = _BV(CS22) | _BV(CS21) | _BV(CS20); 371 TCCR2B = _BV(CS22) | _BV(CS21) | _BV(CS20);
303 // set async mode 372 // set async mode
304 ASSR |= _BV(AS2); 373 ASSR |= _BV(AS2);
305 374
375 DDR_LED |= _BV(PIN_LED);
376 PORT_LED &= ~_BV(PIN_LED);
377 _delay_ms(100);
378 PORT_LED |= _BV(PIN_LED);
379
306 #ifdef TEST_MODE 380 #ifdef TEST_MODE
307 for (;;) 381 for (;;)
308 { 382 {
309 do_comms() 383 do_comms()
310 } 384 }