# EGR 277 – Digital Logic

Document Sample

```					Lecture #8   EGR 262 – Fundamental Circuits Lab

EGR 262
Fundamental Circuits Lab

Presentation for Lab #8
Pulse Width Modulation

Instructor: Paul Gordy
Office: H-115
Phone: 822-7175
Email: PGordy@tcc.edu
1
Lecture #8     EGR 262 – Fundamental Circuits Lab
Pulse Width Modulation
A periodic waveform can be described by v(t) = v(t + T) for some positive value of T,
called the period of the waveform. The pulse waveform below is a periodic
waveform with period T. The frequency, f, of the waveform is 1/T. T is measured in
seconds and f is measured in Hertz.
v(t)

V

t
0       T1       T                 2T                3T

A pulse-width modulated (PWM) signal is one where T1 can vary. The duty cycle,
D, of the waveform is defined below. D is usually expressed as a percentage.

T1                   1
D                   f
T                    T                                           2
Lecture #8      EGR 262 – Fundamental Circuits Lab
Examples:
Determine the period, frequency, and duty cycle of each waveform below.

v(t)
T = ____________
V
F = ____________
t (ms)     D = ____________
0     2        4       6       8      10
v(t)
T = ____________
V
F = ____________
t (us)     D = ____________
0 4             20 24          40 44

v(t)
T = ____________
V
F = ____________
t (ns)   D = ____________
0          65   80          145 160     205                                 3
Lecture #8 EGR 262 – Fundamental Circuits Lab
Applications:
Pulse-width modulated signals are used in many applications. A few are listed below.

v(t)
Motor control. Motor speeds                       Motor turns fast
up or slows down as pulse            V
width (or duty cycle) changes.
t
v(t)
Motor turns slow
V

t

Microwave Oven. The                               80% power setting
microwave is essentially turned      V
on and off as the power setting
is changed.                                                                     t
v(t)
20% power setting (defrost)
V

t
4
Lecture #8     EGR 262 – Fundamental Circuits Lab
Servo control. A servo is a combination         v(t)
motor & gear box where the motor                       Turn Left
position can turn 0 to 180 degrees          V
(typically) as pulse width varies. Servos
are used for steering RC cars.                                                                     t
v(t)
Turn Right
V

t

Digital-to-Analog Converter. In Lab 8
we will generate a PWM signal at one of
the MicroStamp11’s outputs. In Lab 9        v(t)
we will improve on the DAC built earlier               Capacitor charges to larger analog voltage
using an R-2R ladder network by using       V
the PWM signal to charge a capacitor to
the desired analog voltage.                                                                    t
v(t)
Capacitor charges to smaller analog voltage
V

t
5
Lecture #8     EGR 262 – Fundamental Circuits Lab
Generating a PWM signal using the MicroStamp11
In Lab 8 we will generate a PWM signal at the output pin PA4 where the duty cycle
of the signal can be set in the main program. In order to do this we will use
interrupts. The use of interrupts is a powerful, but somewhat complex, operation
that is explained in the following pages.

Interrupts
Hardware Event – a hardware event is something that occurs in the microcontroller’s
hardware that demands immediate attention. It demands attention by generating a
hardware interrupt.
Hardware Interrupt – a hardware interrupt is generated when a hardware event occurs
and the interrupt forces the microcontroller’s program counter to jump to a specific
address in memory called the interrupt vector.
Interrupt Vector – The interrupt vector is the memory address where an interrupt
service routine (ISR) or interrupt handler is located.
Interrupt Service Routine (ISR) – this a program which is to be executed when a
hardware interrupt occurs. After executing the ISR, program control returns to the
original program that the microcontroller was executing before it was interrupted.

The diagram on the following page illustrates the control flow in the
presence of a hardware interrupt.                                              6
Lecture #8    EGR 262 – Fundamental Circuits Lab

Examples of hardware events
1)   Resetting the MicroStamp11. When the reset button is pushed on the
MicroStamp11, pin 9 (Reset) goes low, creating an interrupt. Program control
jumps to the interrupt vector 0xFFFE which is the starting address of your
program as defined in vector.c. As a result, the MicroStamp11 stops what is
was doing and restarts your program.
2)   Deploying an airbag. Your car’s computer is busy displaying dashboard
functions as you drive and other things, but if you run into something, a sensor
detects the impact and generates a hardware interrupt that tells the computer to
7
immediately stop everything else and deploy the airbags.
Lecture #8 EGR 262 – Fundamental Circuits Lab
Interrupt vectors and their sources
The lab manual provides a table of interrupt vectors used by the MicroStamp11 and
the sources of the interrupts.

When you press the Reset
button, program control
jumps to the ISR located

In Lab 8 we will use the
hardware event (output
compare event) OC4
which is related to the
output pin PA4. OC4 is
also used by the pause
command.

8
Lecture #8 EGR 262 – Fundamental Circuits Lab
Output Compare Event
An output compare event is a hardware event that is tied to the microcontroller’s real
time clock. The clock in the MicroStamp11 increments a hardware register named
TCNT. It can increment the clock at 4 possible rates, determined by two bits in the
control register TMSK2. These two bits are called PR1 and PR0.
TMSK2

PR1 PR0

Lab Manual approximates this as 500ns.
Note that f = 1/T = 2 MHz.

Example: Clear PR1 and PR0 so that TCNT clock rate will be 407 ns

9
Lecture #8 EGR 262 – Fundamental Circuits Lab
Output Compare Registers
The counter TCNT cannot be reset or stopped by the user. So to generate timing
events, we compare the value in TCNT to another number in an output compare
register. When the value in TCNT matches the number in the output compare
register, an output compare event is triggered.

There are five output compare registers: TOC1, TOC2, TOC3, TOC4, and TOC5.
There are five output compare events: OC1, OC2, OC3, OC4, and OC5 that are
triggered when the values in these registers equals the value in TCNT.
We will only make use of TOC4 and OC4 in Lab 8.

10
Lecture #8      EGR 262 – Fundamental Circuits Lab
Hardware Registers used by Output Compare Interrupts
There are three important registers used by output compare interrupts.
- Status register used to “acknowledge” the servicing of a caught interrupt
Set OC4F to 1 to acknowledge
the OC4 interrupt

- Control register used to “arm” the interrupt

Set OC4I to 1 to arm OC4

- Used to modify how the output compare interrupt interacts with the MicroStamp11’s
output pins. OM4 and OL4 control how OC4 works. OC4 is tied to the output pin PA4.
OM4 and OL4 control how
OC4 (and thus PA4) responds
as shown in the table below.
OM4    OL4    Effect when TOC4 = TCNT

0      0    No change to OC4 (PA4)

0      1    Toggle OC4 (PA4)

1      0    Clear OC4 (PA4)
11
1      1    Set OC4 (PA4)
Lecture #8      EGR 262 – Fundamental Circuits Lab
Generating an Output Compare Interrupt
An output compare interrupt is generated if:
1)    The interrupt is armed. As seen on the last page, set OC4I to arm OC4.
2)    The interrupt is enabled. Enabling the interrupt means that the software will
pay attention to the interrupt. This is done by clearing the I bit in the condition
coder register in the MicroStamp11. This is cleared in the function init( ) using
the assembly language command cli. In a C program it is executed using:
asm (“cli”).
3)    The interrupt is acknowledged. As seen on the last page, set OC4F to
acknowledge that OC4 has been serviced.

Writing an ISR (Interrupt Service Routing or Interrupt Handler)
Writing an ISR is like writing a function (however, the compiler will know the
difference and treats the ISR differently). Three things are needed in writing an ISR:
1) Initialize the interrupt handler
2) Declare the ISR function
3) Define the ISR’s interrupt vector

12
Lecture #8      EGR 262 – Fundamental Circuits Lab
Initializing the interrupt handler
This is done using the function init( ) which we have been using at the start of any
program that we write. The init( ) function shown below is similar to the one found in
kernel.c.

// disable interrupt handling (set I bit in cond. code reg)
// disable “watchdog timer”
// set baud rate to 38400 for asynch. serial interface (SCI)
// control register for SCI
// control register for SCI

// global time variable = # of times OC4Hand is executed
// set rate at which TCNT is incr. to 407ns (≈500ns)
// arm OC4
// acknowledge any previously received OC4 interrupts
// next OC4 event is 256(500ns) = 128 us
// enable interrupt handling (clear I bit in cond. code reg)

This may read 0x03 in our lab, but be sure to
change this to 0x00 for the correct clock rate!                  13
Lecture #8      EGR 262 – Fundamental Circuits Lab
Interrupt Handler (ISR)
The code for the ISR is also found in kernel.c. The code shown below is similar to the
what we have been using in kernel.c. We use the pre-compiler directive pragma to
identify the code as an ISR rather than a regular function.
// use pragma to identify the function as an ISR
// ISR name and argument types
// Increment TOC4 by 128 us (for next OC4 event)
// Keep track of number of times OC4han executed
// Acknowledge OC4 by setting TFLG1 to 1

Interrupt Vector
Code is required to specify the address (interrupt vector) for the ISR. Recall from an
earlier table that OC4 uses the interrupt vector 0xFFE2.

// pragma also used to specify interrupt vectors
// name of ISR tied to this address

14
Lecture #8     EGR 262 – Fundamental Circuits Lab
Rewriting OC4han to generate a PWM signal
We wish to generate a PWM signal with a period of 2 ms. This is extremely short for a
microcontroller running at 8MHz, so the interrupt handler needs to be as short as
possible (or else it may not be completed before the next OC4 event occurs which may
lock up the system or cause erratic behavior).
The following code is written for an OC4 handler with a period of 2ms (2048 time
ticks) with a duty cycle of 50%.

// use pragma to identify as ISR

// if TCTL1 = 00001000 (or OM4=1,OL4=0) so if PA4=0
// generate next interrupt in 1024(500ns) = 512ms
// TCTL1 = 00001100 (or OM4 =1, OL4 = 1) so set PA4
// keep track of #times OC4han is executed (error?)
// else (meaning PA4 = 1)

// clear PA4

// acknowledge interrupt

// specify interrupt vector (address for ISR above)

15
Lecture #8      EGR 262 – Fundamental Circuits Lab

Programs & program modifications for Lab #7:
Error: Actually 4ms(1 tick/409 ms)
= 9828 ticks
1) Write the main program: (incomplete)
#include “vector.c”
#include “kernel8.c” // modified kernel function for lab 8

void main(void){
init( );
Prompt user to enter a % duty cycle (an integer from 0 to 100)
set_pwm(d);         // call function in kernel8.c to convert d to clock ticks
} handler with a period of 2ms (2048 time ticks) with a duty cycle of 50%.
16
Lecture #8      EGR 262 – Fundamental Circuits Lab
2. Modify kernel.c (Let’s first look at the original kernel.c below:)

kernel.c (portion of original kernel.c)

#pragma interrupt_handler OC4han()        // use pragma to identify as ISR
void OC4han(void){
if(TCTL1==0x08){                        // if TCTL1 = 00001000 (or OM4=1,OL4=0) so if PA4=0
TOC4=TOC4+1024;                       // generate next interrupt in 1024(500ns) = 512ms
TCTL1 = 0x0C;                         // TCTL1 = 00001100 (or OM4 =1, OL4 = 1) so set PA4
Time = _Time +1;                       // error?
}else{                                  // else (meaning PA4 = 1)
TOC4=TOC4+1024;
TCTL1=0x08;                           // clear PA4                            Actually 407 ns
}
TFLG1 |= OC4;                            // acknowledge interrupt
_Time=_Time+1;                          // keep track of #times OC4han is executed
}

extern void OC4han();
void (* OC4_handler[])()={ OC4han };
17
Lecture #8     EGR 262 – Fundamental Circuits Lab
2) Modify kernel.c (save as kernel8.c)
2A) Add the function set_pwm(int D) to kernel8.c
unsigned int highticks;
unsigned int lowticks;
void set_pwm(int D)     //D = percent duty cycle (0 to 100)
{ add code to convert D to clock ticks. Note: 4ms(1 tick/407ns) = 9828 ticks
highticks = …..    (Discuss: D/100*9828, 9828*D/100, 9828/100*D, etc ???)
lowtick = …..                }
2B) Modify the interrupt handler OC4han( ) to use lowticks and highticks
#pragma interrupt_handler OC4han()
void OC4han(void){
if(TCTL1==0x08){
TOC4=TOC4+lowticks
TCTL1 = 0x0C;
etc
}
2C) No changes to the interrupt vector
extern void OC4han();
void (* OC4_handler[])()={ OC4han };
Lecture #8      EGR 262 – Fundamental Circuits Lab
2D) Modify init() to use the correct clock frequency

As discussed earlier (slide 13) change TMSK2 in init() for a clock period of 407ns.

19
Lecture #8    EGR 262 – Fundamental Circuits Lab

Use the program Wavestar on the computer in lab to capture each of the 11
oscilloscope screens (for D = 1%, 10%, 20%, …, 80%, 90%, 99%). Print each 20
of them and tape them into your lab notebook (see example on next page).
Lecture #8     EGR 262 – Fundamental Circuits Lab

Sample Oscilloscope Screen
Used to display PWM output and measure duty cycle.
Include cursors and the calculation of D with each screen capture.
Cursor 1                Cursor 2
Delta
= Cursor 2 – Cursor 1
= TH
= 800.0 us

Input to program: D = 20
D observed on oscilloscope:
D = TH/T*100%
D = 800us/4ms*100 = 20%

f = 250 Hz
T = 1/f = 4 ms   21

```
DOCUMENT INFO
Shared By:
Categories:
Stats:
 views: 18 posted: 10/20/2010 language: French pages: 21