Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of TutoElectro_PWM by
main.cpp@0:647746eea42d, 2015-01-04 (annotated)
- Committer:
- Twistx77
- Date:
- Sun Jan 04 22:12:46 2015 +0000
- Revision:
- 0:647746eea42d
- Child:
- 1:778a662bafcf
Simple PWM tutorial using FRDM-KL25Z made by Twistx77 from TutoElectro
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Twistx77 | 0:647746eea42d | 1 | #include "mbed.h" |
| Twistx77 | 0:647746eea42d | 2 | |
| Twistx77 | 0:647746eea42d | 3 | PwmOut pwm1(PTB0); |
| Twistx77 | 0:647746eea42d | 4 | PwmOut pwmLED(LED_RED); |
| Twistx77 | 0:647746eea42d | 5 | Serial pc(USBTX,USBRX); |
| Twistx77 | 0:647746eea42d | 6 | |
| Twistx77 | 0:647746eea42d | 7 | uint16_t periodPWM1 = 1; |
| Twistx77 | 0:647746eea42d | 8 | uint16_t periodPWMLED =1; |
| Twistx77 | 0:647746eea42d | 9 | float dutyPWM1 = 0; |
| Twistx77 | 0:647746eea42d | 10 | float dutyPWMLED = 0; |
| Twistx77 | 0:647746eea42d | 11 | |
| Twistx77 | 0:647746eea42d | 12 | void rxInterrupt() |
| Twistx77 | 0:647746eea42d | 13 | { |
| Twistx77 | 0:647746eea42d | 14 | |
| Twistx77 | 0:647746eea42d | 15 | switch(pc.getc()) { |
| Twistx77 | 0:647746eea42d | 16 | |
| Twistx77 | 0:647746eea42d | 17 | case 'a': |
| Twistx77 | 0:647746eea42d | 18 | |
| Twistx77 | 0:647746eea42d | 19 | periodPWM1++; |
| Twistx77 | 0:647746eea42d | 20 | periodPWMLED++; |
| Twistx77 | 0:647746eea42d | 21 | |
| Twistx77 | 0:647746eea42d | 22 | pwm1.period_ms(periodPWM1); |
| Twistx77 | 0:647746eea42d | 23 | pwmLED.period_ms(periodPWMLED); |
| Twistx77 | 0:647746eea42d | 24 | |
| Twistx77 | 0:647746eea42d | 25 | |
| Twistx77 | 0:647746eea42d | 26 | |
| Twistx77 | 0:647746eea42d | 27 | break; |
| Twistx77 | 0:647746eea42d | 28 | |
| Twistx77 | 0:647746eea42d | 29 | case 's': |
| Twistx77 | 0:647746eea42d | 30 | |
| Twistx77 | 0:647746eea42d | 31 | if (periodPWM1>1) { |
| Twistx77 | 0:647746eea42d | 32 | periodPWM1--; |
| Twistx77 | 0:647746eea42d | 33 | periodPWMLED--; |
| Twistx77 | 0:647746eea42d | 34 | } |
| Twistx77 | 0:647746eea42d | 35 | |
| Twistx77 | 0:647746eea42d | 36 | pwm1.period_ms(periodPWM1); |
| Twistx77 | 0:647746eea42d | 37 | pwmLED.period_ms(periodPWMLED); |
| Twistx77 | 0:647746eea42d | 38 | |
| Twistx77 | 0:647746eea42d | 39 | break; |
| Twistx77 | 0:647746eea42d | 40 | |
| Twistx77 | 0:647746eea42d | 41 | case 'f': |
| Twistx77 | 0:647746eea42d | 42 | |
| Twistx77 | 0:647746eea42d | 43 | if (dutyPWM1<1) { |
| Twistx77 | 0:647746eea42d | 44 | dutyPWM1 = dutyPWM1 + 0.05 ; |
| Twistx77 | 0:647746eea42d | 45 | dutyPWMLED = dutyPWMLED + 0.05; |
| Twistx77 | 0:647746eea42d | 46 | } |
| Twistx77 | 0:647746eea42d | 47 | |
| Twistx77 | 0:647746eea42d | 48 | pwm1.write(dutyPWM1); |
| Twistx77 | 0:647746eea42d | 49 | pwmLED.write(dutyPWMLED); |
| Twistx77 | 0:647746eea42d | 50 | |
| Twistx77 | 0:647746eea42d | 51 | break; |
| Twistx77 | 0:647746eea42d | 52 | |
| Twistx77 | 0:647746eea42d | 53 | |
| Twistx77 | 0:647746eea42d | 54 | case 'g': |
| Twistx77 | 0:647746eea42d | 55 | |
| Twistx77 | 0:647746eea42d | 56 | if (dutyPWM1>0) { |
| Twistx77 | 0:647746eea42d | 57 | dutyPWM1 = dutyPWM1 - 0.05 ; |
| Twistx77 | 0:647746eea42d | 58 | dutyPWMLED = dutyPWMLED - 0.05; |
| Twistx77 | 0:647746eea42d | 59 | } |
| Twistx77 | 0:647746eea42d | 60 | |
| Twistx77 | 0:647746eea42d | 61 | pwm1.write(dutyPWM1); |
| Twistx77 | 0:647746eea42d | 62 | pwmLED.write(dutyPWMLED); |
| Twistx77 | 0:647746eea42d | 63 | |
| Twistx77 | 0:647746eea42d | 64 | break; |
| Twistx77 | 0:647746eea42d | 65 | |
| Twistx77 | 0:647746eea42d | 66 | default: break; |
| Twistx77 | 0:647746eea42d | 67 | } |
| Twistx77 | 0:647746eea42d | 68 | |
| Twistx77 | 0:647746eea42d | 69 | pc.printf("Period (ms): %i, Duty: %.2f \r\n", periodPWM1,dutyPWM1); |
| Twistx77 | 0:647746eea42d | 70 | |
| Twistx77 | 0:647746eea42d | 71 | } |
| Twistx77 | 0:647746eea42d | 72 | |
| Twistx77 | 0:647746eea42d | 73 | int main() |
| Twistx77 | 0:647746eea42d | 74 | { |
| Twistx77 | 0:647746eea42d | 75 | |
| Twistx77 | 0:647746eea42d | 76 | pc.baud(115200); |
| Twistx77 | 0:647746eea42d | 77 | pc.attach(&rxInterrupt); |
| Twistx77 | 0:647746eea42d | 78 | |
| Twistx77 | 0:647746eea42d | 79 | while(1); |
| Twistx77 | 0:647746eea42d | 80 | } |
