sample program for dimming LED with PWM.
if your platform is F401 or F411, you can use LED1 on board. else connect pwm pin to LED.
main.cpp@0:9f98248c4dd5, 2015-02-22 (annotated)
- Committer:
- Match314
- Date:
- Sun Feb 22 03:38:45 2015 +0000
- Revision:
- 0:9f98248c4dd5
Up
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Match314 | 0:9f98248c4dd5 | 1 | /* sample for dimming led with pwm. |
Match314 | 0:9f98248c4dd5 | 2 | * Copyright (c) 2015 Match |
Match314 | 0:9f98248c4dd5 | 3 | */ |
Match314 | 0:9f98248c4dd5 | 4 | |
Match314 | 0:9f98248c4dd5 | 5 | #include "mbed.h" |
Match314 | 0:9f98248c4dd5 | 6 | |
Match314 | 0:9f98248c4dd5 | 7 | // F401, F411 can use LED1. |
Match314 | 0:9f98248c4dd5 | 8 | // Other platform, use pwm pin and connect led. |
Match314 | 0:9f98248c4dd5 | 9 | PwmOut myled(LED1); |
Match314 | 0:9f98248c4dd5 | 10 | |
Match314 | 0:9f98248c4dd5 | 11 | int main() { |
Match314 | 0:9f98248c4dd5 | 12 | int duty = 0; // unit:% |
Match314 | 0:9f98248c4dd5 | 13 | myled.period_ms(1); // pwm frequency set 1kHz. |
Match314 | 0:9f98248c4dd5 | 14 | |
Match314 | 0:9f98248c4dd5 | 15 | while(1) { |
Match314 | 0:9f98248c4dd5 | 16 | // until 100%, increase by 5% in 50ms intervals |
Match314 | 0:9f98248c4dd5 | 17 | for (; duty <= 100; duty += 5) { |
Match314 | 0:9f98248c4dd5 | 18 | myled = duty / 100.0; |
Match314 | 0:9f98248c4dd5 | 19 | wait_ms(50); |
Match314 | 0:9f98248c4dd5 | 20 | } |
Match314 | 0:9f98248c4dd5 | 21 | // until 0%, decrease by 5% in 50ms intervals |
Match314 | 0:9f98248c4dd5 | 22 | for (; duty >= 0; duty -= 5) { |
Match314 | 0:9f98248c4dd5 | 23 | myled = duty / 100.0; |
Match314 | 0:9f98248c4dd5 | 24 | wait_ms(50); |
Match314 | 0:9f98248c4dd5 | 25 | } |
Match314 | 0:9f98248c4dd5 | 26 | } |
Match314 | 0:9f98248c4dd5 | 27 | } |