cancel
Showing results for 
Search instead for 
Did you mean: 

[SOLVED] STM32l152c Discovery board, Cant see my problem getting ADC working

julian2399
Associate II
Posted on September 22, 2015 at 13:29

Initialisation call from main
adc_setup_data.adc_block = ADC1;
adc_setup_data.adc_channel = 1;
adc_setup_data.adc_number_of_bits = ADC_Resolution_12b;
adc_setup_data.adc_sample_time = ADC_SampleTime_16Cycles;
adc_setup_data.adc_GPIOx = GPIOA;
adc_setup_data.adc_gpio_pin = GPIO_Pin_1;
adc_init( adc_poll_callback, &adc_setup_data);
The routine itself
int adc_init( adc_poll_data poll_cb, void *user_data)
{
ADC_CommonInitTypeDef prescale;
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
adc_setup *setup = user_data;
if( !IS_ADC_ALL_PERIPH(setup->adc_block)) {
return -1;
}
if( !IS_ADC_CHANNEL(setup->adc_channel)) {
return -1;
}
if( !IS_ADC_RESOLUTION(setup->adc_number_of_bits)) {
return -1;
}
if( !IS_ADC_SAMPLE_TIME( setup->adc_sample_time)) {
}
/* Common Prescaler for ADC */
prescale.ADC_Prescaler = ADC_Prescaler_Div4; /* ADC_Prescaler_Div1, ADC_Prescaler_Div2 or ADC_Prescaler_Div4 */
ADC_CommonInit(&prescale);
if( setup->adc_block == ADC1) {
RCC_PCLK2Config(RCC_HCLK_Div16);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
}
else {
return -1;
}
adc_data.adc.ADCx = setup->adc_block;
adc_data.adc.adc_channel = setup->adc_channel;
adc_data.adc.adc_last_value = (1<<
12
) + 1;
adc_data.adc.adc_differential
= 
8
;
adc_data.queue
= 
queue_create
(5);
adc_data.running
= 
FALSE
;
adc_data.poll_callback
= 
poll_cb
;
adc_data.poll_user_data
= user_data;
// Init the GPIO with the structure
GPIO_InitStructure.GPIO_Pin
= 
setup
->adc_gpio_pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(setup->adc_GPIOx, &GPIO_InitStructure);
ADC_DeInit(setup->adc_block);
/* Configuration ------------------------------------------------------*/
ADC_InitStructure.ADC_Resolution = setup->adc_number_of_bits;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_Init(setup->adc_block, &ADC_InitStructure);
//configure channel
ADC_RegularChannelConfig(setup->adc_block, setup->adc_channel, 1, setup->adc_sample_time);
//Enable channel
ADC_Cmd(setup->adc_block, ENABLE);
return 1;
}
 Ive walked this code and cannot see what im doing wrong, but every read from the ADC os 0x000
 Any help
 joolz

11 REPLIES 11
AvaTar
Lead
Posted on September 23, 2015 at 14:33

Fist, I suggest to check your schematics and pin assignments. UM1079, the L152C discovery kit manual, suggests PA1 is reserved for segment 0 of the LCD/glass display.

PA5 is free, PA0 might work, too, if you don't use the pushbutton functionality. As a code example, I suggest the ''ADC1_IDDmeas'' project in the L1xx_StdPeriphLibrary examples. Here the ADC initialization code (continuous mode, no interrupt/DMA):

/**
* @brief Configures the ADC1 channel5.
* @param None
* @retval None
*/
void ADC_Config(void)
{
/* Enable The HSI (16Mhz) */
RCC_HSICmd(ENABLE);
/* Enable the GPIOF or GPIOA Clock */
RCC_AHBPeriphClockCmd(IDD_MEASUREMENT_GPIO_CLK, ENABLE);
/* Configure PF.11 (ADC Channel11) or PA.05 (ADC Channe5) in analog mode */
GPIO_InitStructure.GPIO_Pin = IDD_MEASUREMENT_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(IDD_MEASUREMENT_GPIO, &GPIO_InitStructure);
/* Check that HSI oscillator is ready */
while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);
/* ADC1 Configuration ------------------------------------------------------*/
/* Enable ADC1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
#ifdef USE_STM32L152D_EVAL
/* Select ADC Bank channel */
ADC_BankSelection(ADC1, ADC_Bank_B);
#endif
ADC_StructInit(&ADC_InitStructure);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channel5 or channel1 configuration */
ADC_RegularChannelConfig(ADC1, IDD_MEASUREMENT_ADC_CHANNEL, 1, ADC_SampleTime_192Cycles);
/* Define delay between ADC1 conversions */
ADC_DelaySelectionConfig(ADC1, ADC_DelayLength_Freeze);
/* Enable ADC1 Power Down during Delay */
ADC_PowerDownCmd(ADC1, ADC_PowerDown_Idle_Delay, ENABLE);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Wait until ADC1 ON status */
while (ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS) == RESET)
{
}
/* Start ADC1 Software Conversion */
ADC_SoftwareStartConv(ADC1);
/* Wait until ADC Channel 5 or 1 end of conversion */
while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET)
{
}
}

julian2399
Associate II
Posted on September 25, 2015 at 10:39

Problem FIXED, thanks for the help

The ADC driver was not the cause of the problem, it was the USB driver which on 1st reset calls SystemInit and this was disabling the HSI Clock.

joolz