AUP Xi'an Lab3

Dependencies:   mbed

Committer:
gyth
Date:
Thu May 21 02:23:41 2015 +0000
Revision:
0:37b9817db00a
AUP Xi'an Lab3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gyth 0:37b9817db00a 1 #include "mbed.h"
gyth 0:37b9817db00a 2
gyth 0:37b9817db00a 3 PwmOut led(D5);
gyth 0:37b9817db00a 4 InterruptIn button_up(A2);
gyth 0:37b9817db00a 5 InterruptIn button_center(D4);
gyth 0:37b9817db00a 6 InterruptIn button_down(A3);
gyth 0:37b9817db00a 7 Serial pc(SERIAL_TX, SERIAL_RX);
gyth 0:37b9817db00a 8
gyth 0:37b9817db00a 9 double brightness = 1.0;
gyth 0:37b9817db00a 10 double brightness_inc = 0.1;
gyth 0:37b9817db00a 11
gyth 0:37b9817db00a 12 void press_up()
gyth 0:37b9817db00a 13 {
gyth 0:37b9817db00a 14 brightness -= brightness_inc;
gyth 0:37b9817db00a 15 }
gyth 0:37b9817db00a 16
gyth 0:37b9817db00a 17 void press_down()
gyth 0:37b9817db00a 18 {
gyth 0:37b9817db00a 19 brightness += brightness_inc;
gyth 0:37b9817db00a 20 }
gyth 0:37b9817db00a 21
gyth 0:37b9817db00a 22 void press_center()
gyth 0:37b9817db00a 23 {
gyth 0:37b9817db00a 24 brightness = (brightness>0.5)?1.0:0.0;
gyth 0:37b9817db00a 25 }
gyth 0:37b9817db00a 26
gyth 0:37b9817db00a 27 void serial_in()
gyth 0:37b9817db00a 28 {
gyth 0:37b9817db00a 29 char ch = getc(pc);
gyth 0:37b9817db00a 30 if(ch < '0' || ch > '9')
gyth 0:37b9817db00a 31 pc.printf("Please input a number between 0 to 9.\r\n");
gyth 0:37b9817db00a 32 else
gyth 0:37b9817db00a 33 brightness = 1 - (ch - '0') / 10.0;
gyth 0:37b9817db00a 34 }
gyth 0:37b9817db00a 35
gyth 0:37b9817db00a 36 int main()
gyth 0:37b9817db00a 37 {
gyth 0:37b9817db00a 38 pc.baud(9600);
gyth 0:37b9817db00a 39 pc.attach(&serial_in);
gyth 0:37b9817db00a 40 pc.printf("Hello PWM!\r\n");
gyth 0:37b9817db00a 41 pc.printf("Please input a number between 0 to 9.\r\n");
gyth 0:37b9817db00a 42 // Set PWM
gyth 0:37b9817db00a 43 led.write(brightness);
gyth 0:37b9817db00a 44
gyth 0:37b9817db00a 45 button_up.fall(&press_up);
gyth 0:37b9817db00a 46 button_down.fall(&press_down);
gyth 0:37b9817db00a 47 button_center.fall(&press_center);
gyth 0:37b9817db00a 48
gyth 0:37b9817db00a 49 while (1) {
gyth 0:37b9817db00a 50 brightness = (brightness>1.0)?0.0:brightness;
gyth 0:37b9817db00a 51 brightness = (brightness<0.0)?1.0:brightness;
gyth 0:37b9817db00a 52 led.write(brightness);
gyth 0:37b9817db00a 53 }
gyth 0:37b9817db00a 54 }