Overview | Main | State Machine | Events | IR | Tape | Drivetrain | PWM | Flash | Two Minute Timer | Bump

pwm_e128.c
pwm_e128.h

void pwm_sys_init(unsigned char chan0145range, unsigned char chan23range): initialize the PWM subsystem

void pwm_chan_init(unsigned char chan, unsigned char freq): enable a PWM channel

void pwm_chan_disable(unsigned char chan): disable a PWM channel

void pwm_change_dc(unsigned char chan, unsigned char dc): change the duty cycle of a given channel

void pwm_change_freq(unsigned char chan, unsigned char freq): change the frequency of a given channel

The PWM Module allows PWM signals to be sent from pins U0-U5 at frequencies ranging from approximately 150 Hz to 20000 Hz. These frequencies come in quantized ranges; at any given time, only two of the ranges are achievable, one through channels 0, 1, 4, and 5 and the other through channels 2 and 3.

PSEUDOCODE

(Download Pseudocode .doc 27kb)

void pwm_sys_init(unsigned char chan0145range, unsigned char chan23range)
    Set all channels to have the output polarity that makes sense
    Set all channels to use the scaled versions of the clocks
    Set all channels to be in center-align mode
    Set the clock pre-scale and scale for group A according to chan0145range
    Set the clock pre-scale and scale for group B according to chan23range

void pwm_chan_init(unsigned char chan, unsigned char freq)
    For the given channel
        Set the frequency to the given value
        Set the duty cycle to zero
        Raise both enable bits

void pwm_chan_disable(unsigned char chan)
    For the given channel
        Lower both enable bits

void pwm_change_dc(unsigned char chan, unsigned char dc)
    Truncate the duty cycle at 100, if necessary
    For the given channel
Calculate the new value for the duty cycle register
Update the duty cycle register

void pwm_change_freq(unsigned char chan, unsigned char freq)
    For the given channel
        Extract the duty cycle as a percentage
        Scale this duty cycle according to the new frequency
        Update the frequency register
        Update the duty cycle register