![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
test
Dependencies: mbed
main.cpp@0:41dfb0b1f108, 2016-09-09 (annotated)
- Committer:
- xx123456987
- Date:
- Fri Sep 09 05:40:54 2016 +0000
- Revision:
- 0:41dfb0b1f108
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xx123456987 | 0:41dfb0b1f108 | 1 | #include "mbed.h" |
xx123456987 | 0:41dfb0b1f108 | 2 | #define pwm_period_us 500 |
xx123456987 | 0:41dfb0b1f108 | 3 | //#define pwm_pulsewidth_us 400 |
xx123456987 | 0:41dfb0b1f108 | 4 | //實驗說明:利用鍵盤+-符號來控制馬達速度 |
xx123456987 | 0:41dfb0b1f108 | 5 | PwmOut pwm_1(D9); |
xx123456987 | 0:41dfb0b1f108 | 6 | PwmOut pwm_2(D10); |
xx123456987 | 0:41dfb0b1f108 | 7 | PwmOut pwm_3(D11); |
xx123456987 | 0:41dfb0b1f108 | 8 | float pwm = 0.5; |
xx123456987 | 0:41dfb0b1f108 | 9 | DigitalIn button(USER_BUTTON); |
xx123456987 | 0:41dfb0b1f108 | 10 | int main() |
xx123456987 | 0:41dfb0b1f108 | 11 | { |
xx123456987 | 0:41dfb0b1f108 | 12 | char a; |
xx123456987 | 0:41dfb0b1f108 | 13 | pwm_1.period_us(pwm_period_us); |
xx123456987 | 0:41dfb0b1f108 | 14 | pwm_2.period_us(pwm_period_us); |
xx123456987 | 0:41dfb0b1f108 | 15 | pwm_3.period_us(pwm_period_us); |
xx123456987 | 0:41dfb0b1f108 | 16 | |
xx123456987 | 0:41dfb0b1f108 | 17 | pwm_1.write(0.5); |
xx123456987 | 0:41dfb0b1f108 | 18 | pwm_2.write(0.5); |
xx123456987 | 0:41dfb0b1f108 | 19 | pwm_3.write(0.5); |
xx123456987 | 0:41dfb0b1f108 | 20 | printf("PWM all RESET to %.1f %%\n", pwm_1.read() * 100); |
xx123456987 | 0:41dfb0b1f108 | 21 | |
xx123456987 | 0:41dfb0b1f108 | 22 | while(1) { |
xx123456987 | 0:41dfb0b1f108 | 23 | scanf("%c",&a); |
xx123456987 | 0:41dfb0b1f108 | 24 | switch (a) { |
xx123456987 | 0:41dfb0b1f108 | 25 | case '+': |
xx123456987 | 0:41dfb0b1f108 | 26 | pwm += 0.05; |
xx123456987 | 0:41dfb0b1f108 | 27 | pwm_1.write(pwm); |
xx123456987 | 0:41dfb0b1f108 | 28 | pwm_2.write(pwm); |
xx123456987 | 0:41dfb0b1f108 | 29 | pwm_3.write(pwm); |
xx123456987 | 0:41dfb0b1f108 | 30 | printf("%c ; PWM all set to %.1f %%\n", a, pwm_1.read() * 100); |
xx123456987 | 0:41dfb0b1f108 | 31 | break; |
xx123456987 | 0:41dfb0b1f108 | 32 | case '-': |
xx123456987 | 0:41dfb0b1f108 | 33 | pwm -= 0.05; |
xx123456987 | 0:41dfb0b1f108 | 34 | pwm_1.write(pwm); |
xx123456987 | 0:41dfb0b1f108 | 35 | pwm_2.write(pwm); |
xx123456987 | 0:41dfb0b1f108 | 36 | pwm_3.write(pwm); |
xx123456987 | 0:41dfb0b1f108 | 37 | printf("%c ; PWM all set to %.1f %%\n", a, pwm_1.read() * 100); |
xx123456987 | 0:41dfb0b1f108 | 38 | break; |
xx123456987 | 0:41dfb0b1f108 | 39 | /*case '3': |
xx123456987 | 0:41dfb0b1f108 | 40 | pwm_3.write(pwm); |
xx123456987 | 0:41dfb0b1f108 | 41 | printf("%c ; pwm_3 set to %.1f %%\n", a, pwm_3.read() * 100); |
xx123456987 | 0:41dfb0b1f108 | 42 | break; |
xx123456987 | 0:41dfb0b1f108 | 43 | |
xx123456987 | 0:41dfb0b1f108 | 44 | case '0': |
xx123456987 | 0:41dfb0b1f108 | 45 | pwm_1.write(0.5); |
xx123456987 | 0:41dfb0b1f108 | 46 | pwm_2.write(0.5); |
xx123456987 | 0:41dfb0b1f108 | 47 | pwm_3.write(0.5); |
xx123456987 | 0:41dfb0b1f108 | 48 | printf("%c ; pwm all reset to %.1f %%\n", a, pwm_1.read() * 100); |
xx123456987 | 0:41dfb0b1f108 | 49 | break; |
xx123456987 | 0:41dfb0b1f108 | 50 | */ |
xx123456987 | 0:41dfb0b1f108 | 51 | default: |
xx123456987 | 0:41dfb0b1f108 | 52 | pwm_1.write(0.5); |
xx123456987 | 0:41dfb0b1f108 | 53 | pwm_2.write(0.5); |
xx123456987 | 0:41dfb0b1f108 | 54 | pwm_3.write(0.5); |
xx123456987 | 0:41dfb0b1f108 | 55 | printf("PWM all RESET to %.1f %%\n", pwm_1.read() * 100); |
xx123456987 | 0:41dfb0b1f108 | 56 | break; |
xx123456987 | 0:41dfb0b1f108 | 57 | } |
xx123456987 | 0:41dfb0b1f108 | 58 | } |
xx123456987 | 0:41dfb0b1f108 | 59 | } |