EGR 277 - PowerPoint PPT Presentation

About This Presentation
Title:

EGR 277

Description:

Lecture #8 EGR 262 Fundamental Circuits Lab EGR 262 Fundamental Circuits Lab Presentation for Lab #8 Pulse Width Modulation Instructor: Paul Gordy – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 23
Provided by: tcgo
Learn more at: http://faculty.tcc.edu
Category:
Tags: egr | tick

less

Transcript and Presenter's Notes

Title: EGR 277


1
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_at_tcc.edu
2
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.
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.
3
Lecture 8 EGR 262 Fundamental Circuits Lab
Examples Determine the period, frequency, and
duty cycle of each waveform below.
v(t)
T ____________ F ____________ D ____________
V
t (ms)
6
10
8
0
2
4
v(t)
T ____________ F ____________ D ____________
V
t (us)
0
4
20
40
24
44
v(t)
T ____________ F ____________ D ____________
V
t (ns)
65
80
160
145
205
0
4
Lecture 8 EGR 262 Fundamental Circuits Lab
Applications Pulse-width modulated signals are
used in many applications. A few are listed
below.
Motor control. Motor speeds up or slows down as
pulse width (or duty cycle) changes.
Microwave Oven. The microwave is essentially
turned on and off as the power setting is changed.
5
Lecture 8 EGR 262 Fundamental Circuits Lab
Servo control. A servo is a combination motor
gear box where the motor position can turn 0 to
180 degrees (typically) as pulse width varies.
Servos are used for steering RC cars.
Digital-to-Analog Converter. In Lab 8 we will
generate a PWM signal at one of the
MicroStamp11s outputs. In Lab 9 we will improve
on the DAC built earlier using an R-2R ladder
network by using the PWM signal to charge a
capacitor to the desired analog voltage.
6
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 microcontrollers
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
microcontrollers 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.
7
Lecture 8 EGR 262 Fundamental Circuits Lab
  • Examples of hardware events
  • 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.
  • Deploying an airbag. Your cars 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
    immediately stop everything else and deploy the
    airbags.

8
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 at address 0xFFFE.
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.
9
Lecture 8 EGR 262 Fundamental Circuits Lab
Output Compare Event An output compare event is a
hardware event that is tied to the
microcontrollers 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
Example Clear PR1 and PR0 so that TCNT clock
rate will be 407 ns
10
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.
11
Lecture 8 EGR 262 Fundamental Circuits Lab
Hardware Registers used by Output Compare
Interrupts There are three important registers
used by output compare interrupts.
OM4 OL4 Effect when TOC4 TCNT
0 0 No change to OC4 (PA4)
0 1 Toggle OC4 (PA4)
1 0 Clear OC4 (PA4)
1 1 Set OC4 (PA4)
12
Lecture 8 EGR 262 Fundamental Circuits Lab
  • Generating an Output Compare Interrupt
  • An output compare interrupt is generated if
  • The interrupt is armed. As seen on the last
    page, set OC4I to arm OC4.
  • 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).
  • 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
  • Initialize the interrupt handler
  • Declare the ISR function
  • Define the ISRs interrupt vector

13
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.
14
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. (We will not need to make any
changes to the interrupt vector).
// pragma also used to specify interrupt
vectors // name of ISR tied to this address
15
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.
// specify interrupt vector (address for ISR
above)
16
Lecture 8 EGR 262 Fundamental Circuits Lab
4.1. Pre-lab Tasks (1) Explain how the initial
OC4han interrupt handler works (50 percent duty
cycle PWM). (2) Program Listings Make three
modifications to kernel.c (parts A, B, and C
below) and write a main program as follows. Save
the modified kernel.c as kernel8.c. A) Init()
Modify init( ) for a clock period of 407 ns.
Highlight all changes and explain how it works.
Add comments to all new or changed code. See
following slides for more details. B) kernel8.c
Modify kernel.c as follows Define unsigned
integers highticks and lowticks and write a new
function named setpwm(D) that will convert the
duty cycle, D, into the appropriate number of
highticks and lowticks based on a 9828 time tick
period. Highlight all changes and explain how it
works. Add comments to all new or changed code.
See following slides for more details. C) OC4han(
) Modify the OC4 interrupt handler so it
generates a PWM signal on PA4 that has
approximately a 4 msec period (9828 time tick
period) and a programmable duty cycle. In other
word, use highticks and lowticks rather than the
fixed 1024 ticks in the current function.
Highlight all changes and explain how it works.
See following slides for more details. (D)
Main Program Write a C-language program that
prompts the user to enter a duty cycle D (0 to
100) and then calls the function setpwm. Include
the new kernel function (kernel8.c). Include
many comments. See following slides for more
detail.
17
Lecture 8 EGR 262 Fundamental Circuits Lab
2A) Modify init() to use the correct clock
frequency As discussed earlier, change TMSK2 in
init() for a clock period of 407ns.
18
Lecture 8 EGR 262 Fundamental Circuits Lab
2B) 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/1009828, 9828D/100, 9828/100D, etc ???)
lowticks .. 2C)
Modify the interrupt handler OC4han( ) to use
lowticks and highticks pragma interrupt_handler
OC4han() void OC4han(void) if(TCTL10x08)
TOC4TOC4lowticks // add comments to all
changed code TCTL1 0x0C
etc
19
Lecture 8 EGR 262 Fundamental Circuits Lab
2D) Write the main program (incomplete) incl
ude 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 // repeat program
(infinite loop)
20
Lecture 8 EGR 262 Fundamental Circuits Lab
4.2. In-lab Tasks (1) Remove the R2R ladder
network, buffer circuit, comparator circuit, and
clamp circuit from your breadboard. Leave the
7-segment display connected. (2) Compile and
download your main program written in the pre-lab
task. Be sure that kernel8.c has been saved in
the include folder for the ICC11
compiler. (3) View the output on PA4 on the
oscilloscope. Verify that your program is
working correctly. As you enter different values
for D, you should see the period of the waveform
change on the oscilloscope. (4) Capture
oscilloscope images on the computer in lab to
capture each of the 11 oscilloscope screens (for
D 1, 10, 20, , 80, 90, 99). Add cursors
to each screen to measure TH. Verify that the
period is 4 ms (or f 250 Hz). Print each of
them and tape them into your lab notebook. Show
the input value of D next to each screen capture.
Also show the calculated value of D using the
measured value of TH. See a sample screen
capture on the following slide. (5) Description
of what happened during the in-lab task.
21
Lecture 8 EGR 262 Fundamental Circuits Lab
Sample Oscilloscope Screen (using WaveStar) 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/T100 D 800us/4ms100
20
f 250 Hz T 1/f 4 ms
22
Lecture 8 EGR 262 Fundamental Circuits Lab
4.3. Post-Lab Tasks (1) Form a table
comparing the duty cycle input to your program to
the duty cycle measured on the oscilloscope. (2)
Assess the performance of your program. (3)
Final program listings for any programs that
were modified during lab. Highlight any changes
made to the original program listings and discuss
the changes. (4) Demonstrate the functionality
of your completed system to the instructor.
Write a Comment
User Comments (0)
About PowerShow.com