Tiva Lab 11: Controlling a DC Motor and LED Using PWM

Objective

  • Learn how to use the PWM signal to change the brightness of an LED and the speed of a small DC motor.
  • Learn how to calculate the LOAD and CMP values for the PWM signal.

Required Reading Materials

Overview

DC motor is a rotating machine that converts direct current electrical energy into mechanical energy. It is widely used in electrical power tools, toys, and appliances. They could be powered by a small battery to a DC power adapter. The basic working principle of a DC motor is that whenever a current-carrying conductor is placed in a magnetic field, it experiences a mechanical force that has the tendency to move. If the current direction is reversed, the motor's rotation will also reverse.

The speed control of DC motors for various applications is very important. There are two methods for controlling DC motor speed: current control and voltage control. Voltage control is not a good idea, as low voltage can cause torque loss; the best way to control speed is by current control. A simple way to control current is to add a variable resistor in series with the motor and adjust its resistance to change the current through the motor. But this method is not efficient, because the variable resistor also consumes energy.

The PWM (pulse width modulation) method is a very efficient method and is the most commonly used method. PWM uses digital signals to control the average power across analog devices. It is essentially a fixed-frequency square wave with adjustable pulse width. This method can achieve a smooth speed variation without reducing the motor's starting torque.

PWM Signals

The requirements of the PWM output signal in this lab are shown below:

  1. The frequency of the PWM output is 1000Hz
  2. The range of the duty cycle for PWM could be from 0% to 100%
  3. Right-aligned PWM signal

To calculate the PWM timer clock frequency, you have to know the default system clock frequency.

  • EK-TM4C123GXL LaunchPad: You need to uncheck the "Clock Configuration" in the Keil μVision. After you uncheck the setting, the default system clock is 16 MHz
  • EK-TM4C1294XL LaunchPad: By default, the system clock is 16 MHz.

In this project, ezTivaLib will be imported, and its API will be used to configure the system clock frequency. Therefore, students must examine the template firmware source code to verify the exact system clock frequency used in the project.

Calculations

lab01 PwmOutput
Figure 1: PWM Outputs for DC Motor and LED

You have to calculate the frequency of the PWM timer based on the system clock frequency and the PWM divisor.

${f_{PWMTimer}} = \frac{{SysClk}}{{PWMDivsor}}$

Calculate the count value for the PWM signal. This value will be set to the LOAD register, which is a 16-bit register only. If the count value you calculated is over 65535 (= 216-1), you need to reduce the frequency of the PWM Timer by increasing the value of the PWM Divisor and then recalculate the count value again.

$LOAD = Coun{t_{PWM}} = \frac{{{T_{PWM}}}}{{{T_{PWMTimer}}}} = \frac{{{f_{PWMTimer}}}}{{{f_{PWM}}}} \le 65535$

Changing the CMP value in the PWM module will change the duty cycle of the PWM signal. To calculate the CMP value, you have to know the type of PWM signal that you used: Left-aligned or right-aligned PWM.

  • For Left-Aligned PWM:
    • $duty = {t \over T} \times 100\% = 1 - {{CMP} \over {LOAD}}$
    • If the CMP value is close to the LOAD value, it will decrease the duty cycle of the PWM signal.
    • If the CMP value is closing to zero, it will increase the duty cycle of the PWM signal.
  • For Right-Aligned PWM:
    • $duty = {t \over T} \times 100\% = {{CMP} \over {LOAD}}$
    • If the CMP value is close to the LOAD value, it will increase the duty cycle of the PWM signal.
    • If the CMP value is close to zero, it will decrease the duty cycle of the PWM signal.

In this lab, the range of the duty cycle is from 0% to 100%, and the relationship between CMP and LOAD is:

$0 \le CMP < LOAD$

That means the value for the CMP register must be less than the LOAD value. The detailed calculations for CMP values can be found in this session.

Set the initial values of the duty cycle for both PWM outputs to 0%.

Components Required

DC Motor s 5V DC Motor × 1
R220Kohm 220ohm Resistor × 3
ic 16pin DipChip s L293D Motor Driver × 1
breadboard s Breadboard × 1
breadboard power s Breadboard Power Module × 1
PowerAdapter 64 Power Adapter × 1

Circuit Diagram

The motor will typically draw more current than a microcontroller can support. Therefore, the L293D will provide power to the motor, and its input pin connects to the PWM signal from the microcontroller. Plug the power supply module on the breadboard. The power supply module provides two power sources: +5V and +3.3V. Make sure the power source you connected to the circuit is +5V.

A breadboard power module must be used in this lab. Do not connect +5V from the Tiva board that directly connects to a USB port on the computer. Since the motor needs more current, it may cause USB overcurrent. If this happens, it will trigger a protection circuit to shut down the USB port. To reset the USB controller, disconnect the Tiva board from the USB port, shut the computer down (power off), and then power it on again.

breadboard power adapter s tiva ports 1 s

In this lab, the microcontroller needs to generate two PWM signals. One is connected to a DC motor through a motor controller, and the other is connected to an onboard LED to control its brightness. The code needs to update the duty cycle on both PWM signals.

Port/Pin for TM4C123G

DevicePort.Pin Signal TypePCTLDirectionDrive Mode

Procedure

  1. Create a new folder under the EE3450 folder and name it Lab10_PWMMotorLED.
  2. Launch the Keil μVisio and create a new project. Save the project to the folder you created in the previous step and set the project name to Lab10_PWMMotorLED.
  3. Add the Common and ezTivaLIB folder to the include paths, under the "Options for Target" setting.
  4. Add ezTiva LIB (ez123GLIB.lib or ez1294LIB.lib) into your project, and increase the stack and heap size under the "startup_TM4cXXX.s (Startup)" setting.

MyDefines.h

Add the following definitions to the MyDefines.h file:

MyDefines PWM 0
MyDefines PWM 1

Configurations

Sample Firmware Code

Lab Experiments

Experiment Procedure:

In this experiment, you will generate several PWM duty cycle levels and switch between them every 2 seconds.

Step 1: Calculate the CMP Values

Manually calculate the 'CMP' values for the following duty cycles:

  • 0%
  • 40%
  • 70%
  • 100%

After calculating the values, store them in an integer array named 'cmpDuty[]'.

Step 2: Create the Required Variables

Create the following variables in your program:

  • An integer array 'cmpDuty[]' with the initial 'CMP' values
  • An integer variable 'i', initialized to '0'

Step 3: Update the PWM Duty Cycle Repeatedly

Inside the main loop, perform the following steps repeatedly:

  1. Set both PWMm->_g_CMP? registers to the value stored in cmpDuty[i].
  2. Increase i by 1.
  3. If i is greater than the array's maximum valid index, reset i to 0.
  4. Delay for 2000 ms before changing to the next duty cycle.

You have to use a delay function provided by ezTivaLIB.

Step 4: Repeat Forever

Continue looping so the PWM output cycles through all duty cycle settings continuously.

Pseudocode:

REPEAT:
    Set both PWMm->_g_CMP? to cmpDuty[i]
    i = i + 1
    If i > max index of cmpDuty[]
        i = 0
    Delay 2000 ms
LOOP

© 2026 Air Supply Information Center (Air Supply BBS)