/** * @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) { } }
uint16_t AdcMcuRead( Adc_t *obj, uint8_t channel ) { uint16_t adcData = 0; /* Enable The HSI (16Mhz) */ RCC_HSICmd( ENABLE ); /* Check that HSI oscillator is ready */ while( RCC_GetFlagStatus( RCC_FLAG_HSIRDY ) == RESET ); RCC_APB2PeriphClockCmd( RCC_APB2Periph_ADC1, ENABLE ); // Temperature or Vref measurement if( ( channel == ADC_Channel_16 ) || ( channel == ADC_Channel_17 ) ) { // Yes, enable temperature sensor and internal reference voltage ADC_TempSensorVrefintCmd( ENABLE ); } // Configure selected channel ADC_RegularChannelConfig( ADC1, 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 ) { } adcData = ADC_GetConversionValue( ADC1 ); ADC_Cmd( ADC1, DISABLE ); if( ( channel == ADC_Channel_16 ) || ( channel == ADC_Channel_17 ) ) { // De-initialize ADC ADC_TempSensorVrefintCmd( DISABLE ); } RCC_APB2PeriphClockCmd( RCC_APB2Periph_ADC1, DISABLE ); RCC_HSICmd( DISABLE ); return adcData; }
/** * @brief ADC configuration for automatic IDD measurement. * @param None * @retval None */ void IDD_Measurement_ADC_Config(void) { ADC_InitTypeDef ADC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* Enable GPIOA Clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); /* Configure IDD Measurement pin (ADC Channelxx) as analog input -----------*/ GPIO_InitStructure.GPIO_Pin = IDD_MEASUREMENT_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_Init(IDD_MEASUREMENT_GPIO_PORT, &GPIO_InitStructure); /* ADC1 configuration --------------------------------------------------------*/ /* Enable HSI clock for ADC clock */ RCC_HSICmd(ENABLE); /*!< Wait till HSI is ready */ while (RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET) {} /* Enable ADC clock ----------------------------------------------------------*/ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /* de-initialize ADC ---------------------------------------------------------*/ ADC_DeInit(ADC1); /* ADC configured as follows: - NbrOfChannel = 1 - ADC_Channel_5 - Mode = Single ConversionMode(ContinuousConvMode Enabled) - Resolution = 12Bits - Prescaler = /1 - Sampling time 192 */ /* ADC Configuration */ 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 channel4 configuration */ ADC_RegularChannelConfig(ADC1, IDD_MEASUREMENT_ADC_CHANNEL, 1, ADC_SampleTime_192Cycles); ADC_DelaySelectionConfig(ADC1, ADC_DelayLength_Freeze); 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) { } }
/** * @brief ADC initialization (ADC_Channel_4) * @caller main and ADC_Icc_Test * @param None * @retval None */ void ADC_Icc_Init(void) { ADC_InitTypeDef ADC_InitStructure; /* Enable ADC clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /* de-initialize ADC */ ADC_DeInit(ADC1); /* ADC configured as follow: - NbrOfChannel = 1 - ADC_Channel_4 - Mode = Single ConversionMode(ContinuousConvMode disabled) - Resolution = 12Bits - Prescaler = /1 - sampling time 192 */ /* ADC Configuration */ ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = ENABLE; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; 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 channel4 configuration */ ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_192Cycles); ADC_DelaySelectionConfig(ADC1, ADC_DelayLength_Freeze); 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) { } }
void setup_adc(void) { // Setup an adc... ADC_InitTypeDef adcinit; /* Enable ADC clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //ADC_DeInit(ADC1); // all defaults... ADC_StructInit(&adcinit); ADC_Init(ADC1, &adcinit); //ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 1, ADC_SampleTime_192Cycles); ADC_DelaySelectionConfig(ADC1, ADC_DelayLength_Freeze); 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) ; }
void AdcMcuFormat( Adc_t *obj, AdcResolution AdcRes, AdcNumConversion AdcNumConv, AdcTriggerConv AdcTrig, AdcDataAlignement AdcDataAlig ) { /* Enable The HSI (16Mhz) */ RCC_HSICmd( ENABLE ); /* Check that HSI oscillator is ready */ while(RCC_GetFlagStatus( RCC_FLAG_HSIRDY ) == RESET ); RCC_APB2PeriphClockCmd( RCC_APB2Periph_ADC1, ENABLE ); // Setup lowest possible prescaler in oder to be able to operate // at the whole Vdd rage 1.6V to 3.6V ADC_CommonInitTypeDef Adc_CommInitStructure; Adc_CommInitStructure.ADC_Prescaler = ADC_Prescaler_Div4; ADC_CommonInit( &Adc_CommInitStructure ); ADC_InitTypeDef ADC_InitStructure; ADC_StructInit( &ADC_InitStructure ); if( AdcRes == ADC_12_BIT ) { ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; } else if( AdcRes == ADC_10_BIT ) { ADC_InitStructure.ADC_Resolution = ADC_Resolution_10b; } else if(AdcRes == ADC_8_BIT ) { ADC_InitStructure.ADC_Resolution = ADC_Resolution_8b; } else if(AdcRes == ADC_6_BIT ) { ADC_InitStructure.ADC_Resolution = ADC_Resolution_6b; } ADC_InitStructure.ADC_ScanConvMode = DISABLE; if( AdcNumConv == SINGLE_CONVERSION ) { ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; } else { ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; } if( AdcTrig == CONVERT_MANUAL_TRIG ) { ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; } else if( AdcTrig == CONVERT_RISING_EDGE ) { ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising; } else if( AdcTrig == CONVERT_FALLING_EDGE ) { ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Falling; } else { ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_RisingFalling; } if( AdcDataAlig == DATA_RIGHT_ALIGNED ) { ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; } else { ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Left; } ADC_InitStructure.ADC_NbrOfConversion = 1; ADC_Init( ADC1, &ADC_InitStructure ); ADC_DelaySelectionConfig( ADC1, ADC_DelayLength_Freeze ); RCC_APB2PeriphClockCmd( RCC_APB2Periph_ADC1, DISABLE ); RCC_HSICmd( DISABLE ); }
/** * @brief Configures the ADC1: channel (18 or 31) and channel (5 or 1b). * @param None * @retval None */ void ADC_Config(void) { /* Enable The HSI (16Mhz) */ RCC_HSICmd(ENABLE); /* Check that HSI oscillator is ready */ while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET); /* Configure RV3 input voltage */ /* Enable the GPIOF Clock */ RCC_AHBPeriphClockCmd(IDD_MEASUREMENT_GPIO_CLK, ENABLE); /* Configure PF.10 (ADC Channel31) or PA.12 (ADC channel 18) in analog mode */ GPIO_InitStructure.GPIO_Pin = GPIO_PIN_X; GPIO_Init(IDD_MEASUREMENT_GPIO, &GPIO_InitStructure); /* Configure the IDD input */ /* Configure PF.11 (ADC channel 1b) or PA.05 (ADC channel 5) in analog mode */ GPIO_InitStructure.GPIO_Pin = GPIO_PIN_Y; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(IDD_MEASUREMENT_GPIO, &GPIO_InitStructure); /* ADC1 Configuration ------------------------------------------------------*/ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /* ADC1 DeInit */ ADC_DeInit(ADC1); #ifdef USE_STM32L152D_EVAL /* Select ADC Bank channel */ ADC_BankSelection(ADC1, ADC_Bank_B); #endif /* ADC1 Configuration of channel18/31 and channel5/1b : continuous mode, external trigger (TIM2) */ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = ENABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Falling; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_TRGO; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = 2; ADC_Init(ADC1, &ADC_InitStructure); /* ADC1 Regular Channel Config */ ADC_RegularChannelConfig(ADC1, IDD_MEASUREMENT_ADC_CHANNEL, 1, ADC_SampleTime_192Cycles); ADC_RegularChannelConfig(ADC1, RV3_MEASUREMENT_ADC_CHANNEL, 2, ADC_SampleTime_192Cycles); /* Enables the ADC1 Power Down during Delay */ ADC_PowerDownCmd(ADC1, ADC_PowerDown_Idle_Delay, ENABLE); /* Delay until the converted data has been read */ ADC_DelaySelectionConfig(ADC1, ADC_DelayLength_Freeze); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Wait until the ADC1 is ready */ while(ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS) == RESET) { } }
/* functions */ void adc_init() { GPIO_InitTypeDef gpio; ADC_CommonInitTypeDef adc_com; ADC_InitTypeDef adc_init; NVIC_InitTypeDef nvic; int i; // make sure the HSI is turned on - STM32L1xx uses HSI only for ADC RCC_HSICmd(ENABLE); // enable ADC clock RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); // set up pins as analog inputs // NOTE: clocks were likely enabled earlier. fix if they weren't. gpio.GPIO_Mode = GPIO_Mode_AN; gpio.GPIO_Speed = GPIO_Speed_2MHz; gpio.GPIO_PuPd = GPIO_PuPd_NOPULL; for (i = 0; i < 3; i++) { gpio.GPIO_Pin = adc_gpio[i].pin; GPIO_Init(adc_gpio[i].port, &gpio); } // configure ADC initial values ADC_CommonStructInit(&adc_com); ADC_StructInit(&adc_init); adc_init.ADC_ExternalTrigConv = 0; // set up channels to read ADC_RegularChannelConfig(ADC1, ADC_CHAN_MIC_SIG, ADC_READ_MIC_SIG + 1, ADC_SampleTime_4Cycles); ADC_RegularChannelConfig(ADC1, ADC_CHAN_MIC_PEAK, ADC_READ_MIC_PEAK + 1, ADC_SampleTime_4Cycles); ADC_RegularChannelConfig(ADC1, ADC_CHAN_BATT_VOLTAGE, ADC_READ_BATT_VOLTAGE + 1, ADC_SampleTime_4Cycles); adc_init.ADC_ScanConvMode = ENABLE; adc_init.ADC_NbrOfConversion = 3; // enable end of conversion on each channel read ADC_EOCOnEachRegularChannelCmd(ADC1, ENABLE); // freeze ADC until data has been read ADC_DelaySelectionConfig(ADC1, ADC_DelayLength_Freeze); // initialize the ADC ADC_CommonInit(&adc_com); ADC_Init(ADC1, &adc_init); // it doesn't look like STM32L100 has auto-calibrate feature, // but if it did, we'd set it up here. baw. // set up ADC power saving ADC_PowerDownCmd(ADC1, ADC_PowerDown_Idle_Delay, ENABLE); // enable interrupt ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE); // set interrupt priority nvic.NVIC_IRQChannel = ADC1_IRQn; nvic.NVIC_IRQChannelPreemptionPriority = 3; nvic.NVIC_IRQChannelSubPriority = 0; nvic.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&nvic); // finally, turn on the ADC ADC_Cmd(ADC1, ENABLE); }