1 / 22

Instructor: Paul Gordy Office: H-115 Phone: 822-7175 Email: PGordy@tcc

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. v(t). V. t. 2T. 3T. T. 0. T 1.

judah
Download Presentation

Instructor: Paul Gordy Office: H-115 Phone: 822-7175 Email: PGordy@tcc

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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@tcc.edu

  2. v(t) V t 2T 3T T 0 T1 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 24 40 44 v(t) T = ____________ F = ____________ D = ____________ V t (ns) 65 80 145 160 205 0

  4. v(t) Motor turns fast V t v(t) Motor turns slow V t 80% power setting V t v(t) 20% power setting (defrost) V t 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. v(t) Turn Left V t v(t) Turn Right V t v(t) Capacitor charges to larger analog voltage V t v(t) Capacitor charges to smaller analog voltage V t 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 MicroStamp11’s 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 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.

  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 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 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. Lab Manual approximates this as 500ns. Note that f = 1/T = 2 MHz. 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 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. - 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. Lecture #8 EGR 262 – Fundamental Circuits Lab Hardware Registers used by Output Compare Interrupts There are three important registers used by output compare interrupts.

  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 ISR’s interrupt vector

  13. // 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. 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%. // 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 Actually 407 ns // 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/100*9828, 9828*D/100, 9828/100*D, etc ???) lowticks = ….. } 2C) Modify the interrupt handler OC4han( ) to use lowticks and highticks #pragma interrupt_handler OC4han() void OC4han(void){ if(TCTL1==0x08){ TOC4=TOC4+lowticks // add comments to all changed code TCTL1 = 0x0C; etc }

  19. Lecture #8 EGR 262 – Fundamental Circuits Lab 2D) 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 // 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/T*100% D = 800us/4ms*100 = 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.

More Related