comparison main.c @ 2:ab0e30c4b344

switch to atmega328
author Matt Johnston <matt@ucc.asn.au>
date Tue, 08 May 2012 22:34:58 +0800
parents 1d1897d66b03
children 888be1b234b6
comparison
equal deleted inserted replaced
1:1d1897d66b03 2:ab0e30c4b344
16 // - transmit interval 16 // - transmit interval
17 // - bluetooth params 17 // - bluetooth params
18 // - number of sensors (and range?) 18 // - number of sensors (and range?)
19 19
20 // 1 second. we have 1024 prescaler, 32768 crystal. 20 // 1 second. we have 1024 prescaler, 32768 crystal.
21 static const uint8_t CNT2_COMPARE = 32; 21 #define SLEEP_COMPARE 32
22 static const int SECONDS_WAKE = 60; 22 #define SECONDS_WAKE 60
23
24 #define BAUD 9600
25 #define UBRR ((F_CPU)/16/(BAUD)-1)
23 26
24 static int uart_putchar(char c, FILE *stream); 27 static int uart_putchar(char c, FILE *stream);
25 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, 28 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL,
26 _FDEV_SETUP_WRITE); 29 _FDEV_SETUP_WRITE);
27 30
34 static char readbuf[30]; 37 static char readbuf[30];
35 38
36 static uint8_t sec_count; 39 static uint8_t sec_count;
37 40
38 static void 41 static void
39 uart_init(unsigned int baud) 42 uart_init(unsigned int ubrr)
40 { 43 {
41 // baud rate 44 // baud rate
42 UBRRH = (unsigned char)(baud >> 8); 45 UBRR0H = (unsigned char)(ubrr >> 8);
43 UBRRL = (unsigned char)baud; 46 UBRR0L = (unsigned char)ubrr;
44 UCSRB = (1<<RXEN)|(1<<TXEN); 47 UCSR0B = (1<<RXEN0)|(1<<TXEN0);
45 //8N1 48 //8N1
46 UCSRC = (1<<URSEL)|(3<<UCSZ0); 49 UCSR0C = (1<<UMSEL00)|(3<<UCSZ00);
47 } 50 }
48 51
49 static int 52 static int
50 uart_putchar(char c, FILE *stream) 53 uart_putchar(char c, FILE *stream)
51 { 54 {
52 // XXX should sleep in the loop for power. 55 // XXX should sleep in the loop for power.
53 loop_until_bit_is_set(UCSRA, UDRE); 56 loop_until_bit_is_set(UCSR0A, UDRE0);
54 UDR = c; 57 UDR0 = c;
55 return 0; 58 return 0;
56 } 59 }
57 60
58 static void 61 static void
59 cmd_fetch() 62 cmd_fetch()
98 { 101 {
99 printf("Bad command\n"); 102 printf("Bad command\n");
100 } 103 }
101 } 104 }
102 105
103 ISR(USART_RXC_vect) 106 ISR(USART_RX_vect)
104 { 107 {
105 char c = UDR; 108 char c = UDR0;
106 if (c == '\n') 109 if (c == '\n')
107 { 110 {
108 readbuf[readpos] = '\0'; 111 readbuf[readpos] = '\0';
109 read_handler(); 112 read_handler();
110 readpos = 0; 113 readpos = 0;
118 readpos = 0; 121 readpos = 0;
119 } 122 }
120 } 123 }
121 } 124 }
122 125
123 ISR(TIMER2_COMP_vect) 126 ISR(TIMER2_COMPA_vect)
124 { 127 {
125 sec_count ++; 128 sec_count ++;
126 if (sec_count == SECONDS_WAKE) 129 if (sec_count == SECONDS_WAKE)
127 { 130 {
128 sec_count = 0; 131 sec_count = 0;
132 135
133 static void 136 static void
134 deep_sleep() 137 deep_sleep()
135 { 138 {
136 // p119 of manual 139 // p119 of manual
137 OCR2 = CNT2_COMPARE; 140 OCR2A = SLEEP_COMPARE;
138 loop_until_bit_is_clear(ASSR, OCR2UB); 141 loop_until_bit_is_clear(ASSR, OCR2AUB);
139 142
140 set_sleep_mode(SLEEP_MODE_PWR_SAVE); 143 set_sleep_mode(SLEEP_MODE_PWR_SAVE);
141 sleep_mode(); 144 sleep_mode();
142 } 145 }
143 146
147 need_measurement = 0; 150 need_measurement = 0;
148 } 151 }
149 152
150 int main(void) 153 int main(void)
151 { 154 {
152 uart_init(9600); 155 uart_init(UBRR);
153 156
154 fprintf(&mystdout, "hello %d\n", 12); 157 fprintf(&mystdout, "hello %d\n", 12);
155 158
156 // set up counter2. 159 // set up counter2.
157 // COM21 COM20 Set OC2 on Compare Match (p116) 160 // COM21 COM20 Set OC2 on Compare Match (p116)
158 // WGM21 Clear counter on compare 161 // WGM21 Clear counter on compare
159 // CS22 CS21 CS20 clk/1024 162 // CS22 CS21 CS20 clk/1024
160 TCCR2 = _BV(COM21) | _BV(COM20) | _BV(WGM21) | _BV(CS22) | _BV(CS21) | _BV(CS20); 163 TCCR2A = _BV(COM2A1) | _BV(COM2A0) | _BV(WGM21);
164 TCCR2B = _BV(CS22) | _BV(CS21) | _BV(CS20);
161 // set async mode 165 // set async mode
162 ASSR |= _BV(AS2); 166 ASSR |= _BV(AS2);
163 167
164 for(;;){ 168 for(;;){
165 /* insert your main loop code here */ 169 /* insert your main loop code here */