« Close

Datasheets and User Guides

App Notes

Software & Driver - PWM Output (16-Bit, Mode 0)

Outputs a pulse width modulated rectangular wave output. Value passed should be 0-65535, and determines what portion of the total time is spent low (out of 65536 total increments). That means the duty cycle can be varied from 100% (0 out of 65536 are low) to 0.0015% (65535 out of 65536 are low).

The overall frequency of the PWM output is the clock frequency specified by TimerClockBase/TimerClockDivisor divided by 216. The following table shows the range of available PWM frequencies based on timer clock settings.

Table 16-bit PWM Frequency Ranges

    PWM16 Frequency Ranges 
TimerClockBase   Divisor=1 Divisor=256
0 4 MHz 61.04 N/A
1 12 MHz 138.11 N/A
2 48 MHz (Default) 732.42 N/A
3 1 MHz /Divisor 15.26 0.06
4 4 MHz /Divisor 61.04 0.238
5 12 MHz /Divisor 183.11 0.715
6 48 MHz /Divisor 732.42 2.861

The same clock applies to all timers, so all 16-bit PWM channels will have the same frequency and will have their falling edges at the same time.

PWM output starts by setting the digital line to output-low for the specified amount of time. The output does not necessarily start instantly, but rather has to wait for the internal clock to roll. For 16-bit PWM output, the start delay varies from 0.0 to TimerClockDivisor*65536/TimerClockBase. For example, if TimerClockBase = 48 MHz and TimerClockDivisor = 1, PWM frequency is 732 Hz, PWM period is 1.4 ms, and the start delay will vary from 0 to 1.4 ms.

If a duty cycle of 0.0% (totally off) is required, consider using a simple inverter IC such as the CD74ACT540E from TI. Or you can switch the mode of the timer to some input mode, and add an external pull-down to hold the line low when set to input.



Would you please put this on my wish list for the Holidays:

 New PWM function with 2 Parameters:

#1 a base frequency count for the Off time

#2 a base frequency count for the On time

This would save all my problems to obtain two jitter free signals in synch.



          ┌┐┌┐┌┐                  ┌┐┌┐┌┐                 ┌┐┌┐┌┐


          ┌────┐                  ┌────┐                 ┌────┐ 

 ────┘         └────────┘         └─────┘         └───

(This picture comes out perfect in Notepad)


Did not look good in notepad for me, but looks pretty good in my browser.  The nature of the timer system in the U6 microcontroller is such that the same clock feeds all PWM, so it can't do different frequencies at the same time.  Perhaps you could use the Freqout mode to create the top waveform, and PWM to create the botton waveform, and then the bottom waveform is controlling a gate that is gating the top waveform to get you the middle waveform.


Thank you for your speedy response. This was our original reason to purchase a U6 and we are still working on it.  The problem is we were hoping to get enough precision and flexibility (changing the base frequency and the number of pulses in the envelop) which could be easily done with the proposed PWM mode suggested in our comment. The problem is probably that we had been spoiled by our U12 and hardware solution that is precise within ±1 µsec (although ±1 msec would be enough) but we were hoping to simplify it to reproduce it in multiple units. Thanks again.

It seems like you can do it with my idea of having the U6 produce the 1st and 3rd waveforms, then using the 3rd waveform to gate the 1st waveform thus creating the 2nd waveform.

Looking back at your function suggestion of "base frequency count for on time and base frequency count for off time", how does this relate to the 3 waveforms?  I gather the 1st waveform is the base frequency, then your new mode idea makes one of the other 2 waveforms, and some other mode makes the last?



Basically what we need are waveforms #2 and #3.

Using a very simple hardware latch we can produce #3 from #1 and #2

#1 can be reproduced using Frequency out (mode 7) and #2 using stop on count (mode 9)

The problem is #1 and #2 would need to be in synch in order for the latch to work, but they wobble around.  Is there a way to keep them in synch?

Unfortunately, the same problem would probably exist using the suggested PWM feature.

Two simple subs we use in VB.NET can be found at:


Thanks again for your help.


We know that you can make #3 using a timer in PWM Output mode and #1 using a timer in Frequency Output mode.  Since all the timers are based on the same clock, I would say they will be in sync most interpretations of in sync.  The downside to the fact that they use the same clock, is you are limited in your frequency options for #1.

Once you have #1 and #3, you just add a gate of some sort where #1 is the input to the gate and #3 is connected to the enable for the gate.  Probably also want a pull-down on the output of the gate to hold it low when disabled.

I keep suggesting that as there is not a good way for us to do this in firmware.  The hardware just is not set up for it.


What I meant by being out of phase is illustrated by the jpeg at:


Using the program mentioned in the my previous comment with a "burst count" of 2.

The idea of using a DAQ unit for this application might have been a bad idea although we were close to a very elegant solution. We are now looking into using an embedded microprocessor such as the Parallax Basic Stamp.

Thanks again.

In your program I just see a single frequency output with a stop timer.  That would just create 1 waveform, so what is the other waveform you show in the jpg?

Using my suggested technique the waveforms will be in sync.


The waveform with the 2 pulses comes from Timer0 that is restarted with every "Go1" command.

The continuous waveform comes from Timer2 that is setup in the "ES_LJ_Configuration" procedure.

I tried many ways to stop and restart Timer2 at the same time as Timer0 but the results were always very similar.

I will try again the PWM as you suggested but, if I remember correctly,  we could not get enough flexibility in the resulting time on and time off.  


Using PWM

To make a long story short:

Ideally we would need to vary the pulse on and off times separately in a range of 1 to 500 milliseconds.

Using the base frequency of 48MHz we were able to operate in a range of 4 to 340 milliseconds. Which, for the time being we can accept.

The problem remaining was that 1 kHz could not be reached but this was solved to an acceptable level by using 32 kHz and dividing it in hardware using a 74LS393.  The actual 32kHz varies from 29.4 to 35.3 depending on the values of the various divisors, resulting in the 1 kHz pulses to take values from 922 to 1100 Hz; which is not great bit acceptable.

Thank you for convincing me of pursuing the PWM solution.

Should you be interested, parts of the VB.NET listing and resulting waveforms can be found at:






I have used the PWM output mode to generate a rectangular wave. Right now I don't know how to generate a second wave that is a complement (the negation) to this wave . Could you suggest any functions that I can use?

Thank you!