5.19 LPTIM Guide
This chapter covers the LPTIM driver files in the N32H49x standard peripheral library, its 46 public functions, and the 5 examples. LPTIM is a low-power timer that supports internal or external clocking, continuous or one-shot counting, PWM and set-once output, external triggering, timeout, pulse counting, and quadrature encoder mode, and it can keep counting in low-power modes such as STOP and wake the MCU. Read it together with the “Low-power timer (LPTIM)” chapter of the user manual for clock-source selection, trigger signals, and configuration sequencing.
5.19.1 Related files
| Relative path | Purpose |
|---|---|
firmware/n32h49x_std_periph_driver/inc/n32h49x_lptim.h |
Defines the operating-mode, clock-source, trigger-source, waveform, and filter parameter enumerations, and declares the 46 public functions |
firmware/n32h49x_std_periph_driver/src/n32h49x_lptim.c |
Implements the 46 functions: initialization and enable, count and compare, output and waveform, timeout and trigger, clock and filter, encoder mode, and interrupt and input |
projects/n32h49x_EVAL/examples/LPTIM/ |
Root of the LPTIM examples, holding 5 examples: LPTIM_ENC, LPTIM_NENC, LPTIM_PulseCounter, LPTIM_PWM, LPTIM_WakeUp |
projects/n32h49x_EVAL/examples/LPTIM/LPTIM_ENC/ |
Project layout of that example: MDK-ARM/, EWARM/, inc/, src/, readme.txt |
projects/n32h49x_EVAL/bsp/ |
Board support files referenced by the examples: UART log printing and millisecond delay functions |
5.19.2 API reference
The following 46 public functions are listed in the declaration order of n32h49x_lptim.h in 7 groups; each entry gives a summary and the original comment above the function in n32h49x_lptim.c.
Initialization and enable
LPTIM_DeInit, LPTIM_StructInit, LPTIM_Init, LPTIM_Cmd, LPTIM_IsEnabled
Count, compare, and update
LPTIM_StartCounter, LPTIM_SetUpdateMode, LPTIM_GetUpdateMode, LPTIM_SetAutoReloadValue, LPTIM_GetAutoReloadValue, LPTIM_SetCompareValue, LPTIM_GetCompareValue, LPTIM_GetCounterValue, LPTIM_SetCounterMode, LPTIM_GetCounterMode
Output, waveform, and prescaler
LPTIM_ConfigOutput, LPTIM_SetWaveform, LPTIM_GetWaveform, LPTIM_SetPolarity, LPTIM_GetPolarity, LPTIM_SetPrescaler, LPTIM_GetPrescaler
Timeout and trigger
LPTIM_TimeoutCmd, LPTIM_IsEnabledTimeout, LPTIM_SoftwareTrigger, LPTIM_ConfigTrigger, LPTIM_GetTriggerSource, LPTIM_GetTriggerFilter, LPTIM_GetTriggerPolarity
Clock
LPTIM_SetClockSource, LPTIM_GetClockSource, LPTIM_ConfigClock, LPTIM_GetClockPolarity, LPTIM_GetClockFilter
Encoder mode
LPTIM_SetEncoderModeClockPolarity, LPTIM_GetEncoderModeClockPolarity, LPTIM_EncoderModeCmd, LPTIM_NoEncoderModeCmd, LPTIM_IsEnabledEncoderMode, LPTIM_IsEnabledNoEncoderMode
Interrupt and input
LPTIM_ClearIntFlag, LPTIM_IsActiveIntStatus, LPTIM_ConfigInt, LPTIM_IsEnabledInt, LPTIM_ConfigInput1, LPTIM_ConfigInput2
void LPTIM_DeInit(LPTIM_Module* LPTIMx)Set LPTIMx registers to their reset values.
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)Set each fields of the LPTIM_InitStruct structure to its default value.
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)Configure the LPTIMx peripheral according to the specified parameters.
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)Enables or disables the specified LPTIM instance.
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)Indicates whether the LPTIM instance is enabled.
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)Starts the LPTIM counter in the desired mode.
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)Set the LPTIM registers update mode (enable/disable register preload).
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)Get the LPTIM registers update mode.
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)Set the auto reload value.
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)Get actual auto reload value.
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)Set the compare value.
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)Get actual compare value.
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)Get actual counter value.
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)Set the counter mode (selection of the LPTIM counter clock source).
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)Get the counter mode.
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)Configure the LPTIM instance output.
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)Set waveform shape.
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)Get actual waveform shape.
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)Set output 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)Get actual output polarity.
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)Set actual prescaler division ratio.
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)Get actual prescaler division ratio.
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)Enables or disables the LPTIM timeout function.
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)Indicate whether the timeout function is enabled.
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)Configure the Software trigger.
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)Configure the external trigger used as a trigger event for the 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)Get actual external trigger source.
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)Get actual external trigger filter.
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)Get actual external trigger polarity.
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)Set the source of the clock used by the LPTIM instance.
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)Get actual LPTIM instance clock source.
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)Configure the active edge or edges used by the counter when the LPTIM is clocked by an external clock source.
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)Get actual clock polarity.
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)Get actual clock digital filter.
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)Configure clock polarity in encoder mode.
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)Get clock polarity in encoder mode.
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)Enables or disables the encoder mode.
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)Enables or disables the Non-encoder mode.
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)Indicates whether the LPTIM operates in encoder mode.
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)Indicates whether the LPTIM operates in Non-encoder mode.
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)Clear the specified LPTIM Interrupt status flags.
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)Inform application whether the specified LPTIM interrupt has occurred.
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)Enables or disables the specified LPTIM interrupts.
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)Indicates whether the specified LPTIM interrupts is enabled.
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)Select the connection for Input1 of LPTIM.
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)Select the connection for Input2 of LPTIM.
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 descriptions
This section covers the 5 LPTIM examples: LPTIM_ENC and LPTIM_NENC drive encoder waveforms on external pins and count the resulting edges, LPTIM_PulseCounter counts external pulses, LPTIM_PWM outputs a PWM waveform on PC1, and LPTIM_WakeUp wakes the MCU from STOP mode with a compare-match interrupt. All examples use LSI as the LPTIM1 clock source; except for LPTIM_PWM on PC1, the input pins are PC0/PC2 and the output pins are PB0/PB1.
5.19.3.1 LPTIM_ENC Encoder mode count and waveform output
Example directory: projects/n32h49x_EVAL/examples/LPTIM/LPTIM_ENC/
Enable LSI and select the LPTIM1 clock→Configure the IN1/IN2 and OUT1/OUT2 pins→Configure encoder mode with the auto-reload and compare values→Start continuous counting→Generate 20 encoder waveform sets and read back the counter value to verify
| Item | Source analysis |
|---|---|
| Function | LPTIM1 runs in encoder mode clocked by LSI, with IN1 on PC0, IN2 on PC2, and OUT1/OUT2 on PB0/PB1;main generates 20 encoder waveform sets, then reads the counter value, expects encCNT to be 40, and prints a success or failure message. |
| Source files | src/main.c: main, LptimInOutPinConfig, LptimEncodeConfig, LptimEncodeWaveOutput;inc/main.h: includes n32h49x_lptim.h, n32h49x_gpio.h, n32h49x_rcc.h, n32h49x_pwr.h, delay.h, log.h;src/n32h49x_it.c, inc/n32h49x_it.h: interrupt handlers (exception handling only);readme.txt: function, environment, and usage notes;MDK-ARM/, EWARM/: Keil and IAR projects. |
| Key configuration | Clock: enable the PWR clock and backup-register access, turn on LSI and wait for it to stabilize, and select LSI as the LPTIM1 clock with RCC_ConfigLPTIM1Clk(RCC_LPTIMCLK_SRC_LSI);Pins: IN1/IN2 (PC0/PC2) as AF5 inputs, OUT1/OUT2 (PB0/PB1) as push-pull outputs; LPTIM: prescaler LPTIM_PRESCALER_DIV1, internal clock, LPTIM_EncoderModeCmd(ENABLE), encoder polarity LPTIM_ENCODER_MODE_RISING, ARR 15000, CMP 10000;Start: LPTIM_StartCounter(LPTIM1, LPTIM_OPERATING_MODE_CONTINUOUS). |
| Program and data path | main first enables LSI and configures the LPTIM1 clock, then calls LptimInOutPinConfig and LptimEncodeConfig;it then starts counting in continuous mode and calls LptimEncodeWaveOutput(20) to toggle PB0/PB1 set by set and produce the encoder waveform;finally it reads the counter value with LPTIM_GetCounterValue, when encCNT equals 40 the check passes, otherwise it prints a failure. |
| Main APIs | LPTIM_DeInit, LPTIM_SetPrescaler, LPTIM_SetClockSource, LPTIM_EncoderModeCmd, LPTIM_SetEncoderModeClockPolarity, LPTIM_Cmd, LPTIM_SetAutoReloadValue, LPTIM_SetCompareValue, LPTIM_StartCounter, LPTIM_GetCounterValue |
| Verification | After downloading and running, observe the encoder waveform on PB0/PB1 with an oscilloscope; the serial port printing a success message means the count check passed, and a failure message means the counter value is not 40. |
| Use note | The hardware platform is N32H497ZGL7_EVB; the development environment is KEIL MDK-ARM 5.34 or IAR EWARM 8.50.1; encoder mode requires an internal clock source and prescaler DIV1, and continuous mode must be entered before encoder mode is enabled. |
Core-code walkthrough and porting
The example configures LPTIM1 in encoder mode, counts the edges produced on the external inputs, and finally reads the counter value to verify.
Data path: RCC_ConfigLPTIM1Clk selects LSI → LptimInOutPinConfig configures the IN/OUT pins → LptimEncodeConfig sets encoder mode with the auto-reload and compare values → LPTIM_StartCounter starts continuous counting → LptimEncodeWaveOutput generates the encoder waveform → LPTIM_GetCounterValue reads the counter value
1. Enable LSI and select the LPTIM1 clock. main first enables the PWR clock and backup-register access, turns on LSI and waits for it to stabilize, then configures LSI as the LPTIM1 clock source and enables the LPTIM1 bus clock.
/* 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);
When porting, keep the LSI enable and RCC_ConfigLPTIM1Clk; if you switch to another LPTIM, adjust the peripheral clock enable macro accordingly.
2. Configure the input and output pins. LptimInOutPinConfig configures PC0 and PC2 as AF5 inputs for IN1/IN2, and PB0 and PB1 as push-pull outputs for 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);
}
When changing pins, keep alternate function AF5 and the input/output direction; use push-pull output for OUT1/OUT2 so the encoder edges are easy to drive.
3. Configure encoder mode. LptimEncodeConfig resets LPTIM1, sets the prescaler, internal clock, encoder mode, and polarity, enables LPTIM1, and then writes the auto-reload and compare values.
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);
}
Encoder mode requires an internal clock source and prescaler DIV1; the auto-reload value must be greater than the compare value.
4. Start counting and verify. main starts counting in continuous mode, generates 20 encoder waveform sets, and reads the counter value to print the result.
/* 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);
Each encoder cycle corresponds to two counting edges, so 20 waveform sets correspond to 40 counts; changing the number of waveform sets changes the expected counter value proportionally.
Corresponding source files: src/main.c
5.19.3.2 LPTIM_NENC Non-encoder mode count and waveform output
Example directory: projects/n32h49x_EVAL/examples/LPTIM/LPTIM_NENC/
Enable LSI and select the LPTIM1 clock→Configure the IN1/IN2 and OUT1/OUT2 pins→Configure non-encoder mode with the auto-reload and compare values→Start continuous counting→Generate 20 non-encoder waveform sets and read back the counter value to verify
| Item | Source analysis |
|---|---|
| Function | LPTIM1 runs in non-encoder mode clocked by LSI, with IN1 on PC0, IN2 on PC2, and OUT1/OUT2 on PB0/PB1;main generates 20 waveform sets, then reads the counter value, expects NencCNT to be 20, and prints a success or failure message. |
| Source files | src/main.c: main, LptimInOutPinConfig, LptimNoEncodeConfig, LptimNoEncodeWaveOutput;inc/main.h: includes n32h49x_lptim.h, n32h49x_gpio.h, n32h49x_rcc.h, n32h49x_pwr.h, delay.h, log.h;src/n32h49x_it.c, inc/n32h49x_it.h: interrupt handlers (exception handling only);readme.txt: function, environment, and usage notes;MDK-ARM/, EWARM/: Keil and IAR projects. |
| Key configuration | Clock: enable the PWR clock and backup-register access, turn on LSI and wait for it to stabilize, and select LSI as the LPTIM1 clock with RCC_ConfigLPTIM1Clk(RCC_LPTIMCLK_SRC_LSI);Pins: IN1/IN2 (PC0/PC2) as AF5 inputs, OUT1/OUT2 (PB0/PB1) as push-pull outputs; LPTIM: prescaler LPTIM_PRESCALER_DIV1, internal clock, LPTIM_NoEncoderModeCmd(ENABLE), encoder polarity LPTIM_ENCODER_MODE_FALLING, ARR 15000, CMP 10000;Start: LPTIM_StartCounter(LPTIM1, LPTIM_OPERATING_MODE_CONTINUOUS). |
| Program and data path | main first enables LSI and configures the LPTIM1 clock, then calls LptimInOutPinConfig and LptimNoEncodeConfig;it then starts counting in continuous mode and calls LptimNoEncodeWaveOutput(20) to toggle PB0/PB1 set by set;finally it reads the counter value with LPTIM_GetCounterValue, when NencCNT equals 20 the check passes, otherwise it prints a failure. |
| Main APIs | LPTIM_DeInit, LPTIM_SetPrescaler, LPTIM_SetClockSource, LPTIM_NoEncoderModeCmd, LPTIM_SetEncoderModeClockPolarity, LPTIM_Cmd, LPTIM_SetAutoReloadValue, LPTIM_SetCompareValue, LPTIM_StartCounter, LPTIM_GetCounterValue |
| Verification | After downloading and running, observe the waveform on PB0/PB1 with an oscilloscope; the serial port printing a success message means the count check passed, and a failure message means the counter value is not 20. |
| Use note | The hardware platform is N32H497ZGL7_EVB; the development environment is KEIL MDK-ARM 5.34 or IAR EWARM 8.50.1; non-encoder mode likewise requires an internal clock source and prescaler DIV1, and continuous mode must be entered before it is enabled. |
Core-code walkthrough and porting
The example configures LPTIM1 in non-encoder mode, counts the edges produced on the external inputs, and finally reads the counter value to verify.
Data path: RCC_ConfigLPTIM1Clk selects LSI → LptimInOutPinConfig configures the IN/OUT pins → LptimNoEncodeConfig sets non-encoder mode with the auto-reload and compare values → LPTIM_StartCounter starts continuous counting → LptimNoEncodeWaveOutput generates the waveform → LPTIM_GetCounterValue reads the counter value
1. Enable LSI and select the LPTIM1 clock. main enables the PWR clock and backup-register access, turns on LSI and waits for it to stabilize, then configures LSI as the LPTIM1 clock source.
/* 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);
When porting, keep the LSI enable and RCC_ConfigLPTIM1Clk, and adjust the bus clock enable macro for the peripheral actually used.
2. Configure the input and output pins. LptimInOutPinConfig configures PC0 and PC2 as AF5 inputs, and PB0 and PB1 as push-pull outputs.
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);
}
When changing pins, keep alternate function AF5 and the input/output direction; use push-pull output so the edges are easy to drive.
3. Configure non-encoder mode. LptimNoEncodeConfig resets LPTIM1, sets the prescaler, internal clock, non-encoder mode, and polarity, enables LPTIM1, and then writes the auto-reload and compare values.
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);
}
Compared with encoder mode, only the mode selection and the polarity differ; the auto-reload value must be greater than the compare value.
4. Start counting and verify. main starts counting in continuous mode, generates 20 waveform sets, and reads the counter value to print the result.
/* 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);
In non-encoder mode each waveform set corresponds to one count, so 20 waveform sets correspond to a counter value of 20.
Corresponding source files: src/main.c
5.19.3.3 LPTIM_PulseCounter External pulse counting
Example directory: projects/n32h49x_EVAL/examples/LPTIM/LPTIM_PulseCounter/
Enable LSI and select the LPTIM1 clock→Configure the IN1 (PC0) and OUT1 (PB0) pins→Set the counter to external mode and configure the clock polarity→Start continuous counting→Generate 10 pulse sets and read back the counter value to verify
| Item | Source analysis |
|---|---|
| Function | LPTIM1 runs in external counting mode, with IN1 on PC0 as the external pulse input and OUT1 on PB0 to generate pulses;main generates 10 pulse sets, then reads the counter value, expects tempCNT to be 10, and prints a success or failure message. |
| Source files | src/main.c: main, LptimInOutPinConfig, LptimPWMOutput;inc/main.h: includes n32h49x_lptim.h, n32h49x_gpio.h, n32h49x_rcc.h, n32h49x_pwr.h, delay.h, log.h;src/n32h49x_it.c, inc/n32h49x_it.h: interrupt handlers (exception handling only);readme.txt: function, environment, and usage notes;MDK-ARM/, EWARM/: Keil and IAR projects. |
| Key configuration | Clock: enable the PWR clock and backup-register access, turn on LSI and wait for it to stabilize, and select LSI as the LPTIM1 clock with RCC_ConfigLPTIM1Clk(RCC_LPTIMCLK_SRC_LSI);Pins: IN1 (PC0) as an AF5 input, OUT1 (PB0) as a push-pull output; LPTIM: LPTIM_SetCounterMode(LPTIM1, LPTIM_COUNTER_MODE_EXTERNAL), LPTIM_ConfigClock(LPTIM1, LPTIM_CLK_FILTER_NONE, LPTIM_CLK_POLARITY_RISING) to count on the rising edge, ARR 60000;Start: LPTIM_StartCounter(LPTIM1, LPTIM_OPERATING_MODE_CONTINUOUS). |
| Program and data path | main enables LSI and configures the LPTIM1 clock, and calls LptimInOutPinConfig to set up the pins;it then resets LPTIM1, sets external counting mode, configures the rising-edge clock polarity, enables the timer, writes ARR, and starts in continuous mode; LptimPWMOutput(10) generates 10 pulse sets on PB0, and LPTIM_GetCounterValue reads the counter value and compares it with 10. |
| Main APIs | LPTIM_DeInit, LPTIM_SetCounterMode, LPTIM_ConfigClock, LPTIM_Cmd, LPTIM_SetAutoReloadValue, LPTIM_StartCounter, LPTIM_GetCounterValue |
| Verification | After downloading and running, observe the pulse sequence on PB0 with an oscilloscope; the serial port printing a success message means the counter value equals 10, and a failure message means the count does not match. |
| Use note | The hardware platform is N32H497ZGL7_EVB; the development environment is KEIL MDK-ARM 5.34 or IAR EWARM 8.50.1; the counter mode can only be switched while LPTIM is disabled, so the example sets it right after the reset. |
Core-code walkthrough and porting
The example configures LPTIM1 in external pulse-counting mode, counts the pulses on IN1, and verifies the total.
Data path: RCC_ConfigLPTIM1Clk selects LSI → LptimInOutPinConfig configures IN1/OUT1 → LPTIM_SetCounterMode selects external counting → LPTIM_ConfigClock selects the rising edge → LPTIM_StartCounter starts counting → LptimPWMOutput generates pulses → LPTIM_GetCounterValue reads the counter value
1. Enable LSI and select the LPTIM1 clock. main enables the PWR clock and backup-register access, turns on LSI and waits for it to stabilize, then configures LSI as the LPTIM1 clock source.
/* 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);
When porting, keep the LSI enable and RCC_ConfigLPTIM1Clk; in external counting mode LSI only provides the LPTIM base clock.
2. Configure the input and output pins. LptimInOutPinConfig configures PC0 as an AF5 input for the external pulse input, and PB0 as a push-pull output to generate pulses.
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);
}
When changing pins, keep PC0 as an AF5 input and PB0 as a push-pull output.
3. Configure external counting mode. main resets LPTIM1, sets external counting mode and selects the rising edge as the active clock, then enables the timer and writes the auto-reload value.
/* 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);
The counter mode can only be switched while LPTIM is disabled, so the example sets it right after LPTIM_DeInit; each rising edge counts once.
4. Generate pulses and verify. main generates 10 pulse sets on PB0, then reads the counter value and compares it with 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);
Because only the rising edge is counted, each complete pulse counts once, so 10 pulse sets correspond to a counter value of 10.
Corresponding source files: src/main.c
5.19.3.4 LPTIM_PWM PWM waveform output
Example directory: projects/n32h49x_EVAL/examples/LPTIM/LPTIM_PWM/
Enable LSI and select the LPTIM1 clock→Configure PC1 as an alternate-function push-pull output→Configure the PWM waveform and polarity→Enable the timer and set the auto-reload and compare values→Start in continuous mode and output PWM on PC1
| Item | Source analysis |
|---|---|
| Function | LPTIM1 outputs a PWM waveform on PC1 clocked by LSI, using regular polarity; the auto-reload value 600 and the compare value 300 set the period and duty cycle, and the output is observed with an oscilloscope after the start. |
| Source files | src/main.c: main, LptimOutPinConfig;inc/main.h: includes n32h49x_lptim.h, n32h49x_gpio.h, n32h49x_rcc.h, n32h49x_pwr.h, log.h;src/n32h49x_it.c, inc/n32h49x_it.h: interrupt handlers (exception handling only);readme.txt: function, environment, and usage notes;MDK-ARM/, EWARM/: Keil and IAR projects. |
| Key configuration | Clock: enable the PWR clock and backup-register access, turn on LSI and wait for it to stabilize, and select LSI as the LPTIM1 clock with RCC_ConfigLPTIM1Clk(RCC_LPTIMCLK_SRC_LSI);Pins: OUT1 (PC1) as an alternate-function push-pull output on AF5; LPTIM: LPTIM_SetWaveform(LPTIM1, LPTIM_OUTPUT_WAVEFORM_PWM), LPTIM_SetPolarity(LPTIM1, LPTIM_OUTPUT_POLARITY_REGULAR), ARR 600, CMP 300;Start: LPTIM_StartCounter(LPTIM1, LPTIM_OPERATING_MODE_CONTINUOUS). |
| Program and data path | main enables LSI and configures the LPTIM1 clock, and calls LptimOutPinConfig to configure PC1 as an alternate-function push-pull output;it then resets LPTIM1, selects the PWM waveform and regular polarity, enables the timer, writes ARR and CMP, and finally starts in continuous mode; the while(1) loop no longer touches the peripheral, and the output waveform is produced entirely by hardware. |
| Main APIs | LPTIM_DeInit, LPTIM_SetWaveform, LPTIM_SetPolarity, LPTIM_Cmd, LPTIM_SetAutoReloadValue, LPTIM_SetCompareValue, LPTIM_StartCounter |
| Verification | Observe the PWM waveform on PC1 with an oscilloscope or logic analyzer; changing ARR and CMP adjusts the period and the duty cycle respectively. |
| Use note | The hardware platform is N32H497ZGL7_EVB; the development environment is KEIL MDK-ARM 5.34 or IAR EWARM 8.50.1; a change to the output polarity takes effect immediately, so the default output level changes first during configuration. |
Core-code walkthrough and porting
The example configures LPTIM1 in PWM output mode and produces a continuous PWM waveform on PC1.
Data path: RCC_ConfigLPTIM1Clk selects LSI → LptimOutPinConfig configures PC1 as an alternate-function push-pull output → LPTIM_SetWaveform and LPTIM_SetPolarity configure the waveform and polarity → LPTIM_SetAutoReloadValue and LPTIM_SetCompareValue set the period and duty cycle → LPTIM_StartCounter starts the output
1. Enable LSI and select the LPTIM1 clock. main enables the PWR clock and backup-register access, turns on LSI and waits for it to stabilize, then configures LSI as the LPTIM1 clock source.
/* 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);
When porting, keep the LSI enable and RCC_ConfigLPTIM1Clk; the PWM period is determined by the LSI frequency together with ARR.
2. Configure the output pin. LptimOutPinConfig configures PC1 as an alternate-function push-pull output on AF5 for 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);
}
The output must be configured as alternate-function push-pull (AF_PP) with AF5, otherwise the LPTIM waveform cannot drive the pin.
3. Configure the PWM waveform. main resets LPTIM1, selects the PWM waveform and regular polarity, enables the timer, and writes the auto-reload and compare values.
/* 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);
The auto-reload value sets the period and the compare value sets the duty cycle; the auto-reload value must be greater than the compare value.
4. Observe the output. After the start the main loop no longer touches the peripheral, and the PWM is output continuously by hardware.
log_info("Use oscilloscope or logic analyzer to observe the PWM waveform output from PC1!!!\n");
while (1);
The output waveform is produced entirely by hardware with no CPU intervention, which suits continuous output in low-power scenarios.
Corresponding source files: src/main.c
5.19.3.5 LPTIM_WakeUp Low-power wake-up
Example directory: projects/n32h49x_EVAL/examples/LPTIM/LPTIM_WakeUp/
Initialize the LEDs and the power clock and enable LSI→Configure LPTIM1_WKUP_IRQn and EXTI Line 21→Configure the compare-match interrupt and the auto-reload and compare values→Start counting in continuous mode→The main loop toggles LED1 and enters STOP mode, woken by the compare match
| Item | Source analysis |
|---|---|
| Function | LPTIM1 is clocked by LSI, and its compare-match interrupt reaches LPTIM1_WKUP_IRQn through EXTI Line 21;the interrupt handler toggles LED2, the main loop toggles LED1 and then enters STOP mode, and the compare-match event wakes the MCU and reconfigures the 240 MHz system clock. |
| Source files | src/main.c: main, LedInit, LedOn, LedOff, LedBlink, LptimNvicConfig, SetSysClockToPLL;inc/main.h: includes 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: interrupt handlers (including LPTIM1_WKUP_IRQHandler);readme.txt: function, environment, and usage notes;MDK-ARM/, EWARM/: Keil and IAR projects. |
| Key configuration | Clock: enable the PWR clock and backup-register access, turn on LSI and wait for it to stabilize, and select LSI as the LPTIM1 clock with RCC_ConfigLPTIM1Clk(RCC_LPTIMCLK_SRC_LSI);Pins and interrupts: LED1 on PA3, LED2 on PB4, EXTI Line 21 triggered on the rising edge, and LPTIM1_WKUP_IRQn enabled;LPTIM: prescaler DIV1, LPTIM_ConfigInt(LPTIM1, LPTIM_COMP_MATCH_INT, ENABLE), ARR 35000, CMP 30000;Low power: enter PWR_EnterSTOPMode(PWR_REGULATOR_NORMAL, PWR_STOPENTRY_WFI), and after wake-up SetSysClockToPLL(RCC_PLL_SRC_HSI, 240000000U). |
| Program and data path | main initializes and turns on the LEDs, enables the PWR clock and LSI, and calls LptimNvicConfig to set up EXTI and NVIC;it then resets LPTIM1, sets the prescaler and the compare-match interrupt, enables the timer, writes ARR and CMP, and starts in continuous mode after a 2 s delay; the while(1) loop toggles LED1, then enters STOP mode to wait for the compare-match interrupt to wake it, and reconfigures the system clock after wake-up. |
| Main APIs | LPTIM_DeInit, LPTIM_SetPrescaler, LPTIM_ConfigInt, LPTIM_Cmd, LPTIM_SetAutoReloadValue, LPTIM_SetCompareValue, LPTIM_StartCounter |
| Verification | After downloading and running, LED2 toggles in the compare-match interrupt and LED1 toggles on each wake-up in the main loop; an ammeter shows the MCU switching periodically between STOP and run. |
| Use note | The hardware platform is N32H497ZGL7_EVB; the development environment is KEIL MDK-ARM 5.34 or IAR EWARM 8.50.1; the system clock is stopped in STOP mode, so it must be reconfigured after wake-up; the example delays 2 s before starting the count so that a debugger can be attached. |
Core-code walkthrough and porting
The example configures LPTIM1 as a compare-match wake-up source, and the main loop switches periodically between STOP mode and run mode.
Data path: LedInit initializes the LEDs → RCC_ConfigLPTIM1Clk selects LSI → LptimNvicConfig configures EXTI and NVIC → LPTIM_ConfigInt, LPTIM_SetAutoReloadValue, LPTIM_SetCompareValue configure the interrupt and compare values → LPTIM_StartCounter starts counting → the main loop enters STOP with PWR_EnterSTOPMode and restores the clock after wake-up with SetSysClockToPLL
1. Initialize the LEDs and clocks. main initializes and turns on LED1 (PA3) and LED2 (PB4), enables the PWR clock, backup-register access, and LSI, then configures LSI as the LPTIM1 clock source.
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);
When switching evaluation boards, only the LED port and pin macros need changing; the LSI enable and LPTIM1 clock configuration are the basis of the wake-up function.
2. Configure EXTI and NVIC. LptimNvicConfig configures EXTI Line 21 as a rising-edge interrupt and enables 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);
}
The EXTI trigger line and NVIC channel must match the LPTIM in use; when porting to LPTIM2, switch to the corresponding interrupt source. The comment on that line names EXTI Line 25, which does not match the EXTI_LINE21 the code actually configures: per the user manual, the LPTIM1 wakeup event connects to EXTI line 21 and LPTIM2 to line 22, and the EXTI of an LPTIM1/2 interrupt can only be configured for rising-edge triggering, so follow the code.
3. Configure LPTIM and start. After the reset it sets the prescaler and the compare-match interrupt, enables LPTIM1, writes ARR and CMP, and starts in continuous mode after a 2 s delay.
/* 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);
The interrupt enable must be configured while LPTIM is disabled; ARR and CMP determine the wake-up period.
4. The main loop enters STOP and wakes up. while(1) toggles LED1, requests entry into STOP mode, and switches the system clock back to 240 MHz after the compare-match interrupt wakes the MCU.
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);
}
}
The system clock stops in STOP mode, so the PLL and system clock must be reconfigured after wake-up, otherwise subsequent code runs at the wrong frequency.
Corresponding source files: src/main.c
5.19.4 Notes and troubleshooting
| Scenario | Check & action | Source |
|---|---|---|
| Configuration timing | Once LPTIM is enabled, registers such as LPTIMx_CFG and the interrupt-enable settings cannot be modified;disable LPTIM before changing the configuration, and enable it again afterwards. |
User manual |
| Auto-reload and compare | The auto-reload value must be greater than the compare value (ARR > CMP); after writing LPTIMx_ARR, wait for ARRUPD to be set before writing again, and after writing LPTIMx_CMP, wait for CMPUPD to be set. |
User manual |
| Reading the counter | When LPTIM is clocked by an asynchronous clock, reading LPTIMx_CNT may return unreliable values;read it twice consecutively and confirm the two results are identical. |
User manual |
| Encoder mode | Encoder mode requires an internal clock source and prescaler DIV1, and continuous mode must be configured before it is enabled; when both external edges are active, the internal clock frequency must be at least four times the external clock frequency. |
User manual |
| Trigger source selection | The LPTIM start trigger is selected by LPTIMx_CFG.TRGSEL[3:0]: 0 is LPTIMx_ETR (external GPIO), 1 is RTC_ALARM_A, 2 is RTC_ALARM_B, and 3 to 5 are RTC_TAMP1 to RTC_TAMP3;6 to 15 are reserved. |
User manual |
| Trigger and clock filtering | Digital filtering requires an internal clock source; before configuring a filter for the trigger or the external clock, make sure LPTIM has an available internal clock. |
User manual |
| Timeout function | The first trigger event starts the timer, and later trigger events reset the counter and restart it; a trigger event that arrives when the timer is already started is ignored. |
User manual |