PV198 – One-chip Controllers, Timers / Dávid Danaj 1 PV198 – One-chip Controllers Timers PV198 – One-chip Controllers, Timers / Dávid Danaj 2 Content 1.What is Timer 2.What is it used for 3.How does it work 4.FRDM-K66F Timers 1.Periodic interrupt timers (PIT) 5.Code 6.Application § PV198 – One-chip Controllers, Timers / Dávid Danaj 3 What is Timer §Timer / Counter §Timer – measure time intervals §Counter – count events § § § § PV198 – One-chip Controllers, Timers / Dávid Danaj 4 What is it used for §Using Timer vs Delay § §examples § § § § § PV198 – One-chip Controllers, Timers / Dávid Danaj 5 https://www.nxp.com/docs/en/reference-manual/K66P144M180SF5RMV2.pdf How does it work §PIT – Periodic Interrupt Timer FRDM-K66F Timers §Programmable delay block (PDB) §Flexible timer modules (FTM) §Low Power TPM (TPM) §Periodic interrupt timers (PIT) §Low-power timer (LPTimer) §Carrier modulator timer (CMT) §Real-time clock (RTC) §IEEE 1588 timers PV198 – One-chip Controllers, Timers / Dávid Danaj 6 Periodic interrupt timers (PIT) – Features §4 timers §Ability of timers to generate DMA trigger pulses §Ability of timers to generate interrupts §Independent timeout periods for each timer §Chaining of timers PV198 – One-chip Controllers, Timers / Dávid Danaj 7 Periodic interrupt timers (PIT) § PV198 – One-chip Controllers, Timers / Dávid Danaj 8 Periodic interrupt timers (PIT) §Set value to a timer register Timer Load Value Register (PIT_LDVALn) § §The timer will count down until it reaches 0, then it will generate an interrupt and load this register value again PV198 – One-chip Controllers, Timers / Dávid Danaj 9 Periodic interrupt timers (PIT) §Which value to set? §Example: §Interrupt every 0.5 seconds §Bus clock: 60 MHz -> clock period = 16.67 ns § §LDVAL trigger = (period / clock period) - 1 §0.5 s / 16.67 ns = 30 000 000 - 1 = 29 999 999 cycles § PV198 – One-chip Controllers, Timers / Dávid Danaj 10 Code § void PIT_1_init(void) { § /* Initialize the PIT. */ § PIT_Init(PIT_1_PERIPHERAL, &PIT_1_config); § /* Set channel 0 period to 3 s (180000000 ticks). */ § PIT_SetTimerPeriod(PIT_1_PERIPHERAL, kPIT_Chnl_0, PIT_1_0_TICKS); § /* Enable interrupts from channel 0. */ § PIT_EnableInterrupts(PIT_1_PERIPHERAL, kPIT_Chnl_0, kPIT_TimerInterruptEnable); § /* Enable interrupt PIT_1_0_IRQN request in the NVIC */ § EnableIRQ(PIT_1_0_IRQN); § /* Start channel 0. */ § PIT_StartTimer(PIT_1_PERIPHERAL, kPIT_Chnl_0); § } 11 PV198 – One-chip Controllers, Timers / Dávid Danaj Application §Goal: §Write an application that prints text to console every 1 second §Update it to toggle LED every 0.5 seconds PV198 – One-chip Controllers, Timers / Dávid Danaj 12 Homework §Write interrupt base button debouncing. §Use GPIO interrupt to detect first edge §When GPIO is triggered start Timer and wait for constant time §When Timer interrupt triggers, check GPIO value. §Add Green LED toggle when debounced button press signal is accepted. PV198 – One-chip Controllers, Timers / Dávid Danaj 13