![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
ES305 Lab2 Exercise 4 Adjusts Pulse Width Modulation output to vary the brightness of an onboard LED. Uses the keyboard to increase or decrease the the PWM
Dependencies: mbed
main.cpp@0:5cef91b1b767, 2014-08-14 (annotated)
- Committer:
- brianconnett
- Date:
- Thu Aug 14 17:47:40 2014 +0000
- Revision:
- 0:5cef91b1b767
ES305 Lab2 Exercise 4; Adjusts Pulse Width Modulation output to vary the brightness of an onboard LED. Uses the keyboard to increase or decrease the the PWM
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
brianconnett | 0:5cef91b1b767 | 1 | //**************************************** |
brianconnett | 0:5cef91b1b767 | 2 | // ES305 Linear Control Systems |
brianconnett | 0:5cef91b1b767 | 3 | // Lab 2 - Introduction to mbed microcontroller |
brianconnett | 0:5cef91b1b767 | 4 | // Exercise 4 - Pulse Width Modulation |
brianconnett | 0:5cef91b1b767 | 5 | // Adjusts Pulse Width Modulation output to vary the brightness of an onboard LED. |
brianconnett | 0:5cef91b1b767 | 6 | // Uses the keyboard to increase or decrease the the PWM |
brianconnett | 0:5cef91b1b767 | 7 | // |
brianconnett | 0:5cef91b1b767 | 8 | // Brian Connett, LCDR, USN |
brianconnett | 0:5cef91b1b767 | 9 | // exercise and code derived from mbed.org |
brianconnett | 0:5cef91b1b767 | 10 | //**************************************** |
brianconnett | 0:5cef91b1b767 | 11 | |
brianconnett | 0:5cef91b1b767 | 12 | #include "mbed.h" //mbed header file from mbed.org includes MOST APIs required to operate LPC |
brianconnett | 0:5cef91b1b767 | 13 | |
brianconnett | 0:5cef91b1b767 | 14 | Serial pc(USBTX, USBRX); //Create a Serial port connected to USB |
brianconnett | 0:5cef91b1b767 | 15 | PwmOut pwm1(LED1); //Creates a pulse-width modulation digital output assigned to the variable pwm1 at LED 1 |
brianconnett | 0:5cef91b1b767 | 16 | float duty_cycle=0.0; |
brianconnett | 0:5cef91b1b767 | 17 | |
brianconnett | 0:5cef91b1b767 | 18 | int main() |
brianconnett | 0:5cef91b1b767 | 19 | { |
brianconnett | 0:5cef91b1b767 | 20 | pc.baud(921600); //Set up serial port baud rate |
brianconnett | 0:5cef91b1b767 | 21 | pc.printf("Control of LED dimmer by host terminal\n\r"); |
brianconnett | 0:5cef91b1b767 | 22 | pc.printf("Press 'u' = to increase duty cycle, 'd' to decrease duty cycle\n\r"); |
brianconnett | 0:5cef91b1b767 | 23 | while(1) { |
brianconnett | 0:5cef91b1b767 | 24 | char c =pc.getc(); //Using the getc public member function of SERIAL to retrieve key press |
brianconnett | 0:5cef91b1b767 | 25 | wait(0.001); |
brianconnett | 0:5cef91b1b767 | 26 | if ((c == 'u') && (duty_cycle < 1.0)) { //When 'u' key is pressed duty cycle increases up to 100% |
brianconnett | 0:5cef91b1b767 | 27 | duty_cycle += 0.01; |
brianconnett | 0:5cef91b1b767 | 28 | pwm1 = duty_cycle; |
brianconnett | 0:5cef91b1b767 | 29 | } |
brianconnett | 0:5cef91b1b767 | 30 | if((c=='d')&&(duty_cycle > 0.0)) { //When 'd' key is pressed duty cycle increases down to 0% |
brianconnett | 0:5cef91b1b767 | 31 | duty_cycle -= 0.01; |
brianconnett | 0:5cef91b1b767 | 32 | pwm1 = duty_cycle; |
brianconnett | 0:5cef91b1b767 | 33 | } |
brianconnett | 0:5cef91b1b767 | 34 | pc.printf("%c %1.3f \n \r",c,duty_cycle); |
brianconnett | 0:5cef91b1b767 | 35 | } |
brianconnett | 0:5cef91b1b767 | 36 | |
brianconnett | 0:5cef91b1b767 | 37 | |
brianconnett | 0:5cef91b1b767 | 38 | } |