5.19 LPTIM 使用说明
本章说明 N32H49x 标准外设库中 LPTIM 驱动的文件组成与 46 个公开函数,以及 5 个示例。LPTIM 是低功耗定时器,支持内部或外部时钟、连续或单次计数、PWM 与单次输出、外部触发、超时、脉冲计数与正交编码器模式,可在 STOP 等低功耗模式下继续计数并唤醒 MCU;时钟源选择、触发信号与配置时序请配合用户手册的“低功耗定时器(LPTIM)”章节阅读。
5.19.1 相关文件
| 相对路径 | 作用 |
|---|---|
firmware/n32h49x_std_periph_driver/inc/n32h49x_lptim.h |
定义工作模式、时钟源、触发源、波形与滤波等参数枚举,声明 46 个公开函数 |
firmware/n32h49x_std_periph_driver/src/n32h49x_lptim.c |
实现上述 46 个函数:初始化与使能、计数与比较、输出与波形、超时与触发、时钟与滤波、编码器模式、中断与输入 |
projects/n32h49x_EVAL/examples/LPTIM/ |
LPTIM 示例根目录,含 LPTIM_ENC、LPTIM_NENC、LPTIM_PulseCounter、LPTIM_PWM、LPTIM_WakeUp 5 个示例 |
projects/n32h49x_EVAL/examples/LPTIM/LPTIM_ENC/ |
该示例的工程目录组成:MDK-ARM/、EWARM/、inc/、src/、readme.txt |
projects/n32h49x_EVAL/bsp/ |
该示例引用的公共板级文件:串口日志与毫秒延时函数 |
5.19.2 API 参考
以下 46 个公开函数按 n32h49x_lptim.h 的声明顺序分为 7 组;每个条目给出功能摘要与 n32h49x_lptim.c 中该函数正上方的注释原文。
初始化与使能
LPTIM_DeInit、LPTIM_StructInit、LPTIM_Init、LPTIM_Cmd、LPTIM_IsEnabled
计数、比较与更新
LPTIM_StartCounter、LPTIM_SetUpdateMode、LPTIM_GetUpdateMode、LPTIM_SetAutoReloadValue、LPTIM_GetAutoReloadValue、LPTIM_SetCompareValue、LPTIM_GetCompareValue、LPTIM_GetCounterValue、LPTIM_SetCounterMode、LPTIM_GetCounterMode
输出、波形与预分频
LPTIM_ConfigOutput、LPTIM_SetWaveform、LPTIM_GetWaveform、LPTIM_SetPolarity、LPTIM_GetPolarity、LPTIM_SetPrescaler、LPTIM_GetPrescaler
超时与触发
LPTIM_TimeoutCmd、LPTIM_IsEnabledTimeout、LPTIM_SoftwareTrigger、LPTIM_ConfigTrigger、LPTIM_GetTriggerSource、LPTIM_GetTriggerFilter、LPTIM_GetTriggerPolarity
时钟
LPTIM_SetClockSource、LPTIM_GetClockSource、LPTIM_ConfigClock、LPTIM_GetClockPolarity、LPTIM_GetClockFilter
编码器模式
LPTIM_SetEncoderModeClockPolarity、LPTIM_GetEncoderModeClockPolarity、LPTIM_EncoderModeCmd、LPTIM_NoEncoderModeCmd、LPTIM_IsEnabledEncoderMode、LPTIM_IsEnabledNoEncoderMode
中断与输入
LPTIM_ClearIntFlag、LPTIM_IsActiveIntStatus、LPTIM_ConfigInt、LPTIM_IsEnabledInt、LPTIM_ConfigInput1、LPTIM_ConfigInput2
void LPTIM_DeInit(LPTIM_Module* LPTIMx)将 LPTIMx 寄存器复位为复位值。
name LPTIM_DeInit.
fun Set LPTIMx registers to their reset values.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
return none
void LPTIM_StructInit(LPTIM_InitType* LPTIM_InitStruct)把 LPTIM_InitStruct 结构体的各字段填为默认值。
name LPTIM_StructInit.
fun Set each fields of the LPTIM_InitStruct structure to its default value.
param LPTIM_InitStruct:
- Pointer to the LPTIM_InitType structure which will be initialized.
return none
ErrorStatus LPTIM_Init(LPTIM_Module* LPTIMx, LPTIM_InitType* LPTIM_InitStruct)按给定参数配置 LPTIMx。
name LPTIM_Init.
fun Configure the LPTIMx peripheral according to the specified parameters.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
param LPTIM_InitStruct:
- Pointer to a LPTIM_InitType structure.
return SUCCESS or ERROR.
void LPTIM_Cmd(LPTIM_Module* LPTIMx, FunctionalStatus Cmd)使能或关闭指定的 LPTIM。
name LPTIM_Cmd.
fun Enables or disables the specified LPTIM instance.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
param Cmd (The input parameters must be the following values):
- ENABLE
- DISABLE
return none
uint32_t LPTIM_IsEnabled(LPTIM_Module* LPTIMx)返回 LPTIM 是否已使能。
name LPTIM_IsEnabled.
fun Indicates whether the LPTIM instance is enabled.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
return 1 or 0
void LPTIM_StartCounter(LPTIM_Module* LPTIMx, uint32_t OperatingMode)以指定模式启动 LPTIM 计数器。
name LPTIM_StartCounter.
fun Starts the LPTIM counter in the desired mode.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
param OperatingMode (The input parameters must be the following values):
- LPTIM_OPERATING_MODE_CONTINUOUS
- LPTIM_OPERATING_MODE_ONESHOT
return none
note LPTIM instance must be enabled before starting the counter.
void LPTIM_SetUpdateMode(LPTIM_Module* LPTIMx, uint32_t UpdateMode)设置 LPTIM 寄存器更新模式(使能或关闭寄存器预装载)。
name LPTIM_SetUpdateMode.
fun Set the LPTIM registers update mode (enable/disable register preload).
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
param UpdateMode (The input parameters must be the following values):
- LPTIM_UPDATE_MODE_IMMEDIATE
- LPTIM_UPDATE_MODE_ENDOFPERIOD
return none
note This function must be called when the LPTIM instance is disabled.
uint32_t LPTIM_GetUpdateMode(LPTIM_Module* LPTIMx)获取 LPTIM 寄存器更新模式。
name LPTIM_GetUpdateMode.
fun Get the LPTIM registers update mode.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
return LPTIM_UPDATE_MODE_IMMEDIATE or LPTIM_UPDATE_MODE_ENDOFPERIOD
void LPTIM_SetAutoReloadValue(LPTIM_Module* LPTIMx, uint32_t AutoReload)设置自动重装载值。
name LPTIM_SetAutoReloadValue.
fun Set the auto reload value.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
param AutoReload:
- Value between Min_Data=0x00 and Max_Data=0xFFFF
return none
note The LPTIMx_ARR register content must only be modified when the LPTIM
is enabled.
note After a write to the LPTIMx_ARR register a new write operation to the
same register can only be performed when the previous write operation
is completed. Any successive write before the ARRUPD flag be set, will
lead to unpredictable results.
uint32_t LPTIM_GetAutoReloadValue(LPTIM_Module* LPTIMx)获取当前的自动重装载值。
name LPTIM_GetAutoReloadValue.
fun Get actual auto reload value.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
return AutoReload Value between Min_Data=0x00 and Max_Data=0xFFFF
void LPTIM_SetCompareValue(LPTIM_Module* LPTIMx, uint32_t CompareValue)设置比较值。
name LPTIM_SetCompareValue.
fun Set the compare value.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
param CompareValue:
- Value between Min_Data=0x00 and Max_Data=0xFFFF
return none
note After a write to the LPTIMx_CMP register a new write operation to the
same register can only be performed when the previous write operation
is completed. Any successive write before the CMPUPD flag be set, will
lead to unpredictable results.
uint32_t LPTIM_GetCompareValue(LPTIM_Module* LPTIMx)获取当前的比较值。
name LPTIM_GetCompareValue.
fun Get actual compare value.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
return CompareValue Value between Min_Data=0x00 and Max_Data=0xFFFF
uint32_t LPTIM_GetCounterValue(LPTIM_Module* LPTIMx)获取当前计数值。
name LPTIM_GetCounterValue.
fun Get actual counter value.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
return Counter value
note When the LPTIM instance is running with an asynchronous clock, reading
the LPTIMx_CNT register may return unreliable values. So in this case it
is necessary to perform two consecutive read accesses and verify that the
two returned values are identical.
void LPTIM_SetCounterMode(LPTIM_Module* LPTIMx, uint32_t CounterMode)设置计数器模式(选择 LPTIM 计数时钟源)。
name LPTIM_SetCounterMode.
fun Set the counter mode (selection of the LPTIM counter clock source).
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
param CounterMode (The input parameters must be the following values):
- LPTIM_COUNTER_MODE_INTERNAL
- LPTIM_COUNTER_MODE_EXTERNAL
return none
note The counter mode can be set only when the LPTIM instance is disabled.
uint32_t LPTIM_GetCounterMode(LPTIM_Module* LPTIMx)获取计数器模式。
name LPTIM_GetCounterMode.
fun Get the counter mode.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
return LPTIM_COUNTER_MODE_INTERNAL or LPTIM_COUNTER_MODE_EXTERNAL
void LPTIM_ConfigOutput(LPTIM_Module* LPTIMx, uint32_t Waveform, uint32_t Polarity)配置 LPTIM 的输出。
name LPTIM_ConfigOutput.
fun Configure the LPTIM instance output.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
param Waveform (The input parameters must be the following values):
- LPTIM_OUTPUT_WAVEFORM_PWM
- LPTIM_OUTPUT_WAVEFORM_SETONCE
param Polarity (The input parameters must be the following values):
- LPTIM_OUTPUT_POLARITY_REGULAR
- LPTIM_OUTPUT_POLARITY_INVERSE
return none
note This function must be called when the LPTIM instance is disabled.
note Regarding the LPTIM output polarity the change takes effect immediately,
so the output default value will change immediately after the polarity
is re-configured, even before the timer is enabled.
void LPTIM_SetWaveform(LPTIM_Module* LPTIMx, uint32_t Waveform)设置输出波形。
name LPTIM_SetWaveform.
fun Set waveform shape.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
param Waveform (The input parameters must be the following values):
- LPTIM_OUTPUT_WAVEFORM_PWM
- LPTIM_OUTPUT_WAVEFORM_SETONCE
return none
uint32_t LPTIM_GetWaveform(LPTIM_Module* LPTIMx)获取当前的输出波形。
name LPTIM_GetWaveform.
fun Get actual waveform shape.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
return LPTIM_OUTPUT_WAVEFORM_PWM or LPTIM_OUTPUT_WAVEFORM_SETONCE
void LPTIM_SetPolarity(LPTIM_Module* LPTIMx, uint32_t Polarity)设置输出极性。
name LPTIM_SetPolarity.
fun Set output polarity.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
param Polarity (The input parameters must be the following values):
- LPTIM_OUTPUT_POLARITY_REGULAR
- LPTIM_OUTPUT_POLARITY_INVERSE
return none
uint32_t LPTIM_GetPolarity(LPTIM_Module* LPTIMx)获取当前的输出极性。
name LPTIM_GetPolarity.
fun Get actual output polarity.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
return LPTIM_OUTPUT_POLARITY_REGULAR or LPTIM_OUTPUT_POLARITY_INVERSE
void LPTIM_SetPrescaler(LPTIM_Module* LPTIMx, uint32_t Prescaler)设置实际预分频比。
name LPTIM_SetPrescaler.
fun Set actual prescaler division ratio.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
param Prescaler (The input parameters must be the following values):
- LPTIM_PRESCALER_DIV1
- LPTIM_PRESCALER_DIV2
- LPTIM_PRESCALER_DIV4
- LPTIM_PRESCALER_DIV8
- LPTIM_PRESCALER_DIV16
- LPTIM_PRESCALER_DIV32
- LPTIM_PRESCALER_DIV64
- LPTIM_PRESCALER_DIV128
return none
note This function must be called when the LPTIM instance is disabled.
note When the LPTIM is configured to be clocked by an internal clock
source and the LPTIM counter is configured to be updated by active
edges detected on the LPTIM external Input1, the internal clock
provided to the LPTIM must be not be prescaled.
uint32_t LPTIM_GetPrescaler(LPTIM_Module* LPTIMx)获取实际预分频比。
name LPTIM_GetPrescaler.
fun Get actual prescaler division ratio.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
return Returned value can be one of the following values:
- LPTIM_PRESCALER_DIV1
- LPTIM_PRESCALER_DIV2
- LPTIM_PRESCALER_DIV4
- LPTIM_PRESCALER_DIV8
- LPTIM_PRESCALER_DIV16
- LPTIM_PRESCALER_DIV32
- LPTIM_PRESCALER_DIV64
- LPTIM_PRESCALER_DIV128
void LPTIM_TimeoutCmd(LPTIM_Module* LPTIMx, FunctionalStatus Cmd)使能或关闭 LPTIM 超时功能。
name LPTIM_TimeoutCmd.
fun Enables or disables the LPTIM timeout function.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
param Cmd (The input parameters must be the following values):
- ENABLE
- DISABLE
return none
note This function must be called when the LPTIM instance is disabled.
note The first trigger event will start the timer, any successive trigger
event will reset the counter and the timer will restart.
note The timeout value corresponds to the compare value; if no trigger
occurs within the expected time frame, the MCU is waked-up by the
compare match event.
note A trigger event arriving when the timer is already started will be
ignored.
uint32_t LPTIM_IsEnabledTimeout(LPTIM_Module* LPTIMx)返回超时功能是否已使能。
name LPTIM_IsEnabledTimeout.
fun Indicate whether the timeout function is enabled.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
return 1 or 0
void LPTIM_SoftwareTrigger(LPTIM_Module* LPTIMx)产生软件触发。
name LPTIM_SoftwareTrigger.
fun Configure the Software trigger.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
return none
void LPTIM_ConfigTrigger(LPTIM_Module* LPTIMx, uint32_t Source, uint32_t Filter, uint32_t Polarity)配置作为 LPTIM 触发事件的外部触发。
name LPTIM_ConfigTrigger.
fun Configure the external trigger used as a trigger event for the LPTIM.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
param Source (The input parameters must be the following values):
- LPTIM_TRIG_SOURCE_GPIO
- LPTIM_TRIG_SOURCE_RTCALARMA
- LPTIM_TRIG_SOURCE_RTCALARMB
- LPTIM_TRIG_SOURCE_RTCTAMP1
- LPTIM_TRIG_SOURCE_RTCTAMP2
- LPTIM_TRIG_SOURCE_RTCTAMP3
param Filter (The input parameters must be the following values):
- LPTIM_TRIG_FILTER_NONE
- LPTIM_TRIG_FILTER_2
- LPTIM_TRIG_FILTER_4
- LPTIM_TRIG_FILTER_8
param Polarity (The input parameters must be the following values):
- LPTIM_TRIG_POLARITY_RISING
- LPTIM_TRIG_POLARITY_FALLING
- LPTIM_TRIG_POLARITY_RISING_FALLING
return none
note This function must be called when the LPTIM instance is disabled.
note An internal clock source must be present when a digital filter is
required for the trigger.
uint32_t LPTIM_GetTriggerSource(LPTIM_Module* LPTIMx)获取当前的外部触发源。
name LPTIM_GetTriggerSource.
fun Get actual external trigger source.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
return Returned value can be one of the following values:
- LPTIM_TRIG_SOURCE_GPIO
- LPTIM_TRIG_SOURCE_RTCALARMA
- LPTIM_TRIG_SOURCE_RTCALARMB
- LPTIM_TRIG_SOURCE_RTCTAMP1
- LPTIM_TRIG_SOURCE_RTCTAMP2
- LPTIM_TRIG_SOURCE_RTCTAMP3
uint32_t LPTIM_GetTriggerFilter(LPTIM_Module* LPTIMx)获取当前的外部触发滤波。
name LPTIM_GetTriggerFilter.
fun Get actual external trigger filter.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
return Returned value can be one of the following values:
- LPTIM_TRIG_FILTER_NONE
- LPTIM_TRIG_FILTER_2
- LPTIM_TRIG_FILTER_4
- LPTIM_TRIG_FILTER_8
uint32_t LPTIM_GetTriggerPolarity(LPTIM_Module* LPTIMx)获取当前的外部触发极性。
name LPTIM_GetTriggerPolarity.
fun Get actual external trigger polarity.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
return Returned value can be one of the following values:
- LPTIM_TRIG_POLARITY_RISING
- LPTIM_TRIG_POLARITY_FALLING
- LPTIM_TRIG_POLARITY_RISING_FALLING
void LPTIM_SetClockSource(LPTIM_Module* LPTIMx, uint32_t ClockSource)设置 LPTIM 使用的时钟源。
name LPTIM_SetClockSource.
fun Set the source of the clock used by the LPTIM instance.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
param ClockSource (The input parameters must be the following values):
- LPTIM_CLK_SOURCE_INTERNAL
- LPTIM_CLK_SOURCE_EXTERNAL
return none
note This function must be called when the LPTIM instance is disabled.
uint32_t LPTIM_GetClockSource(LPTIM_Module* LPTIMx)获取当前的 LPTIM 时钟源。
name LPTIM_GetClockSource.
fun Get actual LPTIM instance clock source.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
return Returned value can be one of the following values:
- LPTIM_CLK_SOURCE_INTERNAL
- LPTIM_CLK_SOURCE_EXTERNAL
void LPTIM_ConfigClock(LPTIM_Module* LPTIMx, uint32_t ClockFilter, uint32_t ClockPolarity)配置由外部时钟源驱动时计数器使用的有效边沿。
name LPTIM_ConfigClock.
fun Configure the active edge or edges used by the counter when the
LPTIM is clocked by an external clock source.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
param ClockFilter (The input parameters must be the following values):
- LPTIM_CLK_FILTER_NONE
- LPTIM_CLK_FILTER_2
- LPTIM_CLK_FILTER_4
- LPTIM_CLK_FILTER_8
param ClockPolarity (The input parameters must be the following values):
- LPTIM_CLK_POLARITY_RISING
- LPTIM_CLK_POLARITY_FALLING
- LPTIM_CLK_POLARITY_RISING_FALLING
- LPTIM_CLK_POLARITY_RISING_FALLING_NO
return none
note This function must be called when the LPTIM instance is disabled.
note When both external clock signal edges are considered active ones,
the LPTIM must also be clocked by an internal clock source with a
frequency equal to at least four times the external clock frequency.
note An internal clock source must be present when a digital filter is
required for external clock.
uint32_t LPTIM_GetClockPolarity(LPTIM_Module* LPTIMx)获取当前的时钟极性。
name LPTIM_GetClockPolarity.
fun Get actual clock polarity.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
return Returned value can be one of the following values:
- LPTIM_CLK_POLARITY_RISING
- LPTIM_CLK_POLARITY_FALLING
- LPTIM_CLK_POLARITY_RISING_FALLING
- LPTIM_CLK_POLARITY_RISING_FALLING_NO
uint32_t LPTIM_GetClockFilter(LPTIM_Module* LPTIMx)获取当前的时钟数字滤波。
name LPTIM_GetClockFilter.
fun Get actual clock digital filter.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
return Returned value can be one of the following values:
- LPTIM_CLK_FILTER_NONE
- LPTIM_CLK_FILTER_2
- LPTIM_CLK_FILTER_4
- LPTIM_CLK_FILTER_8
void LPTIM_SetEncoderModeClockPolarity(LPTIM_Module* LPTIMx, uint32_t ClockPolarity)配置编码器模式下的时钟极性。
name LPTIM_SetEncoderModeClockPolarity.
fun Configure clock polarity in encoder mode.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
param ClockPolarity (The input parameters must be the following values):
- LPTIM_ENCODER_MODE_RISING
- LPTIM_ENCODER_MODE_FALLING
- LPTIM_ENCODER_MODE_RISING_FALLING
return none
note This function must be called when the LPTIM instance is disabled.
uint32_t LPTIM_GetEncoderModeClockPolarity(LPTIM_Module* LPTIMx)获取编码器模式下的时钟极性。
name LPTIM_GetEncoderModeClockPolarity.
fun Get clock polarity in encoder mode.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
return Returned value can be one of the following values:
- LPTIM_ENCODER_MODE_RISING
- LPTIM_ENCODER_MODE_FALLING
- LPTIM_ENCODER_MODE_RISING_FALLING
void LPTIM_EncoderModeCmd(LPTIM_Module* LPTIMx, FunctionalStatus Cmd)使能或关闭编码器模式。
name LPTIM_EncoderModeCmd.
fun Enables or disables the encoder mode.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
param Cmd (The input parameters must be the following values):
- ENABLE
- DISABLE
return none
note This function must be called when the LPTIM instance is disabled.
note In this mode the LPTIM instance must be clocked by an internal clock
source. Also, the prescaler division ratio must be equal to 1.
note LPTIM instance must be configured in continuous mode prior enabling
the encoder mode.
void LPTIM_NoEncoderModeCmd(LPTIM_Module* LPTIMx, FunctionalStatus Cmd)使能或关闭非编码器模式。
name LPTIM_NoEncoderModeCmd.
fun Enables or disables the Non-encoder mode.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
param Cmd (The input parameters must be the following values):
- ENABLE
- DISABLE
return none
note This function must be called when the LPTIM instance is disabled.
note In this mode the LPTIM instance must be clocked by an internal clock
source. Also, the prescaler division ratio must be equal to 1.
note LPTIM instance must be configured in continuous mode prior enabling
the encoder mode.
uint32_t LPTIM_IsEnabledEncoderMode(LPTIM_Module* LPTIMx)返回是否工作在编码器模式。
name LPTIM_IsEnabledEncoderMode.
fun Indicates whether the LPTIM operates in encoder mode.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
return 1 or 0
uint32_t LPTIM_IsEnabledNoEncoderMode(LPTIM_Module* LPTIMx)返回是否工作在非编码器模式。
name LPTIM_IsEnabledNoEncoderMode.
fun Indicates whether the LPTIM operates in Non-encoder mode.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
return 1 or 0
void LPTIM_ClearIntFlag(LPTIM_Module* LPTIMx, uint32_t IntFlag)清除指定的 LPTIM 中断状态标志。
name LPTIM_ClearIntFlag.
fun Clear the specified LPTIM Interrupt status flags.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
param IntFlag (The input parameters must be the following values):
- LPTIM_COMP_MATCH_FLAG
- LPTIM_AUTO_RELOAD_FLAG
- LPTIM_EXT_TRIG_EDGE_EVENT_FLAG
- LPTIM_COMP_UPDATE_FLAG
- LPTIM_AUTO_RELOAD_UPDATE_FLAG
- LPTIM_COUNTER_DIR_UP_FLAG
- LPTIM_COUNTER_DIR_DOWN_FLAG
return none
uint32_t LPTIM_IsActiveIntStatus(LPTIM_Module* LPTIMx, uint32_t IntStatus)返回指定的 LPTIM 中断是否发生。
name LPTIM_IsActiveIntStatus.
fun Inform application whether the specified LPTIM interrupt has occurred.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
param IntStatus (The input parameters must be the following values):
- LPTIM_COMP_MATCH_STATUS
- LPTIM_AUTO_RELOAD_STATUS
- LPTIM_EXT_TRIG_EDGE_EVENT_STATUS
- LPTIM_COMP_UPDATE_STATUS
- LPTIM_AUTO_RELOAD_UPDATE_STATUS
- LPTIM_COUNTER_DIR_UP_STATUS
- LPTIM_COUNTER_DIR_DOWN_STATUS
return 1 or 0
void LPTIM_ConfigInt(LPTIM_Module* LPTIMx, uint32_t Interrupt, FunctionalStatus Cmd)使能或关闭指定的 LPTIM 中断。
name LPTIM_ConfigInt.
fun Enables or disables the specified LPTIM interrupts.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
param Interrupt (The input parameters must be the following values):
- LPTIM_COMP_MATCH_INT
- LPTIM_AUTO_RELOAD_INT
- LPTIM_EXT_TRIG_EDGE_EVENT_INT
- LPTIM_COMP_UPDATE_INT
- LPTIM_AUTO_RELOAD_UPDATE_INT
- LPTIM_COUNTER_DIR_UP_INT
- LPTIM_COUNTER_DIR_DOWN_INT
param Cmd (The input parameters must be the following values):
- ENABLE
- DISABLE
return none
note This function must be called when the LPTIM instance is disabled.
uint32_t LPTIM_IsEnabledInt(LPTIM_Module* LPTIMx, uint32_t Interrupt)返回指定的 LPTIM 中断是否已使能。
name LPTIM_IsEnabledInt.
fun Indicates whether the specified LPTIM interrupts is enabled.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
param Interrupt (The input parameters must be the following values):
- LPTIM_COMP_MATCH_INT
- LPTIM_AUTO_RELOAD_INT
- LPTIM_EXT_TRIG_EDGE_EVENT_INT
- LPTIM_COMP_UPDATE_INT
- LPTIM_AUTO_RELOAD_UPDATE_INT
- LPTIM_COUNTER_DIR_UP_INT
- LPTIM_COUNTER_DIR_DOWN_INT
return 1 or 0
void LPTIM_ConfigInput1(LPTIM_Module* LPTIMx, uint32_t ConnectSelect)选择 LPTIM 的 Input1 连接。
name LPTIM_ConfigInput1.
fun Select the connection for Input1 of LPTIM.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
param ConnectSelect (The input parameters must be the following values):
- LPTIM_INPUT1_CONNECT_GPIO
return none
void LPTIM_ConfigInput2(LPTIM_Module* LPTIMx, uint32_t ConnectSelect)选择 LPTIM 的 Input2 连接。
name LPTIM_ConfigInput2.
fun Select the connection for Input2 of LPTIM.
param LPTIMx (The input parameters must be the following values):
- LPTIM1
- LPTIM2
param ConnectSelect (The input parameters must be the following values):
- LPTIM_INPUT2_CONNECT_GPIO
return none
5.19.3 Demo 示例说明
本节说明 LPTIM 的 5 个示例:LPTIM_ENC 与 LPTIM_NENC 通过外部引脚产生编码波形并统计计数值,LPTIM_PulseCounter 统计外部脉冲数,LPTIM_PWM 在 PC1 输出 PWM 波形,LPTIM_WakeUp 以比较匹配中断把 MCU 从 STOP 模式唤醒。LPTIM1 统一以 LSI 作为时钟源,除 PWM 示例使用 PC1 外,其余示例的输入引脚为 PC0/PC2、输出引脚为 PB0/PB1。
5.19.3.1 LPTIM_ENC 编码器模式计数与波形输出
工程目录: projects/n32h49x_EVAL/examples/LPTIM/LPTIM_ENC/
使能 LSI 并选择 LPTIM1 时钟→配置 IN1/IN2 与 OUT1/OUT2 引脚→配置编码器模式与重装载、比较值→启动连续计数→产生 20 组编码波形并读取计数值校验
| 项目 | 源码解读 |
|---|---|
| 功能 | LPTIM1 以 LSI 作为时钟源工作在编码器模式,IN1 为 PC0、IN2 为 PC2,OUT1/OUT2 为 PB0/PB1; main 产生 20 组编码波形后读取计数值,期望 encCNT 为 40,并打印成功或失败信息。 |
| 源码组成 | src/main.c:main、LptimInOutPinConfig、LptimEncodeConfig、LptimEncodeWaveOutput;inc/main.h:包含 n32h49x_lptim.h、n32h49x_gpio.h、n32h49x_rcc.h、n32h49x_pwr.h、delay.h、log.h;src/n32h49x_it.c、inc/n32h49x_it.h:中断服务程序(仅异常处理);readme.txt:功能、环境与使用说明;MDK-ARM/、EWARM/:Keil 与 IAR 工程。 |
| 关键配置 | 时钟:使能 PWR 与备份寄存器访问,开启 LSI 并等待稳定,RCC_ConfigLPTIM1Clk(RCC_LPTIMCLK_SRC_LSI) 选择 LSI 作为 LPTIM1 时钟;引脚:IN1/IN2(PC0/PC2)复用 AF5 输入,OUT1/OUT2(PB0/PB1)推挽输出; LPTIM:预分频 LPTIM_PRESCALER_DIV1、内部时钟、LPTIM_EncoderModeCmd(ENABLE)、编码极性 LPTIM_ENCODER_MODE_RISING、ARR 15000、CMP 10000;启动: LPTIM_StartCounter(LPTIM1, LPTIM_OPERATING_MODE_CONTINUOUS)。 |
| 程序与数据路径 | main 先使能 LSI 并配置 LPTIM1 时钟,再调用 LptimInOutPinConfig 与 LptimEncodeConfig;随后以连续模式启动计数,调用 LptimEncodeWaveOutput(20) 逐组翻转 PB0/PB1 产生编码波形;最后用 LPTIM_GetCounterValue 读取计数值,encCNT 等于 40 时通过,否则打印失败。 |
| 主要 API | LPTIM_DeInit, LPTIM_SetPrescaler, LPTIM_SetClockSource, LPTIM_EncoderModeCmd, LPTIM_SetEncoderModeClockPolarity, LPTIM_Cmd, LPTIM_SetAutoReloadValue, LPTIM_SetCompareValue, LPTIM_StartCounter, LPTIM_GetCounterValue |
| 验证方法 | 下载运行后用示波器观察 PB0/PB1 的编码波形; 串口打印成功信息表示计数校验通过,打印失败信息表示计数值不等于 40。 |
| 使用提示 | 硬件平台为 N32H497ZGL7_EVB; 开发环境为 KEIL MDK-ARM 5.34 或 IAR EWARM 8.50.1; 编码器模式要求内部时钟源与预分频 DIV1,并须先进入连续模式再使能编码器模式。 |
核心代码解读与移植
示例把 LPTIM1 配置为编码器模式并统计外部输入边沿产生的计数,最后读出计数值校验。
数据路径: RCC_ConfigLPTIM1Clk 选择 LSI → LptimInOutPinConfig 配置 IN/OUT 引脚 → LptimEncodeConfig 设置编码器模式与重装载、比较值 → LPTIM_StartCounter 启动连续计数 → LptimEncodeWaveOutput 产生编码波形 → LPTIM_GetCounterValue 读取计数值
1. 使能 LSI 并选择 LPTIM1 时钟。 main 先使能 PWR 时钟与备份寄存器访问,开启 LSI 并等待稳定,再把 LSI 配置为 LPTIM1 的时钟源并使能 LPTIM1 总线时钟。
/* Enable PWR peripheral clock */
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPHEN_PWR, ENABLE);
/* Enable access to backup registers */
PWR_BackupAccessEnable(ENABLE);
/* Enable LSI */
RCC_EnableLsi(ENABLE);
/* Wait for LSI ready */
if (RCC_WaitLsiStable() == ERROR)
{
log_info("LSI oscillator not yet ready!!!\n");
while (1);
}
/* Configure the LSI as the clock source for LPTIM1 */
RCC_ConfigLPTIM1Clk(RCC_LPTIMCLK_SRC_LSI);
/* Enable LPTIM1 peripheral clock */
RCC_EnableLPTIMPeriphClk(RCC_LPTIM1_PERIPH_EN, ENABLE);
移植时保留 LSI 使能与 RCC_ConfigLPTIM1Clk;若改用其他 LPTIM,需相应调整外设时钟使能宏。
2. 配置输入输出引脚。 LptimInOutPinConfig 把 PC0、PC2 配置为 AF5 输入作为 IN1/IN2,把 PB0、PB1 配置为推挽输出作为 OUT1/OUT2。
void LptimInOutPinConfig(void)
{
GPIO_InitType GPIO_InitStructure;
/* Enable the GPIO Clock */
RCC_EnableAHB1PeriphClk(RCC_AHB_PERIPHEN_GPIOB, ENABLE);
RCC_EnableAHB1PeriphClk(RCC_AHB_PERIPHEN_GPIOC, ENABLE);
GPIO_InitStruct(&GPIO_InitStructure);
/* Configure the GPIO pin */
GPIO_InitStructure.Pin = GPIO_PIN_0;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_INPUT;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF5;
GPIO_InitPeripheral(GPIOC, &GPIO_InitStructure);
/* Configure the GPIO pin */
GPIO_InitStructure.Pin = GPIO_PIN_2;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_INPUT;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF5;
GPIO_InitPeripheral(GPIOC, &GPIO_InitStructure);
/* Configure the GPIO pin */
GPIO_InitStructure.Pin = GPIO_PIN_0;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStructure.GPIO_Alternate = GPIO_NO_AF;
GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
/* Configure the GPIO pin */
GPIO_InitStructure.Pin = GPIO_PIN_1;
GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
}
更换引脚时须保持复用号 AF5 与输入/输出方向,OUT1/OUT2 用推挽输出便于产生编码边沿。
3. 配置编码器模式。 LptimEncodeConfig 复位 LPTIM1,设置预分频、内部时钟、编码器模式与极性,并使能 LPTIM1 后写入重装载与比较值。
void LptimEncodeConfig(void)
{
/* Reset LPTIM1 */
LPTIM_DeInit(LPTIM1);
/* Set the actual prescaler division ratio to 1 */
LPTIM_SetPrescaler(LPTIM1, LPTIM_PRESCALER_DIV1);
/* Set the source of the clock used by LPTIM1 */
LPTIM_SetClockSource(LPTIM1, LPTIM_CLK_SOURCE_INTERNAL);
/* Enables the encoder mode of LPTIM1 */
LPTIM_EncoderModeCmd(LPTIM1, ENABLE);
/* Configure clock polarity in encoder mode */
LPTIM_SetEncoderModeClockPolarity(LPTIM1, LPTIM_ENCODER_MODE_RISING);
/* Enables LPTIM1 */
LPTIM_Cmd(LPTIM1, ENABLE);
/* Set the auto reload value */
LPTIM_SetAutoReloadValue(LPTIM1, 15000U);
/* Set the compare value */
LPTIM_SetCompareValue(LPTIM1, 10000U);
}
编码器模式要求内部时钟源与预分频 DIV1;重装载值必须大于比较值。
4. 启动计数并校验。 main 以连续模式启动计数,产生 20 组编码波形后读取计数值并打印结果。
/* Start counting in continuous mode */
LPTIM_StartCounter(LPTIM1, LPTIM_OPERATING_MODE_CONTINUOUS);
/* Great 20 square waves ,and encCNT should be equal to 40 */
LptimEncodeWaveOutput(20U);
/* Get actual counter value */
encCNT = LPTIM_GetCounterValue(LPTIM1);
if (encCNT != 40U)
{
log_info("LPTIM_ENC demo failed!!!\n");
}
else
{
log_info("LPTIM_ENC demo success!!!\n");
}
while (1);
每个编码周期对应两个计数边沿,因此 20 组波形对应 40 次计数;改变波形组数会成比例改变期望计数值。
对应源码: src/main.c
5.19.3.2 LPTIM_NENC 非编码器模式计数与波形输出
工程目录: projects/n32h49x_EVAL/examples/LPTIM/LPTIM_NENC/
使能 LSI 并选择 LPTIM1 时钟→配置 IN1/IN2 与 OUT1/OUT2 引脚→配置非编码器模式与重装载、比较值→启动连续计数→产生 20 组非编码波形并读取计数值校验
| 项目 | 源码解读 |
|---|---|
| 功能 | LPTIM1 以 LSI 作为时钟源工作在非编码器模式,IN1 为 PC0、IN2 为 PC2,OUT1/OUT2 为 PB0/PB1; main 产生 20 组波形后读取计数值,期望 NencCNT 为 20,并打印成功或失败信息。 |
| 源码组成 | src/main.c:main、LptimInOutPinConfig、LptimNoEncodeConfig、LptimNoEncodeWaveOutput;inc/main.h:包含 n32h49x_lptim.h、n32h49x_gpio.h、n32h49x_rcc.h、n32h49x_pwr.h、delay.h、log.h;src/n32h49x_it.c、inc/n32h49x_it.h:中断服务程序(仅异常处理);readme.txt:功能、环境与使用说明;MDK-ARM/、EWARM/:Keil 与 IAR 工程。 |
| 关键配置 | 时钟:使能 PWR 与备份寄存器访问,开启 LSI 并等待稳定,RCC_ConfigLPTIM1Clk(RCC_LPTIMCLK_SRC_LSI);引脚:IN1/IN2(PC0/PC2)复用 AF5 输入,OUT1/OUT2(PB0/PB1)推挽输出; LPTIM:预分频 LPTIM_PRESCALER_DIV1、内部时钟、LPTIM_NoEncoderModeCmd(ENABLE)、编码极性 LPTIM_ENCODER_MODE_FALLING、ARR 15000、CMP 10000;启动: LPTIM_StartCounter(LPTIM1, LPTIM_OPERATING_MODE_CONTINUOUS)。 |
| 程序与数据路径 | main 先使能 LSI 并配置 LPTIM1 时钟,再调用 LptimInOutPinConfig 与 LptimNoEncodeConfig;随后以连续模式启动计数,调用 LptimNoEncodeWaveOutput(20) 逐组翻转 PB0/PB1;最后用 LPTIM_GetCounterValue 读取计数值,NencCNT 等于 20 时通过,否则打印失败。 |
| 主要 API | LPTIM_DeInit, LPTIM_SetPrescaler, LPTIM_SetClockSource, LPTIM_NoEncoderModeCmd, LPTIM_SetEncoderModeClockPolarity, LPTIM_Cmd, LPTIM_SetAutoReloadValue, LPTIM_SetCompareValue, LPTIM_StartCounter, LPTIM_GetCounterValue |
| 验证方法 | 下载运行后用示波器观察 PB0/PB1 的波形; 串口打印成功信息表示计数校验通过,打印失败信息表示计数值不等于 20。 |
| 使用提示 | 硬件平台为 N32H497ZGL7_EVB; 开发环境为 KEIL MDK-ARM 5.34 或 IAR EWARM 8.50.1; 非编码器模式同样要求内部时钟源与预分频 DIV1,并须先进入连续模式再使能。 |
核心代码解读与移植
示例把 LPTIM1 配置为非编码器模式并统计外部输入边沿产生的计数,最后读出计数值校验。
数据路径: RCC_ConfigLPTIM1Clk 选择 LSI → LptimInOutPinConfig 配置 IN/OUT 引脚 → LptimNoEncodeConfig 设置非编码器模式与重装载、比较值 → LPTIM_StartCounter 启动连续计数 → LptimNoEncodeWaveOutput 产生波形 → LPTIM_GetCounterValue 读取计数值
1. 使能 LSI 并选择 LPTIM1 时钟。 main 使能 PWR 时钟与备份寄存器访问,开启 LSI 并等待稳定,再把 LSI 配置为 LPTIM1 的时钟源。
/* Enable PWR peripheral clock */
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPHEN_PWR, ENABLE);
/* Enable access to backup registers */
PWR_BackupAccessEnable(ENABLE);
/* Enable LSI */
RCC_EnableLsi(ENABLE);
/* Wait for LSI ready */
if (RCC_WaitLsiStable() == ERROR)
{
log_info("LSI oscillator not yet ready!!!\n");
while (1);
}
/* Configure the LSI as the clock source for LPTIM1 */
RCC_ConfigLPTIM1Clk(RCC_LPTIMCLK_SRC_LSI);
/* Enable LPTIM1 peripheral clock */
RCC_EnableLPTIMPeriphClk(RCC_LPTIM1_PERIPH_EN, ENABLE);
移植时保留 LSI 使能与 RCC_ConfigLPTIM1Clk,并按实际外设调整总线时钟使能宏。
2. 配置输入输出引脚。 LptimInOutPinConfig 把 PC0、PC2 配置为 AF5 输入,把 PB0、PB1 配置为推挽输出。
void LptimInOutPinConfig(void)
{
GPIO_InitType GPIO_InitStructure;
/* Enable the GPIO Clock */
RCC_EnableAHB1PeriphClk(RCC_AHB_PERIPHEN_GPIOB, ENABLE);
RCC_EnableAHB1PeriphClk(RCC_AHB_PERIPHEN_GPIOC, ENABLE);
GPIO_InitStruct(&GPIO_InitStructure);
/* Configure the GPIO pin */
GPIO_InitStructure.Pin = GPIO_PIN_0;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_INPUT;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF5;
GPIO_InitPeripheral(GPIOC, &GPIO_InitStructure);
/* Configure the GPIO pin */
GPIO_InitStructure.Pin = GPIO_PIN_2;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_INPUT;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF5;
GPIO_InitPeripheral(GPIOC, &GPIO_InitStructure);
/* Configure the GPIO pin */
GPIO_InitStructure.Pin = GPIO_PIN_0;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStructure.GPIO_Alternate = GPIO_NO_AF;
GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
/* Configure the GPIO pin */
GPIO_InitStructure.Pin = GPIO_PIN_1;
GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
}
更换引脚时须保持复用号 AF5 与输入/输出方向,输出用推挽以便产生边沿。
3. 配置非编码器模式。 LptimNoEncodeConfig 复位 LPTIM1,设置预分频、内部时钟、非编码器模式与极性,并使能后写入重装载与比较值。
void LptimNoEncodeConfig(void)
{
/* Reset LPTIM1 */
LPTIM_DeInit(LPTIM1);
/* Set the actual prescaler division ratio to 1 */
LPTIM_SetPrescaler(LPTIM1, LPTIM_PRESCALER_DIV1);
/* Set the source of the clock used by LPTIM1 */
LPTIM_SetClockSource(LPTIM1, LPTIM_CLK_SOURCE_INTERNAL);
/* Enables the Non-encoder mode of LPTIM1 */
LPTIM_NoEncoderModeCmd(LPTIM1, ENABLE);
/* Configure clock polarity in encoder mode */
LPTIM_SetEncoderModeClockPolarity(LPTIM1, LPTIM_ENCODER_MODE_FALLING);
/* Enables LPTIM1 */
LPTIM_Cmd(LPTIM1, ENABLE);
/* Set the auto reload value */
LPTIM_SetAutoReloadValue(LPTIM1, 15000U);
/* Set the compare value */
LPTIM_SetCompareValue(LPTIM1, 10000U);
}
与编码器模式相比只有模式选择与极性不同;重装载值必须大于比较值。
4. 启动计数并校验。 main 以连续模式启动计数,产生 20 组波形后读取计数值并打印结果。
/* Start counting in continuous mode */
LPTIM_StartCounter(LPTIM1, LPTIM_OPERATING_MODE_CONTINUOUS);
/* Great 20 square waves ,and NencCNT should be equal to 20 */
LptimNoEncodeWaveOutput(20U);
/* Get actual counter value */
NencCNT = LPTIM_GetCounterValue(LPTIM1);
if (NencCNT != 20U)
{
log_info("LPTIM_NENC demo failed!!!\n");
}
else
{
log_info("LPTIM_NENC demo success!!!\n");
}
while (1);
非编码器模式下每组波形对应一次计数,因此 20 组波形对应计数值 20。
对应源码: src/main.c
5.19.3.3 LPTIM_PulseCounter 外部脉冲计数
工程目录: projects/n32h49x_EVAL/examples/LPTIM/LPTIM_PulseCounter/
使能 LSI 并选择 LPTIM1 时钟→配置 IN1(PC0) 与 OUT1(PB0) 引脚→将计数器设为外部模式并配置时钟极性→启动连续计数→产生 10 组脉冲并读取计数值校验
| 项目 | 源码解读 |
|---|---|
| 功能 | LPTIM1 工作在外部计数模式,IN1 为 PC0 作为外部脉冲输入,OUT1 为 PB0 用于产生脉冲; main 产生 10 组脉冲后读取计数值,期望 tempCNT 为 10,并打印成功或失败信息。 |
| 源码组成 | src/main.c:main、LptimInOutPinConfig、LptimPWMOutput;inc/main.h:包含 n32h49x_lptim.h、n32h49x_gpio.h、n32h49x_rcc.h、n32h49x_pwr.h、delay.h、log.h;src/n32h49x_it.c、inc/n32h49x_it.h:中断服务程序(仅异常处理);readme.txt:功能、环境与使用说明;MDK-ARM/、EWARM/:Keil 与 IAR 工程。 |
| 关键配置 | 时钟:使能 PWR 与备份寄存器访问,开启 LSI 并等待稳定,RCC_ConfigLPTIM1Clk(RCC_LPTIMCLK_SRC_LSI);引脚:IN1(PC0)复用 AF5 输入,OUT1(PB0)推挽输出; LPTIM: LPTIM_SetCounterMode(LPTIM1, LPTIM_COUNTER_MODE_EXTERNAL),LPTIM_ConfigClock(LPTIM1, LPTIM_CLK_FILTER_NONE, LPTIM_CLK_POLARITY_RISING) 选择上升沿计数,ARR 60000;启动: LPTIM_StartCounter(LPTIM1, LPTIM_OPERATING_MODE_CONTINUOUS)。 |
| 程序与数据路径 | main 使能 LSI 并配置 LPTIM1 时钟,调用 LptimInOutPinConfig 配置引脚;随后复位 LPTIM1、设为外部计数模式、配置上升沿时钟极性并使能,写入 ARR 后以连续模式启动; LptimPWMOutput(10) 在 PB0 产生 10 组脉冲,再用 LPTIM_GetCounterValue 读取计数值并与 10 比较。 |
| 主要 API | LPTIM_DeInit, LPTIM_SetCounterMode, LPTIM_ConfigClock, LPTIM_Cmd, LPTIM_SetAutoReloadValue, LPTIM_StartCounter, LPTIM_GetCounterValue |
| 验证方法 | 下载运行后用示波器观察 PB0 的脉冲序列; 串口打印成功信息表示计数值等于 10,打印失败信息表示计数不符。 |
| 使用提示 | 硬件平台为 N32H497ZGL7_EVB; 开发环境为 KEIL MDK-ARM 5.34 或 IAR EWARM 8.50.1; 计数器模式只能在 LPTIM 关闭时切换,示例在复位后立即设置。 |
核心代码解读与移植
示例把 LPTIM1 配置为外部脉冲计数模式,统计 IN1 上的脉冲数并校验。
数据路径: RCC_ConfigLPTIM1Clk 选择 LSI → LptimInOutPinConfig 配置 IN1/OUT1 → LPTIM_SetCounterMode 设为外部计数 → LPTIM_ConfigClock 选择上升沿 → LPTIM_StartCounter 启动计数 → LptimPWMOutput 产生脉冲 → LPTIM_GetCounterValue 读取计数值
1. 使能 LSI 并选择 LPTIM1 时钟。 main 使能 PWR 时钟与备份寄存器访问,开启 LSI 并等待稳定,再把 LSI 配置为 LPTIM1 的时钟源。
/* Enable PWR peripheral clock */
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPHEN_PWR, ENABLE);
/* Enable access to backup registers */
PWR_BackupAccessEnable(ENABLE);
/* Enable LSI */
RCC_EnableLsi(ENABLE);
/* Wait for LSI ready */
if (RCC_WaitLsiStable() == ERROR)
{
log_info("LSI oscillator not yet ready!!!\n");
while (1);
}
/* Configure the LSI as the clock source for LPTIM1 */
RCC_ConfigLPTIM1Clk(RCC_LPTIMCLK_SRC_LSI);
/* Enable LPTIM1 peripheral clock */
RCC_EnableLPTIMPeriphClk(RCC_LPTIM1_PERIPH_EN, ENABLE);
移植时保留 LSI 使能与 RCC_ConfigLPTIM1Clk;外部计数模式下 LSI 只是给 LPTIM 提供基础时钟。
2. 配置输入输出引脚。 LptimInOutPinConfig 把 PC0 配置为 AF5 输入作为外部脉冲输入,把 PB0 配置为推挽输出用于产生脉冲。
void LptimInOutPinConfig(void)
{
GPIO_InitType GPIO_InitStructure;
/* Enable the GPIO Clock */
RCC_EnableAHB1PeriphClk(RCC_AHB_PERIPHEN_GPIOB, ENABLE);
RCC_EnableAHB1PeriphClk(RCC_AHB_PERIPHEN_GPIOC, ENABLE);
GPIO_InitStruct(&GPIO_InitStructure);
/* Configure the GPIO pin */
GPIO_InitStructure.Pin = GPIO_PIN_0;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_INPUT;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF5;
GPIO_InitPeripheral(GPIOC, &GPIO_InitStructure);
/* Configure the GPIO pin */
GPIO_InitStructure.Pin = GPIO_PIN_0;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStructure.GPIO_Alternate = GPIO_NO_AF;
GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
}
更换引脚时须保持 PC0 的 AF5 输入方向与 PB0 的推挽输出方向。
3. 配置外部计数模式。 main 复位 LPTIM1,设为外部计数模式并选择上升沿作为有效时钟,然后使能并写入重装载值。
/* Reset LPTIM1 */
LPTIM_DeInit(LPTIM1);
/* Configure LPTIM external Input1 */
LPTIM_SetCounterMode(LPTIM1, LPTIM_COUNTER_MODE_EXTERNAL);
/* Configure the rising polarity */
LPTIM_ConfigClock(LPTIM1, LPTIM_CLK_FILTER_NONE, LPTIM_CLK_POLARITY_RISING);
/* Enables LPTIM1 */
LPTIM_Cmd(LPTIM1, ENABLE);
/* Set the auto reload value */
LPTIM_SetAutoReloadValue(LPTIM1, 60000U);
/* Start counting in continuous mode */
LPTIM_StartCounter(LPTIM1, LPTIM_OPERATING_MODE_CONTINUOUS);
计数器模式只能在 LPTIM 关闭时切换,因此示例在 LPTIM_DeInit 之后立即设置;每个上升沿计数一次。
4. 产生脉冲并校验。 main 在 PB0 产生 10 组脉冲后读取计数值并与 10 比较。
/* Great 10 square waves ,and tempCNT should be equal to 10 */
LptimPWMOutput(10U);
/* Get actual counter value */
tempCNT = LPTIM_GetCounterValue(LPTIM1);
if (tempCNT != 10U)
{
log_info("LPTIM_PulseCounter demo failed!!!\n");
}
else
{
log_info("LPTIM_PulseCounter demo success!!!\n");
}
while (1);
因为只选择上升沿计数,每产生一个完整脉冲只计一次数,故 10 组脉冲对应计数值 10。
对应源码: src/main.c
5.19.3.4 LPTIM_PWM PWM 波形输出
工程目录: projects/n32h49x_EVAL/examples/LPTIM/LPTIM_PWM/
使能 LSI 并选择 LPTIM1 时钟→将 PC1 配置为复用推挽输出→配置 PWM 波形与极性→使能并设置重装载、比较值→以连续模式启动并在 PC1 输出 PWM
| 项目 | 源码解读 |
|---|---|
| 功能 | LPTIM1 以 LSI 为时钟源在 PC1 输出 PWM 波形,采用常规极性; 通过重装载值 600 与比较值 300 决定周期与占空比,启动后由示波器观察输出。 |
| 源码组成 | src/main.c:main、LptimOutPinConfig;inc/main.h:包含 n32h49x_lptim.h、n32h49x_gpio.h、n32h49x_rcc.h、n32h49x_pwr.h、log.h;src/n32h49x_it.c、inc/n32h49x_it.h:中断服务程序(仅异常处理);readme.txt:功能、环境与使用说明;MDK-ARM/、EWARM/:Keil 与 IAR 工程。 |
| 关键配置 | 时钟:使能 PWR 与备份寄存器访问,开启 LSI 并等待稳定,RCC_ConfigLPTIM1Clk(RCC_LPTIMCLK_SRC_LSI);引脚:OUT1(PC1)复用 AF5 推挽输出; LPTIM: LPTIM_SetWaveform(LPTIM1, LPTIM_OUTPUT_WAVEFORM_PWM)、LPTIM_SetPolarity(LPTIM1, LPTIM_OUTPUT_POLARITY_REGULAR)、ARR 600、CMP 300;启动: LPTIM_StartCounter(LPTIM1, LPTIM_OPERATING_MODE_CONTINUOUS)。 |
| 程序与数据路径 | main 使能 LSI 并配置 LPTIM1 时钟,调用 LptimOutPinConfig 把 PC1 配置为复用推挽输出;随后复位 LPTIM1、设为 PWM 波形与常规极性、使能并写入 ARR 与 CMP,最后以连续模式启动; while(1) 中不再操作外设,输出波形由硬件自动产生。 |
| 主要 API | LPTIM_DeInit, LPTIM_SetWaveform, LPTIM_SetPolarity, LPTIM_Cmd, LPTIM_SetAutoReloadValue, LPTIM_SetCompareValue, LPTIM_StartCounter |
| 验证方法 | 用示波器或逻辑分析仪观察 PC1 输出的 PWM 波形; 改变 ARR 与 CMP 可分别调整周期与占空比。 |
| 使用提示 | 硬件平台为 N32H497ZGL7_EVB; 开发环境为 KEIL MDK-ARM 5.34 或 IAR EWARM 8.50.1; 输出极性的改动会立即生效,配置期间输出默认电平会先发生变化。 |
核心代码解读与移植
示例把 LPTIM1 配置为 PWM 输出模式,在 PC1 上产生连续 PWM 波形。
数据路径: RCC_ConfigLPTIM1Clk 选择 LSI → LptimOutPinConfig 配置 PC1 复用推挽输出 → LPTIM_SetWaveform 与 LPTIM_SetPolarity 配置波形和极性 → LPTIM_SetAutoReloadValue、LPTIM_SetCompareValue 设置周期与占空比 → LPTIM_StartCounter 启动输出
1. 使能 LSI 并选择 LPTIM1 时钟。 main 使能 PWR 时钟与备份寄存器访问,开启 LSI 并等待稳定,再把 LSI 配置为 LPTIM1 的时钟源。
/* Enable PWR peripheral clock */
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPHEN_PWR, ENABLE);
/* Enable access to backup registers */
PWR_BackupAccessEnable(ENABLE);
/* Enable LSI */
RCC_EnableLsi(ENABLE);
/* Wait for LSI ready */
if (RCC_WaitLsiStable() == ERROR)
{
log_info("LSI oscillator not yet ready!!!\n");
while (1);
}
/* Configure the LSI as the clock source for LPTIM1 */
RCC_ConfigLPTIM1Clk(RCC_LPTIMCLK_SRC_LSI);
/* Enable LPTIM1 peripheral clock */
RCC_EnableLPTIMPeriphClk(RCC_LPTIM1_PERIPH_EN, ENABLE);
移植时保留 LSI 使能与 RCC_ConfigLPTIM1Clk;PWM 周期由 LSI 频率与 ARR 共同决定。
2. 配置输出引脚。 LptimOutPinConfig 把 PC1 配置为 AF5 复用推挽输出作为 OUT1。
void LptimOutPinConfig(void)
{
GPIO_InitType GPIO_InitStructure;
/* Enable the GPIO Clock */
RCC_EnableAHB1PeriphClk(RCC_AHB_PERIPHEN_GPIOC, ENABLE);
GPIO_InitStruct(&GPIO_InitStructure);
/* Configure the GPIO pin */
GPIO_InitStructure.Pin = GPIO_PIN_1;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF5;
GPIO_InitPeripheral(GPIOC, &GPIO_InitStructure);
}
输出必须配置为复用推挽(AF_PP)并选择 AF5,否则 LPTIM 波形无法驱动引脚。
3. 配置 PWM 波形。 main 复位 LPTIM1,选择 PWM 波形与常规极性,使能后写入重装载值与比较值。
/* Reset LPTIM1 */
LPTIM_DeInit(LPTIM1);
/* Set LPTIM1 generates a PWM waveform */
LPTIM_SetWaveform(LPTIM1, LPTIM_OUTPUT_WAVEFORM_PWM);
/* Set output polarity */
LPTIM_SetPolarity(LPTIM1, LPTIM_OUTPUT_POLARITY_REGULAR);
/* Enables LPTIM1 */
LPTIM_Cmd(LPTIM1, ENABLE);
/* Set the auto reload value */
LPTIM_SetAutoReloadValue(LPTIM1, 600U);
/* Set the compare value */
LPTIM_SetCompareValue(LPTIM1, 300U);
/* Start counting in continuous mode */
LPTIM_StartCounter(LPTIM1, LPTIM_OPERATING_MODE_CONTINUOUS);
重装载值决定周期、比较值决定占空比;重装载值必须大于比较值。
4. 观察输出。 启动后主循环不再操作外设,PWM 由硬件持续输出。
log_info("Use oscilloscope or logic analyzer to observe the PWM waveform output from PC1!!!\n");
while (1);
输出波形完全由硬件产生,CPU 无需干预,适合在低功耗场景下持续输出。
对应源码: src/main.c
5.19.3.5 LPTIM_WakeUp 低功耗唤醒
工程目录: projects/n32h49x_EVAL/examples/LPTIM/LPTIM_WakeUp/
初始化 LED 与电源时钟并使能 LSI→配置 LPTIM1_WKUP_IRQn 与 EXTI Line 21→配置比较匹配中断、重装载与比较值→以连续模式启动计数→主循环翻转 LED1 并进入 STOP 模式由比较匹配唤醒
| 项目 | 源码解读 |
|---|---|
| 功能 | LPTIM1 以 LSI 为时钟源,比较匹配中断经 EXTI Line 21 触发 LPTIM1_WKUP_IRQn;中断服务程序翻转 LED2,主循环翻转 LED1 后进入 STOP 模式,比较匹配事件把 MCU 唤醒并重新配置 240 MHz 系统时钟。 |
| 源码组成 | src/main.c:main、LedInit、LedOn、LedOff、LedBlink、LptimNvicConfig、SetSysClockToPLL;inc/main.h:包含 n32h49x_lptim.h、n32h49x_gpio.h、n32h49x_rcc.h、n32h49x_pwr.h、n32h49x_exti.h、misc.h、delay.h、log.h;src/n32h49x_it.c、inc/n32h49x_it.h:中断服务程序(含 LPTIM1_WKUP_IRQHandler);readme.txt:功能、环境与使用说明;MDK-ARM/、EWARM/:Keil 与 IAR 工程。 |
| 关键配置 | 时钟:使能 PWR 与备份寄存器访问,开启 LSI 并等待稳定,RCC_ConfigLPTIM1Clk(RCC_LPTIMCLK_SRC_LSI);引脚与中断:LED1 为 PA3、LED2 为 PB4,EXTI Line 21 上升沿触发, LPTIM1_WKUP_IRQn 使能;LPTIM:预分频 DIV1、 LPTIM_ConfigInt(LPTIM1, LPTIM_COMP_MATCH_INT, ENABLE)、ARR 35000、CMP 30000;低功耗:进入 PWR_EnterSTOPMode(PWR_REGULATOR_NORMAL, PWR_STOPENTRY_WFI),唤醒后 SetSysClockToPLL(RCC_PLL_SRC_HSI, 240000000U)。 |
| 程序与数据路径 | main 初始化 LED 并点亮,使能 PWR 时钟与 LSI,调用 LptimNvicConfig 配置 EXTI 与 NVIC;随后复位 LPTIM1、设置预分频与比较匹配中断、使能并写入 ARR 与 CMP,延时 2 s 后以连续模式启动; while(1) 中翻转 LED1,再进入 STOP 模式等待比较匹配中断唤醒,唤醒后重新配置系统时钟。 |
| 主要 API | LPTIM_DeInit, LPTIM_SetPrescaler, LPTIM_ConfigInt, LPTIM_Cmd, LPTIM_SetAutoReloadValue, LPTIM_SetCompareValue, LPTIM_StartCounter |
| 验证方法 | 下载运行后 LED2 在比较匹配中断中翻转,LED1 在主循环每次唤醒后翻转; 用电流表可观察到 MCU 在 STOP 与运行之间周期性切换。 |
| 使用提示 | 硬件平台为 N32H497ZGL7_EVB; 开发环境为 KEIL MDK-ARM 5.34 或 IAR EWARM 8.50.1; 进入 STOP 模式后系统时钟被关闭,唤醒后必须重新配置时钟; 示例在启动计数前延时 2 s,便于连接调试器观察。 |
核心代码解读与移植
示例把 LPTIM1 配置为比较匹配唤醒源,主循环在 STOP 模式与运行模式之间周期性切换。
数据路径: LedInit 初始化 LED → RCC_ConfigLPTIM1Clk 选择 LSI → LptimNvicConfig 配置 EXTI 与 NVIC → LPTIM_ConfigInt、LPTIM_SetAutoReloadValue、LPTIM_SetCompareValue 配置中断与比较值 → LPTIM_StartCounter 启动计数 → 主循环 PWR_EnterSTOPMode 进入 STOP 并由唤醒后 SetSysClockToPLL 恢复时钟
1. 初始化 LED 与时钟。 main 初始化 LED1(PA3)与 LED2(PB4)并点亮,使能 PWR 时钟、备份寄存器访问与 LSI,然后把 LSI 配置为 LPTIM1 的时钟源。
LedInit(LED1_PORT, LED1_PIN);
LedInit(LED2_PORT, LED2_PIN);
LedOn(LED1_PORT, LED1_PIN);
LedOn(LED2_PORT, LED2_PIN);
/* Enable PWR peripheral clock */
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPHEN_PWR, ENABLE);
/* Enable access to backup registers */
PWR_BackupAccessEnable(ENABLE);
/* Enable LSI */
RCC_EnableLsi(ENABLE);
/* Wait for LSI ready */
if (RCC_WaitLsiStable() == ERROR)
{
log_info("LSI oscillator not yet ready!!!\n");
while (1);
}
/* Configure the LSI as the clock source for LPTIM1 */
RCC_ConfigLPTIM1Clk(RCC_LPTIMCLK_SRC_LSI);
/* Enable LPTIM1 peripheral clock */
RCC_EnableLPTIMPeriphClk(RCC_LPTIM1_PERIPH_EN, ENABLE);
更换评估板时只需修改 LED 端口与引脚宏;LSI 使能与 LPTIM1 时钟配置是唤醒功能的基础。
2. 配置 EXTI 与 NVIC。 LptimNvicConfig 把 EXTI Line 21 配置为上升沿中断,并使能 LPTIM1_WKUP_IRQn。
void LptimNvicConfig(void)
{
NVIC_InitType NVIC_InitStructure;
EXTI_InitType EXTI_InitStructure;
/* Configures the LPTIM1 connected to EXTI Line 25 */
EXTI_InitStruct(&EXTI_InitStructure);
EXTI_InitStructure.EXTI_Line = EXTI_LINE21;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitPeripheral(&EXTI_InitStructure);
/* Configures the priority group */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
/* Configures the LPTIM1_WKUP_IRQn NVIC */
NVIC_InitStructure.NVIC_IRQChannel = LPTIM1_WKUP_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = NVIC_PRE_PRIORITY_0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = NVIC_SUB_PRIORITY_0;
NVIC_Init(&NVIC_InitStructure);
}
EXTI 的触发行号与 NVIC 通道必须与所用 LPTIM 对应;移植到 LPTIM2 时需改用相应中断源。示例该行的注释写的是 EXTI Line 25,与代码实际配置的 EXTI_LINE21 不符:按用户手册,LPTIM1 唤醒事件连接到 EXTI 线 21、LPTIM2 连接到线 22,且 LPTIM1/2 中断的 EXTI 只能配置为上升沿触发,以代码为准。
3. 配置 LPTIM 并启动。 复位后设置预分频与比较匹配中断,使能 LPTIM1 并写入 ARR 与 CMP,延时 2 s 后以连续模式启动。
/* Reset LPTIM1 */
LPTIM_DeInit(LPTIM1);
/* Set actual prescaler division ratio to 1 */
LPTIM_SetPrescaler(LPTIM1, LPTIM_PRESCALER_DIV1);
/* Enable LPTIM1 Compare match Interrupt */
LPTIM_ConfigInt(LPTIM1, LPTIM_COMP_MATCH_INT, ENABLE);
/* Enables LPTIM1 */
LPTIM_Cmd(LPTIM1, ENABLE);
/* Set the auto reload value */
LPTIM_SetAutoReloadValue(LPTIM1, 35000U);
/* Set the compare value */
LPTIM_SetCompareValue(LPTIM1, 30000U);
systick_delay_ms(2000U);
/* Start counting in continuous mode */
LPTIM_StartCounter(LPTIM1, LPTIM_OPERATING_MODE_CONTINUOUS);
中断使能须在 LPTIM 关闭状态下配置;ARR 与 CMP 决定唤醒周期。
4. 主循环进入 STOP 并唤醒。 while(1) 翻转 LED1 后请求进入 STOP 模式,比较匹配中断唤醒后再把系统时钟切回 240 MHz。
while (1)
{
/* Toggle LED1 */
LedBlink(LED1_PORT, LED1_PIN);
/* Request to enter STOP mode*/
PWR_EnterSTOPMode(PWR_REGULATOR_NORMAL, PWR_STOPENTRY_WFI);
/* Configures system clock after wake-up from STOP: enable HSI, PLL and select
PLL as system clock source */
SetSysClockToPLL(RCC_PLL_SRC_HSI, 240000000U);
}
}
进入 STOP 后系统时钟停止,因此唤醒后必须重新配置 PLL 与系统时钟,否则后续代码会以错误频率运行。
对应源码: src/main.c
5.19.4 注意与排障
| 场景 | 检查与建议 | 来源 |
|---|---|---|
| 配置时机 | LPTIM 使能后 LPTIMx_CFG、中断使能等寄存器不可修改;须先关闭 LPTIM 再修改配置,改好后再使能。 |
用户手册 |
| 重装载与比较 | 自动重装载值必须大于比较值(ARR > CMP); 写入 LPTIMx_ARR 后须等 ARRUPD 置位才能再次写入,写入 LPTIMx_CMP 后须等 CMPUPD 置位。 |
用户手册 |
| 计数器读取 | 当 LPTIM 由异步时钟驱动时,读 LPTIMx_CNT 可能返回不可靠值;应连续读两次并确认两次结果一致。 |
用户手册 |
| 编码器模式 | 编码器模式要求使用内部时钟源、预分频 DIV1,且须先配置为连续模式再使能; 两个外部边沿都有效时,内部时钟频率须不低于外部时钟的 4 倍。 |
用户手册 |
| 触发信号选择 | LPTIM 启动触发信号由 LPTIMx_CFG.TRGSEL[3:0] 选择:0 为 LPTIMx_ETR(外部 GPIO)、1 为 RTC_ALARM_A、2 为 RTC_ALARM_B、35为RTC_TAMP1TAMP3;6~15 保留。 |
用户手册 |
| 触发与时钟滤波 | 数字滤波需要内部时钟源; 为触发或外部时钟配置滤波前,须确保 LPTIM 有可用的内部时钟。 |
用户手册 |
| 超时功能 | 第一个触发事件启动定时器,之后的触发事件会复位计数器并重新开始; 定时器已启动时到达的触发事件会被忽略。 |
用户手册 |