S32K144开发板——定时器的使用
主要内容 配置定时器周期性中断,在中断中对LED进行翻转,观察LED的变化,同时利用串口发送数据观察周期。
具体步骤 1、添加FTM中断外设 2、配置定时器 3、附上代码
#include "Cpu.h"
#include <string.h>
volatile int exit_code = 0;
#define Msg "Interrupt is triggered\r\n"
int flag;
void ftmTimerISR(void)
{
flag = 1;
PINS_DRV_TogglePins(PTD,1 << 15);
FTM_DRV_ClearStatusFlags(INST_FLEXTIMER_MC1,(uint32_t)FTM_TIME_OVER_FLOW_FLAG);
}
void delay(volatile int cycles)
{
while(cycles--);
}
int main(void)
{
ftm_state_t ftmStateStruct;
#ifdef PEX_RTOS_INIT
PEX_RTOS_INIT();
#endif
CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT,
g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_AGREEMENT);
FTM_DRV_Init(INST_FLEXTIMER_MC1, &flexTimer_mc1_InitConfig,&ftmStateStruct);
INT_SYS_InstallHandler(FTM0_Ovf_Reload_IRQn,&ftmTimerISR,(isr_t*) 0U);
INT_SYS_EnableIRQ(FTM0_Ovf_Reload_IRQn);
FTM_DRV_InitCounter(INST_FLEXTIMER_MC1, &flexTimer_mc1_TimerConfig);
FTM_DRV_CounterStart(INST_FLEXTIMER_MC1);
PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr);
I2C_MasterInit(&i2c1_instance,&i2c1_MasterConfig0);
LPUART_DRV_Init(INST_LPUART1, &lpuart1_State, &lpuart1_InitConfig0);
while(1)
{
if(flag==1)
{
LPUART_DRV_SendData(INST_LPUART1,(uint8_t *)Msg, strlen(Msg));
flag=0;
}
delay(1);
}
#ifdef PEX_RTOS_START
PEX_RTOS_START();
#endif
for(;;) {
if(exit_code != 0) {
break;
}
}
return exit_code;
}
4、FTM周期中断计算公式 重装载值/(主频/分频)单位:秒 例如:设置100ms的中断周期 以我们这里为例。37500(重装载值)/(48MHZ/128)=0.1秒=100ms 主频/分频=MCU运行频率 5、串口显示
|