![](/media/cache/profiles/6c18d13e9a2e04855a83a6ce1a9117fe.jpg.50x50_q85.jpg)
/** 03_pwm_led * This program controls the duty cycle of * a PWM output connected to LED1 (LED_RED) * through the serial connection. * * Valid commands: '1', '5', '9' * Corresponding duty cycle: 10%, 50%, 90% * Period: 2000 ms * * Hardware requirements: * - FRDM-KL25Z board */
/ 03_pwm_led
- This program controls the duty cycle of
- a PWM output connected to LED1 (LED_RED)
- through the serial connection.
- Valid commands: '1', '5', '9'
- Corresponding duty cycle: 10%, 50%, 90%
- Period: 2000 ms
- Hardware requirements:
- - FRDM-KL25Z board
- /
main.cpp@0:68ad188ca8c4, 2015-10-21 (annotated)
- Committer:
- icserny
- Date:
- Wed Oct 21 12:31:41 2015 +0000
- Revision:
- 0:68ad188ca8c4
First version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
icserny | 0:68ad188ca8c4 | 1 | /** 03_pwm_led |
icserny | 0:68ad188ca8c4 | 2 | * This program controls the duty cycle of |
icserny | 0:68ad188ca8c4 | 3 | * a PWM output connected to LED1 (LED_RED) |
icserny | 0:68ad188ca8c4 | 4 | * through the serial connection. |
icserny | 0:68ad188ca8c4 | 5 | * |
icserny | 0:68ad188ca8c4 | 6 | * Valid commands: '1', '5', '9' |
icserny | 0:68ad188ca8c4 | 7 | * Corresponding duty cycle: 10%, 50%, 90% |
icserny | 0:68ad188ca8c4 | 8 | * Period: 2000 ms |
icserny | 0:68ad188ca8c4 | 9 | * |
icserny | 0:68ad188ca8c4 | 10 | * Hardware requirements: |
icserny | 0:68ad188ca8c4 | 11 | * - FRDM-KL25Z board |
icserny | 0:68ad188ca8c4 | 12 | */ |
icserny | 0:68ad188ca8c4 | 13 | |
icserny | 0:68ad188ca8c4 | 14 | #include "mbed.h" |
icserny | 0:68ad188ca8c4 | 15 | |
icserny | 0:68ad188ca8c4 | 16 | PwmOut myled(LED1); |
icserny | 0:68ad188ca8c4 | 17 | Serial pc(USBTX, USBRX); // tx, rx |
icserny | 0:68ad188ca8c4 | 18 | |
icserny | 0:68ad188ca8c4 | 19 | int main() { |
icserny | 0:68ad188ca8c4 | 20 | myled.period_ms(20); //Period = 20 ms |
icserny | 0:68ad188ca8c4 | 21 | myled.write(1.0); //Led off at start |
icserny | 0:68ad188ca8c4 | 22 | pc.printf("\r\n03_pwm_led: this program controls the duty cycle of a \r\nPWM output connected to LED1 (LED_RED) through the serial connection.\r\n"); |
icserny | 0:68ad188ca8c4 | 23 | pc.printf("Period: 20 ms, Valid commands are: '1', '5', '9'\r\nThe corresponding duty cycle are: 1%, 50%, 99%\r\n"); |
icserny | 0:68ad188ca8c4 | 24 | |
icserny | 0:68ad188ca8c4 | 25 | while(1) { |
icserny | 0:68ad188ca8c4 | 26 | char c = pc.getc(); //read next character |
icserny | 0:68ad188ca8c4 | 27 | if(c=='1') { |
icserny | 0:68ad188ca8c4 | 28 | myled = 0.99f; //LED has negative logic! |
icserny | 0:68ad188ca8c4 | 29 | pc.printf("Duty cycle = 0.01\r\n"); |
icserny | 0:68ad188ca8c4 | 30 | } |
icserny | 0:68ad188ca8c4 | 31 | else if(c=='5') { |
icserny | 0:68ad188ca8c4 | 32 | myled = 0.5f; |
icserny | 0:68ad188ca8c4 | 33 | pc.printf("Duty cycle = 0.5\r\n"); |
icserny | 0:68ad188ca8c4 | 34 | } |
icserny | 0:68ad188ca8c4 | 35 | else if(c=='9') { |
icserny | 0:68ad188ca8c4 | 36 | myled = 0.01f; //LED has negative logic! |
icserny | 0:68ad188ca8c4 | 37 | pc.printf("Duty cycle = 0.99\r\n"); |
icserny | 0:68ad188ca8c4 | 38 | } |
icserny | 0:68ad188ca8c4 | 39 | wait(0.2); |
icserny | 0:68ad188ca8c4 | 40 | } |
icserny | 0:68ad188ca8c4 | 41 | } |