PWM Tutorial 2 – Using PWM and an H-Bridge to Control Motor Speed

This tutorial continues from where PWM Tutorial 1 left off

Introduction

The purpose of this tutorial is to apply the “breathing” PWM approach toward a motor driven by an H-Bridge

Steps

  1. Duplicate the project from PWM Tutorial 1 into a new project in the same workspace.
    1. Right click on the Breathing LED project and select “Save as…”
    2. Rename the project to something descriptive like “H-Bridge”. When it asks if you would like to rename the internal files as well, accept.
    3. Add the breathing LED project back to your workspace
      1. Right click on the workspace and select “Add existing project”
      2. Select the Breathing LED project
    4. Activate the new “H-Bridge” project by right clicking on it and selecting “Set as Active Project”
  2. Open up the Top Design. Copy the clock, PWM, and digital output and paste the blocks back in twice, next to or below the first, to create PWM_2 and PWM_3 blocks
    1. Change the clock frequencies going into the new blocks to 256 kHz.
  3. In the Design Wide Resources > Pins window, connect the two new output pins to new, unused ports.
  4. Remove the switches and other components that will not be used in this assignment (motor, resistors) from your H-Bridge circuit.
  5. Connect two jumper wires from those new ports to the two forward/backward inputs of your H-Bridge circuit from ICC3.
  6. Open main.c and add four lines of code in the line before the outer for(;;) loop: two to initialize and two to start the two new PWM subsystems.
  7. In the first of the two, inner for() loops, add code that:
    1. Sets the compare value of PWM_2 to the value of ii.
    2. Sets the compare value of PWM_3 to zero.
  8. In the second of the two, inner for() loops, add code that:
    1. Sets the compare value of PWM_2 to zero.
    2. Sets the compare value of PWM_3 to the value of ii.
  9. Compile and build your code to see what happens. Then, modify your code so that the motor “breathes” both forward and backward, going from a small pwm value to a large and back down in both directions.

Continue on to PWM Tutorial 3 – Using Low-Pass Filters with PWM Signals