1、.c文件,该定时器配置为计数模式,每次定时周期到将响应中断
/* * lptmr.c * * Created on: 2020年8月19日 * Author: Mr.W */ #include "lptmr.h" #include "fsl_gpio.h" /*********************************************************************************************************************** * LPTMR0 initialization code **********************************************************************************************************************/ /* clang-format off */ /* TEXT BELOW IS USED AS SETTING FOR TOOLS ************************************* instance: - name: 'LPTMR0' - type: 'lptmr' - mode: 'LPTMR_GENERAL' - custom_name_enabled: 'false' - type_id: 'lptmr_2eeab91a1a42f8238f9ac768f18c65ae' - functional_group: 'BOARD_InitPeripherals' - peripheral: 'LPTMR0' - config_sets: - fsl_lptmr: - lptmr_config: - timerMode: 'kLPTMR_TimerModeTimeCounter' - pinSelect: 'ALT.0' - pinPolarity: 'kLPTMR_PinPolarityActiveHigh' - enableFreeRunning: 'false' - bypassPrescaler: 'true' - prescalerClockSource: 'kLPTMR_PrescalerClock_1' - clockSource: 'BOARD_BootClockRUN' - value: 'kLPTMR_Prescale_Glitch_0' - timerPeriod: '10000 us' - enableInterrupt: 'true' - interrupt: - IRQn: 'LPTMR0_IRQn' - enable_priority: 'false' - priority: '0' - enable_custom_name: 'false' * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR TOOLS **********/ /* clang-format on */ const lptmr_config_t LPTMR0_config = { .timerMode = kLPTMR_TimerModeTimeCounter, .pinSelect = kLPTMR_PinSelectInput_0, .pinPolarity = kLPTMR_PinPolarityActiveHigh, .enableFreeRunning = false, .bypassPrescaler = true, .prescalerClockSource = kLPTMR_PrescalerClock_1, .value = kLPTMR_Prescale_Glitch_0 }; void lptmr0_init(void) { /* Initialize the LPTMR */ LPTMR_Init(LPTMR0_PERIPHERAL, &LPTMR0_config); /* Set LPTMR period * Note : the parameter "ticks" of LPTMR_SetTimerPeriod should be equal or greater than 1. */ LPTMR_SetTimerPeriod(LPTMR0_PERIPHERAL, USEC_TO_COUNT(LPTMR0_USEC_COUNT, LPTMR0_CLK_FREQ)); /* Configure timer interrupt */ LPTMR_EnableInterrupts(LPTMR0_PERIPHERAL, kLPTMR_TimerInterruptEnable); /* 在使能LPTMR0_IRQn中断前需要先清除该中断,否则会出现打开定时器就进入中断的问题 */ NVIC_ClearPendingIRQ(LPTMR0_IRQn); /* Enable interrupt LPTMR0_IRQn request in the NVIC */ EnableIRQ(LPTMR0_IRQn); } void lptmr0_start(void) { LPTMR_StartTimer(LPTMR0_PERIPHERAL); } void lptmr0_stop(void) { LPTMR_StopTimer(LPTMR0_PERIPHERAL); } void LPTMR0_IRQHandler(void) { LPTMR_ClearStatusFlags(LPTMR0_PERIPHERAL, kLPTMR_TimerCompareFlag); GPIO_TogglePinsOutput(GPIOB, 1 << 18); }
2、.h文件
/* * lptmr.h * * Created on: 2020年8月19日 * Author: Mr.W */ #ifndef LPTMR_H_ #define LPTMR_H_ /*********************************************************************************************************************** * Included files **********************************************************************************************************************/ #include "fsl_common.h" #include "fsl_lptmr.h" #if defined(__cplusplus) extern "C" { #endif /* __cplusplus */ /*********************************************************************************************************************** * Definitions **********************************************************************************************************************/ /* Definitions for BOARD_InitPeripherals functional group */ /* BOARD_InitPeripherals defines for LPTMR0 */ /* Definition of peripheral ID */ #define LPTMR0_PERIPHERAL LPTMR0 /* Definition of the clock source frequency */ #define LPTMR0_CLK_FREQ 1000UL /* Definition of the prescaled clock source frequency */ #define LPTMR0_INPUT_FREQ 1000UL /* Definition of the timer period in us */ #define LPTMR0_USEC_COUNT 10000UL /* Definition of the timer period in number of ticks */ #define LPTMR0_TICKS 10UL /* LPTMR0 interrupt vector ID (number). */ #define LPTMR0_IRQN LPTMR0_IRQn /* LPTMR0 interrupt handler identifier. */ #define LPTMR0_IRQHANDLER LPTMR0_IRQHandler /*********************************************************************************************************************** * Global variables **********************************************************************************************************************/ extern const lptmr_config_t LPTMR0_config;
/*********************************************************************************************************************** * Initialization functions **********************************************************************************************************************/ void lptmr0_init(void); void lptmr0_start(void); void lptmr0_stop(void); #if defined(__cplusplus) } #endif #endif /* LPTMR_H_ */
#end