added arduino archive programs to /firmware/ref/archive-code/arduino
[athena:athena.git] / firmware / ref / archive-code / atmega / AccWithUsart / AccWithUsart.c
1 #define F_CPU 16000000UL\r
2 \r
3 #include <avr/io.h>\r
4 #include <util/delay.h>\r
5 \r
6 \r
7 \r
8 \r
9 \r
10 \r
11 typedef unsigned int uint;\r
12 typedef unsigned char uchar;\r
13 \r
14 \r
15 void adc_init()\r
16 {\r
17         ADMUX=(1<<REFS0);\r
18         // to set AREF => "01"...\r
19 \r
20 \r
21         ADCSRA = (1<<ADEN)|(1<<ADIE)|(1<<ADPS2)|(1<<ADPS1);\r
22         // enable ADC...\r
23         // enable interrupt...\r
24         // setting prescaler as 64 ==> "110"...\r
25 }\r
26 \r
27 uint adc_read(uchar ch)\r
28 {\r
29 \r
30         //uchar ch=0x00;\r
31 \r
32         ADMUX|=ch;\r
33 \r
34         ADCSRA|=0x40;\r
35 \r
36         //wait for conversion...\r
37         while(!(ADCSRA & 0x10));\r
38 \r
39         ADCSRA|=(1<<ADIF);\r
40 \r
41         return ADC;\r
42 \r
43 }\r
44 \r
45 void USARTInit(uint16_t ubrr_value)\r
46 {\r
47 \r
48 \r
49 \r
50 \r
51 \r
52    UBRRL = ubrr_value;\r
53    UBRRH = (ubrr_value>>8);\r
54    UCSRC|=(1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);\r
55 \r
56 \r
57    UCSRB=(1<<RXEN)|(1<<TXEN);\r
58    UCSRA|=(1<<RXC);\r
59 \r
60 \r
61 }\r
62 \r
63 unsigned char USARTReadChar()\r
64 {\r
65 \r
66 \r
67    while(!(UCSRA & (1<<RXC)));\r
68 \r
69 \r
70 \r
71    return UDR;\r
72 }\r
73 \r
74 void USARTWriteChar(unsigned char data)\r
75 {\r
76 \r
77    while(!(UCSRA & (1<<UDRE)));\r
78  \r
79 \r
80    UDR=data;\r
81 }\r
82 \r
83 \r
84 char cX[3],cY[3],cZ[3];\r
85 \r
86 void USART_write_word(char var,char* buf)\r
87 {\r
88         USARTWriteChar(var);\r
89         USARTWriteChar(':');\r
90 \r
91         for(uchar i=0;i<3;i++)\r
92         USARTWriteChar(buf[i]);\r
93 \r
94 }\r
95 \r
96 \r
97 \r
98 int main()\r
99 {\r
100         \r
101 \r
102         adc_init();\r
103         USARTInit(103);  \r
104 /*\r
105                 uint x0=adc_read(0);\r
106                 uint y0=adc_read(1);\r
107                 uint z0=adc_read(2);\r
108 */\r
109                 uint x1=0;\r
110                         uint y1=0;\r
111                         uint z1=0;\r
112                 \r
113 \r
114 \r
115         while(1)\r
116         {\r
117         \r
118                 uint x=adc_read(0)-x1;\r
119                 uint y=adc_read(1)-y1;\r
120                 uint z=adc_read(2)-z1;\r
121                 \r
122 \r
123                 sprintf(cX,"%3d",x);\r
124                 // writing to USART...\r
125                 USART_write_word('x',cX);\r
126                 USARTWriteChar(' ');\r
127 \r
128                 sprintf(cY,"%3d",y);\r
129                 // writing to USART...\r
130                 USART_write_word('y',cY);\r
131                 USARTWriteChar(' ');\r
132 \r
133 \r
134                 sprintf(cZ,"%3d",z);\r
135                 // writing to USART...\r
136                 USART_write_word('z',cZ);\r
137                 USARTWriteChar(13);\r
138                 USARTWriteChar(10);\r
139 \r
140                 \r
141 \r
142 \r
143                 _delay_ms(500);\r
144 \r
145         }\r
146 \r
147 \r
148 }\r