STM32-ADC笔记

// stm32L0XX_hal_adc.
// 此文件提供固件函数,用于管理模数转换器ADC的以下功能:
// 外设功能:
// 外设控制
// 外设状态
// 在stm32l0xx_hal_adc_ex.c文件中也提供了其他扩展功能

// 本软件是根据本软件组织根目录的LICENSE文件中的条款获得的许可的,如果此软件未附带许可证文件,则按原样提供。

// ADC peripheral features
// ADC外围函数 特点;特征;特色

// 12bit、10bit、8bit、16bit configurable resolution.
// 12位、10位、8位、16位可配置分辨率
// configurable:可配置的;结构的
// resolution:决议;分辨率;解决;消除;鉴定;决心

// Interrupt generation at the end of regular conversion and in case of analog watchdog or
// overrun events.
// 在常规转换结束时以及在模拟看门狗或溢出事件的情况下产生中断。

// 扩展知识:ADC的模拟看门狗用于检查电压是否越界,分上下两个边界,可以在ADC的寄存器中设置。
// 当模拟看门狗检测到电压高于上限或低于下限是就会产生看门狗中断。
// 看门狗比较是在数据对齐之前完成的,先进行看门狗比较,再将数据放入ADC_DR数据寄存器中。

// Single and continuous conversion modes.
// 单次和连续转换模式。

// Scan mode for conversion of several channels sequentially.
// 扫描模式,用于按顺序转换多个通道。

// Data alignment with in-built data coherensy.
// 数据与内置数据一致性。

// Programmable sampling time (common for all channels).
// 可编程采样时间(所有通道通用)。

// External trigger (timer or EXTI) with configurable polarity.
// 具有可配置极性的外部触发器(定时器或EXTI)。

// DMA request generation for transfer of conversions data of regular group.
// 用于传输常规组的转换数据的DMA请求生成。

// ADC calibration.
// ADC校准

// ADC conversion of regular group.
// 常规组的ADC转换
// regular:定时的;定期的;经常的;常规的;普通的

// ADC supply requirements:1.62V to  3.6V.
// ADC电源要求:1.62V 至 3.6V
// requirements:必要条件;必备条件;所需的东西

// ADC input range:from Vref-(connected to Vssa) to Vref+(connected to Vdda or to an external
// voltage reference).
// ADC输入范围:从Vref-到Vref+(连接到Vdda或外部参考电压)

// How to use this driver
// 如何使用此驱动程序

// Configuration of top level parameters related to ADC 
// 与ADC相关的顶级参数配置

// Enable the ADC interface
// 启用ADC接口

// As prerequisite ,ADC clock must be configured at RCC top level.
// 作为必要条件,ADC时钟必须配置再RCC顶层。

// Caution:On STM32L0,ADC clock frequency max is 16MHz (refer to device datasheet).
// Therefore,ADC clock prescaler must be configured in function of ADC clock source
// frequency to remain below this maximum frequency.
// 注意:在STM32L0上,ADC时钟频率最大为16MHz(请参阅设备数据表)。
// 因此,必须根据ADC时钟源的功能配置ADC时钟预分频器频率保持在该最大频率下。

// Two clock setting are mandatory:
// 两种时钟设置是强制性的:
// mandatory:强制性的;法定的;义务的;

// ADC clock (conversions clock).
// ADC时钟(转换时钟)

// Two possible clock sources:synchronous clock derived from APB clock or asynchronous clock
// derived from ADC dedicated HSI RC oscillator 16MHz.
// 两种可能的时钟源j:源自APB时钟的同步时钟或异步时钟源自ADC专用HSI RC 16MHz振荡器。

// If asynchronous clock is selected ,parameter "HSIState" must be set either:
// to "...HSIState = RCC_HSI_ON" to maintain the HSI16 oscillator always enabled:can be used to
// supply the main system clock.
// 如果选择异步时钟,参数“HSIState”必须设置为至“。。。HSIStata=RCC_HSI_ON”,以保持HSI16振荡器始终启用:
// 可用于提供主系统时钟。

Example:
Into HAL_ADC_MspInit() (recommended code location) or with other device clock parameters configuration:
实例:
进入HAL_ADC_MspInit()(推荐的代码位置)或其他设备时钟参数配置:

__HAL_RCC_ADC1_CLK_ENABLE(); (mandatory强制性的)

HSI enable  (optional:if asynchronous clock selected 可选:如果选择异步时钟)

RCC_OscInitTypeDef RCC_OscInitStructure;
RCC_OscInitStructure.OscillatorType=RCC_OSCILLATORTYPE_HIS;
RCC_OscInitStructure.HSI16CalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStructure.HSIState = RCC_HSI_ON;
Rcc_OscInitStrucrute.PLL...(optional if used for system clock).
HAL_RCC_OscConfig(&RCC_OscInitStructure);

// ADC clock source and clock prescaler are configured at ADC level with parameter "ClockPrescaler" using
// function HAL_ADC_Init().
// ADC时钟源和时钟预分频器在ADC级别配置,参数为ClockPreacaler,使用函数HAL_ADC_Init()。

// ADC pins configuration
// ADC引脚配置

// Enable the clock for the ADC GPIOs using macro __HAL_RCC_GPIOx_CLK_ENABLE().
// 使用宏__HAL_RCC_GPIOx_CLK_ENABLE()为ADC GPIO启用时钟

// Configure these ADC pins in analog mode using function HAL_GPIO_Init().
// 使用函数 HAL_GPIO_Init()在模式模式下配置这些ADC引脚。

// Optionally,in case of usage of ADC with interruptions:
// 可选的,在ADC使用中断的情况下:

// Configure the NVIC for ADC using function HAL_NVIC_EnableIRQ(ADCx_IRQn)
// 使用函数HAL_NVIC_EnableIRQ(ADCx_IRQn)为AQDC配置NVIC

// Insert the ADC interruption handler function HAL_ADC_IRQHandler() into the function of 
//corresponding ADC interruption vector ADCx_IRQHandler().
// 将ADC中断处理程序函数HAL_ADC_IRQHandler()插入到相应ADC中断向量ADCx_IRQHandler()的函数中。

// Optionally,in case of usage of DMA;
// 可选的,在使用DMA的情况下;

// Configure the DMA(DMA channle ,mode normal or circular ,...) using function HAL_DMA_Init().
// 使用函数HAL_DMA_Init()配置DMA(DMA通道、正常模式还是循环模式,。。。)

// Configure the NVIC for DMA using function HAL_NVIC_EnableIRQ(DMAx_Channlex_IRQn).
// 使用函数HAL_NVIC_EnableIRQ(DMAx_Channlex_IRQn)为DMA配置NVIC。

// Insert the ADC interruption handler function HAL_ADC_IRQHandler() into the function of corresponding
// DMA interruption vector DMAx_Channelx_IRQHandler().
// 将ADC中断处理程序函数HAL_ADC_IRQHandler()插入相应的DMA中断向量DMAx_Channelx_IRQHandler()。


// Configuration of ADC ,group regular,channels parameters 
// ADC、常规组、通道参数的配置

// Configure the ADC parameter(resolution,data alignment,...) and regular group parameters(conversion trigger,
// sequencer,...) using function HAL_ADC_Init().
// 使用函数HAL_ADC_Init()配置ADC参数(分辨率、数据对齐)和常规组参数(转换触发器、定时器。。。)。

// Configure the channels for regular group parameters(channel number,channel rank into sequencer,...into 
// regular group) using function HAL_ADC_ConfigChannle().
// 使用函数HAL_ADC_ConfigChannle()为常规组参数配置通道(通道编号、进入序列器的通道等级。。。进入常规组)。

// Optionally,configure the analog watchdog parameters(channels monitored,thresholds,...) using function 
// HAL_ADC_AnalogWDGConfig().
// 可选的,使用HAL_ADC_AnalogWDGConfig()函数配置模拟看门狗参数(监控的通道、阈值。。。)

// When device is in mode low-power (low-power run ,low-power sleep or stop mode),function 
// "HAL_ADCEx_EnableVREFINT()" must be called before function HAL_ADC_Init().
// 当设置处于低功耗模式(低功率运行、低功耗睡眠或停止模式)时,HAL_ADCEx_EnableVREFINT() 必须在
// HAL_ADC_Init()函数之前调用。


// In case of internal temperature sensor to be measured:
// function "HAL_ADCEx_EnableVREFINITempSensor()" must be called similarilly.
// 如果要测量内部温度传感器;
// HAL_ADCEx_EnableVREFINITempSensor()函数必须以类似的方式调用。

// Execution of ADC conversions
// ADC转换的执行
// execution:执行;实施;处决

// Optionally, perform an automatic ADC calibration to improve the conversion accuracy using function 
// HAL_ADCEx_Calibration_Start().
// 可选择的,使用HAL_ADCEx_Calibration_Start()函数执行自动ADC校准以提高转换精度。

// ADC driver can be used among three modes:polling,interruption,transfer by DMA.
// ADC驱动程序可用三种模式:轮询、中断和DMA传输。
// among:在。。。中;在 其中 ;在。。。之间

// ADC conversion by polling:
// 通过轮询进行ADC转换:

// Activate the ADC peripheral and start conversions using function HAL_ADC_Start().
// 激活ADC外围设备并使用函数HAL_ADC_START()启动转换。

// Wait for ADC conversion completion using funciotn HAL_ADC_PollForConversion().
// 使用 HAL_ADC_PollForConversion() 函数来等待ADC转换完成。

// Retrieve conversion results using function HAL_ADC_GetValue().
// 使用函数HAL_ADC_GetValue()检索转换结果。
// retrieve:取回;收回;检索操作;重新得到

// Stop conversion and disable the ADC peripheral using function HAL_ADC_Stop().
// 使用HAL_ADC_Stop()函数停止转换并禁用ADC外围设备。

// ADC conversion by interruption:
// ADC中断转换:

// Activate the ADC peripheral and start conversions using function HAL_ADC_Start_IT().
// 使用 HAL_ADC_Start_IT() 函数激活ADC外围设备开始转换。

// Wait for ADC conversion completion by call of function HAL_ADC_ConvCpltCallback()(
// this function must be implemented in user program).
// 通过调用 HAL_ADC_ConvCpltCallback() 函数来等待ADC转换完成(该函数必须在用户程序中实现)。

// Retrieve conversion results using function HAL_ADC_GetValue().
// 使用函数 HAL_ADC_GetValue() 检索转换结果。

// Stop conversion and disable the ADC peripheral using function HAL_ADC_Stop_IT().
// 使用函数 HAL_ADC_Stop_IT() 停止转换并禁用ADC外围设备。

// ADC conversion with transfer by DMA:
// 通过DMA传输的ADC转换:

// Activate the ADC peripheral and start conversions using function HAL_ADC_Start_DMA().
// 使用函数 HAL_ADC_Start_DMA() 激活ADC外设开始转换

// Wati for ADC conversion completion by call of function HAL_ADC_ConvCpltCallback() or 
// HAL_ADC_ConvHalfCpltCallbcak().(these functions must be implemented in user program).
// 通过调用函数 HAL_ADC_ConvCpltCallback() 和 HAL_ADC_ConvHalfCpltCallbcak() 来等待ADC转换
// (这些功能必须在用户代码中实现)。

// Conversion results are automatically transferred by DMA into destination veriable address.
// 转换结果由DMA自动传输到目的地 可验证地址。

// Stop conversion and disable the ADC peripheral using function HAL_ADC_Stop_DMA().
// 停止转换并使用函数 HAL_ADC_Stop_DMA() 禁用ADC外设。

// Callback functions must be implemented in user program:
// 回调函数必须在用户程序中实现:
 
// HAL_ADC_ErrorCallback()
// HAL_ADC_LevelOutOfWindowCallback() (callback of analog watchdog 模拟看门狗的回调)
// HAL_ADC_ConvCpltCallback()
// HAL_ADC_ConvHalfCpltCallback()

// Deinitialization of ADC
// ADC的去初始化

// Disable the ADC interface
// 禁用ADC接口

// ADC clock can be hard reset and disabled at RCC top level.
// ADC时钟可以在RCC顶级进行硬复位和禁用。

// Hard reset of ADC peripherals using macro __ADCx_FORCE_RESET(),__ADCx_RELEASE_RESET().
// 使用宏 __ADCx_FORCE_RESET(),__ADCx_RELEASE_RESET() 对ADC外围设备进行硬复位。

// ADC clokc disable using the equivalent macro/functions as configuration step.
// 使用等效宏/函数作为配置步骤禁用ADC时钟。

// Example:

// Into HAL_ADC_MspDeinit() (recommended code location) or with other device clock parameters configuration:
// 进入 HAL_ADC_MspDeinit() (推荐代码位置)或其他设备时钟参数配置:

// RCC_OscInitStructure.OscillatorType = RCC_OSCILLATORTYPE_HSI;
// RCC_OscInitStructure.HSIState = RCC_HSI_OFF;(if not used system clock)
// HAL_RCC_OscConfig(&RCC_OscInitStructure);

// ADC pins configuration
// ADC引脚配置

// Disable the clock for the ADC GPIOs using macro __HAL_RCC_GPIOx_CLK_DISABLE()
// 使用宏  __HAL_RCC_GPIOx_CLK_DISABLE() 禁用ADC GPIO的时钟

// Optionally,in case of usage of ADC with interruptions:
// 可选的,在ADC使用中断的情况下:

// Disable the NVIC for ADC using function HAL_NVIC_EnableIRQ(ADCx_IRQn)
// 使用函数  HAL_NVIC_EnableIRQ(ADCx_IRQn) 禁用ADC的NVIC

// Optionally,in case of usage of DMA:
// 可选的,使用DMA的情况下:

// Deinitialize the DMA using  function HAL_DMA_Init().
// 使用函数 HAL_DMA_Init() 取消DMA的初始化

// Disable the NVIC for DMA using function HAL_NVIC_EnableIRQ(DMAx_Channelx_IRQn)
// 使用函数 HAL_NVIC_EnableIRQ(DMAx_Channelx_IRQn) 禁用DMA的NVIC

// Callback registration
// 回调注册

// The compilation flag USE_HAL_ADC_REGISTER_CALLBACKS,when set to 1,allows the user to configure dynamically
// the driver callbacks.Use Functions HAL_ADC_RegisterCallback() to register an interrupt callback.
// 当编译标志 USE_HAL_ADC_REGISTER_CALLBACKS 设置为1时,允许用户动态配置回调,使用函数 HAL_ADC_RegisterCallback()
// 注册中断回调。


// Function HAL_ADC_RegisterCallback() allows to register following callbacks:
// 函数  HAL_ADC_RegisterCallback() 允许注册以下回调:

// ConvCpltCallback:ADC conversion complete callback
// ADC转换完成回调

// ConvHalfCpltCallback:ADC conversion DMA half-transfer callback
// ADC转换DMA半传输回调

// LevelOutOfWindowCallback:ADC analog watchdog 1 callback
// ADC模拟看门狗1回调

// ErrorCallback:ADC error callback
// ADC错误回调

// MspInitCallback:ADC Msp Init callback
// ADC Msp Init回调

// MspDeInitCallback:ADC Msp DeInit callback
// ADC Msp DeInit回调

// This function takes as paramteters the HAL peripheral handle,the Callback ID and a pointer to the user
// callback function.
// 此函数将HAL外围句柄、回调ID和指向用户的指针作为参数回调函数

// Use function HAL_ADC_UnRegisterCallbcak to reset a callback to the default weak function.
// 使用函数 HAL_ADC_UnRegisterCallbcak 将回调重置为默认的弱函数。

// HAL_ADC_UnRegisterCallback takes as paramters the HAL peripheral handle, and the Callback ID.
// HAL_ADC_UnRegisterCallback 将HAL外围句柄和Callback ID作为参数。

// This function allows to reset following callbacks:
// 此功能允许重置以下回调:

// ConvCpltCallback:ADC conversion complete callback
// ADC转换完成回调

// ConvHalfCpltCallback:ADC conversion DMA half-transfer callback
// ADC转换DMA半传输回调

// LevelOutOfWindowCallback:ADC analog watchdog 1 callback
// ADC模拟看门狗1回调

// ErrorCallback:ADC error callback
// ADC错误回调

// MspInitCallback:ADC Msp Init callback
// ADC Msp Init回调

// MspDeInitCallback:ADC Msp DeInit callback
// ADC Msp DeInit回调

// By default,after the HAL_ADC_Init() and when the state is HAL_ADC_STATE_RESET all callbacks are set to the
// corresponding weak functions:
// 在默认情况下,在 HAL_ADC_Init()之后,当状态为 HAL_ADC_STATE_RESET 时,所有回调都设置为相应的弱函数。

// examples HAL_ADC_ConvCpltCallback(),HAL_ADC_ErrowCallback().

// Exception done for MspInit and MspDeInit functions that are reset to the legacy weak functions in the 
// HAL_ADC_Init() / HAL_ADC_DeInit() only when these callbacks are null(not registered beforehand).
// Exception:例外;规则的例外;一般情况以外的人或事物;例外的事物;
// legacy:遗留;一颤;后遗症;遗赠财务;已停产的(计算机)
// not registered beforehand:未事先登记的

// Callbacks can be registered/unregistered in HAL_ADC_STATE_REAGY state only.
// 只能在 HAL_ADC_STATE_REAGY状态下注册/取消回调

// Exception done MspInit/MspDeInit functions that can be registered/unregistered in  HAL_ADC_STATE_READY
// or HAL_ADC_STATE_RESET state,thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
// 可以在 HAL_ADC_STATE_READY 中注册/取消注册的 MspInit/MspDeInit 函数出现异常或者  HAL_ADC_STATE_RESET 状态,因此
// 在  MspInit/DeInit 期间可以使用注册的(用户)回调。

// Then,the user first registers the MspInit/MspDeInit user callbacks using HAL_ADC_RegisterCallback() befor
// calling HAL_ADC_DeInit() or HAL_ADC_Init() function.

// 然后,用户首先使用 HAL_ADC_RegisterCallback() 函数,在调用 HAL_ADC_DeInit() or HAL_ADC_Init() 函数。

// When the compilation flag USE_HAL_ADC_REGISTER_CALLBACKS is set to 0 or not defined,the callback registration 
// feature is not available and all callbacks are set to the correspongding weak functions.
// 当编译标志 USE_HAL_ADC_REGISTER_CALLBACKS 设置为0或未定义时,回调注册功能不可用,所有回调都设置为相应的弱函数。

#include "stm32l0xx_hal.h"

// ADC HAL module driver
// ADC HAL模块驱动器

#ifdef HAL_ADC_MODULE_ENABLE

// ADC_Private_Constants ADC Private Constants
// ADC 专用常量

// Delay for ADC stabilization time.
// ADC稳定时间延迟

// Maximum delay is 1us(refer to device datasheet,parameter tSTART).
// 最大延迟为1us(请参阅设备数据表,参数:tSTART)

// Unit:us
// 单位:us

#define ADC_STAB_DELAY_US (1U)

// Delay for temperature sensor stabilization time.
// 温度传感器稳定时间延迟

// Maximum delay is 10us(refer to device datasheet,parameter tSTART)
// 最大延迟为10us

// Unit:us
// 单位:us

#define ADC_TEMPSENDOR_DELAY_US (10U)

// Private macor
// 个人宏定义

// Private variables
// 个人变量

// Private function prototypes
// 个人函数原型

static HAL_StatusTypeDef ADC_Enable(ADC_HandleTypeDef *hadc);
static HAL_StatusTypeDef ADC_Disable(ADC_HandleTypeDef *hadc);
static HAL_StatusTypeDef ADC_ConversionStop(ADC_HandleTypeDef *hadc);
static void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma);
static void ADC_DMAError(DMA_HandletypeDef *hdma);
static void ADC_DelayMicroSecond(uint32_t microSecond);//ADC微秒延时

// Exported functions
// 导出的函数

// ADC_Exported_Functions ADC Exported Funcions
// ADC导出的函数

// ADC_Exported_Functions_Gruop1 Initilaization and de-initialization functions
// ADC_Exported_Functions_Gruop1 初始化和去初始化函数

// ADC Initialization and Configuration functions.
// ADC初始化和配置函数

// Initialization and de-initialization functions
// 初始化和取消初始化函数

// This section provides functions allowing to:
// 本节提供的函数允许:

// Initialize and configure the ADC.
// 初始化和配置ADC

// De-initialize the ADC.
// 取消初始化ADC

Initialize the ADC peripheral and regular group according to parameters 
specified in structure "ADC_InitTypeDef".
根据结构体 ADC_InitTypeDef 中指定的参数初始化ADC外围设备和常规组。

As prerequisite,ADC clock must be configured at RCC top level depending on possible clock
sources:APB clock of HSI clock.
See commented example code below that can be copied and uncommented into HAL_ADC_MspInit().
作为先决条件,ADC时钟必须根据可用的时钟进行时钟配置,要高于RCC,HSI时钟的APB时钟。
请参阅下面注释的示例代码,这些代码可以复制并取消注释到 HAL_ADC_MspInit()中。

Possibility to updata paramters on the fly:
This function initializes the ADC MSP (HAL_ADC_MspInit()) only when coming from ADC state reset.
Following calls to this function can be used to reconfigure some parameters of ADC_InitTypeDef
structure on the fly,without modifying MSP configuration.If ADC MSP has to be modified again,
HAL_ADC_DeInit() must be called before HAL_ADC_Init().
在执行中更新数据参数的可行性:
此函数仅在ADC状态重置时初始化ADC MSP (HAL_ADC_MspInit()) 以下对此函数的调用可用于重新配置
ADC_InitTypeDef的某些结构体中的参数,而无需修改MSP配置。如果必须再次修改ADC MSP,必须在
HAL_ADC_Init() 之前调用 HAL_ADC_DeInit()。

// The setting of these parameters is conditioned to ADC state.
// 这些参数的设置以ADC状态为条件

// For parameters constraints,see comments of structure "ADC_InitTypeDef".
// 有关参数约束,请参考结构体 "ADC_InitTypeDef" 的注释。

// This function configures the ADC within 2 scopes:scope of entire ADC and scope of regular group.
此函数在两个范围内配置ADC:整个ADC的范围和常规组的范围。

// For parameters details,see comments of structure "ADC_InitTypeDef".
有关参数的详细信息,请参阅结构体 "ADC_InitTypeDef" 的注释。

// within:在内部;在里面;在(某段时间)之内;内部
// scopes:范围;机会,能力;仔细看;彻底检查
// entire:整个的;完全的;全部的
// details:详情;资料;全部细节;详细说明;详述
// comments:评论;批评;解释;指责;议论;表达意见

// When device is in mode low-power (low-power run,low-power sleep or stop mode),
// function "HAL_ADCEx_EableVREFINT()" must be called before function HAL_ADC_Init()
// (in case of previous ADC operations:function HAL_ADC_DeInit() must be called first) .
// 当设置处于低功率模式(低功率运行、低功率睡眠或停止模式)时,函数 "HAL_ADCEx_EableVREFINT()"
// 必须在函数 HAL_ADC_Init() 之前调用(如果是以前的ADC操作:必须首先调用函数   HAL_ADC_DeInit())

// In case of internal temperature sensor to be measured:
// 如果要测量内部温度传感器:

// function "HAL_ADCEx_EnableVREFINTTempSensor()" must be called similarilly.
// 函数 "HAL_ADCEx_EnableVREFINTTempSensor()" 必须以类似的方式调用。

HAL_StatusTypeDef HAL_ADC_Init(ADC_HandleTypeDef *hadc)
{
    // 检查ADC句柄
    if(hadc == NULL)
    {
        return HAL_ERROR;
    }

    // 检查参数
    assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
    assert_param(IS_ADC_CLOCKPRESCALER(hadc->Init.ClockPrescaler));
    


}


//20230925 LPTIM Counter Start IT

#include "main.h"
#include "lptim.h"
#include "gpio.h"

void SystemClock_Config(void);

int main(void)
{
    HAL_Init();
    SystemClock_Config();

    //在做低功耗测试时最好在开机前加一些延时,否则很容易出现不能下载程序的情况。
    HAL_Delay(1000);

    MX_GPIO_Init();
    MX_LPTIM1_Init();

    if(HAL_LPTIM_Counter_Start_IT(&HLPTIM1,5-1) != HAL_OK)
    {
        Error_Handler();
    }

    while(1)
    {
        HAL_Delay(500);
    }
}

void HAL_LPTIM_AutoReloadMatchCallback(LPTIM_HandleTypeDef * hlptime)
{
    //波形输出引脚
    HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_6);
}

void SystemClock_Config(void)
{
    RCC_OscInitTypeDef RCC_OscInitStruct = {0};
    RCC_OscInitTypeDef RCC_ClkInitStruct = {0};
    RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

    // 配置主内部调节器输出电压
    __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

    // 根据RCC_OscInitTypeDef结构中的指定的参数初始化RCC振荡器
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
    RCC_OscInitStruct.HSIState = RCC_HSI_ON;
    RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
    RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
    RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_3;
    RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV2;
    
    if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
    {
        Error_Handler();
    }

    // 初始化CPU,AHB和APB总线时钟
    RCC_ClkInitStruct.ClokcType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK|
                                    RCC_CLKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

    if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct,FLASH_LATENCY_1) != HAL_OK)
    {
        Error_Handler();
    }

    PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_LPTIM1;
    PeriphClkInit.LptimClockSelection = RCC_LPTIM1CLKSOURCE_PCLK;

    if(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
    {
        Error_handler();
    }  
}

// 发生错误时执行此函数
void Error_Handler(void)
{
    __disable_irq();
    while(1)
    {
        
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值