AnsweredAssumed Answered

STM32F3 internal temp sensor

Question asked by Arganas on Mar 7, 2014
Latest reply on Jul 22, 2014 by Clive One
Hello everyone, i am trying to measure the temperature with an internal temperature sensor. I looked down a lot of posts on this forum, but still didn't manage to find the answer. The code is compiling and i can run it without any problems. Not to long ago i tried to do the same with STM32F1 and had no problems. Could anyone please look down through my code and point out what i'm doing wrong? Or maybe im just missing something? I think that im just forgeting something, because ADC data register is always 0, and ADC value which is printed in the terminal is also always 0. Here is all my code, any help will be appreciated.

#include "stm32f30x.h"
#include "stdio.h"


USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
uint16_t us;


#define Get_temperature 0x33
#define Command_done 0xAA


uint16_t AD_value;
uint16_t V25 = 1933;// when V25=1.41V at ref 3.3V
float Avg_Slope = 5.33; //when avg_slope=4.3mV/C at ref 3.3V
int16_t TemperatureC;


void Timer3_config(void)
{
  TIM_TimeBaseInitStructure.TIM_Period = 0xFFFF-1; // 0.1us*time = x us. Vadinasi counteris iki X skaiciuos  x us.
  TIM_TimeBaseInitStructure.TIM_Prescaler = 72-1; // Padalina clocko 72Mhz is 72. Vadinasi 1 clocko tick'as yra 1us
  TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure );
  
  TIM_Cmd(TIM3, ENABLE);
}


void Delay_us(uint16_t us)
{
  TIM_SetCounter(TIM3, 0x00);
  uint16_t start = TIM_GetCounter(TIM3);
  while((TIM_GetCounter(TIM3) - start) < us);
}


void USART1_Config(void)
{
  USART_InitStructure.USART_BaudRate = 9600; // Baud rate. Bits (or characters) per second.
  USART_InitStructure.USART_WordLength = USART_WordLength_8b; // Word lenght = 8 bits
  USART_InitStructure.USART_StopBits = USART_StopBits_1; // Use one stop bit
  USART_InitStructure.USART_Parity = USART_Parity_No ; // No parity
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // Receive and transmit enabletd
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // Hardware flow control disabled (RTS and CTS signals)
  
  USART_Init(USART1, &USART_InitStructure); // Send values to the registewrs
  
  USART_Cmd(USART1, ENABLE); // Enable USART1 parameters
  
  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // Enable Rx interrupt
 
  NVIC_EnableIRQ(USART1_IRQn); // Enable USART1 global interrupt
}


void USART1_Rx_Tx_Config(void)
{
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; // use 9 and 10 pins for rx and tx
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_7); // Connect USART rx and tx pins to I/O pins
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_7);
}


void ADC1_config(void)
{
  ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_CommonInitStructure.ADC_Clock = ADC_Clock_AsynClkMode;  
  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
  ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_SampleTime_2Cycles5;
  ADC_CommonInit(ADC1, &ADC_CommonInitStructure);
  
  ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Disable; // Isjunkti continous ADC converting rezima
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; // Naudoti 12 bitu ADC
  ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0;         
  ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//
  ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable;   //
  ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Disable;  //
  ADC_InitStructure.ADC_NbrOfRegChannel = 1; // Kanalu skaicius, kurie bus convertinti
  ADC_Init(ADC1, &ADC_InitStructure);
 
  
}


void USART1_IRQHandler(void)
{
  if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) // Check if interrupt has occured or not
    {
        if(USART_ReceiveData(USART1) == Get_temperature) // If recieved t on rx pin , transmit T from tx pin
        {
           ADC_ClearFlag(ADC1, ADC_FLAG_EOC);
            AD_value=ADC_GetConversionValue(ADC1);
            printf("\r\n ADC value: %d \r\n", AD_value);
            TemperatureC = (int16_t)(((V25-AD_value)/Avg_Slope) + 25);
            printf("Temperature: %d%cC\r\n", TemperatureC, 176);
            USART_SendData(USART1, Command_done);
        }
    }
}




int main(void)
{
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); // Enable clock for port A
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE, ENABLE); // Enable clock for port E
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // USART1 clock enable
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); // Enable clock for port C
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12, ENABLE);
  RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div2);
   
  USART1_Config();
  USART1_Rx_Tx_Config();
  Timer3_config();
  USART1_IRQHandler();
  
    // Port E pin 8 blue led configuration
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; // use 8 pin to blink led
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; 
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOE, &GPIO_InitStructure);
  
  // NVIC structure for USART1 interrupt
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);


  // Calibration command sequence
  ADC_VoltageRegulatorCmd(ADC1, ENABLE);
  
  Delay_us(10);
  
  ADC_SelectCalibrationMode(ADC1, ADC_CalibrationMode_Single);
  ADC_StartCalibration(ADC1);
  
  while(ADC_GetCalibrationStatus(ADC1) != RESET );
  
  ADC1_config();


  ADC_RegularChannelConfig(ADC1, ADC_Channel_16, 1, ADC_SampleTime_7Cycles5 );
  //////////////////////////////////////
  // Start ADC
  ADC_Cmd(ADC1, ENABLE);
  // Wake up temp sensor
  ADC_TempSensorCmd(ADC1, ENABLE);


  while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_RDY));
  ADC_StartConversion(ADC1);  
  while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);


while (1)
  { 
  }
}




Outcomes