Here is a list of things you must do to start a simple PWM, you mentioned using it for Port D pins 1 and 2 which would be TPM2 so I will only talk about TPM2. The DZ60 contains another timer, TPM1 which can use Port D pins 2-7. Port D pin 0 would be Channel 0 and pin 1 would be Channel 1 for TPM2.
1) Select a clock source (bits 3 & 4 of TPM2SC) and a prescale factor (bits 0-2 of TPM2SC), a table for this can be found on page 327 of the MC9S08DZXX data sheet there is a link to this document at the bottom of the post.
TPM2SC = 0x09; // Use bus rate clock, set prescale factor of 2
2) Enable edge-aligned PWM through bits 4-5 of TPM2C0SC for channel 1 and TPM2C1SC for channel 2. Also use bits 2-3 to set each channel to be high-true or low-true (High true will have the channel be always high for a 100% duty cycle). Table for this is on page 330.
TPM2C0SC = 0x28; // Edge-aliged PWM, high-true
TPM2C1SC = 0x28;
3) Set the peiord of the PWM signal through the modulus register, comprised of 2 8-bit registers TPM2MODH and TPM2MODL of which the 16-bit value can be accessed through TPM2MOD. The modulus register will be the same for all TPM2 channels (as bigmac mentioned).
TPM2MOD = 0x1000; // Set period to 4096 * (bus clock/2)
4) Set the duty cycle of the PWM signal through the timer channel register value (16-bits made of 2 8-bit registers again), TPM2C0V. Setting it from 0 (0%) to the value in TPM2MOD + 1 (100%). You can set it higher than TPM2MOD but doing so will just give you a duty cycle of 100%.
TPM2C0V = 0x800; // Set duty cycle to 50%
TPM2C1V = 0x400; // Set duty cycle to 25%
Now you can change TPM2MOD to change the period at any point in time and change TPM2CXV to change the duty cycle for either channel as well.
Message Edited by allawtterb on
2008-02-22 09:57 PM