MPC5744P定时器Timer(1)——PIT
Periodic Interrupt Timer(PIT)简介
PIT模块是一个定时器的数组,可以用来触发中断和DMA通道。
功能:
1.产生DMA触发脉冲;
2.产生中断;
3.可屏蔽中断;
4.多个独立的时间周期。
寄存器概述
PIT_MCR:
PIT_0.MCR.B.MDIS = 0; /* Enable PIT module. NOTE: PIT module must be */
/* enabled BEFORE writing to it's registers. */
/* Other cores will write to PIT registers so the */
/* PIT is enabled here before starting other cores. */
PIT_0.MCR.B.FRZ = 1; /* Freeze PIT timers in debug mode */
PIT_0_Init();
PIT_0.MCR.B.FRZ = 0; //Unfreeze timers
在初始化PIT前要使能PIT模块、停止PIT定时器。
PIT_LTMR64H、PIT_LTMR64L
当timer0和timer1链接起来时,读取64位寄存器获取当前时间。先读PIT_LTMR64H后读PIT_LTMR64L。
PIT_LDVALn
PIT_0.TIMER[1].LDVAL.R = LDVAL;
PIT的定时器起始值,当此值减为0时,触发中断,再次重新计数。
PIT_CVALn
读取当前计时器的值
PIT_TCTRLn
CHN链接PITChannel的timer;
TIE控制Timer Interrupt是否使能;
TEN使能Timer。
PIT_TFLGn
清楚中断标志;
在中断触发后,执行完中断操作后写入1,
PIT_0.TIMER[2].TFLG.R |= 1; /* Clear interrupt flag */
通常会在中断函数最后执行。
PIT中断点灯实验
初始化PIT0的channel0
PIT_0.MCR.B.MDIS = 0; /* Enable PIT module. NOTE: PIT module must be */
/* enabled BEFORE writing to it's registers. */
/* Other cores will write to PIT registers so the */
/* PIT is enabled here before starting other cores. */
PIT_0.MCR.B.FRZ = 1; /* Freeze PIT timers in debug mode */
PIT_0.TIMER[0].LDVAL.R = 40000000; /* Load # PIT clocks to count */
/* timeout= 40M PITclks x 4 sysclks/1 PITclk x 1 sec/160Msysck */
/* = 40M x 4 / 160M = 160/160 = 1 sec. */
PIT_0.TIMER[0].TCTRL.B.TIE = 1; /* Enable interrupt */
INTC_0.PSR[226].R = 0x800A;
PIT_0.TIMER[0].TCTRL.B.TEN = 1; /* enable channel */
PIT_0.MCR.B.FRZ = 0; //Unfreeze timers
在中断向量表内对应位置加入中断函数PIT0_isr
const uint32_t __attribute__ ((section (".intc_vector_table"))) IntcIsrVectorTable[]
中断函数的实现,反转LED,清空中断标志位。LED管脚控制详见GPIO(1)。
void PIT0_isr(void) {
LED1 = ~LED1; /* Toggle LED1 port */
PIT_0.TIMER[0].TFLG.R |= 1; /* Clear interrupt flag. w1c */
}