KL0x - Timer PWM Interrupt

Showing results for 
Search instead for 
Did you mean: 

KL0x - Timer PWM Interrupt

Contributor III


Platform: KL02 dev board, MCUXpresso SDK

Problem: I´m trying to generate a sequence of 4kHz burst pulses using the timer in PWM mode. The IO outputs 4kHz a clock for 100ms. Next 100ms is off. Next 100ms is on. The idea is to drive a buzzer (beep...silence...beep...silence...beep...silence)

I´m able to generate the 4KHz clock out of the pin using TMP1 (PTA12 | TMP1_CH0). I could use the LPTMR or the TMP0 to turn on/off TMP1 at every 100ms, but they are already assigned to other tasks.

I was wondering if I could use the TMP1 interrupt handler to change the state of tpmParam.level (from kTPM_HighTrue to kTPM_NoPwmSignal) at every 100ms. So I was asuming the timer in PWM mode would behave like a periodic timer overflow, therefore after a X-number of the clocks the interrupt handler is reached where the timer parameter (e.g. tpmParam.level) could be changed.

In my code I wa trying to toggle the led that would represent the PWM output ON and OFF.

First question: is that feasable? If so could you please take a look at my code and suggest a change?

Thank you!

Andre VB

#define TPM1_HANDLER TPM1_IRQHandler

void TPM1_HANDLER(void)
/* Clear interrupt flag.*/
TPM_ClearStatusFlags(TPM1, kTPM_TimeOverflowFlag);
GPIO_PortToggle(GPIOB, 1 << 10);

int main(void)
/* Board pin, clock, debug console init */

tpm_config_t tpmInfo;
tpm_chnl_pwm_signal_param_t tpmParam;

/* Configure tpm params with frequency 4kHZ */
tpmParam.chnlNumber = (tpm_chnl_t)0U; // TIMER 1 at PTA12
tpmParam.level = kTPM_HighTrue;
tpmParam.dutyCyclePercent = 50;
/* Select the clock source for the TPM counter as MCGFLLCLK */
 tpmInfo.prescale = kTPM_Prescale_Divide_128;
 tpmInfo.useGlobalTimeBase = false;
 tpmInfo.enableDoze = false;
 tpmInfo.enableDebugMode = true;
 tpmInfo.enableReloadOnTrigger = false;
 tpmInfo.enableStopOnOverflow = false;
 tpmInfo.enableStartOnTrigger = false;
 tpmInfo.enablePauseOnTrigger = false;
 tpmInfo.triggerSelect = kTPM_Trigger_Select_0;
 tpmInfo.triggerSource = kTPM_TriggerSource_External;
/* Initialize TPM module */
TPM_Init(TPM1, &tpmInfo);

TPM_SetupPwm(TPM1, &tpmParam, 1U, kTPM_CenterAlignedPwm, 4000U, CLOCK_GetFreq(kCLOCK_McgFllClk));
TPM_EnableInterrupts(TPM1, kTPM_TimeOverflowInterruptEnable);  

TPM_StartTimer(TPM1, kTPM_SystemClock);

while (1)


Labels (2)
Tags (4)
0 Kudos
2 Replies

Contributor III


It works, as well as using TMP0/LPTMR to turn on/off TMP1 (in PWM mode). But what I was trying to do is to avoid using another time to perform the task. 

In summary: how to create a X-kHz burst pulses using a single PWM timer channel. Maybe it can´t be done, I would like to confirm that.

Thank you,

Andre VB

0 Kudos

NXP Employee
NXP Employee


You can still use the TPM0. For example , one task is 100ms.  task2 is 200ms.

You can set TPM0 generate interrupt at 100ms. And define a global variable like 'interruptCount'. Use it for judge if the task2 need to be run.  When you go to the interrupt the 'interruptCount' will be added. The task1 will run. When the 'interruptCount' is 2, the judgement is active, the task 2 will also run. And clear the 'interruptCount '

If the task2 time is smaller than the task, you can also do this.

Have a great day,

- If this post answers your question, please click the "Mark Correct" button. Thank you!

- We are following threads for 7 days after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.