stm32F401温度adc采集 LL库
时间: 2023-09-03 19:06:53 浏览: 160
以下是基于LL库的STM32F401温度ADC采集代码示例:
```c
#include "stm32f4xx_ll_adc.h"
#include "stm32f4xx_ll_bus.h"
#include "stm32f4xx_ll_rcc.h"
int main(void)
{
// Enable ADC clock
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOA);
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_ADC1);
// Configure ADC pins
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = LL_GPIO_PIN_0;
GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
// Configure ADC
LL_ADC_InitTypeDef ADC_InitStruct = {0};
LL_ADC_StructInit(&ADC_InitStruct);
ADC_InitStruct.Clock = LL_ADC_CLOCK_SYNC_PCLK_DIV2;
ADC_InitStruct.Resolution = LL_ADC_RESOLUTION_12B;
LL_ADC_Init(ADC1, &ADC_InitStruct);
// Calibration
if(LL_ADC_IsEnabled(ADC1))
{
LL_ADC_StartCalibration(ADC1, LL_ADC_SINGLE_ENDED);
while(LL_ADC_IsCalibrationOnGoing(ADC1)) {}
}
// Enable ADC
LL_ADC_Enable(ADC1);
// Start conversion
LL_ADC_REG_StartConversion(ADC1);
// Wait for conversion to complete
while(!LL_ADC_IsActiveFlag_EOC(ADC1)) {}
// Read temperature
uint16_t adc_value = LL_ADC_REG_ReadConversionData12(ADC1);
float temperature = (adc_value - 500.0f) / 10.0f;
// Disable ADC
LL_ADC_Disable(ADC1);
while (1)
{
// Do something
}
}
```
以上代码将ADC1通道0配置为温度传感器输入,并使用LL库进行ADC初始化和采集。ADC采集完成后,将ADC转换值转换为温度值,并在主循环中执行其他操作。
阅读全文