Page 129 - Embedded Microprocessor Systems Real World Design
P. 129

previous reading and subtract the new reading from it. When the counter rolls over
                  from FF to 00, the software must recognize that fact and correct the numbers; if it
                  doesn’t, negative values will result. Many microcontrollers that provide a capture-
                  type timer also provide a means for the counter to generate an interrupt when it
                  rolls over, which can simplify this software task.

                   PWM
                  Figure 3.5D shows a simple PWM scheme, similar to that implemented on the Atmel
                  AT9OS microcontrollers. An 8-bit up/down  counter counts from 00 to FF, then back
                  down to 00. An &bit comparator compares the value in the 8-bit latch to the counter
                  value. When  the  two  values are  equal, the  comparator  clocks the  “D” flipflop
                   (again, timing logic makes sure everything works correctly). If the counter is count-
                  ing up, a  “1” is  clocked into the  “D” flip-flop. If  the  counter is  counting down,
                  a  “0” is loaded. The flip-flop output is connected to one of the microcontroller
                  output pins.
                     Say the microprocessor writes a value of WE into the latch. The counter counts
                  from 00 to FE, where the PWM output goes to “1” because the counter bits match
                  the latched value. The counter continues to FF, then back down through FE to zero.
                  When the counter passes through FE, the PWM output goes to zero. So in this case,
                  the PWM output is high for two counts (FE and FF) out of 256, or about .78 percent
                  duty cycle. If  the  microprocessor writes OxFO  to the  latch, the  PWM  output will
                  be  high from FO  to FF and back to FO,  for a  total of  30  counts or  11.7 percent
                  duty cycle.
                     A more sophisticated PWM timer would include a second latch and comparator
                  so  the counter can reverse direction  at values other than FF. In such a timer, this
                  comparator would set the frequency of the PWM signal while the other compara-
                   tor would set the duty cycle.
                     Some microprocessors provide other means to generate PWM. The 80188 does
                  not use an up/down  counter but instead provides two comparators. After the first
                  count value is reached, the counter is reset and the second comparator is used to
                  indicate end-of-count. The output pin indicates which comparator is being used so
                   a PWM output can be generated by controlling the ratios of the comparator values.


                   Other Counter Features
                   Most microcontrollers provide a means to prescale the clock applied to the counter.
                  For instance, the 8-bit clock in the Atmel AT9OS8515 can be incremented with the
                   CPU clock, or the CPU clock can be divided by  8, 64, 256, or 1024. In addition,
                   the clock can be supplied by  either the rising or falling edge of the external pin
                  on the device. All of these features are software-selectable by writing to registers.
                     The 1Gbit timer in the AT9OS8515 can be programmed to control an output pin
                  when it times out. The pin can be programmed to toggle each time the timer times


                   110                                             Embedded Microprocessor Systems
   124   125   126   127   128   129   130   131   132   133   134