cancel
Showing results for 
Search instead for 
Did you mean: 

Reading ADC Multichannel

motorhead_i_t
Associate II
Posted on February 04, 2013 at 18:08

I want to read valuefromaccelerometer .

Model 1
void GPIO_ADC1_setup()
{
GPIO_InitTypeDef GPIO_InitStructure;
// Enable GPIOC clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
// Configure PA1 as analog input(ADC Channel) 
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void ADC_setup()
{
ADC_InitTypeDef ADC_InitStructure;
// Enable ADC1 clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
// ADC1 configuration
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 3;
ADC_Init(ADC1, &ADC_InitStructure);
// ADC1 regular channel1 configuration 
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_13Cycles5); // X Axis
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_13Cycles5); // Y Axis
ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_13Cycles5); // Z Axis
ADC_Cmd(ADC1, ENABLE); // Enable ADC1 
ADC_ResetCalibration(ADC1); // Enable ADC1 reset calibaration register 
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1); // Start ADC1 calibaration 
while(ADC_GetCalibrationStatus(ADC1)); // Check the end of ADC1 calibration 
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
int main (void) {
int16_t Sensor;
uint16_t SenVal[2];
ADC_setup();
while(1) {
for (Sensor=0;Sensor<3;Sensor++)
{
switch(Sensor)
{
case 0:
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_13Cycles5);
break;
case 1:
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_13Cycles5);
break;
case 2:
ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_13Cycles5);
break;
}
while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC)==RESET);
SenVal[Sensor]= ADC_GetConversionValue(ADC1);

Compile success but results are incorrect . where is wrong? Thank You.
2 REPLIES 2
motorhead_i_t
Associate II
Posted on February 04, 2013 at 18:13

Model 2

int main (void) {

__IO uint16_t ADC1ConvertedValue[2];

int16_t Sensor;

uint16_t SenVal[2];

ADC_setup();

while(1){

for(Sensor=0;Sensor<3;Sensor++)

{

SenVal[Sensor] = ADC1ConvertedValue[Sensor];

{

}

Posted on February 04, 2013 at 18:57

Compile success but results are incorrect . where is wrong?

Compilers test syntax, they can compile bad code.

Multiple channels on ADC, use DMA to transfer measurements to an array.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..