![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
STM32F411RE WORKING HAL ADC EXAMPLE
#include "mbed.h" #include "string" #include "cmsis.h" #include "pinmap.h" #include "PeripheralPins.h" Serial pc(USBTX, USBRX); uint32_t g_ADCValue; float g_ADC_Value_Volts; const float maxAdcBits = 4095.0f; // Using Float for clarity const float maxVolts = 3.3f; // Using Float for clarity const float voltsPerBit = (maxVolts / maxAdcBits); int main() { //Start of initializing analog input on pin PC_1 ADC_HandleTypeDef AdcHandle; GPIO_InitTypeDef gpioInit; //Start GPIO and ADC1 Clocks __GPIOC_CLK_ENABLE(); __ADC1_CLK_ENABLE(); //Define PC_1 which is Pin one of group C and connected to a 10K potentiometer. gpioInit.Pin = GPIO_PIN_1; gpioInit.Mode = GPIO_MODE_ANALOG; gpioInit.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOC, &gpioInit); // Configure ADC AdcHandle.Instance = ADC1; AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2; AdcHandle.Init.Resolution = ADC_RESOLUTION12b; AdcHandle.Init.ScanConvMode = DISABLE; AdcHandle.Init.ContinuousConvMode = ENABLE; AdcHandle.Init.DiscontinuousConvMode = DISABLE; AdcHandle.Init.NbrOfDiscConversion = 0; AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1; AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; AdcHandle.Init.NbrOfConversion = 1; AdcHandle.Init.DMAContinuousRequests = DISABLE; AdcHandle.Init.EOCSelection = DISABLE; if (HAL_ADC_Init(&AdcHandle) != HAL_OK) { pc.printf("Cannot initialize ADC\n"); while(1){ } } ADC_ChannelConfTypeDef sConfig = {0}; // Configure ADC channel sConfig.Rank = 1; sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES; sConfig.Offset = 0; sConfig.Channel = ADC_CHANNEL_11; if (HAL_ADC_ConfigChannel(&AdcHandle, &sConfig) != HAL_OK) { pc.printf("Cannot config channel\r\n"); while(1){ } } HAL_ADC_ConfigChannel(&AdcHandle, &sConfig); //Not sure why, but I have to do this twice? pc.printf("AnalogIn example\r\n"); HAL_ADC_Start(&AdcHandle); // Start conversion while(1) { g_ADCValue = 0; if (HAL_ADC_PollForConversion(&AdcHandle, 1000000) == HAL_OK) { g_ADCValue = HAL_ADC_GetValue(&AdcHandle); g_ADC_Value_Volts = g_ADCValue*voltsPerBit; pc.printf("Conversion ready \r\n"); printf("measure = %f V \r\n", g_ADC_Value_Volts); } else { pc.printf("Conversion not ready \r\n"); } wait(1.0); // 1 second } }
main.cpp@0:797cb3866420, 2018-06-29 (annotated)
- Committer:
- trevieze
- Date:
- Fri Jun 29 13:30:19 2018 +0000
- Revision:
- 0:797cb3866420
STM32F411RE HAL ADC WORKING EXAMPLE.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
trevieze | 0:797cb3866420 | 1 | #include "mbed.h" |
trevieze | 0:797cb3866420 | 2 | #include "string" |
trevieze | 0:797cb3866420 | 3 | |
trevieze | 0:797cb3866420 | 4 | #include "cmsis.h" |
trevieze | 0:797cb3866420 | 5 | #include "pinmap.h" |
trevieze | 0:797cb3866420 | 6 | #include "PeripheralPins.h" |
trevieze | 0:797cb3866420 | 7 | |
trevieze | 0:797cb3866420 | 8 | |
trevieze | 0:797cb3866420 | 9 | Serial pc(USBTX, USBRX); |
trevieze | 0:797cb3866420 | 10 | |
trevieze | 0:797cb3866420 | 11 | uint32_t g_ADCValue; |
trevieze | 0:797cb3866420 | 12 | float g_ADC_Value_Volts; |
trevieze | 0:797cb3866420 | 13 | const float maxAdcBits = 4095.0f; // Using Float for clarity |
trevieze | 0:797cb3866420 | 14 | const float maxVolts = 3.3f; // Using Float for clarity |
trevieze | 0:797cb3866420 | 15 | const float voltsPerBit = (maxVolts / maxAdcBits); |
trevieze | 0:797cb3866420 | 16 | |
trevieze | 0:797cb3866420 | 17 | int main() |
trevieze | 0:797cb3866420 | 18 | { |
trevieze | 0:797cb3866420 | 19 | //Start of initializing analog input on pin PC_1 |
trevieze | 0:797cb3866420 | 20 | ADC_HandleTypeDef AdcHandle; |
trevieze | 0:797cb3866420 | 21 | GPIO_InitTypeDef gpioInit; |
trevieze | 0:797cb3866420 | 22 | //Start GPIO and ADC1 Clocks |
trevieze | 0:797cb3866420 | 23 | __GPIOC_CLK_ENABLE(); |
trevieze | 0:797cb3866420 | 24 | __ADC1_CLK_ENABLE(); |
trevieze | 0:797cb3866420 | 25 | //Define PC_1 which is Pin one of group C and connected to a 10K potentiometer. |
trevieze | 0:797cb3866420 | 26 | gpioInit.Pin = GPIO_PIN_1; |
trevieze | 0:797cb3866420 | 27 | gpioInit.Mode = GPIO_MODE_ANALOG; |
trevieze | 0:797cb3866420 | 28 | gpioInit.Pull = GPIO_NOPULL; |
trevieze | 0:797cb3866420 | 29 | HAL_GPIO_Init(GPIOC, &gpioInit); |
trevieze | 0:797cb3866420 | 30 | |
trevieze | 0:797cb3866420 | 31 | // Configure ADC |
trevieze | 0:797cb3866420 | 32 | AdcHandle.Instance = ADC1; |
trevieze | 0:797cb3866420 | 33 | AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2; |
trevieze | 0:797cb3866420 | 34 | AdcHandle.Init.Resolution = ADC_RESOLUTION12b; |
trevieze | 0:797cb3866420 | 35 | AdcHandle.Init.ScanConvMode = DISABLE; |
trevieze | 0:797cb3866420 | 36 | AdcHandle.Init.ContinuousConvMode = ENABLE; |
trevieze | 0:797cb3866420 | 37 | AdcHandle.Init.DiscontinuousConvMode = DISABLE; |
trevieze | 0:797cb3866420 | 38 | AdcHandle.Init.NbrOfDiscConversion = 0; |
trevieze | 0:797cb3866420 | 39 | AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; |
trevieze | 0:797cb3866420 | 40 | AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1; |
trevieze | 0:797cb3866420 | 41 | AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; |
trevieze | 0:797cb3866420 | 42 | AdcHandle.Init.NbrOfConversion = 1; |
trevieze | 0:797cb3866420 | 43 | AdcHandle.Init.DMAContinuousRequests = DISABLE; |
trevieze | 0:797cb3866420 | 44 | AdcHandle.Init.EOCSelection = DISABLE; |
trevieze | 0:797cb3866420 | 45 | |
trevieze | 0:797cb3866420 | 46 | if (HAL_ADC_Init(&AdcHandle) != HAL_OK) { |
trevieze | 0:797cb3866420 | 47 | pc.printf("Cannot initialize ADC\n"); |
trevieze | 0:797cb3866420 | 48 | while(1){ |
trevieze | 0:797cb3866420 | 49 | } |
trevieze | 0:797cb3866420 | 50 | } |
trevieze | 0:797cb3866420 | 51 | ADC_ChannelConfTypeDef sConfig = {0}; |
trevieze | 0:797cb3866420 | 52 | // Configure ADC channel |
trevieze | 0:797cb3866420 | 53 | sConfig.Rank = 1; |
trevieze | 0:797cb3866420 | 54 | sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES; |
trevieze | 0:797cb3866420 | 55 | sConfig.Offset = 0; |
trevieze | 0:797cb3866420 | 56 | sConfig.Channel = ADC_CHANNEL_11; |
trevieze | 0:797cb3866420 | 57 | |
trevieze | 0:797cb3866420 | 58 | if (HAL_ADC_ConfigChannel(&AdcHandle, &sConfig) != HAL_OK) { |
trevieze | 0:797cb3866420 | 59 | pc.printf("Cannot config channel\r\n"); |
trevieze | 0:797cb3866420 | 60 | while(1){ |
trevieze | 0:797cb3866420 | 61 | } |
trevieze | 0:797cb3866420 | 62 | } |
trevieze | 0:797cb3866420 | 63 | HAL_ADC_ConfigChannel(&AdcHandle, &sConfig); //Not sure why, but I have to do this twice? |
trevieze | 0:797cb3866420 | 64 | pc.printf("AnalogIn example\r\n"); |
trevieze | 0:797cb3866420 | 65 | HAL_ADC_Start(&AdcHandle); // Start conversion |
trevieze | 0:797cb3866420 | 66 | while(1) { |
trevieze | 0:797cb3866420 | 67 | g_ADCValue = 0; |
trevieze | 0:797cb3866420 | 68 | if (HAL_ADC_PollForConversion(&AdcHandle, 1000000) == HAL_OK) { |
trevieze | 0:797cb3866420 | 69 | g_ADCValue = HAL_ADC_GetValue(&AdcHandle); |
trevieze | 0:797cb3866420 | 70 | g_ADC_Value_Volts = g_ADCValue*voltsPerBit; |
trevieze | 0:797cb3866420 | 71 | pc.printf("Conversion ready \r\n"); |
trevieze | 0:797cb3866420 | 72 | printf("measure = %f V \r\n", g_ADC_Value_Volts); |
trevieze | 0:797cb3866420 | 73 | } else { |
trevieze | 0:797cb3866420 | 74 | pc.printf("Conversion not ready \r\n"); |
trevieze | 0:797cb3866420 | 75 | } |
trevieze | 0:797cb3866420 | 76 | wait(1.0); // 1 second |
trevieze | 0:797cb3866420 | 77 | } |
trevieze | 0:797cb3866420 | 78 | } |
trevieze | 0:797cb3866420 | 79 | |
trevieze | 0:797cb3866420 | 80 | |
trevieze | 0:797cb3866420 | 81 | |
trevieze | 0:797cb3866420 | 82 | |
trevieze | 0:797cb3866420 | 83 | |
trevieze | 0:797cb3866420 | 84 | |
trevieze | 0:797cb3866420 | 85 |