PIC32MX: PWM Motor Control

From Mech
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)
  void OpenTimerX(config, period)
  unsigned int SetDCOCXPWM(dutycycle)


where X is the module (1-5) or timer(2-3) that you want to use. Each of these functions are described below.

OpenOCX configures the OCX module and loads the R(value2) and RS (value1) registers with default values. An example is shown below:

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

The different configuration constants are shown below. If not specified, the default configuration is used. These constants are mutually exclusive for each category (ie you can only use one of them).

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

For the operation mode, to use PWM one of the first two constants must be used.

The output compare modules use either Timer 2 (default) or Timer 3. These must be set up using OpenTimerX function, where X is either 2 or 3. The period can be between 0 and 0xFFFF inclusive. The OC can be configured to use a 32 bit timer in which both Timer 2 and Timer 3 are used. An example is shown below:

  OpenTimer2( T2_ON | T2_PS_1_1 | T2_SOURCE_INT, 0xFFFF);

The configuration constants are shown below for Timer 2. Replace the 2 with a 3 for Timer 3.

Config Constant Description
T2_ON Turns the Timer ON
T2_OFF Default - Turns the Timer Off
Stop-in-idle control
T2_IDLE_STOP stop in idle mode
T2_IDLE_CON Default - continue operation in idle mode
Timer gate control
T2_GATE_ON Timer Gate accumulation mode ON
T2_GATE_OFF Default - Timer Gate accumulation mode OFF
Prescale values
T2_PS_1_256 Prescaler 1:256
T2_PS_1_64 1:64
T2_PS_1_32 1:32
T2_PS_1_16 1:16
T2_PS_1_8 1:8
T2_PS_1_4 1:4
T2_PS_1_2 1:2
T2_PS_1_1 Default - 1:1
32-bit or 16-bit
T2_32BIT_MODE_ON Enable 32-bit mode
T2_32BIT_MODE_OFF Default
Sync external clock option
T2_SOURCE_EXT External clock source
T2_SOURCE_INT Internal Clock source

The duty cycle can be updated by using SetDCOCXPWM(dutycycle) where X is the module. The duty cycle gets updated on the next cycle. The duty cycle must be less than or equal to the period register defined in Timer 2 or Timer 3. An example is shown below:

   SetDCOC1PWM(PR2 / 2);

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