Generating PWM Signals Using Timer/Pulse-Width Modulator (TPM) Module for the QE Microcontrollers
QE128 Quick Reference User Guide, Rev. 1.0
12-2
Freescale Semiconductor
12.2
PWM project for EVB
12.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 the TPM1 clock module enabled.
•
GPIO_Init – Configure PTE0 pin as output.
•
TPM_Init – TPM module configuration.
•
TPM_ISR — The PWM duty cycle is incremented and the PWM signal is shown in the PTE0 pin.
This example describes the initialization code for the TPM module using the PWM feature. The TPM
module uses channel 1 to generate a PWM signal. When Channel 1 interrupt is detected, the PWM
duty-cycle is incremented. When the counter reaches a value of 0x00F0, the counter value is reset to 1.
This part of the code is the MCU initialization. These instructions disable the watchdog, enable the Reset
option and backgroud pin. The system option register 1 (SOPT1) is used to configure the MCU. The
SCGC1 and SCGC2 are registers used for power saving consumption, here the bus clock to peripherals
can be enabled or disabled. In this example only the bus clock to the TPM module is active. The other
peripheral clocks are disabled.
void MCU_Init(void) {
SOPT1 = 0x23; // Watchdog disable. Stop Mode Enable. Background Pin enable.
// RESET pin enable
SCGC1 = 0x20; // Bus Clock to the TPM1 module is enabled
SCGC2 = 0x00; // Disable Bus clock to unused peripherals
}
This is the General Purpose Input/Output configuration. These code lines configure the pin directions for
the PTE port. In this example one LED is connected to the PTE port; therefore the 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
}
These line codes initialize the TPM module for the QE MCU. This application enables the TPM Channel
1 interrupt and configures the TPM module to work in the PWM mode. The MCU bus clock works at 4
MHz. This is the MCU default speed. This clock is divided by 128 and used in the TPM module.
void TPM_Init (void) {
TPM1MOD = 0x00FE; // Store 0x00FE value
TPM1C1SC = 0x68; // Channel 1 interrupt enable. PWM edge aligned
TPM1C1VH = 0x00; // TPM1C1V is a 16 bit register, for this example is only
// needed to store 0x0001
TPM1C1VL = 0x01; // in this register at the beggining because the next value
// will be increased by 1 in the ISR
TPM1SC = 0x0F; // TPM Clock Source is the bus rate clock. This bus is
// divided by 128
}