Script to get the temp sensor value for STM32L0

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 #define TEMP130_CAL_ADDR ((uint16_t*) ((uint32_t) 0x1FF8007E))
00004 #define TEMP30_CAL_ADDR ((uint16_t*) ((uint32_t) 0x1FF8007A))
00005 #define VDD_CALIB ((uint16_t) (300))
00006 #define VDD_APPLI ((uint16_t) (330))
00007 Serial pcMain(USBTX, USBRX);
00008 
00009 
00010 int32_t ComputeTemperature(uint32_t measure)
00011 {
00012   int32_t temperature;
00013   temperature = ((measure * VDD_APPLI / VDD_CALIB) - (int32_t)*TEMP30_CAL_ADDR );
00014   temperature = temperature *(int32_t)(130-30);
00015   temperature = temperature /(int32_t)(*TEMP130_CAL_ADDR -*TEMP30_CAL_ADDR);
00016   temperature = temperature + 30;
00017   return(temperature);
00018 }
00019 
00020 
00021 void ConfigTemperature(void){
00022     //Clock configuration
00023     RCC->APB2ENR |= RCC_APB2ENR_ADC1EN;
00024     ADC1->CFGR2 |= ADC_CFGR2_CKMODE;
00025 
00026     //*******************//NEW PART BEGIN//******************//
00027     //ADC Calibration
00028     if ((ADC1->CR & ADC_CR_ADEN) != 0) /* (1) */
00029     {
00030      ADC1->CR &= (uint32_t)(~ADC_CR_ADEN); /* (2) */
00031     }
00032     ADC1->CR |= ADC_CR_ADCAL; /* (3) */
00033     while ((ADC1->ISR & ADC_ISR_EOCAL) == 0) /* (4) */
00034     {
00035         pcMain.printf("Calib");
00036     }
00037     ADC1->ISR |= ADC_ISR_EOCAL;
00038     
00039     //Enable the ADC
00040     ADC1->ISR |= ADC_ISR_ADRDY; /* (1) */
00041     ADC1->CR |= ADC_CR_ADEN; /* (2) */
00042     if ((ADC1->CFGR1 & ADC_CFGR1_AUTOFF) == 0)
00043     {
00044      while ((ADC1->ISR & ADC_ISR_ADRDY) == 0) /* (3) */
00045      {
00046          pcMain.printf("Enable");
00047      }
00048     }
00049     //*******************//NEW PART END//******************//
00050 
00051     //Configuration of the temp sensor
00052     ADC1->CFGR1 |= ADC_CFGR1_CONT; /* (2) */
00053     ADC1->CHSELR = ADC_CHSELR_CHSEL18; /* (3) */
00054     ADC1->SMPR |= ADC_SMPR_SMP; /* (4) */
00055     wait(1);
00056     ADC->CCR |= ADC_CCR_TSEN;
00057     wait(1);
00058     uint32_t measure = ADC1->DR;
00059     pcMain.printf("Measure %u\n\r", measure);
00060     pcMain.printf("The temperature value is %i\n\r",ComputeTemperature(measure));
00061 
00062 }
00063 
00064 /***************************************************************************//**
00065  * @fn main(void)
00066  * @brief Main of the project
00067  * @param void
00068  * @return int
00069  * @author Edson CALSIN, Francois DREVETON, Simon NOWAK
00070  *
00071  ******************************************************************************/
00072 int main(void)
00073 {
00074     ConfigTemperature();
00075 }