39 #include "gpio-util.h"
44 #define ADC_CHANS_ENABLED 0x1FF // all channels, including battery reference voltage
48 #define ADC_SAMPLE_FREQ 400 // Hz (minimum of ~366.21 Hz)
49 #define ADC_PRESCALE_VALUE (REF_OSC / ADC_SAMPLE_FREQ)
51 #define ADC_USE_TIMER 0
52 #define ADC_USE_INTERRUPTS 0 // incomplete support
54 uint16_t adc_reading[NUM_ADC_CHAN];
56 void ADC_flush(
void) {
57 while(ADC->FIFO_STATUSbits.EMPTY == 0) ADC->FIFO_READ;
60 uint16_t ADC_READ(
void) {
61 while(ADC->FIFO_STATUSbits.EMPTY);
62 return ADC->FIFO_READ;
66 void adc_service(
void) {
69 while (ADC->FIFO_STATUSbits.EMPTY == 0) {
70 value = ADC->FIFO_READ;
71 channel = value >> 12;
72 if (channel < NUM_ADC_CHAN) adc_reading[channel] = value & 0xFFF;
79 ADC->CLOCK_DIVIDER = 80;
80 ADC->PRESCALE = ADC_PRESCALE_VALUE - 1;
102 ADC->CONVERT_TIME = 1000000 / (115200 / 8) - 1;
106 #if ADC_USE_INTERRUPTS
107 ADC->FIFO_CONTROL = 7;
109 ADC->FIFO_CONTROL = 0;
113 ADC->SR_1_HIGH = 0x0000;
114 ADC->SR_1_LOW = (REF_OSC / ADC_PRESCALE_VALUE) / (115200 / 8) + 1;
125 ADC->CONTROL = 0xF001
130 ADC->OVERRIDE = (1 << 8);
132 for (n=0; n<=8; n++) {
133 if ((ADC_CHANS_ENABLED >> n) & 1) {
134 gpio_select_function(30 + n, 1);
135 gpio_set_pad_dir(30 + n, PAD_DIR_INPUT);