From 9fb7b17e7c2dc96275dd4fb03fce34401a289ab7 Mon Sep 17 00:00:00 2001 From: Oskar Weigl Date: Fri, 27 May 2016 19:59:03 +0200 Subject: [PATCH] implement voltage ADC --- Inc/adc.h | 2 ++ Odrive.ioc | 4 ++-- Src/adc.c | 9 +++++++-- Src/test.c | 5 +++-- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Inc/adc.h b/Inc/adc.h index 4b3fdecf..7c67d867 100644 --- a/Inc/adc.h +++ b/Inc/adc.h @@ -57,6 +57,8 @@ void MX_ADC2_Init(void); /* USER CODE BEGIN Prototypes */ +float read_ADC_volts(ADC_HandleTypeDef* hadc); + /* USER CODE END Prototypes */ #ifdef __cplusplus diff --git a/Odrive.ioc b/Odrive.ioc index 59177276..aee5e836 100755 --- a/Odrive.ioc +++ b/Odrive.ioc @@ -1,5 +1,5 @@ #MicroXplorer Configuration settings - do not modify -ADC1.Channel-0\#ChannelRegularConversion=ADC_CHANNEL_5 +ADC1.Channel-0\#ChannelRegularConversion=ADC_CHANNEL_0 ADC1.Channel-1\#ChannelInjectedConversion=ADC_CHANNEL_10 ADC1.ClockPrescaler=ADC_CLOCK_SYNC_PCLK_DIV4 ADC1.ContinuousConvMode=DISABLE @@ -24,7 +24,7 @@ ADC1.SamplingTime-0\#ChannelRegularConversion=ADC_SAMPLETIME_3CYCLES ADC1.SamplingTime-1\#ChannelInjectedConversion=ADC_SAMPLETIME_3CYCLES ADC1.ScanConvMode=DISABLE ADC1.master=1 -ADC2.Channel-0\#ChannelRegularConversion=ADC_CHANNEL_5 +ADC2.Channel-0\#ChannelRegularConversion=ADC_CHANNEL_0 ADC2.Channel-1\#ChannelInjectedConversion=ADC_CHANNEL_11 ADC2.ClockPrescaler=ADC_CLOCK_SYNC_PCLK_DIV4 ADC2.ContinuousConvMode=DISABLE diff --git a/Src/adc.c b/Src/adc.c index 215a5f56..da1b4456 100644 --- a/Src/adc.c +++ b/Src/adc.c @@ -67,7 +67,7 @@ void MX_ADC1_Init(void) /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. */ - sConfig.Channel = ADC_CHANNEL_5; + sConfig.Channel = ADC_CHANNEL_0; sConfig.Rank = 1; sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; HAL_ADC_ConfigChannel(&hadc1, &sConfig); @@ -109,7 +109,7 @@ void MX_ADC2_Init(void) /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. */ - sConfig.Channel = ADC_CHANNEL_5; + sConfig.Channel = ADC_CHANNEL_0; sConfig.Rank = 1; sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; HAL_ADC_ConfigChannel(&hadc2, &sConfig); @@ -304,6 +304,11 @@ void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc) /* USER CODE BEGIN 1 */ +float read_ADC_volts(ADC_HandleTypeDef* hadc) { + uint32_t ADCValue = HAL_ADC_GetValue(&hadc1); + return (3.3f/((float)(1<<12))) * ADCValue; +} + /* USER CODE END 1 */ /** diff --git a/Src/test.c b/Src/test.c index 66dc4880..31b564e1 100644 --- a/Src/test.c +++ b/Src/test.c @@ -19,9 +19,10 @@ static void test_read_ADC(void){ HAL_ADC_Start(&hadc1); osDelay(2); if (HAL_ADC_PollForConversion(&hadc1, 1000) == HAL_OK) { - uint32_t ADCValue = HAL_ADC_GetValue(&hadc1); + float vbus_s = read_ADC_volts(&hadc1); + float busvoltage = (1.0f + 10.0f)/1.0f * vbus_s; ++measnum; } - osDelay(1000); + osDelay(10); } }