2016/2/29 LED Yu-Hsuan edited
Dependencies: mbed
main.cpp@2:9e5b526db3aa, 2016-03-03 (annotated)
- Committer:
- winstonkuo
- Date:
- Thu Mar 03 05:09:48 2016 +0000
- Revision:
- 2:9e5b526db3aa
- Parent:
- 1:d5cf0fdad0f1
- Child:
- 3:b81cc31bfcba
2016/3/3; LED; Winston edited
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ChangYuHsuan | 0:bf4612a77210 | 1 | /*LAB_LED*/ |
ChangYuHsuan | 0:bf4612a77210 | 2 | #include "mbed.h" |
ChangYuHsuan | 0:bf4612a77210 | 3 | |
ChangYuHsuan | 0:bf4612a77210 | 4 | #define LED_1 PA_5 |
ChangYuHsuan | 0:bf4612a77210 | 5 | #define LED_2 D12 |
ChangYuHsuan | 0:bf4612a77210 | 6 | |
ChangYuHsuan | 0:bf4612a77210 | 7 | DigitalOut led_1(LED_1); |
ChangYuHsuan | 0:bf4612a77210 | 8 | DigitalOut led_2(LED_2); |
ChangYuHsuan | 0:bf4612a77210 | 9 | Ticker timer_1; |
winstonkuo | 2:9e5b526db3aa | 10 | bool light; |
ChangYuHsuan | 0:bf4612a77210 | 11 | |
ChangYuHsuan | 0:bf4612a77210 | 12 | int timer_1_counter; |
ChangYuHsuan | 0:bf4612a77210 | 13 | |
ChangYuHsuan | 0:bf4612a77210 | 14 | void init_IO(void) |
ChangYuHsuan | 0:bf4612a77210 | 15 | { |
ChangYuHsuan | 0:bf4612a77210 | 16 | led_1 = 0; |
ChangYuHsuan | 0:bf4612a77210 | 17 | led_2 = 0; |
ChangYuHsuan | 0:bf4612a77210 | 18 | |
ChangYuHsuan | 0:bf4612a77210 | 19 | timer_1_counter = 0; |
winstonkuo | 2:9e5b526db3aa | 20 | light = false; |
ChangYuHsuan | 0:bf4612a77210 | 21 | } |
ChangYuHsuan | 0:bf4612a77210 | 22 | |
ChangYuHsuan | 0:bf4612a77210 | 23 | void timer_1_interrupt() |
ChangYuHsuan | 0:bf4612a77210 | 24 | { |
ChangYuHsuan | 0:bf4612a77210 | 25 | timer_1_counter += 1; |
ChangYuHsuan | 0:bf4612a77210 | 26 | } |
ChangYuHsuan | 0:bf4612a77210 | 27 | |
ChangYuHsuan | 0:bf4612a77210 | 28 | void init_TIMER(void) |
ChangYuHsuan | 0:bf4612a77210 | 29 | { |
ChangYuHsuan | 0:bf4612a77210 | 30 | timer_1.attach_us(&timer_1_interrupt, 1000.0);//1ms interrupt period (1 KHz) |
ChangYuHsuan | 0:bf4612a77210 | 31 | } |
ChangYuHsuan | 0:bf4612a77210 | 32 | |
ChangYuHsuan | 0:bf4612a77210 | 33 | int main() |
ChangYuHsuan | 0:bf4612a77210 | 34 | { |
ChangYuHsuan | 0:bf4612a77210 | 35 | init_IO(); |
ChangYuHsuan | 0:bf4612a77210 | 36 | init_TIMER(); |
ChangYuHsuan | 0:bf4612a77210 | 37 | |
ChangYuHsuan | 0:bf4612a77210 | 38 | while(1) |
ChangYuHsuan | 0:bf4612a77210 | 39 | { |
ChangYuHsuan | 0:bf4612a77210 | 40 | if(timer_1_counter == 1000) |
ChangYuHsuan | 0:bf4612a77210 | 41 | { |
ChangYuHsuan | 0:bf4612a77210 | 42 | // led_1 flashes |
winstonkuo | 2:9e5b526db3aa | 43 | if(light == false) |
winstonkuo | 2:9e5b526db3aa | 44 | { |
winstonkuo | 2:9e5b526db3aa | 45 | led_1 = 1; |
winstonkuo | 2:9e5b526db3aa | 46 | light = true; |
winstonkuo | 2:9e5b526db3aa | 47 | } |
winstonkuo | 2:9e5b526db3aa | 48 | else |
winstonkuo | 2:9e5b526db3aa | 49 | { |
winstonkuo | 2:9e5b526db3aa | 50 | led_1 = 0; |
winstonkuo | 2:9e5b526db3aa | 51 | light = false; |
winstonkuo | 2:9e5b526db3aa | 52 | } |
ChangYuHsuan | 0:bf4612a77210 | 53 | |
ChangYuHsuan | 0:bf4612a77210 | 54 | // reset timer_1_counter to zero |
ChangYuHsuan | 0:bf4612a77210 | 55 | timer_1_counter = 0; |
ChangYuHsuan | 0:bf4612a77210 | 56 | } |
ChangYuHsuan | 0:bf4612a77210 | 57 | } |
ChangYuHsuan | 0:bf4612a77210 | 58 | } |