Lei Lei
/
AUP_Lab2_PWM_UART
AUP_Lab2_PWM_UART
Fork of Lab2_PWM_UART by
main.cpp@0:65e58765adf7, 2015-05-21 (annotated)
- Committer:
- gyth
- Date:
- Thu May 21 02:23:19 2015 +0000
- Revision:
- 0:65e58765adf7
- Child:
- 1:08431d9e3d95
AUP Xi'an Lab2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gyth | 0:65e58765adf7 | 1 | #include "mbed.h" |
gyth | 0:65e58765adf7 | 2 | |
gyth | 0:65e58765adf7 | 3 | PwmOut led(D5); |
gyth | 0:65e58765adf7 | 4 | DigitalIn button_up(A2); |
gyth | 0:65e58765adf7 | 5 | DigitalIn button_center(D4); |
gyth | 0:65e58765adf7 | 6 | DigitalIn button_down(A3); |
gyth | 0:65e58765adf7 | 7 | Serial pc(SERIAL_TX, SERIAL_RX); |
gyth | 0:65e58765adf7 | 8 | |
gyth | 0:65e58765adf7 | 9 | double brightness = 1.0; |
gyth | 0:65e58765adf7 | 10 | double brightness_inc = 0.1; |
gyth | 0:65e58765adf7 | 11 | |
gyth | 0:65e58765adf7 | 12 | int main() |
gyth | 0:65e58765adf7 | 13 | { |
gyth | 0:65e58765adf7 | 14 | int bt_flag = 0; |
gyth | 0:65e58765adf7 | 15 | pc.baud(9600); |
gyth | 0:65e58765adf7 | 16 | pc.printf("Hello PWM!\r\n"); |
gyth | 0:65e58765adf7 | 17 | // Set PWM |
gyth | 0:65e58765adf7 | 18 | led.write(brightness); |
gyth | 0:65e58765adf7 | 19 | |
gyth | 0:65e58765adf7 | 20 | while (1) { |
gyth | 0:65e58765adf7 | 21 | bt_flag = 1; |
gyth | 0:65e58765adf7 | 22 | if(button_up==1) |
gyth | 0:65e58765adf7 | 23 | brightness -= brightness_inc; |
gyth | 0:65e58765adf7 | 24 | else if(button_down==1) |
gyth | 0:65e58765adf7 | 25 | brightness += brightness_inc; |
gyth | 0:65e58765adf7 | 26 | else if(button_center==1) |
gyth | 0:65e58765adf7 | 27 | brightness = (brightness>0.5)?1.0:0.0; |
gyth | 0:65e58765adf7 | 28 | else |
gyth | 0:65e58765adf7 | 29 | bt_flag = 0; |
gyth | 0:65e58765adf7 | 30 | if(bt_flag==1) |
gyth | 0:65e58765adf7 | 31 | { |
gyth | 0:65e58765adf7 | 32 | brightness = (brightness>1.0)?0.0:brightness; |
gyth | 0:65e58765adf7 | 33 | brightness = (brightness<0.0)?1.0:brightness; |
gyth | 0:65e58765adf7 | 34 | led.write(brightness); |
gyth | 0:65e58765adf7 | 35 | pc.printf("Brightness = %.1f\r\n", 1.0-brightness); |
gyth | 0:65e58765adf7 | 36 | } |
gyth | 0:65e58765adf7 | 37 | wait(0.2); // 200 ms |
gyth | 0:65e58765adf7 | 38 | } |
gyth | 0:65e58765adf7 | 39 | } |