Demonstration of STM Internal Temperature read using ST HAL library within mbed. Reads Temperature, Vref, or Vbat based on config

Dependencies:   mbed-dev

Committer:
sixBase3
Date:
Thu Mar 31 15:52:25 2016 +0000
Revision:
0:ab323c9b71db
Demonstration of how to read internal ADC channels in STM micros using STM HAL library. F446 chosen

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sixBase3 0:ab323c9b71db 1 #include "mbed.h"
sixBase3 0:ab323c9b71db 2
sixBase3 0:ab323c9b71db 3 //mbed Declaration left here as example
sixBase3 0:ab323c9b71db 4 AnalogIn analog_value(A0);
sixBase3 0:ab323c9b71db 5
sixBase3 0:ab323c9b71db 6 DigitalOut led(LED1);
sixBase3 0:ab323c9b71db 7
sixBase3 0:ab323c9b71db 8 //ST HAL For ADC since we want to access the internal channels
sixBase3 0:ab323c9b71db 9 ADC_HandleTypeDef hadc1;
sixBase3 0:ab323c9b71db 10
sixBase3 0:ab323c9b71db 11 int main() {
sixBase3 0:ab323c9b71db 12 float meas;
sixBase3 0:ab323c9b71db 13
sixBase3 0:ab323c9b71db 14 ADC_ChannelConfTypeDef sConfig; //Declare the ST HAL ADC object
sixBase3 0:ab323c9b71db 15
sixBase3 0:ab323c9b71db 16 /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
sixBase3 0:ab323c9b71db 17 */
sixBase3 0:ab323c9b71db 18 hadc1.Instance = ADC1;
sixBase3 0:ab323c9b71db 19 hadc1.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV4;
sixBase3 0:ab323c9b71db 20 hadc1.Init.Resolution = ADC_RESOLUTION12b;
sixBase3 0:ab323c9b71db 21 hadc1.Init.ScanConvMode = DISABLE;
sixBase3 0:ab323c9b71db 22 hadc1.Init.ContinuousConvMode = DISABLE;
sixBase3 0:ab323c9b71db 23 hadc1.Init.DiscontinuousConvMode = DISABLE;
sixBase3 0:ab323c9b71db 24 hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
sixBase3 0:ab323c9b71db 25 hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
sixBase3 0:ab323c9b71db 26 hadc1.Init.NbrOfConversion = 1;
sixBase3 0:ab323c9b71db 27 hadc1.Init.DMAContinuousRequests = DISABLE;
sixBase3 0:ab323c9b71db 28 hadc1.Init.EOCSelection = EOC_SINGLE_CONV;
sixBase3 0:ab323c9b71db 29 HAL_ADC_Init(&hadc1); //Go turn on the ADC
sixBase3 0:ab323c9b71db 30
sixBase3 0:ab323c9b71db 31 sConfig.Channel = ADC_CHANNEL_TEMPSENSOR; //ADC_CHANNEL_VREFINT, ADC_CHANNEL_VBAT
sixBase3 0:ab323c9b71db 32 sConfig.Rank = 1;
sixBase3 0:ab323c9b71db 33 sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
sixBase3 0:ab323c9b71db 34
sixBase3 0:ab323c9b71db 35 printf("\nAnalogIn example\n");
sixBase3 0:ab323c9b71db 36
sixBase3 0:ab323c9b71db 37 while(1) {
sixBase3 0:ab323c9b71db 38 meas = analog_value.read(); // Converts and read the analog input value (value from 0.0 to 1.0)
sixBase3 0:ab323c9b71db 39 meas = meas * 3300; // Change the value to be in the 0 to 3300 range
sixBase3 0:ab323c9b71db 40 printf("A0 input: measure = %.0f mV\n", meas);
sixBase3 0:ab323c9b71db 41 if (meas > 2000) { // If the value is greater than 2V then switch the LED on
sixBase3 0:ab323c9b71db 42 led = 1;
sixBase3 0:ab323c9b71db 43 }
sixBase3 0:ab323c9b71db 44 else {
sixBase3 0:ab323c9b71db 45 led = 0;
sixBase3 0:ab323c9b71db 46 }
sixBase3 0:ab323c9b71db 47 wait(0.2); // 200 ms
sixBase3 0:ab323c9b71db 48
sixBase3 0:ab323c9b71db 49 //Now go read the STM internal channel selected above
sixBase3 0:ab323c9b71db 50 HAL_ADC_ConfigChannel(&hadc1, &sConfig);
sixBase3 0:ab323c9b71db 51 HAL_ADC_Start(&hadc1); // Start conversion
sixBase3 0:ab323c9b71db 52
sixBase3 0:ab323c9b71db 53 // Wait end of conversion and get value
sixBase3 0:ab323c9b71db 54 if (HAL_ADC_PollForConversion(&hadc1, 10) == HAL_OK) {
sixBase3 0:ab323c9b71db 55 uint16_t value = HAL_ADC_GetValue(&hadc1);
sixBase3 0:ab323c9b71db 56 printf("Internal Channel Value = %04X\n\r",value);
sixBase3 0:ab323c9b71db 57 } else {
sixBase3 0:ab323c9b71db 58 error("Conversion Error");
sixBase3 0:ab323c9b71db 59 }
sixBase3 0:ab323c9b71db 60
sixBase3 0:ab323c9b71db 61 }
sixBase3 0:ab323c9b71db 62 }