How do I set the TPM prescaler?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How do I set the TPM prescaler?

1,668 Views
clivepalmer
Contributor III

HI,

I have been trying to flash an LED in a TPM interrupt at 5HZ using a 48MHZ MCGFLLCLK. I cannot seem to set the TPM0 prescaler to divide down the MCGFLLCLK. I'm clearly doing something wrong and maybe not figured out the TPM fully yet.

My code is below. Note I have not set the prescaler and TPM0_C0V value to the correct values for 5HZ operation yet. Any help or pointwers greatly appreciated, as its driving me mad.

int timerInit()

{

NVIC_ICER = 1 << (17%32);

SIM_SOPT2 &= ~(SIM_SOPT2_TPMSRC_MASK);

SIM_SOPT2 |= SIM_SOPT2_TPMSRC(1); // MCGFLLCLK is source

SIM_SCGC6 = SIM_SCGC6_TPM0_MASK;  // enable clock for Uart0

TPM0_SC = 0;

TPM0_SC = TPM_SC_PS(7);

TPM0_MOD = 0xFFFF;

TPM0_CNT = 0;

TPM0_SC = TPM_SC_TOIE_MASK|TPM_SC_CMOD(1); // NB Default to 'up-counting' mode.

TPM0_C0SC = TPM_CnSC_MSB_MASK| TPM_CnSC_ELSB_MASK; // MSB:MSA 1:0 Edge Aligned mode (A defaults to 0)

//TPM0_C0SC |= TPM_CnSC_CHIE_MASK; //channel interrupt enable

TPM0_C0V = 0x00;

NVIC_ICPR |= 1 << (17%32);
}

void FTM0_IRQHandler (void)

{

tick = TPM0_CNT;

TPM0_SC |= TPM_SC_TOF_MASK; 

TPM0_C0V = 0x1FFF

TPM0_C0SC |= TPM_STATUS_CH0F_MASK;

NegLED();

}

0 Kudos
Reply
2 Replies

1,314 Views
mjbcswitzerland
Specialist V

Clive

I think that you are overwriting the prescaler setting which you first set to 7 [TPM0_SC = TPM_SC_PS(7);] and later set back to 0 with TPM0_SC = TPM_SC_TOIE_MASK|TPM_SC_CMOD(1);


Also note that you won't be able to get a 5Hz interrupt (200ms) since the longst delay that you can set from 48MHz clock is about 174ms (prescaler at max = 7 and divide at max 0xffff) .


Regards


Mark



0 Kudos
Reply

1,314 Views
clivepalmer
Contributor III

Thanks Mark.  What a plonker I am. Yes I missed a '|' .A case of not seeing the woods for the trees! That was indeed the problem.

Yes I realise that wont give me the 5Hz that I ideally would like.

0 Kudos
Reply