Timer 2 based PWM on ZX128ne

Discussion specific to the 24-pin ZX microcontrollers, e.g. ZX-24r, ZX-24s and ZX-24t.
Post Reply
sturgessb
Posts: 287
Joined: 25 April 2008, 6:34 AM
Location: Norwich, UK

Timer 2 based PWM on ZX128ne

Post by sturgessb »

Sorry if this thread is not in the right place, but I cant see a folder for zx-128.

Im looking to move the Radio Control PPM capture from the separate zx-24n onto the main zx-128 to save the need for the second processor.

However, because I am needing to output 4 PWM channels I don't have any timers left.

So this leads me onto a few questions.

Reading App note 216, it mentions that you can use Timer 2 for 8 bit PWM output. Does this support 1 channel or 2 channels of PWM output?

Also I see in that app note (Table 1) that using prescaler 1 (which i assume is default) that using Fast PWM a maximum of 225hz output is possible. I must admit i didn't know that and I have my PWM frequency set at 400hz, so does this mean its actually only operating at 225hz?

With this is mind, an 8 bit PWM would probably be better for me. The Motor controllers only support 255 steps of power so an 8bit resolution would be fine, and presumably would give me the ability to output at a true 400hz.

So if all the above is true I guess my two main questions are.

• Is it possible to use Timer 2 to output PWM on ZX-128ne, and if so where can i get further information on doing this.
• If that PWM output (from timer 2) is going to be 8 bit, what do i need to do to make the other timer (1 or 3) to operate in 8 bit resolution, to both get the same resolution and speed as the timer 2 one.

Thanks

Ben
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: Timer 2 based PWM on ZX128ne

Post by dkinzer »

sturgessb wrote:Sorry if this thread is not in the right place, but I cant see a folder for zx-128.
That's because Elba/ZBasic doesn't produce a ZX-128. You're welcome to post questions here, though. The ZX-1281 is probably the most like the ZX-128ne that you're using so that would probably be the best place to post questions that are specific to the device.
sturgessb wrote:Reading App note 216, it mentions that you can use Timer 2 for 8 bit PWM output. Does this support 1 channel or 2 channels of PWM output?
The answer to this question lies in the Atmel datasheet for the ATmega128. The section entitled "8-bit Timer/Counter2 with PWM" is the place to begin. In the block diagram, you'll see that there is but one waveform generation output labeled OCn. This means that there is only one PWM output associated with Timer2. In contrast, the ATmega1281 (on which the ZX-1281 is based) has two PWM outputs on Timer2.
sturgessb wrote:[S]o does this mean its actually only operating at 225hz?
No. If you call OpenPWM() specifying 400Hz, the PWM signal will be 400Hz. The table in AN216 indicates that the highest frequency at which you can get full 16-bit pulse width resolution in Phase/Frequency Correct mode is 225Hz. At 400 Hz, the pulse width resolution is 14 bits, calculated by the formula below. The Top value is the value calculated by OpenPWM() and written to the ICR register. For 400Hz, you may recall that Top is calculated to be 18432.

Code: Select all

Resolution = log(Top + 1) / log(2)
sturgessb wrote:Is it possible to use Timer 2 to output PWM on ZX-128ne, and if so where can i get further information on doing this.
Follow the general principles outlined in AN216 and consult the relevant portions of the datasheet to discover the register names and bit positions.
- Don Kinzer
sturgessb
Posts: 287
Joined: 25 April 2008, 6:34 AM
Location: Norwich, UK

Post by sturgessb »

Thanks Don

Ill give all that a go tonight. 1 Channel should be fine as I can use 3 on the other available timer.

With regards to making sure the PWM output on both timer 1 and timer 2 have the same resolution, if timer 1 is at 14bit resolution and timer 2 is 8 bit, is there any way or even need to reduce the resolution of timer 1 to 8 bit to match?

Or do i just make sure my hightime values are of 8bit resolution? i.e 0-255 steps.

Many thanks again

Ben
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

Post by mikep »

sturgessb wrote:Ill give all that a go tonight. 1 Channel should be fine as I can use 3 on the other available timer.
That was my thought too. I believe the second output on timer 2 is only available on the mega1280 and not the mega128/1281.
sturgessb wrote: With regards to making sure the PWM output on both timer 1 and timer 2 have the same resolution, if timer 1 is at 14bit resolution and timer 2 is 8 bit, is there any way or even need to reduce the resolution of timer 1 to 8 bit to match?

Or do i just make sure my hightime values are of 8bit resolution? i.e 0-255 steps.
You will probably have to write all of the register manipulation yourself for both timer 2 and timer1/3. This is how AN216 was developed before Don added the PWM APIs to ZBasic.

I would suggest you use timer 1/3 waveform generation mode 5 and timer 2 mode 3 which restricts the "top" value to 255. The output compare value has to then be set to a value between 0 and 255 to give the required 0 to 100% duty cycle for the PWM.
Mike Perks
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

sturgessb wrote:s there any way or even need to reduce the resolution of timer 1 to 8 bit to match?
I would think it is not necessary.

Timer1 has a maximum PWM resolution of 16 bits and Timer2 has a maximum PWM resolution of 8 bits. In both cases, the actual maximum will be less if the ICR value is less than 65535 (for Timer1) or 255 (for Timer2). This resolution refers only to how precisely you can set the duty cycle of the PWM waveform. The worst-case resolution occurs when the ICR value is 3, giving a PWM resolution of 2 bits. In this case the only possible values of OCR (which control the duty cycle) are shown in the table below. Note that N-bit PWM resolution implies 2^(N-1) distinct duty cycle values are available.

[table][mrow]OCR value[mcol]Duty Cycle
[row]0[col]0%
[row]1[col]33.3%
[row]2[col]66.7%
[row]3[col]100%
[/table]
sturgessb wrote:[D]o i just make sure my hightime values are of 8bit resolution? i.e 0-255 steps.
You may, if you wish, artificially limit the number of distinct duty cycle values that you use. This has the effect of reducing the PWM resolution.
- Don Kinzer
sturgessb
Posts: 287
Joined: 25 April 2008, 6:34 AM
Location: Norwich, UK

Post by sturgessb »

Cool so i guess the simplest way would be to calculate my hightime value in 8 bit resolution for timer 2 and then just scale it up to 14 bit for timer 1.

There ought to be a buy Don and Mike a beer button on this site (well, and not to mention all the other kind and helpful folk!)

Ben
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

mikep wrote:I believe the second output on timer 2 is only available on the mega1280 and not the mega128/1281.
That is correct but on the ZX-1281 and ZX-1280, the roles of Timer0 and Timer2 are reversed from what they are with the other ZX devices (including the ZX-128e and ZX-128ne). On the ZX-1281/ZX-1280, Timer2 is used for the RTC (and thus not available for other purposes) and Timer0 is used for the software UARTS. If none of COM3-COM6 are being used, Timer0 can be used for PWM and otherwise. On both these of devices, Timer0 has OC0A and OC0B available, meaning that you can have two 8-bit PWM channels.
- Don Kinzer
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

Timer 2 based PWM on ZX128ne

Post by mikep »

ZX-24 wrote:
mikep wrote: I believe the second output on timer 2 is only available on the mega1280 and not the mega128/1281. That is correct but on the ZX-1281 and ZX-1280, the roles of Timer0 and Timer2 are reversed from what they are with the other ZX devices (including the ZX-128e and ZX-128ne). On the ZX-1281/ZX-1280, Timer2 is used for the RTC (and thus not available for other purposes) and Timer0 is used for the software UARTS. If none of COM3-COM6 are being used, Timer0 can be used for PWM and otherwise. On both these of devices, Timer0 has OC0A and OC0B available, meaning that you can have two 8-bit PWM channels.



- Don Kinzer



Mike Perks
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

Post by mikep »

sturgessb wrote:Cool so i guess the simplest way would be to calculate my hightime value in 8 bit resolution for timer 2 and then just scale it up to 14 bit for timer 1.
You could do that but my thought was to make life easy and just do all of the PWMs using 8 bits.
sturgessb wrote:There ought to be a buy Don and Mike a beer button on this site (well, and not to mention all the other kind and helpful folk!)
Be careful what you say :) I was born in England and visit every few years. My home town is in Essex which is not too far away from you :D
Mike Perks
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

Post by mikep »

dkinzer wrote:
mikep wrote:I believe the second output on timer 2 is only available on the mega1280 and not the mega128/1281.
That is correct but on the ZX-1281 and ZX-1280, the roles of Timer0 and Timer2 are reversed from what they are with the other ZX devices (including the ZX-128e and ZX-128ne). On the ZX-1281/ZX-1280, Timer2 is used for the RTC (and thus not available for other purposes) and Timer0 is used for the software UARTS. If none of COM3-COM6 are being used, Timer0 can be used for PWM and otherwise. On both these of devices, Timer0 has OC0A and OC0B available, meaning that you can have two 8-bit PWM channels.
Atmel make great microcontrollers but unfortunately they are always changing something so no 2 chips are ever quite compatible.

Don and I had a lengthy discussion about the pros and cons of timer 0 versus timer 2 on the mega128 and mega1281. I did think about using timer 2 on the mega128 for the RTC thus leaving two PWM channels on timer 0. However you then lose other capabilities such as being able to create a modulated signal using Timer 1 and Timer 2 (see page 162 in the ATmega128 datasheet). It also turns out that the second timer 0 output (OC0B) is on PG5 which is an I/O pin not available to the 40-pin packaging of the Oak Micros devices.
Mike Perks
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: Timer 2 based PWM on ZX128ne

Post by dkinzer »

sturgessb wrote:Is it possible to use Timer 2 to output PWM [...]
I have posted a set of 8-bit PWM routines in the Files Forum that use either Timer2 or Timer0 and support either 1 or 2 channels, both depending on the underlying processor.
- Don Kinzer
Post Reply