comparison main.c @ 0:c8b14b2950b9

Some basic bits
author Matt Johnston <matt@ucc.asn.au>
date Sun, 06 May 2012 23:13:14 +0800
parents
children 1d1897d66b03
comparison
equal deleted inserted replaced
-1:000000000000 0:c8b14b2950b9
1 /* Name: main.c
2 * Author: <insert your name here>
3 * Copyright: <insert your copyright message here>
4 * License: <insert your license reference here>
5 */
6
7 #include <avr/io.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <util/crc16.h>
11
12 // configuration params
13 // - measurement interval
14 // - transmit interval
15 // - bluetooth params
16 // - number of sensors (and range?)
17
18 static int uart_putchar(char c, FILE *stream);
19 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL,
20 _FDEV_SETUP_WRITE);
21
22 static uint8_t n_measurements;
23 static uint8_t measurements[500];
24
25 static uint8_t readpos;
26 static char readbuf[30];
27
28 static void
29 uart_init(unsigned int baud)
30 {
31 // baud rate
32 UBRRH = (unsigned char)(baud >> 8);
33 UBRRL = (unsigned char)baud;
34 UCSRB = (1<<RXEN)|(1<<TXEN);
35 //8N1
36 UCSRC = (1<<URSEL)|(3<<UCSZ0);
37 }
38
39 static int
40 uart_putchar(char c, FILE *stream)
41 {
42 // XXX should sleep in the loop for power.
43 loop_until_bit_is_set(UCSRA, UDRE);
44 UDR = c;
45 return 0;
46 }
47
48 static void
49 cmd_fetch()
50 {
51 uint16_t crc = 0;
52 int i;
53 printf("%d measurements\n", n_measurements);
54 for (i = 0; i < n_measurements; i++)
55 {
56 printf("%3d : %d\n", i, measurements[i]);
57 crc = _crc_ccitt_updatec(crc, measurements[i]);
58 }
59 printf("CRC : %d\n", i, measurements[i]);
60 }
61
62 static void
63 cmd_clear()
64 {
65 }
66
67 static void
68 cmd_btoff()
69 {
70 }
71
72 static void
73 read_handler()
74 {
75 if (strcmp(readbuf, "fetch") == 0)
76 {
77 cmd_fetch();
78 }
79 else if (strcmp(readbuf, "clear") == 0)
80 {
81 cmd_clear();
82 }
83 else if (strcmp(readbuf, "btoff") == 0)
84 {
85 cmd_btoff();
86 }
87 else
88 {
89 printf("Bad command\n");
90 }
91 }
92
93 ISR(UART_RX_vect)
94 {
95 char c = UDR;
96 if (c == '\n')
97 {
98 readbuf[readpos] = '\0';
99 read_handler();
100 readpos = 0;
101 }
102 else
103 {
104 readbuf[readpos] = c;
105 readpos++;
106 if (readpos >= sizeof(readbuf))
107 {
108 readpos = 0;
109 }
110 }
111 }
112
113 int main(void)
114 {
115 uart_init(9600);
116
117 fprintf(&mystdout, "hello %d\n", 12);
118
119 // get current time
120
121 for(;;){
122 /* insert your main loop code here */
123 }
124 return 0; /* never reached */
125 }