【MCXA156开发板测评】-3-LPTMR体验
[复制链接]
本帖最后由 慕容雪花 于 2025-1-10 07:04 编辑
MCXA156片上定时器外设可谓是非常丰富,有Standart Counter/Timers 即CTimer, 还有Windowed Watchdog Timer,Micro-Tick Timer, OS Event Timer,Wake Timer。
在NXP的论坛上,也找到了一些对不同种类的NXP片上定时器的介绍:链接
1)SCTimer/PWM: can generate complicated PWM signal to control even motor, have capture function, can generate interrupt with programmable interval, can be a frequency divider.
2)CTimer: can generate simple PWM signal, can generate interrupt with programmable interval, has capture function, can be a frequency divider.
3)Multi-Rate Timer (MRT): can only generate interrupt
4)Repetitive Interrupt Timer (RIT): can only generate interrupt
5)System tick timer (SYSTICK): can generate interrupt for core
6)Micro-tick Timer (UTICK): can generate interrupt, and have capture function
本次体验一下使用NXP的工具全家桶MCUXPRESSO IDE配合内置的MCU CONFIG TOOL来快速制作一个1ms时基的中断服务程序,来闪烁小灯,串口输出。
首先点击使能LPTMR的时钟:
点击IDE右上角的USB符号,进入到外设配置界面。
之后可以输入LPTMR0进行搜索,添加该模块后,进入到配置界面,比如运行模式,频率,是否开启中断,是否要在初始化函数中启动LPTMR0等。
区别于别的IDE,在MCU ConfigTool里面还提供了ISR模板,可以点击“Copy to clipboard”来复制然后粘贴到自己对应用代码中。
如下所示即为添加了闪烁小灯与串口打印后的ISR。
/* LPTMR0_IRQn interrupt handler */
void LPTMR0_IRQHANDLER(void) {
uint32_t intStatus;
/* Reading all interrupt flags of status register */
intStatus = LPTMR_GetStatusFlags(LPTMR0_PERIPHERAL);
LPTMR_ClearStatusFlags(LPTMR0_PERIPHERAL, intStatus);
/* Place your code here */
static uint32_t cnt = 0;
cnt++;
if(cnt%1000 == 0){
/* Toggle pin connected to LED */
GPIO_PortToggle(GPIO3, 1u << 0);
PRINTF("\r\n GPIO3_0 RED LED TOOGLED, cnt = %d", cnt);
}
/* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F
Store immediate overlapping exception return operation might vector to incorrect interrupt. */
#if defined __CORTEX_M && (__CORTEX_M == 4U)
__DSB();
#endif
}
下面是IDE自动生成的LPTMR0的配置及初始化代码:
const lptmr_config_t LPTMR0_config = {
.timerMode = kLPTMR_TimerModeTimeCounter,
.pinSelect = kLPTMR_PinSelectInput_0,
.pinPolarity = kLPTMR_PinPolarityActiveHigh,
.enableFreeRunning = false,
.bypassPrescaler = true,
.prescalerClockSource = kLPTMR_PrescalerClock_3,
.value = kLPTMR_Prescale_Glitch_0
};
static void LPTMR0_init(void) {
/* Initialize the LPTMR */
LPTMR_Init(LPTMR0_PERIPHERAL, &LPTMR0_config);
/* Set LPTMR period */
LPTMR_SetTimerPeriod(LPTMR0_PERIPHERAL, LPTMR0_TICKS);
/* Configure timer interrupt */
LPTMR_EnableInterrupts(LPTMR0_PERIPHERAL, kLPTMR_TimerInterruptEnable);
/* Interrupt vector LPTMR0_IRQn priority settings in the NVIC. */
NVIC_SetPriority(LPTMR0_IRQN, LPTMR0_IRQ_PRIORITY);
/* Enable interrupt LPTMR0_IRQN request in the NVIC */
EnableIRQ(LPTMR0_IRQN);
/* Start the LPTMR timer */
LPTMR_StartTimer(LPTMR0_PERIPHERAL);
}
烧录,全速运行,看到串口大概1s输出一次。
换个带时间戳的串口助手,看的更细节:
|