Introduction
 
 
The general ideas of Pulse Width Modulation (PWM) are discussed in the Introduction to PWM page. In this short tutorial, we follow on from this and will investigate PWM using a simple hardware demo. We will connect the output of PWM0 (pin PP0 on the Dragon12) to LED0 on Port B (pin PB0). Using Dbug12, we will then set up various PWM waveforms, each affecting the LED differently. As a reminder, the relevant registers are discussed in the above PWM link.
 
For both this part of the tutorial, we will use mini-jumper cables for the connections. This photo (pwm_la.jpg) shows mini-jumpers connected to an LA2124 logic analyzer. Each mini-jumper is a short wire that has a female square connector at each end that we connect to the 0.025" square male header pins on the Dragon12 or the LA2124 Logic Analyzer. See the TA to get some mini-jumpers for your station.
 
Steps for this demo:
 
   1. Start the Dragon12, and then start a Hyperterminal/DBug12 session.
   2. Enter the following commands to set up port B as an output register:
 
      >mm 026A C3   (this will set port J DDR to output, needed for Rev. E board)
      >mm 0268 00   (this will set Port J bits low to enable Port B LEDs on Rev. E boards)
      >mm 03 FF    (this will set the Port B Data Direction Register to be all outputs)
 
   3. Connect a mini-jumper from pin PP0 to pin PB0. Note that both of these pins are part of the main header assembly, and are located on the header strip just to the left of the mini-breadboard. This will connect the output from PWM Channel 0 (PWM0 uses pin PP0) to LED0 on Port B (which uses pin PB0).
   4. Do the calculations to determine the values in the various registers needed to create a PWM waveform with a half-second period and 50% duty cycle. See the PWM demo for details of how to do the calculations.
    5. Set up the necessary registers so that PWM0 generates the desired waveform. To do this, use the following "mm" or memory-modify commands. Note that to save typing, we do not need to use the leading two zeros of an address, and we also don't need upper case or the leading $ sign since all DBug12 numbers are in hex. So, we can use a2 for address $00A2.
 
     >mm a2 01    ; set PWM0 to use clock SA
      >mm a3 07    ; set PWMPERCLK with divider 128 (A clock rate = E/128, the slowest).
      >mm a8 00    ; set PWMSCLA to further divide SA clock to be E/(2*128*256) = 366.2 Hz,
      >mm b4 b8    ; multiple PWM0 period by $b8 = 184, 184*2.72 msec = 500.5 msec.
      >mm bc 5c    ; ($b8-$5c)/$b8 = (184-92)/184 gives PWM0 duty cycle = 50%
 
   6. Once the registers are set up as above and you have double checked your values, start the actual PWM waveform using the command "mm a0 01". LED0 should turn on for 1/4 second, turn off for 1/4 second, etc. If the period is not correct, double check you register values using the memory display "md" command (eg, "md a2") and change as needed.
   7. To turn off PWM0, write a $00 to register $00a0 using the command "mm a0 00". You can restart the PWM0 output using command "mm a0 01".
    8. Make sure that you get LED behavior as described above. If you don't, please see the TA.
   9. Set up the following PWM0 output: a ~ 1 msec period with a ~10% duty cycle. The DBug12 code to do this is shown below:
 
    >mm a2 01    ; set PWM0 to use clock SA
      >mm a3 07    ; set PWMPERCLK with divider 128 (A clock rate = E/128).
      >mm a8 04    ; set PWMSCLA to further divide SA clock by 4 (SA = E/(2 * 128 * 4) = 23,438 Hz
      >mm b4 18    ; multiple PWM0 period by $18 = 24, 24 * 43 microseconds = 1.032 msec.
      >mm bc 16    ; ($18 - $16)/$18 = (24 - 22)/24 gives PWM0 duty cycle = 8.3%
 
      Note that we do not get the exact values we desired because of using 8 bit integer math. We could get much closer by setting up 16 bit PWM registers, but that is not covered in this course.
  10. Once the registers are set up as above and you have double checked your values, start the actual PWM waveform using the command "mm a0 01". LED0 should barely light with this low of a duty cycle. Make sure that you get LED behavior as described above. If you don't, please see the TA.
 
 
PWM in Debug12