implement voltage ADC

This commit is contained in:
Oskar Weigl
2016-05-27 19:59:03 +02:00
parent 3949e01e61
commit 9fb7b17e7c
4 changed files with 14 additions and 6 deletions
+2
View File
@@ -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
+2 -2
View File
@@ -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
+7 -2
View File
@@ -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 */
/**
+3 -2
View File
@@ -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);
}
}