AnsweredAssumed Answered

STm32L151 temperature sensor example

Question asked by chowdhury.tahsin on Nov 23, 2015
I am new to STm32 architecture. Trying  to get the internal temperature sensor going. I am using a STm32L151VC microcontroller, STM32-QFP100 programmer adapter and IAR tool chain. I am trying to use the following code, but looks like ADC conversion is giving me some random value. Sometimes, it shows a random number between 600~700 ( which doesn't make sense as the hot calibration voltage is 626 at 100 degrees according to the data sheet), and the most other time variable AD_value shows 0. Any help/comment will be highly appreciated.

int main(void)
{
 
/* Configure PB.12 (ADC1 Channel18) or PF.10 (ADC1 Channel31) in analog mode -*/
  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
/* Enable the GPIOB Clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  GPIO_Init(GPIOB, &GPIO_InitStructure);  
/* Enable the HSI */
  RCC_HSICmd(ENABLE);
  /* Wait until HSI oscillator is ready */
  while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);
/* ADC1 Configuration -----------------------------------------------------*/
  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_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfConversion = 1;             // Set conversion data alignement
  ADC_Init(ADC1, &ADC_InitStructure);
 
  ADC_TempSensorVrefintCmd(ENABLE);
 /* Wait until ADC + Temp sensor start */
  T_StartupTimeDelay = 1024;
  while (T_StartupTimeDelay--);
 
 /* Setup ADC common init struct */
  ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;
  ADC_CommonInit(&ADC_CommonInitStructure);
 
ADC_RegularChannelConfig(ADC1, ADC_Channel_16, 1, ADC_SampleTime_384Cycles);
   
 /* Start ADC1 Software Conversion */
  while (1)
  {
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
  ADC_Cmd(ADC1, ENABLE);
  /* Wait until the ADC1 is ready */
  while(ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS) == RESET);
  ADC_SoftwareStartConv(ADC1);
  AD_value=ADC_GetConversionValue(ADC1);
 
 
  }

Outcomes