SLIDE 1 ECE 3 3567 M Microc
ers L Lab
Spring 2020
Laboratory #3 – Pulse Width Modulation
1
SLIDE 2 Lab #3 ab #3 Over erview Preliminar aries
- 1. Download updated 3567.h
- 2. Edit Init_PWM() for 3 PWM Output Channels.
Create a new c-file - RGB_LED.c
- a. make a function called update_RGB()
- b. make a function called PWM_null()
- c. Move Init_PWM() to RGB_LED( ).
NOTE: Create a Lab 3 Project and copy in the files from Lab 2 as a starting place.
SLIDE 3
Overview
Part 1 – Set up 3 Channels of Pulse Width Modulation and find the combination of duty cycles that produces the best WHITE LED. Part 2 – Create update_RGB() to ramp the pulse widths such that the LED changes colors smoothly and continuously through all color combinations. Part 3 – Edit update_RGB() function that changes the color of the LED every 1 second.
SLIDE 4
- 1. Download and Install the NEW version of 3567.h
This file includes the following variables, definitions, and function prototypes for Lab #3.
/**************************** Lab 3 # defines *********************************/ #define RGB_Period 0x00D0 // NOTE: MAXs can't be larger than this number #define MAX_Red 0x0070 #define MAX_Green 0x004A #define MAX_Blue 0x007A void Init_PWM(void); // Initializes Timer_B0 and PWM 2, 3, and 4 for RGB LED Control void update_RGB(void); // Selects color and updates RGB LED - RGB_LED.c void PWM_null(void); // Sets all PWMS and variables to zero for the RGB LED, Turning it off - RGB_LED.c
SLIDE 5
Timer B0 Initialization
Timer B0 is identical to Timer A0 in Lab #2. You are just using different CCR modules: TB0CTL – Timer B0 Control Register TB0CCTL0 – Leave this at Default values TB0CCTL2 – Comparator 2 Control Register TB0CCTL3 – Comparator 3 Control Register TB0CCTL4 – Comparator 4 Control Register You must also write compare values to the following registers TB0CCR0 – Comparator 0 Register (period) TB0CCR2 – Comparator 2 Register (blue duty cycle) TB0CCR3 – Comparator 3 Register (green duty cycle) TB0CCR4 – Comparator 4 Register (red duty cycle)
SLIDE 6 ECE 3 3567 M Microc
ers TI TI LaunchPad (MSP430FR 430FR6989) 6989)
SLIDE 7 fcutoff = 6 Hz
A5 input on ADC12 RC charging circuit P2.1 RC PWM output
ADC – A5
ECE 3 3567 M Microc
ers Custom D Design gned Boo
rPack ack
RGB PWMs to LED FET Switch for Heating Element Heating Element
SLIDE 8
Timer B0 will be used for the RGB LED Pulse Width Modulation. 1) The Period Comparator (TB0CCR0) will be set to the value of #defined by RGB_Period in 3567.h . The Control Register for this CCR0 is the same value you found in Lab #2. 2) For each of the PWM outputs, a) Set the pin direction to output. b) Set the function to SECONDARY, PxSEL1 = 1 (for the correct bit), and PxSEL0 = 0 (for the correct bit); TB0CCR2 is connected to P3.6 -Blue TB0CCR3 is connected to P3.7 - Green TB0CCR4 is connected to P2.2 - Red c) Initialize all three Comparator Registers to zero. d) The Control Register for all three comparators is the same value as in Lab #2.
SLIDE 9
3) You MUST use the following INTERMEDIATE variables when changing the pulse widths: duty_cycle_blue duty_cycle_green duty_cycle_red The reason will become obvious as the lab progresses. Define these as volatile unsigned int in RGB_LED.c .
SLIDE 10
- 3. Create a New C-file called RGB_LED.c
Add two functions to the file: 1) Create a function called update_RGB(). The function will determine the new values for each of the RGB pulse widths. You will have 2 versions of this function,
- ne for Part 2 and one for Part 3 of the lab. The LAST four lines of both versions the
function should be: TB0CCR2 = duty_cycle_blue; TB0CCR3 = duty_cycle_green; TB0CCR4 = duty_cycle_red; return; 2) Create a function called PWM_null(). This function should set all 3 CCR duty cycle registers to zero and clear all 3 duty_cycle_xxxx variables.
SLIDE 11
PART 1
1) At the beginning of Init_PWM() duty_cycle_blue = 0x00?0; duty_cycle_green = 0x00?0; duty_cycle_red = 0x00?0; return; 2) After initializing each of the TB0CCTLx Control registers, set each Comparator register value to equal the corresponding duty_cycle_xxx variable. TB0CCTL4 = 0x00E0; TB0CCR4 = duty_cycle_red; for example. 3) Change the values of the duty cycle variables to initialize the RGB LED to a perfectly WHITE color. Each of the variables will be a value between 0x0010 and 0x00D0.
SLIDE 12
ECE 3567 – Lab #2
SLIDE 13
PART 1
1) Make sure that you call Init_PWM() in main(). 2) Do not call update_RGB() in main for this part of the lab.
SLIDE 14
ECE 3567 – Lab #3
Checkpoint #1: Have the Lab Staff verify that your Init_PWM() function and main() are correct.
SLIDE 15
PART 1
Compile and run the Part 1 Program
SLIDE 16
ECE 3567 – Lab #3
Checkpoint #2: Demonstrate the WHITE LED to the Lab Staff and verify that your RGB color combination is within limits.
SLIDE 17
PART 2
In update_RGB(), you will write a simple 7-state state machine to ramp the pulse widths in combination to achieve a smooth transition through every color. Begin by creating a function in RGB_LED.c called update_RGB(). The last four lines of the function should be as follows: TB0CCR2 = duty_cycle_blue; TB0CCR3 = duty_cycle_green; TB0CCR4 = duty_cycle_red; return;
SLIDE 18
PART 2
In update_RGB(), you will write a simple 7-state state machine to ramp the pulse widths in combination to achieve a smooth transition through every color. Before proceeding further document Init_PWM for Part 1. Then change the duty cycle variable to initialize the LED to RED: duty_cycle_blue = 0x0000; duty_cycle_green = 0x0000; duty_cycle_red = 0x0070;
SLIDE 19
Use a SWITCH statement on a variable named State to changes states Within each state increment or decrement the duty_cycle_xxx variable by +/- 2 counts.
SLIDE 20
PART 2
For the CASE of State = 1, simply increment a counter called red_count to 15 calls to the function, then switch to State = 2; case 1: // Delay Red for 1.5 Seconds red_count ++; if(red_count >=15) { red_count = 0; State = 2; } Eventually update_RGB() is called on the main loop every time a 100 mSec interrupt occurs.
SLIDE 21 PART 2
For all the other CASEs for State use an IF-ELSE statement to implement the
if(duty_cycle_green < MAX_Green) duty_cycle_green += step_size; // #define step_size as 2 else State = 3; break;
SLIDE 22 PART 2
For all the other CASEs for State use an IF-ELSE statement to implement the
if(duty_cycle_green >= step_size) duty_cycle_green -= step_size; // #define step_size as 2 else State = 3; break;
SLIDE 23
PART 2
Call update_RGB() in the main loop so that it executes every 100 milliseconds
SLIDE 24
PART 2
Checkpoint #3: The RGB smooth transitions between colors.
SLIDE 25 PA PART 3 3
- Document the update_RGB() function from Part 2.
- Create a new copy of update_RGB()
- Remove the SWITCH statement and replace it with the following 3 lines of code:
LED_Color++; // Change color if(LED_Color > White) // Loop back to Red from White LED_Color = Red;
Note that the colors are #defined in 3567.h. LED_Color should be declared as a volatile short, and initialized to Red
SLIDE 26 PA PART 3 3
- Following the LED_Color counter, add a Switch statement for LED_Color.
- Include a case for Red, Orange, Yellow, Green, Blue, Violet, White, No_Color, and
the default.
- Use the following duty_cycle definitions for each case:
- Red
duty_cycle_red = 0x070; duty_cycle_green = 0x000; duty_cycle_blue = 0x000;
SLIDE 27
ECE 3567 – Lab #3
SLIDE 28 PA PART 3 3
- Use the following duty_cycle definitions for each case:
- Orange
duty_cycle_red = 0x00C4; duty_cycle_green = 0x0024; duty_cycle_blue = 0x0000;
duty_cycle_red = 0x00C4; duty_cycle_green = 0x00AB; duty_cycle_blue = 0x0000;
SLIDE 29 PA PART 3 3
- Use the following duty_cycle definitions for each case:
- Green
duty_cycle_red = 0x0000; duty_cycle_green = 0x004A; duty_cycle_blue = 0x0000;
duty_cycle_red = 0x0000; duty_cycle_green = 0x0000; duty_cycle_blue = 0x007A;
SLIDE 30
PA PART 3 3
SLIDE 31 PA PART 3 3
- Use the following duty_cycle definitions for each case:
- Violet
duty_cycle_red = 0x0026; duty_cycle_green = 0x0000; duty_cycle_blue = 0x007A;
- White – Use the values found in Part #1
duty_cycle_red = 0x00x0; duty_cycle_green = 0x00x0; duty_cycle_blue = 0x00x0;
SLIDE 32 PA PART 3 3
- Use the following duty_cycle definitions for each case:
- No_Color
duty_cycle_red = 0x0000; duty_cycle_green = 0x0000; duty_cycle_blue = 0x0000;
duty_cycle_red = 0x0000; duty_cycle_green = 0x0000; duty_cycle_blue = 0x0000;
SLIDE 33
PART 3
Call update_RGB() inside of the 1 second if statement of the main loop
SLIDE 34
ECE 3567 – Lab #3
Checkpoint #4: Demonstrate the RGB LED changes once a second, between 7 colors: Red, Orange, Yellow, Green, Blue, Violet, and White.
SLIDE 35
ECE 3567 – Lab #3 End of Laboratory #3