2009-10-06 04:19 AM
Impossible to use ADC on several channels (repost)
2011-05-17 06:05 AM
Hi,
Something goes wrong with the forum. Please discard my previous message and read this one: I am writing an application with STM8S208RB on the Raisonance REva evaluation board. I read analog values from entries IN1, IN2, potentiometer and temperature sensor (on PF4-7). I use single conversion, no IT, no Schmidt trigger. Everything is simple, except that I need to be able to read any one of the entries above when I need it. I wrote a simple application that does just that, but the values that I read from the ADC change in an inconsistent manner. I spent quite some time trying to understand what happens, and the ST library examples work well. Following is the faulty code. I attach a zip file with the Ride7 project so that you can experiment wil the full project. Can anyone see what is the problem? Thanks for your Help, Bruno #include #include ''stm8s.h'' /** * Analog I/Os are as follows on the STM8S208 daughterboard: * IN1 PF.4 - External analog input * IN2 PF.5 - External analog input * TEMP PF.6 - Temperature sensor analog input * POT PF.7 - Potentiometer analog input, min=0V, max=VDD */ #define CHANNEL_IN1 ADC2_CHANNEL_12 #define CHANNEL_IN2 ADC2_CHANNEL_13 #define CHANNEL_TEMP ADC2_CHANNEL_14 #define CHANNEL_POT ADC2_CHANNEL_15 #define MAX_ADC_VAL 0x3FF /* 10-bit ADCs */ /** * Voltage (in mV) of the Vcc of the Daughterboard. * This (global) variable is used in the voltage conversions from the ADC * for the conversion between the ADC range to the required voltage. * The default value is 4700 (4.7V). */ unsigned short VDDmV = 4700; /** * @brief Initializes ADC2 inputs for PF.4-7 * @param None * @retval None */ void ADC_Init(void) { /* Init GPIO for ADC2 */ GPIO_Init(GPIOF, GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7, GPIO_MODE_IN_FL_NO_IT); /* ADC2 configuration */ ADC2_DeInit(); /* Disable interrupts */ ADC2_ITConfig(DISABLE); } /** * Read the value from a given channel * @param channel The channel number to read from * @retval The analog value read (in mV) */ unsigned short ADC_ReadChannel(unsigned char channel) { unsigned short val; /* Configure ADC2 for a single capture on given channel */ ADC2_Init(ADC2_CONVERSIONMODE_SINGLE, channel, ADC2_PRESSEL_FCPU_D2, ADC2_EXTTRIG_TIM, DISABLE, ADC2_ALIGN_RIGHT, channel, DISABLE); ADC2_Cmd(ENABLE); ADC2_StartConversion(); /* Wait for end of conversion */ while(ADC2_GetFlagStatus() == RESET) ; val = ADC2_GetConversionValue(); ADC2_Cmd(DISABLE); ADC2_ClearFlag(); return val; } void main(void) { ADC_Init(); while(1) { printf(''I1 = %04x '', ADC_ReadChannel(CHANNEL_IN1)); printf(''I2 = %04x '', ADC_ReadChannel(CHANNEL_IN2)); printf(''POT = %04x '', ADC_ReadChannel(CHANNEL_POT)); printf(''TEMP = %04x\n'', ADC_ReadChannel(CHANNEL_TEMP)); } }