ini
[aversive.git] / modules / hardware / adc / test / main.c
1 /*  
2  *  Copyright Droids Corporation, Microb Technology, Eirbot (2005)
3  * 
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  *  Revision : $Id: main.c,v 1.5.4.3 2007-09-06 08:15:37 zer0 Exp $
19  *
20  */
21  
22 #include <avr/io.h>
23 #include <aversive/wait.h>
24 #include <aversive.h>
25
26 #include <adc.h>
27
28 #include <uart.h>
29 #include <stdio.h>
30 #include <aversive/pgmspace.h>
31
32 void event(int16_t);
33
34
35
36 int main(void)
37 {
38         int16_t a; int32_t b;
39
40         uart_init();
41         fdevopen(uart0_dev_send,NULL);
42
43         sei();
44
45         adc_init();
46   
47         while(1) {
48     
49                 printf_P(PSTR("\n\nHello everybody\n This is the ADC test\n"));
50     
51                 wait_ms(20);
52         
53                 /* simple polling */
54                 a = adc_get_value( ADC_REF_AVCC | MUX_ADC0 );
55     
56                 printf_P(PSTR("polling : ADC0 = %i\n"),a);
57                 wait_ms(20);
58     
59                 /* pre-launch */
60     
61                 adc_launch( ADC_REF_AVCC | MUX_ADC1 );
62                 wait_ms(1);
63                 /* this function should take less time */
64                 a = adc_get_value( ADC_NO_CONFIG );
65     
66                 printf_P(PSTR("pre-launch : ADC1 = %i\n"),a);
67                 wait_ms(20);
68     
69                 /* test of free running mode */
70     
71                 a = adc_get_value( ADC_REF_AVCC | MUX_ADC2 | ADC_MODE_TRIGGERED );
72                 printf_P(PSTR("free run mode : ADC2 = %i\n"),a);
73                 wait_ms(1);
74                 /* this function should take less time */
75                 a = adc_get_value( ADC_REF_AVCC | MUX_ADC3 | ADC_MODE_TRIGGERED ); 
76                 printf_P(PSTR("free run mode : ADC3 = %i\n\n"),a);
77                 wait_ms(1);
78     
79     
80                 /* test of different outputs formats */
81     
82                 a = adc_get_value( ADC_REF_AVCC | MUX_ADC0  | ADC_MODE_16_BITS );
83                 printf_P(PSTR("normal output 16: ADC0 = %u ( div = %u)\n"),
84                          a, ((uint16_t)a)/(1<<6));
85     
86                 b = adc_get_value32( (void*)(ADC_REF_AVCC | MUX_ADC0  | 
87                                              ADC_MODE_16_BITS) );
88                 printf_P(PSTR("normal output 16(32): ADC0 = %ld ( div = %lu)\n"),
89                          b, b/(1l<<6));
90     
91                 /* ADC_MODE_10_BITS default */
92                 a = adc_get_value( ADC_REF_AVCC | MUX_ADC0  );
93                 printf_P(PSTR("normal output 10: ADC0 = %u\n"),a); 
94     
95                 /* ADC_MODE_10_BITS default */
96                 b = adc_get_value32( (void*)(ADC_REF_AVCC | MUX_ADC0 ) );
97                 printf_P(PSTR("normal output 10(32): ADC0 = %lu\n"),b);
98     
99                 printf_P(PSTR("now try a signed differential conversion\n"));
100     
101                 a = adc_get_value( ADC_REF_AVCC | MUX_ADC0  | ADC_MODE_10_BITS );
102                 b = adc_get_value( ADC_REF_AVCC | MUX_ADC1  | ADC_MODE_10_BITS );
103                 printf_P(PSTR("computed : ADC0-ADC1 = %i\n"), 
104                          ((int16_t)a - (int16_t)b) /2);
105     
106                 a = adc_get_value( ADC_REF_AVCC | MUX_ADC0_ADC1);
107     
108                 printf_P(PSTR("signed output 10: ADC0-ADC1 = %i\n"),a);
109     
110                 b = adc_get_value32( (void*)(ADC_REF_AVCC | MUX_ADC0_ADC1 ) );
111                 printf_P(PSTR("signed output 10(32): ADC0-ADC1 = %li\n"),b);
112     
113                 a = adc_get_value( ADC_REF_AVCC | MUX_ADC0_ADC1 | 
114                                    ADC_MODE_16_BITS );
115                 printf_P(PSTR("signed output 16: ADC0-ADC1 = %i ( div = %i)\n"),
116                          a, a/(1<<6)); 
117     
118                 b = adc_get_value32( (void*)(ADC_REF_AVCC | MUX_ADC0_ADC1  | 
119                                              ADC_MODE_16_BITS ) );
120                 printf_P(PSTR("signed output 16(32): ADC0-ADC1 = %li ( div = %li)\n\n"),
121                          b, b/(1<<6));
122     
123                 /* test of interrupt mode : we scan once the 8
124                    inputs */
125     
126                 adc_register_event(event);
127                 adc_launch( ADC_REF_AVCC | MUX_ADC0 | ADC_MODE_INT );
128                 wait_ms(20);
129     
130                 wait_ms(2000);
131         }
132
133         return 0;
134 }
135
136
137
138 void event(int16_t result)
139 {
140         static int i = 0;
141
142         sei();
143   
144         /* The printf in an interrupt is not a good idea, but ok for
145          * the test program */
146         printf_P(PSTR("from interrupt : ADC%i = %i\n"),i,result); 
147   
148         if(++i != 8)
149                 adc_launch( ADC_REF_AVCC | ( MUX_ADC0 + i ) | ADC_MODE_INT );
150         else
151                 i = 0; /* end, reinitialisation for the next time */
152 }