PWM - PalouseRobosub/SUBLIBinal GitHub Wiki
PWM (Pulse-Width Modulation)
SUBLIBinal offers a high level implementation of Pulse-Width modulated waves with the PWM external library. Note that the PWM library is not intended for full arbitrary wave form generation, and is designed for easy implementation of duty-cycle controlled motors and lights. The PWM library utilizes a single timer and output compare module to create a pulse width modulated wave on an output pin. Note that the PWM peripheral library is one of the few that does not implement a callback function. This is due to the fact that interrupts are not triggered when the output compare module on the microcontroller is triggered. Timer callback functions may be used for PWM interrupts instead. The documentation for the PWM peripheral library has been split into three sections: enumerations, structure defintions, and available functions.
Note: It is important to know that only Timer_2 and Timer_3 may be used for PWMs!
Please select the relevant section of documentation from the list below to be redirected.
PWM Implementation
To view examples of implementing a PWM wave form utilizing the SUBLIBinal library, please refer to the PWM Example.
What is a PWM?
PWM stands for Pulse-Width Modulation and is a way of providing a pseudo-analog voltage from a digital system. The way PWM works is by creating a basic square wave utilizing a duty cycle and a frequency. The frequency defines a single period of time that will repeat continually for the PWM signal and the duty cycle defines how long the voltage will be high during this period of time. With a 100% duty cycle, the voltage is always high. With 0%, the voltage is always low. With 50% duty cycle, a waveform will be created that is on for the first half of a period and then off for the second half.
Both frequency and duty cycle are important for PWM. If we are controlling a motor and provide a frequency that is very slow (1 Hz) with a duty cycle of 50%, then the motor will turn on for half a second and then off for half a second. However, if we do the same duty cycle of 50% with a frequency of 10KHz, our motor will move at roughly half speed. This is because it is rapidly turning on and off, which results in a slightly slower motion. PWM signals are great way to control motors or LEDs.