cancel
Showing results for 
Search instead for 
Did you mean: 

Problem Reading 10 Ports with ADC1

douglas
Associate
Posted on February 16, 2014 at 21:24

Hello Based on some examples on the forum, here (Thank you).

I am using the STM32F407 based discovery board. I have the following code that works fine, but PA2, PA3, and PC4 do not give me the correct readings. As a test all Ain are connected to 1/2 VDD so the 12 bit response should be 0x0800 but for some reason they are being driven by something else. Right now I get 07FB080406DB0616080208010800080106DF07FD PA0 PA1PA2PA3PB0PB1PC1PC2PC4PC5 So my values for PA2, PA3, and PC4 are wrong and I can scope signals on PA2.

volatile uint16_t ADCValues[10]; 
#define PORTA_ADCPORT GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 
#define PORTB_ADCPORT GPIO_Pin_0 | GPIO_Pin_1 
#define PORTC_ADCPORT GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_5 
/**************************************************************************************/ 
void RCC_Configuration(void) 
{ 
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); 
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); 
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); 
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); 
} 
/**************************************************************************************/ 
void GPIO_Configuration(void) 
{ 
GPIO_InitTypeDef GPIOA_InitStructure; 
GPIO_InitTypeDef GPIOB_InitStructure; 
GPIO_InitTypeDef GPIOC_InitStructure; 
GPIO_StructInit(&GPIOA_InitStructure); 
GPIOA_InitStructure.GPIO_Pin = PORTA_ADCPORT; 
GPIOA_InitStructure.GPIO_Mode = GPIO_Mode_AN; 
GPIOA_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; 
GPIO_Init(GPIOA, &GPIOA_InitStructure); 
GPIOB_InitStructure.GPIO_Pin = PORTB_ADCPORT; 
GPIOB_InitStructure.GPIO_Mode = GPIO_Mode_AN; 
GPIOB_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; 
GPIO_Init(GPIOB, &GPIOB_InitStructure); 
GPIOC_InitStructure.GPIO_Pin = PORTC_ADCPORT; 
GPIOC_InitStructure.GPIO_Mode = GPIO_Mode_AN; 
GPIOC_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; 
GPIO_Init(GPIOC, &GPIOC_InitStructure); 
} 
void ADC1_Configuration(void) 
{ 
ADC_InitTypeDef ADC_InitStructure; 
ADC_CommonInitTypeDef ADC_CommonInitStructure; 
DMA_InitTypeDef DMA_InitStructure; 
/* Enable peripheral clocks *************************************************/ 
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); 
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); 
/* DMA2_Stream0 channel0 configuration **************************************/ 
DMA_DeInit(DMA2_Stream0); 
DMA_InitStructure.DMA_Channel = DMA_Channel_0; 
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR; 
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADCValues[0]; 
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; 
DMA_InitStructure.DMA_BufferSize = 10; 
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; 
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; 
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; 
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; 
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; 
DMA_InitStructure.DMA_Priority = DMA_Priority_High; 
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; 
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; 
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; 
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; 
DMA_Init(DMA2_Stream0, &DMA_InitStructure); 
/* DMA2_Stream0 enable */ 
DMA_Cmd(DMA2_Stream0, ENABLE); 
/* ADC Common Init **********************************************************/ 
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; 
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; 
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; 
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; 
ADC_CommonInit(&ADC_CommonInitStructure); 
/* ADC1 Init ****************************************************************/ 
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; 
ADC_InitStructure.ADC_ScanConvMode = ENABLE; 
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; 
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; 
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; 
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; 
ADC_InitStructure.ADC_NbrOfConversion = 10; 
ADC_Init(ADC1, &ADC_InitStructure); 
/* ADC1 regular channel configuration ******************************/ 
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_480Cycles); // PA0 
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_480Cycles); // PA1 
ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_480Cycles); // PA2 
ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 4, ADC_SampleTime_480Cycles); // PA3 
ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 5, ADC_SampleTime_480Cycles); // PB0 
ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 6, ADC_SampleTime_480Cycles); // PB1 
ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 7, ADC_SampleTime_480Cycles); // PC1 
ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 8, ADC_SampleTime_480Cycles); // PC2 
ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 9, ADC_SampleTime_480Cycles); // PC4 
ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 10, ADC_SampleTime_480Cycles); // PC5 
/* Enable DMA request after last transfer (Single-ADC mode) */ 
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE); 
/* Enable ADC1 DMA */ 
ADC_DMACmd(ADC1, ENABLE); 
/* Enable ADC1 **************************************************************/ 
ADC_Cmd(ADC1, ENABLE); 
/* Start ADC1 Software Conversion */ 
ADC_SoftwareStartConv(ADC1); 
} 
void ReadADC(void) 
{ 
puts(''ADC Testing\r\n''); 
while (!(rxready())) 
{ 
if (ADCValues[9] != 0xFFFF) 
{ 
for (int loop = 0; loop < 10; loop++) 
{ 
printf(''%04X '', ADCValues[loop]); 
} 
printf(''\r\n''); 
ADCValues[9] = 0xFFFF; 
} 
} 
}

2 REPLIES 2
douglas
Associate
Posted on February 17, 2014 at 04:50

Hello All,

So PA2 and PA3, were my dumb mistake. (was using them as SCI).

And it turns out I didn't assign PC4.

So everything works as it should,

Thanks for the great examples people.

Posted on February 17, 2014 at 14:50

So everything works as it should, Thanks for the great examples people.

That's good, I hadn't seen clashes on the pins in question.

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