ECE 3567 Microcontrollers Laboratory 3 Pulse Width Modulation
ECE 3567 Microcontrollers Laboratory #3 – Pulse Width Modulation & RGB LED Autumn 2019 Dr. Gregg Chapman 1
ECE 3567 – Lab #3
ECE 3567 – Lab #3
millicandelas normalized ECE 3567 – Lab #3 wavelength (nm)
ECE 3567 – Lab #2
Quiz #2 Material
I/O Ports Port Number and Bit Number TI Conventions From Lecture #2 This is the number of the PORT This is the number of the BIT in the PORT P 3. 6 - Bit 6 of Port 3
I/O Ports From Lecture #2
I/O Ports From Lecture #2
MSP 430 FR 6989 HARDWARE Quick Start Guide From end of Lecture #2 This Document is Incorrect! 10
MSP 430 FR 6989 HARDWARE Quick Start Guide From end of Lecture #2 0 0 1 1 1 Default Primary Secondary Tertiary GPIO This Document is Incorrect! 11
MSP 430 FR 6989 HARDWARE Quick Start Guide FOR LAB #3 Secondary 12
I/O Ports – Putting It All Together Example from end of Lecture #2 Secondary Function Suppose that you wanted to configure BIT 2 of PORT 2 as a Timer TB 0. 4 output for a PWM application Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 P 2 OUT X X X OUT X X P 2 DIR X X X 1 X X P 2 SEL 1 X X X 1 X X P 2 SEL 0 X X X 0 X X P 2 REN X X X X P 2 SEL 0 |= BIT 2; P 2 SEL 1 &= ~BIT 2; P 2 DIR |= BIT 2;
ECE 3567 – Lab #1 9. Based on the following schematic, configure the two GPIO pins as outputs using the registers and bitwise operators discussed in Lecture #2. 10. Turn ON the RED LED and turn OFF the GREEN LED. P 1. 0 P 9. 7 Jumpers are installed
ECE 3567 – Lab #2 Pulse Width Modulation Add code to the Timer A 0 Interrupt Service Routine to 22. INCREMENT the Duty Cycle comparator (TA 0 CCR 1) by 10 every interrupt. 23. If the Duty Cycle is >= 0 x 0 CCD, reset it to 0 x 0010 24. Configure P 1. 6 to output TA 0. 1 NOTE: You will need to change the pin function to TERTIARY, by programming bit 6 in both P 1 SEL 0 and P 1 SEL 1 to 1. Don’t forget to make P 1. 6 an OUTPUT. 25. Connect CHANNEL 1 of the oscilloscope to P 1. 6 on the Launchpad header and observe the Pulse Width Modulation. Save a few screen captures and explain what is happening in your Lab #2 report.
MSP 430 FR 6989 HARDWARE Quick Start Guide P 1 SEL 0 |= BIT 6; P 1 SEL 1 |= BIT 6; P 1 DIR |= BIT 6; 0 0 Default GPIO 0 1 Primary 1 0 Secondary 1 1 Tertiary This particular line is correct. Output #, always the same as the duty cycle Comparator # Timer # 16
MSP 430 FR 6989 HARDWARE FOR LAB #2 Tertiary 17
MSP 430 FR 6989 HARDWARE Quick Start Guide FOR LAB #3 Secondary 18
MSP 430 FR 6989 HARDWARE Quick Start Guide FOR LAB #3 Secondary
Output Examples “Pulse Width Modulation”
QUIZZES • Quizzes will be ONLINE in CARMEN for the rest of course. • You may take the quiz any time BEFORE or AFTER the Lab, up until MIDNIGHT on the final day of each lab (second week). • 5 MULTIPLE CHOICE questions are chosen at random from 20 possible questions. Each question is worth 1 point. • There is a 30 -minute time limit for each attempt. • You may retake the quiz ONE TIME. • Grades are recorded automatically. • There is NO PARTIAL CREDIT.
Laboratory #3
Lab #3 - OVERVIEW 1) Download 3 files: Revised 3567. h, RGB_LED. c, and RC_Voltage. c and add them to your Lab 3 Project 2) Modify main. c to speed up ISR 3) In RGB_LED. c create an initialization function for 3 channels of Pulse Width Modulation, Init_PWM() 4) Configure the Init_PWM to set the duty cycles for fixed values that will make the RGB LED WHITE. 5) In RGB_LED. c create an update_RGB() function to increase the duty cycle on the RED PWM output channel. 6) Modify the main() function to ramp the duty cycle on the RED element of the RGB LED. 7) Modify update_RGB() to ramp the duty cycle on the GREEN element of the RGB LED. 8) Modify update_RGB() to ramp the duty cycle on the BLUE element of the RGB LED. 9) Modify the update_RGB() function to step through each color: WHITE, RED, ORANGE, YELLOW, GREEN, BLUE, and PURPLE. Call the function at the correct place in main() to update the LED color once every second. 10) In RC_Voltage. c create another initialization routine for a 4 th channel of Pulse Width Modulation, Init_RC() 11) In RC_Voltage. c create a function called update_RC() that ramps the duty cycle on the correct PWM channel to change the RC FILTERED VOLTAGE from 0 to 3. 29 Volts. Call this from main() to execute once a second. 12) In RC_Voltage. c, modify, update_RC() to change the color of the RGB LED every 0. 55 Volts.
ECE 3567 – Lab #3 Part 1 Clean-up main()
ECE 3567 – Lab #3 Additional Files Needed 1. Download the Lab 3. zip under Lab 3 on the ECE 3567 website and add the UPDATED ECE 3567. h, RGB_LED. c, and RC_Voltage. c to your Lab 3 project.
ECE 3567 – Lab #3 ISR Changes 2. Change the Timer A 0 ISR to: #pragma vector=TIMER 0_A 0_VECTOR __interrupt void Timer_A(void) { NOTE 1: A function call here would execute how often? ? ISR_Counter++; // Count ISRs if(ISR_Counter>=10) // 10 ISRs make 1 second { ISR_Flag = 1; // Flag to main() ISR_Counter=0; // Reset 1 Second counter } return; }
ECE 3567 – Lab #3 main() Changes 3. Change the main() function to: void main (void) { //****** TI Macro used to stop the watchdog timer WDT_A_hold(__MSP 430_BASEADDRESS_WDT_A__); //****** Disable the GPIO power-on default high-impedance mode PMM_unlock. LPM 5(); //*** Initializations *** Init_GPIO(); Init_Timer_A 0(); //************** Enables all Configured Interrupts ********* __enable_interrupt(); NOTE 2: A function call here would execute how often? ? //********** THE MAIN INFINITE LOOP ******************** while(1) { NOTE 3: A function call here would execute how often? ? if(ISR_Flag == 1) // Timer A 0 has occurred. Used to count to 1 Second { NOTE 4: A function call here would execute how often? ? // Alternates RED / GREEN) P 1 OUT ^= BIT 0; // RED LED P 9 OUT ^= BIT 7; // GREEN LED ISR_Flag = 0; // Clear Interrupt Service Flag } } }
ECE 3567 – Lab #3 Add Functions 4. The following Function prototypes have already been added to 3567. h: void Init_GPIO(void); void Init_Timer_A 0(void); Copy the following functions after the ISR in main. c: void Init_Timer_A 0(void) { //*******Configure Timer A 0 for a 10 Hz interrupt ****** TA 0 CCR 0 = 0 x 0 CCE; // for 10 Hz=100 ms and count is 3278 TA 0 CTL = 0 x 0110; // Set ACLK, UP MODE for Timer_0 TA 0 CCTL 0 |= CCIE; // Enable interrupt for Timer_0 TA 0 CCTL 1 = 0 x 04 E 0; // CCR 1 reset/set TA 0 CCR 1 = 0 x 0667; // CCR 1 PWM at 50 % duty cycle, on TA 0. 1 P 1 DIR |= 0 x 40; P 1 SEL 0 |=0 x 40; P 1 SEL 1 |= 0 x 40; return; } void Init_GPIO(void) { //****** TI Macro Port Configurations for the RED and GREEN LEDs //****** Set pin P 1. 0 to output direction and turn LED off GPIO_set. As. Output. Pin( GPIO_PORT_P 1, GPIO_PIN 0 ); GPIO_set. Output. High. On. Pin( GPIO_PORT_P 1, GPIO_PIN 0 ); //****** Set pin P 9. 7 to output direction and turn LED off GPIO_set. As. Output. Pin( GPIO_PORT_P 9, GPIO_PIN 7 ); GPIO_set. Output. Low. On. Pin( GPIO_PORT_P 9, GPIO_PIN 7 ); return; }
ECE 3567 – Lab #3 Checkpoint #1: Demonstrate that your Lab #3 Project operates identically to Lab #2 AFTER making any changes to main. c, and UPDATING the 3567. h file.
ECE 3567 – Lab #3 Part 2 Timer B 0 Initialization, RGB LED PWM Initialization & the WHITE LED
ECE 3567 – Lab #3 Part 2 void PWM_null() { duty_cycle_red = 0 x 000; duty_cycle_green = 0 x 000; duty_cycle_blue = 0 x 000; TB 0 CCR 2 = 0000; TB 0 CCR 3 = 0000; TB 0 CCR 4 = 0000; return; }
ECE 3567 – Lab #3 Part 2 /* RGB duty cycle variables */ volatile unsigned int duty_cycle_red; volatile unsigned int duty_cycle_green; volatile unsigned int duty_cycle_blue; volatile short LED_Color;
void Init_PWM(void){ unsigned int period = PERIOD; duty_cycle_red= 0 x 0100; duty_cycle_green= 0 x 0120; duty_cycle_blue= 0 x 0120; LED_Color = White; ECE 3567 – Lab #3 Part 2 // Period is 0 x 0148 // WHITE LED // This variable is used to switch duty cycles in update_RGB // Set P 2. 2 to output // Set P 2. 2 to SECONDARY OUTPUT, TB 0. 4, (10) // Set P 3. 6 and P 3. 7 to output // Set P 3. 6 and P 3. 7 to SECONDARY OUTPUT, TB 0. 2 and TB 0. 3, (10) TB 0 CTL = ; TB 0 CCR 0 = period; // ACLK, up mode // PWM Period TB 0 CCTL 2 = ; TB 0 CCR 2 = duty_cycle_blue; // CCR 2 reset/set // CCR 2 PWM duty cycle, TB 0. 2 TB 0 CCTL 3 = ; TB 0 CCR 3 = duty_cycle_green; // CCR 3 reset/set // CCR 3 PWM duty cycle, TB 0. 3 TB 0 CCTL 4 = ; TB 0 CCR 4 = duty_cycle_red; // CCR 4 reset/set // CCR 4 PWM duty cycle, TB 0. 4 return; }
ECE 3567 – Lab #3 Checkpoint #2: Display your WHITE LED to the Lab Monitors
ECE 3567 – Lab #3 Part 3 Ramp Each Element of the RGB LED
ECE 3567 – Lab #3 Part 3 void update_RGB() { /* Your Ramping CODE Goes Here. Edit and demonstrate the code for each of the 3 colors */ NOTE 1: Don’t allow the duty cycle to become larger than the period. NOTE 2: Where will you call this function so that the ramping is not too fast or too slow /* Always end by writing the duty_cycle_ variables the proper compare registers */ TB 0 CCR 2 = duty_cycle_blue; TB 0 CCR 3 = duty_cycle_green; TB 0 CCR 4 = duty_cycle_red; return; } // CCR 2 PWM duty cycle is BLUE // CCR 3 PWM duty cycle is GREEN // CCR 4 PWM duty cycle is RED
ECE 3567 – Lab #3 Checkpoint #3: Display your RED GREEN and BLUE duty cycle ramping code to the Lab Monitors.
ECE 3567 – Lab #3 Part 4 Change the RGB LED Color
ECE 3567 – Lab #3 Part 4 - Change the RGB LED Color Edit update_RGB() to change the color of the RGB LED once a second. Comment out the ramping code. Implement the following colors: COLOR DUTY CYCLES RED GREEN BLUE RED 0 x 0070 0 x 0000 ORANGE 0 x 00 C 4 0 x 0024 0 x 0000 YELLOW 0 x 00 C 4 0 x 00 AB 0 x 0000 GREEN 0 x 0000 0 x 004 A 0 x 0000 BLUE 0 x 0000 0 x 007 A PURPLE 0 x 0026 0 x 0000 0 x 007 A WHITE 0 x 0100 0 x 0120 Since the color #defines in 3567. h are sequential, you can increment LED_Color and loop back to RED after WHITE.
ECE 3567 – Lab #3 Checkpoint #4: Display your update_RGB function to change colors every 1 second to the Lab Monitors
ECE 3567 – Lab #3 Part 5 Initialize and Ramp the RC Voltage
The RC Circuit RC charging circuit P 2. 1 PWM output ADC A 5 fcutoff = 6 Hz A 5 input on ADC 12
ECE 3567 – Lab #3 Part 5 - Ramp the RC Voltage Given that TB 0. 5 is the SECONDARY function on pin P 2. 1, write the code to initialize the PWM channel to the RC time constant. Call the function Init_RC() The function should be in the RC_Voltage. c file. NOTE: The xxxx. c files DO NOT require a #include to be compiled and linked in the project. Add the function call to the beginning of main().
ECE 3567 – Lab #3 Part 5 - Ramp the RC Voltage Write a function to increment the RC Voltage PWM and use it to ramp the RC Voltage from 0 to the value that sets the voltage to 3. 29 V. Determine this value experimentally. Increment the duty cycle by a step size that results in a 10 millivolt change in the RC Voltage. Determine this value experimentally. Call the function update_RC() The function should be in the RC_Voltage. c file. Call the function in main() so that it executes once per second.
ECE 3567 – Lab #3 Checkpoint #5: Display your update_RC() function to the Lab Monitors using a Digital Multi-meter to measure voltage.
ECE 3567 – Lab #3 Part 6 Change the LED Color based on RC Voltage Output
ECE 3567 – Lab #3 Part 6 Write a function to increment the RC Voltage PWM and use it to ramp the RC Voltage from 0 to the value that sets the voltage to 3. 29 V. Determine this value experimentally. Increment the duty cycle by a step size that results in a 10 millivolt change in the RC Voltage. Determine this value experimentally. Call the function update_RC() The function should be in the RC_Voltage. c file. Call the function in main() so that it executes once per second.
ECE 3567 – Lab #3 Part 6 – Indicate the Voltage Range with the LED Edit update_RGB() to change the color of the RGB LED to indicate which range the RC Voltage is in (see table). Comment out the previous code. COLOR Voltage Range Min Voltage (m. V) Max Voltage (m. V) RED 0 550 ORANGE 551 1100 YELLOW 1101 1650 GREEN 1651 2200 BLUE 2201 2750 PURPLE 2751 3290 WHITE * > 3290 NA * NOTE: Your code should NOT allow voltage this high. Include WHITE to indicate an OVERVOLTAGE. Since the color #defines in 3567. h are sequential, you can increment LED_Color and loop back to RED after WHITE.
ECE 3567 – Lab #3 Part 6 – Indicate the Voltage Range with the LED IN Lab #4 you will measure the RC Voltage using the Analog to Digital Converter. In Lab 3, you must base the LED color on the value that you are writing to the duty cycle register.
ECE 3567 – Lab #3 Checkpoint #6: Demonstrate your MODIFIED update_RC() function that changes the LED color every 550 m. V as the RC Voltage ramps up.
ECE 3567 – Lab #3 End of Laboratory #3
- Slides: 51