TIM based PWM on HC08GR8

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

TIM based PWM on HC08GR8

1,779 Views
AveryZ
Contributor I
Hi,

What I am trying to do is build an LED light fader using the TIM channels for PWM generation. The tricky part is that I want to control the fades/brightens by sending little control packets to the MCU from a PC via SCI (this data communication is not tricky, its the variable nature of the fader).

A sample packet would look like this:

L (char, lets the proccesor know i am sending a Lighting instruction)
255 (desired R level)
255 (desired G level)
255 (desired B level)
011 (Timing High Byte)
184 (Timing Low Byte) --> Timing together in decimal = 3000
E (char, ends packet)

So with this packet I am requesting that the MCU raise the levels of 3 LEDs from whatever level they are at (lets say 0 for the example) to 255 (full), and to do it over the course of 3000ms (3s).

I am having an extraordinary amount of trouble with this, can anyone offer some suggestions? I am coding in C.

Thanks!
Avery
Labels (1)
0 Kudos
Reply
3 Replies

569 Views
bigmac
Specialist III
Hello Avery,
 
To elaborate on Rocco's response, for unbuffered PWM the output frequency would be determined by the setting of the TMOD registers, with a trade-off between resolution and maximum frequency.  For your case, the TMOD setting would probably be 0x00FF, assuming a linear relationship between the pulse period and the input level value.  The update rate for the colour levels will be determined by the timer overflow period.
 
To set up the TIM channels, each would need to have its "toggle on overflow" bit set, and would clear the output on the occurrence of each output compare.  You would need to update the output compare value within the TIM channel ISR, ready for the next output compare event in the next PWM cycle.
 
Note that if you require output values of zero and 256 (100 percent duty cycle), these conditions will require special handling within the ISR.
 
A general comment - if you need to control more than one lighting device from the PC, you would need to also allow for a "device address" value in the communications protocol.  In this case you might consider a multi-drop RS485 interface for the SCI.
 
Regards,
Mac
 
0 Kudos
Reply

569 Views
AveryZ
Contributor I
Hi Rocco & Mac,

Thanks for your responses! I am going to integrate your suggestions into my existing code and see whats what! I'll update this post with the progress in a few days (I have to go back up to school now so I won't be able to work on it for 48 hours :-/ )

*I am using a 9.8304mhz crystal

Thanks,
Avery
0 Kudos
Reply

569 Views
rocco
Senior Contributor II
Hi, Avery:

I did an almost identical project in the mid '80s with the MC68HC05, except I used three 300 watt halogen bulbs and dichroic filters for color. I also had to bit-bang the three PWMs.

I assume you are having problems with the three PWMs? It is tricky with the GR8, since you need to use unbuffered-PWM to get three channels. Are you using a crystal or the PLL?

Here is a rough outline. Much of it will be obvious. Note the difference between the two TIMERS, and the three CHANNELS.

You set the two timers to use the same clock-divider. Enable them at the same time, to have them in sync (they will be at least 3 cycles off, nonetheless). You can use the overflow from one of them to keep track of your milliseconds for the fade-rate.

Set all three channels to unbuffered PWM. You would then update them every cycle. I would do the calculation for the next cycle in the background.

The tricky part is deciding whether to update them on the timer-overflow interrupt, or on the channel interrupt. They both have subtle timing issues, which may not be visible when fading an LED.

Hope that gets you started. Sorry if you were already past this part.
0 Kudos
Reply