From b9defeee0288beba157a9a8143b4a28a95696e1a Mon Sep 17 00:00:00 2001 From: Mariano Alvira Date: Sun, 21 Oct 2012 11:38:37 -0400 Subject: [PATCH] small tweaks to how ADC channels are enabled. --- lib/adc.c | 39 ++++++++++++++++++++++++--------------- lib/include/adc.h | 9 +++++++++ tests/adc.c | 7 ++++++- 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/lib/adc.c b/lib/adc.c index 841855107..efde94019 100644 --- a/lib/adc.c +++ b/lib/adc.c @@ -34,9 +34,7 @@ */ #include -#include "crm.h" -#include "adc.h" -#include "gpio-util.h" +#include "mc1322x.h" //#define ADC_CHANS_ENABLED 0x3F // channels 0-5 enabled //#define ADC_CHANS_ENABLED 0x7E // channels 1-6 enabled @@ -73,9 +71,26 @@ void adc_service(void) { } } -void adc_init(void) { - uint8_t n; +#define _adc_setup_chan(x) do { \ + if ( channel == x ) { \ + ADC->SEQ_1bits.CH##x = 1; \ + GPIO->FUNC_SEL.ADC##x = 1; \ + GPIO->PAD_DIR.ADC##x = 0; \ + GPIO->PAD_PU_EN.ADC##x = 0; \ +}} while (0) +void adc_setup_chan(uint8_t channel) { + _adc_setup_chan(0); + _adc_setup_chan(1); + _adc_setup_chan(2); + _adc_setup_chan(3); + _adc_setup_chan(4); + _adc_setup_chan(5); + _adc_setup_chan(6); + _adc_setup_chan(7); +} + +void adc_init(void) { ADC->CLOCK_DIVIDER = 80; // 300 KHz ADC->PRESCALE = ADC_PRESCALE_VALUE - 1; // divide by 24 for 1MHz. @@ -114,13 +129,13 @@ void adc_init(void) { ADC->SR_1_LOW = (REF_OSC / ADC_PRESCALE_VALUE) / (115200 / 8) + 1; #endif - ADC->SEQ_1 = 0 + ADC->SEQ_1 = 0; #if ADC_USE_TIMER - | (1 << 15) // sequence based on Timer 1. + ADC->SEQ_1bits.SEQ_MODE = 1; // sequence based on Timer 1 #else - | (0 << 15) // sequence based on convert time. + ADC->SEQ_1bits.SEQ_MODE = 0; // sequence based on convert time. #endif - | ADC_CHANS_ENABLED; + ADC->SEQ_1bits.BATT = 1; ADC->CONTROL = 0xF001 //#if ADC_USE_TIMER @@ -129,10 +144,4 @@ void adc_init(void) { ; ADC->OVERRIDE = (1 << 8); - for (n=0; n<=8; n++) { - if ((ADC_CHANS_ENABLED >> n) & 1) { - gpio_select_function(30 + n, 1); // Function 1 = ADC - gpio_set_pad_dir(30 + n, PAD_DIR_INPUT); - } - } } diff --git a/lib/include/adc.h b/lib/include/adc.h index 66dfa1f8a..3d018e009 100644 --- a/lib/include/adc.h +++ b/lib/include/adc.h @@ -39,6 +39,9 @@ #include #include "utils.h" +/* the Vbatt measurment reads about 200mV low --- trim by ADC_VBATT_TRIM */ +#define ADC_VBATT_TRIM 200 + /* ADC registers are all 16-bit wide with 16-bit access only */ #define ADC_BASE (0x8000D000) @@ -153,8 +156,14 @@ static volatile struct ADC_struct * const ADC = (void *) (ADC_BASE); #define adc_enable() (ADC->CONTROLbits.ON = 1) #define adc_disable() (ADC->CONTROLbits.ON = 0) #define adc_select_channels(chans) (ADC->SEQ_1 = (ADC->SEQ_1 & 0xFE00) | chans) +void adc_setup_chan(uint8_t channel); extern uint16_t adc_reading[NUM_ADC_CHAN]; +/* use the internal reference to return adc_readings in mV */ +#define adc_voltage(x) (adc_reading[x] * 1200/adc_reading[8]) +/* return vbatt voltage in mV */ +#define adc_vbatt 4095 * 1200/adc_reading[8] + ADC_VBATT_TRIM + void ADC_flush(void); uint16_t ADC_READ(void); void read_scanners(void); diff --git a/tests/adc.c b/tests/adc.c index 28a98ce75..dea333962 100644 --- a/tests/adc.c +++ b/tests/adc.c @@ -52,12 +52,17 @@ int main(void) printf("\x1B[2J"); // clear screen + for (c=0; c<=7; c++) { + adc_setup_chan(c); + } + for(;;) { printf("\x1B[H"); // cursor home printf("# Value\r\n"); for (c=0; c