F446RE ADC internal channels test: VREF, VBAT, temp sensor
F446RE ADC internal channels test: VREF, VBAT, temp sensor
main.cpp@0:0e42d22824a3, 2015-11-12 (annotated)
- Committer:
- manitou
- Date:
- Thu Nov 12 23:21:14 2015 +0000
- Revision:
- 0:0e42d22824a3
- Child:
- 1:709e8e98f880
F446RE ADC internal channel tests, VREF, VBAT, temp sensor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
manitou | 0:0e42d22824a3 | 1 | // access F446RE internal ADC channels temp sensor, VBAT, VREF |
manitou | 0:0e42d22824a3 | 2 | // based on pyboard implementation and analogin_api.c |
manitou | 0:0e42d22824a3 | 3 | // VBAT and temp are both channel 18, vref channel 17 |
manitou | 0:0e42d22824a3 | 4 | #include "mbed.h" |
manitou | 0:0e42d22824a3 | 5 | |
manitou | 0:0e42d22824a3 | 6 | #define PRREG(z) printf(#z" 0x%x\n",z) |
manitou | 0:0e42d22824a3 | 7 | |
manitou | 0:0e42d22824a3 | 8 | #define CORE_TEMP_V25 (4096*.76/3.3) // (0.76v/3.3v)*(2^ADC resoultion) |
manitou | 0:0e42d22824a3 | 9 | #define CORE_TEMP_AVG_SLOPE (4096*.0025/3.3) // (2.5mv/3.3v)*(2^ADC resoultion) |
manitou | 0:0e42d22824a3 | 10 | |
manitou | 0:0e42d22824a3 | 11 | // ? from pyboard version |
manitou | 0:0e42d22824a3 | 12 | #define VBAT_DIV (4) |
manitou | 0:0e42d22824a3 | 13 | |
manitou | 0:0e42d22824a3 | 14 | |
manitou | 0:0e42d22824a3 | 15 | static ADC_HandleTypeDef AdcHandle; |
manitou | 0:0e42d22824a3 | 16 | |
manitou | 0:0e42d22824a3 | 17 | void adc_init() { |
manitou | 0:0e42d22824a3 | 18 | // Enable ADC clock |
manitou | 0:0e42d22824a3 | 19 | __ADC1_CLK_ENABLE(); |
manitou | 0:0e42d22824a3 | 20 | |
manitou | 0:0e42d22824a3 | 21 | // Configure ADC |
manitou | 0:0e42d22824a3 | 22 | AdcHandle.Instance = (ADC_TypeDef *)ADC1; |
manitou | 0:0e42d22824a3 | 23 | AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2; |
manitou | 0:0e42d22824a3 | 24 | AdcHandle.Init.Resolution = ADC_RESOLUTION12b; |
manitou | 0:0e42d22824a3 | 25 | AdcHandle.Init.ScanConvMode = DISABLE; |
manitou | 0:0e42d22824a3 | 26 | AdcHandle.Init.ContinuousConvMode = DISABLE; |
manitou | 0:0e42d22824a3 | 27 | AdcHandle.Init.DiscontinuousConvMode = DISABLE; |
manitou | 0:0e42d22824a3 | 28 | AdcHandle.Init.NbrOfDiscConversion = 0; |
manitou | 0:0e42d22824a3 | 29 | AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; |
manitou | 0:0e42d22824a3 | 30 | AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1; |
manitou | 0:0e42d22824a3 | 31 | AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; |
manitou | 0:0e42d22824a3 | 32 | AdcHandle.Init.NbrOfConversion = 1; |
manitou | 0:0e42d22824a3 | 33 | AdcHandle.Init.DMAContinuousRequests = DISABLE; |
manitou | 0:0e42d22824a3 | 34 | AdcHandle.Init.EOCSelection = DISABLE; |
manitou | 0:0e42d22824a3 | 35 | HAL_ADC_Init(&AdcHandle); |
manitou | 0:0e42d22824a3 | 36 | } |
manitou | 0:0e42d22824a3 | 37 | |
manitou | 0:0e42d22824a3 | 38 | uint16_t adc_read_channel(uint32_t channel) { |
manitou | 0:0e42d22824a3 | 39 | ADC_ChannelConfTypeDef sConfig; |
manitou | 0:0e42d22824a3 | 40 | sConfig.Channel = channel; |
manitou | 0:0e42d22824a3 | 41 | sConfig.Rank = 1; |
manitou | 0:0e42d22824a3 | 42 | sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; |
manitou | 0:0e42d22824a3 | 43 | sConfig.Offset = 0; |
manitou | 0:0e42d22824a3 | 44 | HAL_ADC_ConfigChannel(&AdcHandle, &sConfig); |
manitou | 0:0e42d22824a3 | 45 | |
manitou | 0:0e42d22824a3 | 46 | HAL_ADC_Start(&AdcHandle); // Start conversion |
manitou | 0:0e42d22824a3 | 47 | |
manitou | 0:0e42d22824a3 | 48 | // Wait end of conversion and get value |
manitou | 0:0e42d22824a3 | 49 | if (HAL_ADC_PollForConversion(&AdcHandle, 10) == HAL_OK) { |
manitou | 0:0e42d22824a3 | 50 | return (HAL_ADC_GetValue(&AdcHandle)); |
manitou | 0:0e42d22824a3 | 51 | } else { |
manitou | 0:0e42d22824a3 | 52 | return 0; |
manitou | 0:0e42d22824a3 | 53 | } |
manitou | 0:0e42d22824a3 | 54 | } |
manitou | 0:0e42d22824a3 | 55 | |
manitou | 0:0e42d22824a3 | 56 | int main() { |
manitou | 0:0e42d22824a3 | 57 | uint16_t val; |
manitou | 0:0e42d22824a3 | 58 | float tempc, Vbat, Vcc; |
manitou | 0:0e42d22824a3 | 59 | |
manitou | 0:0e42d22824a3 | 60 | printf("\nSystemCoreClock %d %s %s\n",SystemCoreClock,__TIME__,__DATE__); |
manitou | 0:0e42d22824a3 | 61 | adc_init(); |
manitou | 0:0e42d22824a3 | 62 | |
manitou | 0:0e42d22824a3 | 63 | val = adc_read_channel(ADC_CHANNEL_VBAT); |
manitou | 0:0e42d22824a3 | 64 | Vbat = 3.3*VBAT_DIV*val/4096; |
manitou | 0:0e42d22824a3 | 65 | printf("%d Vbat %.2f\n",val,Vbat); |
manitou | 0:0e42d22824a3 | 66 | ADC->CCR=0; // clear CCR to avoid conflicting use |
manitou | 0:0e42d22824a3 | 67 | val = adc_read_channel(ADC_CHANNEL_TEMPSENSOR); |
manitou | 0:0e42d22824a3 | 68 | tempc = ((val - CORE_TEMP_V25) / CORE_TEMP_AVG_SLOPE) + 25.; |
manitou | 0:0e42d22824a3 | 69 | printf("%d %.1f C\n",val, tempc); |
manitou | 0:0e42d22824a3 | 70 | val = adc_read_channel(ADC_CHANNEL_VREFINT); |
manitou | 0:0e42d22824a3 | 71 | Vcc = 4095*1.21/val; |
manitou | 0:0e42d22824a3 | 72 | printf("%d Vcc %.2f\n",val,Vcc); |
manitou | 0:0e42d22824a3 | 73 | |
manitou | 0:0e42d22824a3 | 74 | PRREG(ADC1->CR1); |
manitou | 0:0e42d22824a3 | 75 | PRREG(ADC1->CR2); |
manitou | 0:0e42d22824a3 | 76 | PRREG(ADC->CCR); |
manitou | 0:0e42d22824a3 | 77 | |
manitou | 0:0e42d22824a3 | 78 | } |