To select the MCGIRCLK as the source clock for the TPM module you need to enable it first, this is done writing register MCG_C1[IRCLKEN]:
//Enables Internal Reference Clock
MCG_C1 |= MCG_C1_IRCLKEN_MASK;
#include "derivative.h" /* include peripheral declarations */
#define BLUE_LED_LOC (1<<1)
#define MCG_IRC_CLK 0x03000000
#define BLUE_LED_ON GPIOD_PCOR = BLUE_LED_LOC
#define BLUE_LED_OFF GPIOD_PSOR = BLUE_LED_LOC
#define BLUE_LED_TOGGLE GPIOD_PTOR = BLUE_LED_LOC
void Blue_LED_Init(void);
void TPM0_Init(void);
int main(void)
{
Blue_LED_Init();
TPM0_Init();
for(;;) {
while((TPM0_SC & 0x80) == 0) {}
TPM0_SC |= 0x80;
BLUE_LED_TOGGLE;
}
return 0;
}
/*
* Initialise the Blue LED (PTD1)
*
*/
void Blue_LED_Init(void)
{
SIM_SCGC5 |= SIM_SCGC5_PORTD_MASK; /* Turn on clock to PortD module */
PORTD_PCR1 = PORT_PCR_MUX(1); /* Set the PTD1 pin multiplexer to GPIO mode */
GPIOD_PDDR |= BLUE_LED_LOC; /* Set the pins direction to output */
GPIOD_PSOR = BLUE_LED_LOC; /*Turn off Blue LED*/
}
/*
* Initialise TPM0
*
*/
void TPM0_Init(void)
{
//Enables Internal Reference Clock
MCG_C1 |= MCG_C1_IRCLKEN_MASK;
SIM_SCGC6 |= SIM_SCGC6_TPM0_MASK; /* Enable clock to TPM0 module */
SIM_SOPT2 |= 0x03000000; /* Source clock to TPM0 module PLL select, MCGPLLCLK/2 */
TPM0_SC |= 0x00; /* Disable timer/counter during configuration, set up for up-counter */
TPM0_SC |= 0x02; /* Set Prescalar to DIV by 128 */
TPM0_MOD = 40960 - 1; /* Max modulous */
TPM0_SC |= 0x80; /* Clear TOF flag */
TPM0_SC |= 0x08; /* Enable counter free running mode */
}
And this will keep the LED toggling.
Earl.