Pulse Width Modulator example. - DEMO9S08DZ60

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Pulse Width Modulator example. - DEMO9S08DZ60

3,891 Views
Saga
Contributor II
Hi to all, im very new in the signals stuff, i'm using the DZ60 uC and i need to make an output modify the duty cycle of the pwm in PTD0 and output a signal and modify the frequency in the PTD1... Do you guys have an example code where you output a signal and modify the duty cycle and frequency to your will?

Thank you very much!
 
 
 
 
 
 
 
 
Added p/n to subject.


Message Edited by NLFSJ on 2008-02-23 01:19 PM
Labels (1)
7 Replies

1,659 Views
bigmac
Specialist III
Hello,
 
Is my assumption correct that the PWM output at PTD0 will have a fixed frequency, and the output at PTD1 would have a constant duty cycle, perhaps a square wave.
 
If this is true, I see that you are using different channels for the same TPM module.  Therefore, to produce a variable frequency at PTD1, and not also affect the frequency at PTD0, will require that you use output compare interrupts to generate the variable frequency signal.  The interrupt would need to occur on each transition of the output signal.  If the TPM module is not free-running, in order to provide the required PWM frequency on the other channel. the calculation of the time to the next interrupt will be significantly more complex.
 
Even more complication would arise if the variable frequency needed to be lower than the PWM frequency.  Ultimately, the degree of complication within the ISR is likely to limit the maximum frequency that can be achieved.
 
However, if you were to use different TPM modules for each output, this would give more flexibility, and probably result in simpler code.  In this case, the variable frequency output could utilize PWM mode, and would not require the processing of an interrupt each half-cycle of the output waveform.
 
To vary the frequency, the TPM modulo value would be changed, and the pulse width also changed proportionally, to maintain a constant duty cycle.  The alteration of the modulo value would need to happen immediately following TPM overflow.
 
Regards,
Mac
 


Message Edited by bigmac on 2008-02-23 08:06 AM
0 Kudos
Reply

1,659 Views
Saga
Contributor II
Hi big mac, it is not a big problem, i can use different PWM to eliminate the complexity, my biggest problem here, is that i don't know how to just output a frequency through a PWM and how to output a fixed frequency... I've never done this before, do you have some sample code that just output some frequency through any PWM, I'm using a MC9S08DZ60 uC, I'm kinda newbie with all the signals stuff, i don't understand it so well...

Thanks a lot for your help...
0 Kudos
Reply

1,659 Views
venkateshgv
Contributor II

Hello guys ...

How to sweep the frequency from 1KHz to 2KHz ...

Do youn guys any example code for this.?

Have a great day ahead!!!!!!!!!

Regards

VenkatGV

0 Kudos
Reply

1,659 Views
Ake
Contributor II
Hi,
Here is a TPM used for generating a variable pulse width/period.
I have tried it on a DEMO9S08DZ60, and there is an oscilloscope photo in the subdirectory "source".
It uses the overflow interrupt and I have only included 3 different output values, but I hope it shows how it can be done.
Hope it can be of any use.
 
Regards,
Ake
0 Kudos
Reply

1,659 Views
Saga
Contributor II
Hi Ake, Thanks for your help...

However i didn't find any code in the example you gave me, this is the main function:

Code:
void main(void) {  EnableInterrupts; /* enable interrupts */  /* include your code here */  for(;;) {    __RESET_WATCHDOG(); /* feeds the dog */  } /* loo

 i did see the oscilloscope image you told me, probably you did not attach the correct source... Or where are the TPM's being configured? Thats what i'm really lost...

Thanks for helping!

0 Kudos
Reply

1,659 Views
allawtterb
Contributor IV
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

1,659 Views
Saga
Contributor II
Hi allawtterb,

Thanks a lot for your instructions, that's what i didn't know, how to configurate the registers!!

You gave that little push i needed :smileytongue:

Sorry but this is my very first time im working with signals stuff, I've experience in programming but not at so low level, thanks!
0 Kudos
Reply