Hello,
I am trying to use TPM1 Module on MC9S08JM60 to generate an interrupt every second.
I am operating in PEE mode using 12MHz external crystal and bus frequency of 24MHz.
But since 24MHz seems too high to get 1 second interrupt usint TPM I'm using "Fixed system clock" source for TPM which runs at 1.5MHz since RDIV value is 8.
So, I figured, if I set TPM prescaler to divide by 32: 1.5MHz/32=46,875 or 0xB71B, all I need is to set Modulo value to 0xB71B and that should give me one overflow (TOF) per second?
No matter what value I set TPM1MOD to it seems to have no effect on terminal count. I mean, looking at TPM1CNT in emulator it counts up from 0 to about 32767 then counts from -32767 to 0 and repeats???
//*** SET-UP TIMER - TPM1 MODULE
TPM1SC_TOF = 0; //timer has not overflowed
TPM1SC_CLKSA = 0; //select fixed system clock (1.5MHz)
TPM1SC_CLKSB = 1;
TPM1SC_CPWMS =0;
TPM1SC_PS = 0b100; //clock source divided 16
TPM1MOD = 0x0FFF; //set counter modulus (terminal count)
TPM1SC_TOIE = 1; //enable TPM1 timer overflow interrupts
//*** Configure Multi-purpose Clock Generator (MCG) for 24MHz bus frequency
//Switching from FEI Mode to PEE Mode
//FEI --> FBE
MCGC2 = 0x36;
while(!MCGSC_OSCINIT); //loop until crystal has been initialized, OSCINIT = 1
DisableInterrupts; //disable interrupts so no extra steps occur in 1.5MHz range
MCGC1 = 0x98; //CLKS=10 (Ext. ref); RDIV=011 (12MHz/8=1.5MHz)
while(MCGSC_IREFST == 1); //loop until IREFST = 0; Ext. reference is source
while(MCGSC_CLKST != 0b10); //loop until external ref. clock is selected to feed MCGOUT
//FBE --> BLPE
MCGC2 = 0x3E; //LP = 1; BLPE MODE ON
EnableInterrupts;
MCGC3 = 0x48; //1.5MHz*32=48MHz; PLL is selected (prepared for PBE)
while(!MCGSC_PLLST); //loop until current source for PLLS clock is PLL; PLLST=1
//BLPE --> PBE
MCGC2 = 0x36; //LP = 0; BLPE MODE OFF, PBE MODE ENTERED
while(!MCGSC_LOCK); //loop until PLL acquired lock; LOCK=1
//PBE --> PEE
MCGC1 = 0x18; //CLKS = 00; output of PLL or FLL is selected
while(MCGSC_CLKST != 0b11); //loop until PLL output is selected to feed MCGOUT