small tweaks to how ADC channels are enabled.
This commit is contained in:
parent
009180e0a9
commit
b9defeee02
3 changed files with 39 additions and 16 deletions
39
lib/adc.c
39
lib/adc.c
|
@ -34,9 +34,7 @@
|
|||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -39,6 +39,9 @@
|
|||
#include <stdint.h>
|
||||
#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);
|
||||
|
|
|
@ -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<NUM_ADC_CHAN; c++) {
|
||||
adc_service();
|
||||
printf("%u %04u\r\n", c, adc_reading[c]);
|
||||
printf("%u %04u %04u mV\r\n", c, adc_reading[c], adc_voltage(c));
|
||||
}
|
||||
printf("vbatt: %04u mV", adc_vbatt);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue