How do I implement PWM (Pulse Width Modulation) functionality on the BB-400?


Digital IO Pins on the BB-400

The BB-400 uses an Arduino based on the Atmel ATMEGA32U4-MU 8-bit microcontroller to implement it’s Digital IO functionality. The controller uses an external 16MHz crystal for clock generation and can be re-programmed with custom firmware to use other peripheral functions that are mapped to some of the same Digital IO pins. The BB-400 comes with the default DIO functionality which uses 8 pins of the microcontroller as general digital input and another 8 pins as general digital output (configured as open-drain on the PCB). The pins used for digital output also have alternate Output Compare functions which can be used for other purposes.

The table below lists the microcontroller pins that are used to implement the digital output functionality on the Yellow and Green terminal block connectors of the BB-400. Also listed are the Timer/Counter Output Compare function options which can be configured in any combination.

Port Pins (ATMEGA32U4-MU) PB5 PB6 PC6 PD4 PC7 PD7 PB7 PD6
Output Compare (option1) OC1A OC1B OC3A OC4A OC4D OC0A
Output Compare (option2) ~OC4B OC4B ~OC4A OC1C ~OC4D

Note: The microcontroller port pins specified above can only be used for output based functions on the BB-400 and cannot be re-configured for input based functions due to hardware constraints.

Options for Implementing PWM Functionality on DIO Pins

The discussion below provides the most applicable information to assist users who may want to include PWM functionality on the BB-400 as part of their custom firmware. For a complete and more detailed architectural description of PWM capabilities, the user should also consult the datasheet for the ATMEGA32U4 microcontroller.

ATMEGA32U4 PWM Specific Architectural Features

The table in the previous section showed the options available for assigning ‘Output Compare’ (OC) functions to some of the DIO pins on the BB-400. These functions can be used to implement PWM (Pulse Width Modulation) if desired. The microcontroller incorporates a ‘Waveform Generator’ that can control the Output Compare signal behaviour depending on the setting of certain mode bits in the respective Timer/Counter’s Control Registers (TCCRx where x is the peripheral number). The key control bits that determine PWM mode and OC signal behaviour are:

  1. WGM[0:2] (Waveform Generator Mode) bits
  2. COM[0:1] (Compare Output Mode) bits

The mapping of Output Compare functions on the DIO pins covers all the Timer/Counter peripherals that exist in the microcontroller. The Waveform Generator Modes that are possible are not all supported in all the Timer/Counters. Furthermore, the maximum resolution of each Timer/Counter is also different, and so careful consideration should be given to what is required (PWM signal frequency and phase characteristics for example), and what is available when pin mapping Output Compare functions. The table below shows the various PWM waveform generation modes available in each Timer/Counter (T/C0, T/C1, T/C3 and T/C4).

Timer/Counter (T/C) T/C0 T/C1 T/C3 T/C4
Resolution 8-bits 16-bits 16-bits 10-bits
Fast PWM Y Y Y Y
Phase Correct PWM Y Y Y N
Phase and Frequency Correct PWM N Y Y Y
PWM6 Mode N N N Y
WGM setting
(settings for ‘Fast PWM’ only)
TOP=0xFF (mode=3)
TOP=OCR0A (mode=7)
TOP=0x00FF (mode=5)
TOP=0x01FF (mode=6)
TOP=0x03FF (mode=7)
TOP=OCR1A (mode=15)
TOP=0x00FF (mode=5)
TOP=0x01FF (mode=6)
TOP=0x03FF (mode=7)
TOP=OCR3A (mode=15)
TOP=OCR4C (mode=0)

The table above shows the four different types of PWM that are supported in the ATMEGA32U4. The WGM settings are specified for ‘Fast PWM’ only since this is capable of producing the highest signal frequency. See the datasheet for WGM settings for other generation methods. The main differences between the four methods of PWM generation are as follows.

  • Fast PWM: This is a high frequency PWM signal generation method that is based on single-slope timer/counter operation. The counter increments it’s count value all the way up to the maximum (TOP) value, rolls over to the reset (BOTTOM) value, and continues. The ‘pulse width’ is determined by the ‘Output Compare’ register value setting.
  • Phase Correct PWM: This produces a more accurate phase correct PWM waveform which can be up to half the maximum frequency of the Fast PWM option. This method is based on dual-slope operation in which the counter increments it’s value from BOTTOM to TOP, and then decrements it’s value from TOP to BOTTOM. The ‘Output Compare’ register setting is the same on both slopes and causes the PWM signal to switch state once on the upward slope, and once again on the downward slope.
  • Phase and Frequency Correct PWM: This method also generates a more accurate phase and frequency correct PWM waveform which can be up to half the frequency of the Fast PWM option. This method is also based on dual-slope operation as for the Phase Correct PWM option. The main difference with this approach is the time that the Output Compare Register gets updated between counter cycles.
  • PWM6 Mode: This method can be used to generate PWM waveforms that can control BLDC Motors. Using this method (exclusive to Timer/Counter4 only) ‘Output Compare’ register OCR4A is used to generate six identical PWM waveforms from separate IO pins with phase relationships controlled by using the OC4OE Override Enable register.

PWM Pulse Width and Frequency Control

Pulse width or duty cycle control of the PWM signal is straightforward and is set by writing an appropriate value to the relevant ‘Output Compare’ register assigned to a pin. The value written should be restricted between the maximum (MAX), and BOTTOM values of the counter. Setting values equal to these extremes are special cases where MAX produces a constant ‘HIGH’ or ‘LOW’ signal and BOTTOM produces a sequence of narrow spikes.

The table above shows the WGM mode settings that can be used to generate PWM signals using the ‘Fast PWM’ method. The range and resolution of signal frequencies possible is different depending on the method used to generate the signal, what Timer/Counter is used, and what resources it has available. Where the TOP value is set to the maximum Timer/Counter value, signal frequency is fixed to one value only unless the discrete clock pre-scaler settings are adjusted. Where TOP can be set to an ‘Output Compare’ register value, the signal frequency can be changed by changing the value written to the ‘Output Compare’ register. Note however, that this register typically also determines pulse width and so provision must be made in firmware to switch the value in the register between complete timer/counter cycles, which would consequently add some latency.

Example Use Cases

When considering what peripheral resources are available in the ATMEGA32U4 and what pins are available for these resources on the BB-400, a number of possible use cases can be realised. Examples of these are listed below.

  • Ordinary separate general purpose PWM signals available on all six DIO pins specified above with a number of options for setting discrete signal frequencies.
  • PWM6 Mode in Timer/Counter4 is available for use in BLDC Motor applications.
  • Accurate Phase Correct, Phase and Frequency Correct, and high frequency Fast PWM methods available for generating signals used in stepper motor applications.