PDA

View Full Version : analogReadRes function replacement



cdmiller
11-04-2012, 11:57 PM
void analogReadRes(unsigned int bits)
{
// if (bits > 16) bits = 16;
// analog_right_shift = 16 - bits;
// TODO: actually reconfigure A/D for desired resolution

/*
ADC0_CFG1 bits 3-2 set the number of bits in the ADC result.
For single ended:
00 = 8 bits
01 = 12 bits
10 = 10 bits
11 = 16 bits
*/
switch (bits) {
case 8:
ADC0_CFG1 |= 0;
break;
case 12:
ADC0_CFG1 |= 4;
break;
case 16:
ADC0_CFG1 |= 12;
break;
default:
ADC0_CFG1 |= 8;
}
}

JBeale
11-05-2012, 12:19 AM
this looks great, thank you!

cdmiller
11-05-2012, 02:22 AM
Actually should probably use the stuff in mk20dx128.h



#define ADC0_CFG1 *(volatile uint32_t *)0x4003B008 // ADC configuration register 1
#define ADC_CFG1_ADLPC (uint32_t)0x80 // Low-power configuration
#define ADC_CFG1_ADIV(n) (uint32_t)(((n) & 3) << 5) // Clock divide select, 0=direct, 1=div2, 2=div4, 3=div8
#define ADC_CFG1_ADLSMP (uint32_t)0x10 // Sample time configuration, 0=Short, 1=Long
#define ADC_CFG1_MODE(n) (uint32_t)(((n) & 3) << 2) // Conversion mode, 0=8 bit, 1=12 bit, 2=10 bit, 3=16 bit
#define ADC_CFG1_ADICLK(n) (uint32_t)(((n) & 3) << 0) // Input clock, 0=bus, 1=bus/2, 2=OSCERCLK, 3=async