AnsweredAssumed Answered

Temperature sensor in stm32f4-discovery problems

Question asked by ashraf.salahuddin on May 4, 2014
Latest reply on May 5, 2014 by ashraf.salahuddin
Hi everyone, 
I'm trying to read temperature from temperature sensor in stm32f4-discovery kit 
I've configured ADC1 and then read converted value and do some math on it 

now when debugging it goes to hard fault
why ?? I don't know

and if there're other notes on this code ?? 

<b>
/*
 *In this project I need to read the temperature sensor, convert it into digital and 
 *send this reading to be displayed on the PC (RealTerm)using USART.
 */
 
#include "stm32f4_discovery.h"
 
/**************************************************************************************/


void delay(uint32_t);


void RCC_Configuration(void)
{
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
}
 
/**************************************************************************************/
 
 
void ADC_Configuration(void)
{
     /*
      * In stm32f4-discovery kit, the temperature sensor is connected to ADC1_IN16
      */
     
     
  ADC_CommonInitTypeDef ADC_CommonInitStruct;
  ADC_InitTypeDef ADC_InitStruct;
 
     ADC_CommonInitStruct.ADC_Mode = ADC_Mode_Independent;
     ADC_CommonInitStruct.ADC_Prescaler = ADC_Prescaler_Div8;
     ADC_CommonInitStruct.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
     ADC_CommonInitStruct.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
     ADC_CommonInit(&ADC_CommonInitStruct);


     ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b;
     ADC_InitStruct.ADC_ScanConvMode = DISABLE;
     ADC_InitStruct.ADC_ContinuousConvMode = ENABLE;
     ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
     ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
     ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;
     ADC_InitStruct.ADC_NbrOfConversion = 1;
     ADC_Init(ADC1, &ADC_InitStruct);


     //The next statement configures the ADC1 channel, rank and sample time
     ADC_RegularChannelConfig(ADC1, ADC_Channel_TempSensor, 1, ADC_SampleTime_144Cycles);  //ADC_Channel_TempSensor = ADC_Channel_16


     /* Then we enable the internal temperature sensor
      * Bit 23 TSVREFE: Temperature sensor and VREFINT enable
      * This bit is set and cleared by software to enable/disable the temperature sensor and the 
   * VREFINT channel.
      * ADC->CCR |= (uint32_t)ADC_CCR_TSVREFE;
      */
     ADC_TempSensorVrefintCmd(ENABLE);


     //The next statement enables ADC1
     ADC_Cmd(ADC1, ENABLE);
}
 
/**************************************************************************************/
 


#define BUFFERSIZE 128


uint16_t ADCConvertedValues[BUFFERSIZE];               //an array to hold the converted values
 
int main(void)
{
  static volatile float TemperatureValue = 0;
     
  RCC_Configuration();
 
  ADC_Configuration();
 
  STM_EVAL_LEDInit(LED3);                                                                                           // Configure LEDs to monitor program status 
 
  STM_EVAL_LEDOn(LED3);                                                                                                // Turn LED3 on 
 
  while(1)                                                                                                                                   // Don't want to exit
  {
    ADC_SoftwareStartConv(ADC1); //Start the conversion
          
          while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET)
                    ; //Processing the conversion
          
          /*
           * To calculate the temperature we follow this formula
           * Temperature (in C) = {(Vsense - V25)/(Avg_Slope)}+25 
           * Vsense = 0.76V, Avg_Slope = 2.5mV
           * Temperature = 
           */
          TemperatureValue = ADC_GetConversionValue(ADC1); //Return the converted data
          TemperatureValue *= 3300;
          TemperatureValue /= 0xfff;                               //Reading in mV
          TemperatureValue /= (float)1000.0;          //Reading in Volts
          TemperatureValue -= (float)0.760;           // Subtract the reference voltage at 25°C
          TemperatureValue /= (float).0025;           // Divide by slope 2.5mV
          TemperatureValue += (float)25.0;                // Add the 25°C
 
    /* Toggle LED3 and delay */
    STM_EVAL_LEDToggle(LED3);
          delay(3*1000000);
  }
}






//delay function
void delay(uint32_t cnt)
{
while(cnt-->0);
}
</b>


Thanks a lot for your time

Outcomes