pwm

Dependencies:   mbed

Committer:
miguelmstein
Date:
Thu Apr 06 16:40:18 2017 +0000
Revision:
0:3a363ca6f374
2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
miguelmstein 0:3a363ca6f374 1 #include "mbed.h"
miguelmstein 0:3a363ca6f374 2
miguelmstein 0:3a363ca6f374 3 DigitalOut my_led(LED1);
miguelmstein 0:3a363ca6f374 4 InterruptIn my_button(USER_BUTTON);
miguelmstein 0:3a363ca6f374 5 PwmOut my_pwm(PB_3);
miguelmstein 0:3a363ca6f374 6
miguelmstein 0:3a363ca6f374 7 void pressed() {
miguelmstein 0:3a363ca6f374 8 if (my_pwm.read() == 0.25) {
miguelmstein 0:3a363ca6f374 9 my_pwm.write(0.75);
miguelmstein 0:3a363ca6f374 10 }
miguelmstein 0:3a363ca6f374 11 else {
miguelmstein 0:3a363ca6f374 12 my_pwm.write(0.25);
miguelmstein 0:3a363ca6f374 13 }
miguelmstein 0:3a363ca6f374 14 }
miguelmstein 0:3a363ca6f374 15
miguelmstein 0:3a363ca6f374 16 int main()
miguelmstein 0:3a363ca6f374 17 {
miguelmstein 0:3a363ca6f374 18 // Set PWM
miguelmstein 0:3a363ca6f374 19 my_pwm.period_ms(10);
miguelmstein 0:3a363ca6f374 20 my_pwm.write(0.5);
miguelmstein 0:3a363ca6f374 21
miguelmstein 0:3a363ca6f374 22 // Set button
miguelmstein 0:3a363ca6f374 23 my_button.fall(&pressed);
miguelmstein 0:3a363ca6f374 24
miguelmstein 0:3a363ca6f374 25 while (1) {
miguelmstein 0:3a363ca6f374 26 my_led = !my_led;
miguelmstein 0:3a363ca6f374 27 wait(0.5); // 500 ms
miguelmstein 0:3a363ca6f374 28 }
miguelmstein 0:3a363ca6f374 29 }