PWM Object

Top  Previous  Next

Provides pulse-width-modulated (PWM) outputs.


The WebCell has two independent PWM modules, each with four channels. The PWM object is a two-element array of these PWM modules. PWM[0] uses pins B0, B1, B2, and B3, while PWM[1] uses pins C0, C1, C2, and C3. Each channel is individually enabled so it is possible to use some channels of a PWM module while allowing the other pins of the module to be used for other purposes.


All four channels of a PWM module run at the same frequency, which can range from 60 Hz to 100 kHz or more. At very high frequencies, the resolution becomes limited. A PWM module is enabled by setting its frequency property to a non-zero value.


The pulse width of the each PWM channel is set using the width property array. The array has four elements, one for each channel. A pulse width value is assigned to the property array element that corresponds to the PWM channel. Assigning a non-negative value enables the channel; assigning -1 disables the channel and frees the corresponding pin.


The pulse width value ranges from zero to the value of the max property. The max property varies with PWM frequency. A duty cycle of 100% is achieved when the width element is assigned the value max. The value to use for pulse width for any duty cycle in the range 0 - 100% can be computed as follows:


Cell.PWM[n].width[channel] = duty / 100 * Cell.PWM[n].max;


where duty has been assigned the duty cycle, channel is in the range 0 - 3, and n is 0 or 1.


In some applications it is useful to set the PWM pulse width to a specific duration instead of using duty cycle. The value to use for pulse width given a duration in fractional seconds can be computed as follows:


Cell.PWM[n].width[channel] = duration * Cell.PWM[n].resolution;


where duration has been assigned the pulse width in fractional seconds, channel is in the range 0 - 3, and n is 0 or 1.



Gets the clock frequency for the PWM module.

Static Properties


Gets or sets the PWM frequency.


Gets the value that represents 100% pulse width.


An array that gets or sets the pulse width for each PWM channel.