jcontrol.io
Class PWM

java.lang.Object
  extended by jcontrol.io.PWM
Direct Known Subclasses:
PWMext

public class PWM
extends Object

Controls the Pulse Width Modulation of JControl. This is used to control analog devices, e.g., lights, motors, buzzers.

The number of available PWM-channels is hardware dependent, you can

This class is very close to the JControl hardware, so the frequency or period of the signal is restricted by the edges of the hardware. If you set a frequency or period this results in calculating a prescaler (3 bit) and a counter preload (8 bit), the prescaler sets the octave, the preload the frequecy within the octave, so higher frequencies are less precise. The preload also affects duty cycle precision. You can check the hardware-chosen value by reading back the frequency.

Version:
1.1
Author:
Helge Böhme
Available on JControl Devices:
all

Constructor Summary
protected PWM()
          Disables instantiation.
 
Method Summary
static int getDuty(int channel)
          Gets the duty cycle of the given channel.
static int getFrequency(int channel)
          Returns the current PWM signal frequency resulting of current scale and load.
static void setActive(int channel, boolean active)
          Turns the specified channel on or off.
protected static void setCycles(int scale, int load)
          Sets both the prescale and the preload value of the PWM unit.
static void setDuty(int channel, int duty)
          Sets the duty cycle of the given channel.
static void setFrequency(int freq)
          Sets the frequency of the PWM signal (in Hz), uses setCycles().
 
Methods inherited from class java.lang.Object
clone, equals, notifyAll, wait
 

Constructor Detail

PWM

protected PWM()
Disables instantiation.

Method Detail

getDuty

public static int getDuty(int channel)
Gets the duty cycle of the given channel.

Parameters:
channel - to use (valid range: 0...3, hardware dependent)
Returns:
duty cycle (values -256..0..255 are scaled from hardware and frequency dependent parameters)

getFrequency

public static int getFrequency(int channel)
Returns the current PWM signal frequency resulting of current scale and load.

Parameters:
channel - to use (valid range: 0...3, hardware dependent)
Returns:
the frequency
See Also:
setCycles(int scale, int load), setFrequency(int freq)

setActive

public static void setActive(int channel,
                             boolean active)
Turns the specified channel on or off. This doesn't affect current frequency or duty cycle.

Parameters:
channel - to turn on of off
active - true activates the channel

setCycles

protected static void setCycles(int scale,
                                int load)
Sets both the prescale and the preload value of the PWM unit. This affects all channels. Setting these values also affect current duty cycle of all channels so this must be set or refreshed afterwards.

Parameters:
scale - the prescaler value (valid range: 0...7).
load - the preload value of the counter (valid range: 0...255).
The counter frequncy is calculated as follows: fcounter=fCPU/2scale
Note: fCPU is 2MHz for standard versions of JControl products based on the JCVM8 technology The PWM frequency is calculated as follows: fPWM=fcounter/(256-load)
The PWM resolution is affected by load in the following way: resolution=1/(256-load)
See Also:
#setFrequency(short freq)

setDuty

public static void setDuty(int channel,
                           int duty)
Sets the duty cycle of the given channel. Frequency is not affected.

Parameters:
channel - to use (valid range: 0...3, hardware dependent)
duty - cycle (values -256..0..256 are scaled to hardware and frequency dependent parameters)

setFrequency

public static void setFrequency(int freq)
Sets the frequency of the PWM signal (in Hz), uses setCycles(). This method affects the frequency of all channels. Setting frequency also affect current duty cycle of all channels so this must be set or refreshed afterwards.

Parameters:
freq - the frequency to use (range 244 ... 32767 - hardware dependent)
See Also:
setCycles(int scale, int load), #setDuty(byte, int)