Juan Carlos Suárez Barón
/
PWM_EXAMPLE
Keyboard and pwm led brightness
main.cpp
- Committer:
- juanitoinig84
- Date:
- 2016-06-30
- Revision:
- 0:ae5e8aa73f3a
File content as of revision 0:ae5e8aa73f3a:
#include "mbed.h" PwmOut pwm1(PTC11); PwmOut pwmLED(LED_RED); //AnalogOut dac(DAC0_OUT); Serial pc(USBTX,USBRX); uint16_t periodPWM1 = 1; uint16_t periodPWMLED =1; float dutyPWM1 = 0; float dutyPWMLED = 0; void rxInterrupt() { switch(pc.getc()) { case 'a': periodPWM1++; periodPWMLED++; pwm1.period_ms(periodPWM1); pwmLED.period_ms(periodPWMLED); break; case 's': if (periodPWM1>1) { periodPWM1--; periodPWMLED--; } pwm1.period_ms(periodPWM1); pwmLED.period_ms(periodPWMLED); break; case 'f': if (dutyPWM1<1) { dutyPWM1 = dutyPWM1 + 0.05 ; dutyPWMLED = dutyPWMLED + 0.05; } pwm1.write(dutyPWM1); pwmLED.write(dutyPWMLED); break; case 'g': if (dutyPWM1>0) { dutyPWM1 = dutyPWM1 - 0.05 ; dutyPWMLED = dutyPWMLED - 0.05; } pwm1.write(dutyPWM1); pwmLED.write(dutyPWMLED); break; default: break; } pc.printf("Period (ms): %i, Duty: %.2f \r\n", periodPWM1,dutyPWM1); } int main() { // pc.baud(115200); pc.attach(&rxInterrupt); while(1); }