Tiva Lab 07: 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 Material

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 the 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 direction of the current is reversed, the rotation of the motor will also be reversed.

The speed control of DC motors for various applications is very important. There are two methods to control the DC motor speed: current control and voltage control. Voltage control is not a good idea as low voltage could lose torque, hence the best way to control the speed is by current control. A simple method of controlling the current is to add a variable resistor in series with the motor and adjust the resistance of the variable resistor to change the current flowing 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 obtain a smooth speed variation without reducing the starting torque of the motor.

PWM Signals

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

  1. The frequency of PWM output single 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 unchecked the setting, the default system clock is 16 MHz
  • EK-TM4C1294XL LaunchPad: By default, the system clock is 16 MHz.

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 16-bit length 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 closing 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 closing to the LOAD value, it will increase the duty cycle of the PWM signal.
    • If the CMP value is closing 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, you can find in this session.

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

Required Components List

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 / Schematic Diagram

The motor will typically draw more current than a microcontroller can support. Therefore, the L293D will be used to 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 over-current. If this happens, it will trigger a protection circuit to shut down the USB port. To reset the USB controller, you need to disconnect the Tiva board from the USB port, shut down (power off) the computer, 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, another one is connected to an onboard LED to control the brightness of the LED. 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 Lab07_PWMMotorLED.
  2. Launch the Keil μVisio and create a new project. Save the project to the project folder you just created in the previous step and set the project name to Lab07_PWMMotorLED.
  3. Add the Common and ezTivaLIB folder to the include paths which is 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

  • Generate two PWM signals for motor and LED. Then configure the frequency of PWM signals to 1000Hz.
  • Set the initial duty cycle value to 0%. (Inside the Setup_PWM() function)
  • In the main() code, the PWM duty cycle will be updated from 0%, 40%, 70%, to 100%, then reset the duty cycle to 0% and continue to update the duty cyle.
  • The intervals between the updating are 2 second

for main() function is shown below:

// manually calculate the CMP value with duty cycle 0%, 40%, 70%, and 100%, and set them to the array cmpDuty

create an integer array with the initial values of the duty: cmpDuty[]
create an integer variable with an initial value 0: i

Setup_PWM();
Setup_GPIO();

REPEAT:
set both PWMm->_g_CMP? to cmpDuty array with index i
increase i value by 1
if i is greater than the maximum value of array index number, then set i value to 0
delay for 2000 msec (You can use software delay, or delay function in ezTivaLIB)
LOOP

© 2024 Air Supply Information Center (Air Supply BBS)