PIC32MX: PWM Motor Control

From Mech
Revision as of 17:44, 29 July 2009 by Andrew Long (talk | contribs)
Jump to navigationJump to search

Pulse Width Modulation, or PWM, is a technique used to vary the average magnitude of a signal by changing its duty cycle (the proportion of time that a signal is active or "high"). For a more in-depth introduction to PWM motor control click here.

PWM for PIC32 is discussed in more detail in the Microchip Output Compare documention.

Available Pins

The pins available for PWM are 5 input pins (OC1, OC2, OC3, OC4, and OC5) and 2 output pins (OCFA and OCFB). The output pins are for fault pin protection.

General Approach

PWM can be set up by either changing the directly altering register bits or using the functions in outcompare.h (a header file included in the peripheral library (plib.h). The latter case is more straightforward and easier. This wiki describes how to use the outcompare functions. The register bits approach is described in detail in theMicrochip Output Compare documention.

There are three main functions that are used for PWM.

  void OpenOCX( config, value1, value2)

where X is the module that you want to use (1-5). This function configures the OCX module and loads the R and RS registers with default values. An example is shown below:

  OpenOC1( OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0);

The different configurations are shown below. If not specified, the default configuration is used.

Config Constant Description
OC_ON Turns the Module ON
OC_OFF Default - Turns the Module Off
Stop-in-idle control
OC_IDLE_STOP stop in idle mode
OC_IDLE_CON Default - continue operation in idle mode
16/32 bit mode
OC_TIMER_MODE32 Use 32 bit Mode
OC_TIMER_MODE16 Default - Use 16 bit Mode
Timer select
OC_TIMER3_SRC Timer 3 is clock source
OC_TIMER2_SRC Default - Timer 2 is clock source
Operation mode select
OC_PWM_FAULT_PIN_ENABLE PWM Mode on OCx, fault pin enabled
OC_PWM_FAULT_PIN_DISABLE PWM Mode on OCx, fault pin disabled
OC_CONTINUE_PULSE Generates Continuous Output pulse on OCx Pin
OC_SINGLE_PULSE Generates Single Output pulse on OCx Pin
OC_TOGGLE_PULSE Compare toggles OCx pin
OC_HIGH_LOW Compare1 forces OCx pin Low
OC_LOW_HIGH Compare1 forces OCx pin High
OC_MODE_OFF Default - OutputCompare x Off




Unidirectional Motor Control

This section will detail how to set up a simple program and circuit to control a motor using a PIC microcontroller and PWM.

Sample Code