35
ECE 3567 Microcontrollers Lab Spring 2020 Dr. Gregg Chapman Laboratory #3 – Pulse Width Modulation 1

ECE 3567 Microcontrollers Lab

  • Upload
    others

  • View
    12

  • Download
    0

Embed Size (px)

Citation preview

Page 1: ECE 3567 Microcontrollers Lab

ECE 3567 Microcontrollers Lab

Spring 2020Dr. Gregg Chapman

Laboratory #3 – Pulse Width Modulation

1

Page 2: ECE 3567 Microcontrollers Lab

Lab #3 OverviewPreliminaries

1. Download updated 3567.h2. Edit Init_PWM() for 3 PWM Output Channels.

Create a new c-file - RGB_LED.ca. 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.

Page 3: ECE 3567 Microcontrollers Lab

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.

Page 4: ECE 3567 Microcontrollers Lab

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 Controlvoid update_RGB(void); // Selects color and updates RGB LED - RGB_LED.cvoid PWM_null(void); // Sets all PWMS and variables to zero for the RGB LED, Turning it off - RGB_LED.c

Page 5: ECE 3567 Microcontrollers Lab

Timer B0 Initialization

Timer B0 is identical to Timer A0 in Lab #2. You are just using different CCR modules:

TB0CTL – Timer B0 Control RegisterTB0CCTL0 – Leave this at Default valuesTB0CCTL2 – Comparator 2 Control RegisterTB0CCTL3 – Comparator 3 Control RegisterTB0CCTL4 – Comparator 4 Control RegisterYou must also write compare values to the following registersTB0CCR0 – 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)

Page 6: ECE 3567 Microcontrollers Lab

ECE 3567 MicrocontrollersTI LaunchPad (MSP430FR6989)

Page 7: ECE 3567 Microcontrollers Lab

fcutoff = 6 Hz

A5 input on ADC12

RC charging circuit P2.1 RC PWM output

ADC – A5

ECE 3567 MicrocontrollersCustom Designed BoosterPack

RGB PWMs to LED

FET Switch for Heating ElementHeating Element

Page 8: ECE 3567 Microcontrollers Lab

2. Modify Init_PWM()

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 -BlueTB0CCR3 is connected to P3.7 - GreenTB0CCR4 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.

Page 9: ECE 3567 Microcontrollers Lab

2. Modify Init_PWM()

3) You MUST use the following INTERMEDIATE variables when changing the pulse widths:

duty_cycle_blueduty_cycle_greenduty_cycle_red

The reason will become obvious as the lab progresses.

Define these as volatile unsigned int in RGB_LED.c .

Page 10: ECE 3567 Microcontrollers Lab

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, one 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.

Page 11: ECE 3567 Microcontrollers Lab

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.

Page 12: ECE 3567 Microcontrollers Lab

ECE 3567 – Lab #2

Page 13: ECE 3567 Microcontrollers Lab

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.

Page 14: ECE 3567 Microcontrollers Lab

ECE 3567 – Lab #3

Checkpoint #1: Have the Lab Staff verify that your Init_PWM() function and main() are correct.

Page 15: ECE 3567 Microcontrollers Lab

PART 1

Compile and run the Part 1 Program

Page 16: ECE 3567 Microcontrollers Lab

ECE 3567 – Lab #3

Checkpoint #2: Demonstrate the WHITE LED to the Lab Staff and verify that your RGB color combination is within limits.

Page 17: ECE 3567 Microcontrollers Lab

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;

Page 18: ECE 3567 Microcontrollers Lab

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;

Page 19: ECE 3567 Microcontrollers Lab

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.

Page 20: ECE 3567 Microcontrollers Lab

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 Secondsred_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.

Page 21: ECE 3567 Microcontrollers Lab

PART 2

For all the other CASEs for State use an IF-ELSE statement to implement the ramps. For instance:

if(duty_cycle_green < MAX_Green)duty_cycle_green += step_size; // #define step_size as 2

elseState = 3;

break;

Page 22: ECE 3567 Microcontrollers Lab

PART 2

For all the other CASEs for State use an IF-ELSE statement to implement the ramps. For instance:

if(duty_cycle_green >= step_size)duty_cycle_green -= step_size; // #define step_size as 2

elseState = 3;

break;

Page 23: ECE 3567 Microcontrollers Lab

PART 2

Call update_RGB() in the main loop so that it executes every 100 milliseconds

Page 24: ECE 3567 Microcontrollers Lab

PART 2

Checkpoint #3: The RGB smooth transitions between colors.

Page 25: ECE 3567 Microcontrollers Lab

PART 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

Page 26: ECE 3567 Microcontrollers Lab

PART 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:

• Redduty_cycle_red = 0x070;duty_cycle_green = 0x000;duty_cycle_blue = 0x000;

Page 27: ECE 3567 Microcontrollers Lab

ECE 3567 – Lab #3

Page 28: ECE 3567 Microcontrollers Lab

PART 3

• Use the following duty_cycle definitions for each case:• Orange

duty_cycle_red = 0x00C4;duty_cycle_green = 0x0024;duty_cycle_blue = 0x0000;

• Yellowduty_cycle_red = 0x00C4;duty_cycle_green = 0x00AB;duty_cycle_blue = 0x0000;

Page 29: ECE 3567 Microcontrollers Lab

PART 3

• Use the following duty_cycle definitions for each case:• Green

duty_cycle_red = 0x0000;duty_cycle_green = 0x004A;duty_cycle_blue = 0x0000;

• Blueduty_cycle_red = 0x0000;duty_cycle_green = 0x0000;duty_cycle_blue = 0x007A;

Page 30: ECE 3567 Microcontrollers Lab

PART 3

Page 31: ECE 3567 Microcontrollers Lab

PART 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 #1duty_cycle_red = 0x00x0;duty_cycle_green = 0x00x0;duty_cycle_blue = 0x00x0;

Page 32: ECE 3567 Microcontrollers Lab

PART 3

• Use the following duty_cycle definitions for each case:• No_Color

duty_cycle_red = 0x0000;duty_cycle_green = 0x0000;duty_cycle_blue = 0x0000;

• defaultduty_cycle_red = 0x0000;duty_cycle_green = 0x0000;duty_cycle_blue = 0x0000;

Page 33: ECE 3567 Microcontrollers Lab

PART 3

Call update_RGB() inside of the 1 second if statement of the main loop

Page 34: ECE 3567 Microcontrollers Lab

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.

Page 35: ECE 3567 Microcontrollers Lab

ECE 3567 – Lab #3

End of Laboratory #3