stm32f0xx.h文件内容

本文详细解析了STM32F0xx HAL库的结构与使用方法,包括器件定义、HAL驱动使用、设备版本号、头文件包含、标志类枚举类型及寄存器操作宏等关键内容。

这个文件的内容

第一部分定义使用的器件

第二部分定义USE_HAL_DRIVER,这个是在编译器中定义的

第三部分定义CMSIS Device version number

第四部分根据使用的器件包含相应器件的头文件

第五部分定义了一些标志类枚举类型

第六部分定义了一些寄存器操作的宏

第七部分包含stm32f0xx_hal.h头文件

 

#include "current_sensor.h" #include "stm32f0xx.h" #include "stm32f0xx_adc.h" #include "stm32f0xx_gpio.h" #include "systick.h" #include "usart.h" #include <math.h> uint16_t adc_samples[SAMPLE_COUNT]; void CurrentSensor_Init(void) { // 1. 使能时钟 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); // 采样引脚GPIO时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); // ADC1时钟 // 2. 配置采样引脚(如PA1,模拟输入) GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStruct); // 3. 初始化ADC(严格匹配你的ADC_InitTypeDef结构体) ADC_InitTypeDef ADC_InitStruct; ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b; // 12位分辨率 ADC_InitStruct.ADC_ContinuousConvMode = DISABLE; // 单次转换模式 ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; // 禁用外部触发 ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_TRGO; // 无效值(因禁用外部触发) ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right; // 数据右对齐 ADC_InitStruct.ADC_ScanDirection = 0; // 单通道用默认向上扫描 ADC_Init(ADC1, &ADC_InitStruct); // 4. 配置采样通道(如通道1,PA1)和采样时间 ADC_ChannelConfig(ADC1, ADC_Channel_1, ADC_SampleTime_239_5Cycles); // 足够长的采样时间 // 5. 使能ADC ADC_Cmd(ADC1, ENABLE); // 6. 执行ADC校准(F0必须做,否则精度差) ADC_GetCalibrationFactor(ADC1); } static void ADC_CollectSamples(void) { // 1. 清除转换完成标志(避免残留标志影响判断) ADC_ClearFlag(ADC1, ADC_FLAG_EOC); // 2. 启动ADC软件转换(用库函数替代寄存器操作) ADC_StartOfConversion(ADC1); // 等效于操作CR2的SWSTART位,无寄存器访问错误 uint32_t adc_timeout = 0xFFFF; // 超时计数 for (uint16_t i = 0; i < SAMPLE_COUNT; i++) { // 3. 等待转换完成(EOC标志置位) while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET) { if (--adc_timeout == 0) { printf("[错误] ADC采样超时!\r\n"); return; // 超时退出,避免死循环 } } // 4. 读取采样值 adc_samples[i] = ADC_GetConversionValue(ADC1); // 5. 清除标志,准备下一次转换 ADC_ClearFlag(ADC1, ADC_FLAG_EOC); adc_timeout = 0xFFFF; // 重置超时计数 } // 6. 停止转换(单次模式下可不操作,若需显式停止用库函数) ADC_StopOfConversion(ADC1); // 可选,确保转换停止 } float CurrentSensor_ReadRMS(float *rms_voltage) { ADC_CollectSamples(); uint64_t sum_squares = 0; float voltage_mv; float current_amp; for (uint16_t i = 0; i < SAMPLE_COUNT; i++){ voltage_mv = (float)adc_samples[i] * REFERENCE_VOLTAGE / ADC_RESOLUTION; voltage_mv -= V_OFFSET; sum_squares += (uint64_t)(voltage_mv * voltage_mv); } *rms_voltage = sqrt((float)sum_squares / SAMPLE_COUNT); current_amp = (*rms_voltage / 1000.0f / SHUNT_RESISTANCE / AMP_GAIN) * CT_RATIO; return current_amp; } void CurrentSensor_PeriodicCheck(void) { static uint32_t last_check_time = 0; if (g_tick_ms - last_check_time >= 1000) { last_check_time = g_tick_ms; float rms_voltage; float current_amp = CurrentSensor_ReadRMS(&rms_voltage); printf("[电流传感器]电流:%.3f A, 电压:%.3f V\r\n", current_amp, rms_voltage / 1000.0f); uint8_t current_level = GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_15); GPIO_WriteBit(GPIOA, GPIO_Pin_15, (BitAction)(1 - current_level)); } } 有很多报错信息Rebuild started: Project: template *** Using Compiler 'V5.06 update 7 (build 960)', folder: 'D:\keil MDK\Keil5\ARM\ARMCC\Bin' Rebuild target 'Target 1' assembling startup_stm32f030.s... compiling stm32f0xx_adc.c... ..\Libraries\CMSIS\stm32f0xx.h(111): error: #35: #error directive: "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" #error "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" ..\Libraries\Src\stm32f0xx_adc.c: 0 warnings, 1 error compiling usart.c... ..\Libraries\CMSIS\stm32f0xx.h(111): error: #35: #error directive: "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" #error "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" ..\User\usart.c: 0 warnings, 1 error compiling stm32f0xx_crs.c... ..\Libraries\CMSIS\stm32f0xx.h(111): error: #35: #error directive: "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" #error "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" ..\Libraries\Src\stm32f0xx_crs.c: 0 warnings, 1 error compiling gpio_input.c... ..\Libraries\CMSIS\stm32f0xx.h(111): error: #35: #error directive: "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" #error "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" ..\User\gpio_input.c: 0 warnings, 1 error compiling main.c... ..\Libraries\CMSIS\stm32f0xx.h(111): error: #35: #error directive: "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" #error "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" ..\User\main.c: 0 warnings, 1 error compiling stm32f0xx_dbgmcu.c... ..\Libraries\CMSIS\stm32f0xx.h(111): error: #35: #error directive: "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" #error "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" ..\Libraries\Src\stm32f0xx_dbgmcu.c: 0 warnings, 1 error compiling stm32f0xx_flash.c... ..\Libraries\CMSIS\stm32f0xx.h(111): error: #35: #error directive: "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" #error "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" ..\Libraries\Src\stm32f0xx_flash.c: 0 warnings, 1 error compiling systick.c... ..\Libraries\CMSIS\stm32f0xx.h(111): error: #35: #error directive: "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" #error "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" ..\User\systick.c: 0 warnings, 1 error compiling stm32f0xx_can.c... ..\Libraries\CMSIS\stm32f0xx.h(111): error: #35: #error directive: "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" #error "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" ..\Libraries\Src\stm32f0xx_can.c: 0 warnings, 1 error compiling stm32f0xx_comp.c... ..\Libraries\CMSIS\stm32f0xx.h(111): error: #35: #error directive: "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" #error "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" ..\Libraries\Src\stm32f0xx_comp.c: 0 warnings, 1 error compiling system_clock.c... ..\Libraries\CMSIS\stm32f0xx.h(111): error: #35: #error directive: "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" #error "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" ..\User\system_clock.c: 0 warnings, 1 error compiling stm32f0xx_cec.c... ..\Libraries\CMSIS\stm32f0xx.h(111): error: #35: #error directive: "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" #error "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" ..\Libraries\Src\stm32f0xx_cec.c: 0 warnings, 1 error compiling stm32f0xx_dac.c... ..\Libraries\CMSIS\stm32f0xx.h(111): error: #35: #error directive: "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" #error "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" ..\Libraries\Src\stm32f0xx_dac.c: 0 warnings, 1 error compiling stm32f0xx_dma.c... ..\Libraries\CMSIS\stm32f0xx.h(111): error: #35: #error directive: "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" #error "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" ..\Libraries\Src\stm32f0xx_dma.c: 0 warnings, 1 error compiling stm32f0xx_exti.c... ..\Libraries\CMSIS\stm32f0xx.h(111): error: #35: #error directive: "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" #error "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" ..\Libraries\Src\stm32f0xx_exti.c: 0 warnings, 1 error compiling stm32f0xx_iwdg.c... ..\Libraries\CMSIS\stm32f0xx.h(111): error: #35: #error directive: "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" #error "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" ..\Libraries\Src\stm32f0xx_iwdg.c: 0 warnings, 1 error compiling stm32f0xx_gpio.c... ..\Libraries\CMSIS\stm32f0xx.h(111): error: #35: #error directive: "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" #error "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" ..\Libraries\Src\stm32f0xx_gpio.c: 0 warnings, 1 error compiling system_stm32f0xx.c... ..\Libraries\CMSIS\stm32f0xx.h(111): error: #35: #error directive: "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" #error "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" ..\Libraries\CMSIS\system_stm32f0xx.c: 0 warnings, 1 error compiling stm32f0xx_crc.c... ..\Libraries\CMSIS\stm32f0xx.h(111): error: #35: #error directive: "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" #error "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" ..\Libraries\Src\stm32f0xx_crc.c: 0 warnings, 1 error compiling current_sensor.c... ..\Libraries\CMSIS\stm32f0xx.h(111): error: #35: #error directive: "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" #error "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" ..\User\current_sensor.c: 0 warnings, 1 error compiling stm32f0xx_i2c.c... ..\Libraries\CMSIS\stm32f0xx.h(111): error: #35: #error directive: "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" #error "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" ..\Libraries\Src\stm32f0xx_i2c.c: 0 warnings, 1 error compiling stm32f0xx_misc.c... ..\Libraries\CMSIS\stm32f0xx.h(111): error: #35: #error directive: "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" #error "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" ..\Libraries\Src\stm32f0xx_misc.c: 0 warnings, 1 error compiling stm32f0xx_pwr.c... ..\Libraries\CMSIS\stm32f0xx.h(111): error: #35: #error directive: "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" #error "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" ..\Libraries\Src\stm32f0xx_pwr.c: 0 warnings, 1 error compiling stm32f0xx_rcc.c... ..\Libraries\CMSIS\stm32f0xx.h(111): error: #35: #error directive: "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" #error "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" ..\Libraries\Src\stm32f0xx_rcc.c: 0 warnings, 1 error compiling stm32f0xx_rtc.c... ..\Libraries\CMSIS\stm32f0xx.h(111): error: #35: #error directive: "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" #error "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" ..\Libraries\Src\stm32f0xx_rtc.c: 0 warnings, 1 error compiling stm32f0xx_spi.c... ..\Libraries\CMSIS\stm32f0xx.h(111): error: #35: #error directive: "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" #error "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" ..\Libraries\Src\stm32f0xx_spi.c: 0 warnings, 1 error compiling stm32f0xx_usart.c... ..\Libraries\CMSIS\stm32f0xx.h(111): error: #35: #error directive: "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" #error "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" ..\Libraries\Src\stm32f0xx_usart.c: 0 warnings, 1 error compiling stm32f0xx_syscfg.c... ..\Libraries\CMSIS\stm32f0xx.h(111): error: #35: #error directive: "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" #error "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" ..\Libraries\Src\stm32f0xx_syscfg.c: 0 warnings, 1 error compiling stm32f0xx_tim.c... ..\Libraries\CMSIS\stm32f0xx.h(111): error: #35: #error directive: "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" #error "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" ..\Libraries\Src\stm32f0xx_tim.c: 0 warnings, 1 error compiling stm32f0xx_wwdg.c... ..\Libraries\CMSIS\stm32f0xx.h(111): error: #35: #error directive: "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" #error "Please select first the target STM32F0xx device used in your application (in stm32f0xx.h file)" ..\Libraries\Src\stm32f0xx_wwdg.c: 0 warnings, 1 error ".\Objects\template.axf" - 30 Error(s), 0 Warning(s). Target not created. Build Time Elapsed: 00:00:01 这是基于stm32f030k6t6的开发板开发的互感器模块每秒检测电流电压数据并打印到串口的代码current_sensor.c文件
最新发布
11-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值