Juan Carlos Suárez Barón
/
PWM_EXAMPLE
Keyboard and pwm led brightness
main.cpp@0:ae5e8aa73f3a, 2016-06-30 (annotated)
- Committer:
- juanitoinig84
- Date:
- Thu Jun 30 23:28:52 2016 +0000
- Revision:
- 0:ae5e8aa73f3a
PWM brightness with keyboard input
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
juanitoinig84 | 0:ae5e8aa73f3a | 1 | #include "mbed.h" |
juanitoinig84 | 0:ae5e8aa73f3a | 2 | |
juanitoinig84 | 0:ae5e8aa73f3a | 3 | PwmOut pwm1(PTC11); |
juanitoinig84 | 0:ae5e8aa73f3a | 4 | PwmOut pwmLED(LED_RED); |
juanitoinig84 | 0:ae5e8aa73f3a | 5 | //AnalogOut dac(DAC0_OUT); |
juanitoinig84 | 0:ae5e8aa73f3a | 6 | Serial pc(USBTX,USBRX); |
juanitoinig84 | 0:ae5e8aa73f3a | 7 | |
juanitoinig84 | 0:ae5e8aa73f3a | 8 | |
juanitoinig84 | 0:ae5e8aa73f3a | 9 | uint16_t periodPWM1 = 1; |
juanitoinig84 | 0:ae5e8aa73f3a | 10 | uint16_t periodPWMLED =1; |
juanitoinig84 | 0:ae5e8aa73f3a | 11 | float dutyPWM1 = 0; |
juanitoinig84 | 0:ae5e8aa73f3a | 12 | float dutyPWMLED = 0; |
juanitoinig84 | 0:ae5e8aa73f3a | 13 | |
juanitoinig84 | 0:ae5e8aa73f3a | 14 | void rxInterrupt() |
juanitoinig84 | 0:ae5e8aa73f3a | 15 | { |
juanitoinig84 | 0:ae5e8aa73f3a | 16 | switch(pc.getc()) { |
juanitoinig84 | 0:ae5e8aa73f3a | 17 | |
juanitoinig84 | 0:ae5e8aa73f3a | 18 | case 'a': |
juanitoinig84 | 0:ae5e8aa73f3a | 19 | |
juanitoinig84 | 0:ae5e8aa73f3a | 20 | periodPWM1++; |
juanitoinig84 | 0:ae5e8aa73f3a | 21 | periodPWMLED++; |
juanitoinig84 | 0:ae5e8aa73f3a | 22 | |
juanitoinig84 | 0:ae5e8aa73f3a | 23 | pwm1.period_ms(periodPWM1); |
juanitoinig84 | 0:ae5e8aa73f3a | 24 | pwmLED.period_ms(periodPWMLED); |
juanitoinig84 | 0:ae5e8aa73f3a | 25 | |
juanitoinig84 | 0:ae5e8aa73f3a | 26 | break; |
juanitoinig84 | 0:ae5e8aa73f3a | 27 | |
juanitoinig84 | 0:ae5e8aa73f3a | 28 | case 's': |
juanitoinig84 | 0:ae5e8aa73f3a | 29 | |
juanitoinig84 | 0:ae5e8aa73f3a | 30 | if (periodPWM1>1) { |
juanitoinig84 | 0:ae5e8aa73f3a | 31 | periodPWM1--; |
juanitoinig84 | 0:ae5e8aa73f3a | 32 | periodPWMLED--; |
juanitoinig84 | 0:ae5e8aa73f3a | 33 | } |
juanitoinig84 | 0:ae5e8aa73f3a | 34 | pwm1.period_ms(periodPWM1); |
juanitoinig84 | 0:ae5e8aa73f3a | 35 | pwmLED.period_ms(periodPWMLED); |
juanitoinig84 | 0:ae5e8aa73f3a | 36 | |
juanitoinig84 | 0:ae5e8aa73f3a | 37 | break; |
juanitoinig84 | 0:ae5e8aa73f3a | 38 | |
juanitoinig84 | 0:ae5e8aa73f3a | 39 | case 'f': |
juanitoinig84 | 0:ae5e8aa73f3a | 40 | |
juanitoinig84 | 0:ae5e8aa73f3a | 41 | if (dutyPWM1<1) { |
juanitoinig84 | 0:ae5e8aa73f3a | 42 | dutyPWM1 = dutyPWM1 + 0.05 ; |
juanitoinig84 | 0:ae5e8aa73f3a | 43 | dutyPWMLED = dutyPWMLED + 0.05; |
juanitoinig84 | 0:ae5e8aa73f3a | 44 | } |
juanitoinig84 | 0:ae5e8aa73f3a | 45 | |
juanitoinig84 | 0:ae5e8aa73f3a | 46 | pwm1.write(dutyPWM1); |
juanitoinig84 | 0:ae5e8aa73f3a | 47 | pwmLED.write(dutyPWMLED); |
juanitoinig84 | 0:ae5e8aa73f3a | 48 | |
juanitoinig84 | 0:ae5e8aa73f3a | 49 | break; |
juanitoinig84 | 0:ae5e8aa73f3a | 50 | |
juanitoinig84 | 0:ae5e8aa73f3a | 51 | |
juanitoinig84 | 0:ae5e8aa73f3a | 52 | case 'g': |
juanitoinig84 | 0:ae5e8aa73f3a | 53 | if (dutyPWM1>0) { |
juanitoinig84 | 0:ae5e8aa73f3a | 54 | dutyPWM1 = dutyPWM1 - 0.05 ; |
juanitoinig84 | 0:ae5e8aa73f3a | 55 | dutyPWMLED = dutyPWMLED - 0.05; |
juanitoinig84 | 0:ae5e8aa73f3a | 56 | } |
juanitoinig84 | 0:ae5e8aa73f3a | 57 | pwm1.write(dutyPWM1); |
juanitoinig84 | 0:ae5e8aa73f3a | 58 | pwmLED.write(dutyPWMLED); |
juanitoinig84 | 0:ae5e8aa73f3a | 59 | |
juanitoinig84 | 0:ae5e8aa73f3a | 60 | break; |
juanitoinig84 | 0:ae5e8aa73f3a | 61 | |
juanitoinig84 | 0:ae5e8aa73f3a | 62 | default: break; |
juanitoinig84 | 0:ae5e8aa73f3a | 63 | } |
juanitoinig84 | 0:ae5e8aa73f3a | 64 | |
juanitoinig84 | 0:ae5e8aa73f3a | 65 | pc.printf("Period (ms): %i, Duty: %.2f \r\n", periodPWM1,dutyPWM1); |
juanitoinig84 | 0:ae5e8aa73f3a | 66 | |
juanitoinig84 | 0:ae5e8aa73f3a | 67 | } |
juanitoinig84 | 0:ae5e8aa73f3a | 68 | |
juanitoinig84 | 0:ae5e8aa73f3a | 69 | int main() |
juanitoinig84 | 0:ae5e8aa73f3a | 70 | { |
juanitoinig84 | 0:ae5e8aa73f3a | 71 | // pc.baud(115200); |
juanitoinig84 | 0:ae5e8aa73f3a | 72 | pc.attach(&rxInterrupt); |
juanitoinig84 | 0:ae5e8aa73f3a | 73 | while(1); |
juanitoinig84 | 0:ae5e8aa73f3a | 74 | } |