Using the Output Compare function with the Timer/Pulse-Width Modulator (TPM) module for the QE Microcontrollers
QE128 Quick Reference User Guide, Rev. 1.0
13-2
Freescale Semiconductor
13.2
TPM Project for EVB
13.2.1
Code Example and Explanation
This example code is available from the Freescale Web site www.freescale.com
The project file contains the following functions:
•
main — Endless loop waiting for the TPM interrupt to occur.
•
MCU_Init – MCU initialization, watchdog disable and bus clock to the TPM1 module enabled.
•
GPIO_Init – Configure PTE0 pin as output.
•
TPM_Init – TPM module configuration.
•
TPM_ISR — The ISR instructions will toggle PTE0 pin.
This example describes the initialization code for the Timer-Pulse Width Modulator module using the
toggle on output compare feature. The firmware configures TPM module channel 3 to toggle a LED when
the counter counts up to 0xFFFF. The toggle instruction is executed within the ISR.
This part of code is the MCU initialization, this instructions disable the watchdog and enable the Reset
option and backgroud pin. The SOPT1 register is the System Option Register 1, and is used to configure
the MCU. SCGC1 and SCGC2 are registers used for power consumption save, where the bus clock to
peripherals can be enable or disable. In this example only the bus clock to the TPM module is active, the
others peripheral clocks are disable.
void MCU_Init(void) {
SOPT1 = 0x23; // Watchdog disable. Stop Mode Enable. Background Pin enable.
// RESET pin enable
SCGC1 = 0x80; // Bus Clock to the TPM3 module is enabled
SCGC2 = 0x00; // Disable Bus clock to unused peripherals
}
This is the General Purpose Input/Output configuration, this code lines configure the pin directions for
PTE port. In this example only one LED is connected to port PTE, for that reason PTE0 pin is configured
as output.
void GPIO_Init(void) {
PTEDD = 0x01; // Configure PTE0 pin as output
PTED = 0x00; // Put 0's in PTE port
}
This lines code initializes the Timer-Pulse Width Modulator module for the QE MCU. This application
enables TPM Channel 3 interrupt and configures the TPM module to work as toggle output on compare
mode. The MCU bus clock will work at 4MHz approximately (default operation), this clock is divided by
128 and used in the Timer-Pulse Width Modulator module.
void TPM_Init (void) {
TPM3MOD = 0xFFFF; // The counter counts up to 0xFFFF
TPM3C3V = 0x0000; // The channel interrupt will happen when counter matches
TPM3C3SC = 0x54; // Channel interrupt enabled and configured as toggle output on compare
TPM3SC = 0x0F; // TPM clock source is: Bus rate clock divided by 128
}