openpcd/openpicc/application/adc.c @ master
1 | ee1f0214 | henryk | /* AT91 ADC for OpenPICC
|
---|---|---|---|
2 | * (C) 2007 Henryk Plötz <henryk@ploetzli.ch>
|
||
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 | */
|
||
19 | |||
20 | |||
21 | #include "openpicc.h"
|
||
22 | #include "board.h"
|
||
23 | |||
24 | #include <sys/types.h>
|
||
25 | #include <lib_AT91SAM7.h>
|
||
26 | |||
27 | static AT91S_ADC* adc = AT91C_BASE_ADC; |
||
28 | |||
29 | int adc_get_field_strength(void) |
||
30 | {
|
||
31 | adc->ADC_CR = AT91C_ADC_START; |
||
32 | while( (adc->ADC_SR & AT91C_ADC_EOC4)==0) ; |
||
33 | return adc->ADC_CDR4; |
||
34 | }
|
||
35 | |||
36 | void adc_init(void) |
||
37 | {
|
||
38 | // PRESCAL = 4 -> ADC clock = MCLK / 10 = 4.8 MHz (max is 5 MHz at 10 bit)
|
||
39 | // Start time <20 us -> STARTUP=11
|
||
40 | // Track and hold time >600 ns -> SHTIM=2
|
||
41 | adc->ADC_MR = AT91C_ADC_TRGEN_DIS | AT91C_ADC_LOWRES_10_BIT | |
||
42 | AT91C_ADC_SLEEP_MODE | (4 << 8) | (11 << 16) | (2 << 24); |
||
43 | adc->ADC_IDR = 0xfffff; |
||
44 | adc->ADC_CHER = OPENPICC_ADC_FIELD_STRENGTH; |
||
45 | }
|