Hello
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 */
BOARD_InitPins();
BOARD_BootClockRUN();
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 */
CLOCK_SetTpmClock(1U);
/*
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;
*/
TPM_GetDefaultConfig(&tpmInfo);
/* Initialize TPM module */
TPM_Init(TPM1, &tpmInfo);
TPM_SetupPwm(TPM1, &tpmParam, 1U, kTPM_CenterAlignedPwm, 4000U, CLOCK_GetFreq(kCLOCK_McgFllClk));
TPM_EnableInterrupts(TPM1, kTPM_TimeOverflowInterruptEnable);
EnableIRQ(TPM1_IRQn);
TPM_StartTimer(TPM1, kTPM_SystemClock);
while (1)
{}