2016/2/29 LED Yu-Hsuan edited
Dependencies: mbed
main.cpp@0:bf4612a77210, 2016-02-29 (annotated)
- Committer:
- ChangYuHsuan
- Date:
- Mon Feb 29 15:39:36 2016 +0000
- Revision:
- 0:bf4612a77210
- Child:
- 1:d5cf0fdad0f1
2016/2/29; LED; Yu-Hsuan 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; |
ChangYuHsuan | 0:bf4612a77210 | 10 | |
ChangYuHsuan | 0:bf4612a77210 | 11 | int timer_1_counter; |
ChangYuHsuan | 0:bf4612a77210 | 12 | |
ChangYuHsuan | 0:bf4612a77210 | 13 | void init_IO(void) |
ChangYuHsuan | 0:bf4612a77210 | 14 | { |
ChangYuHsuan | 0:bf4612a77210 | 15 | led_1 = 0; |
ChangYuHsuan | 0:bf4612a77210 | 16 | led_2 = 0; |
ChangYuHsuan | 0:bf4612a77210 | 17 | |
ChangYuHsuan | 0:bf4612a77210 | 18 | timer_1_counter = 0; |
ChangYuHsuan | 0:bf4612a77210 | 19 | } |
ChangYuHsuan | 0:bf4612a77210 | 20 | |
ChangYuHsuan | 0:bf4612a77210 | 21 | void timer_1_interrupt() |
ChangYuHsuan | 0:bf4612a77210 | 22 | { |
ChangYuHsuan | 0:bf4612a77210 | 23 | timer_1_counter += 1; |
ChangYuHsuan | 0:bf4612a77210 | 24 | } |
ChangYuHsuan | 0:bf4612a77210 | 25 | |
ChangYuHsuan | 0:bf4612a77210 | 26 | void init_TIMER(void) |
ChangYuHsuan | 0:bf4612a77210 | 27 | { |
ChangYuHsuan | 0:bf4612a77210 | 28 | timer_1.attach_us(&timer_1_interrupt, 1000.0);//1ms interrupt period (1 KHz) |
ChangYuHsuan | 0:bf4612a77210 | 29 | } |
ChangYuHsuan | 0:bf4612a77210 | 30 | |
ChangYuHsuan | 0:bf4612a77210 | 31 | int main() |
ChangYuHsuan | 0:bf4612a77210 | 32 | { |
ChangYuHsuan | 0:bf4612a77210 | 33 | init_IO(); |
ChangYuHsuan | 0:bf4612a77210 | 34 | init_TIMER(); |
ChangYuHsuan | 0:bf4612a77210 | 35 | |
ChangYuHsuan | 0:bf4612a77210 | 36 | while(1) |
ChangYuHsuan | 0:bf4612a77210 | 37 | { |
ChangYuHsuan | 0:bf4612a77210 | 38 | if(timer_1_counter == 1000) |
ChangYuHsuan | 0:bf4612a77210 | 39 | { |
ChangYuHsuan | 0:bf4612a77210 | 40 | // led_1 flashes |
ChangYuHsuan | 0:bf4612a77210 | 41 | led_1 = 1; |
ChangYuHsuan | 0:bf4612a77210 | 42 | wait_ms(1); |
ChangYuHsuan | 0:bf4612a77210 | 43 | led1 = 0; |
ChangYuHsuan | 0:bf4612a77210 | 44 | |
ChangYuHsuan | 0:bf4612a77210 | 45 | // reset timer_1_counter to zero |
ChangYuHsuan | 0:bf4612a77210 | 46 | timer_1_counter = 0; |
ChangYuHsuan | 0:bf4612a77210 | 47 | } |
ChangYuHsuan | 0:bf4612a77210 | 48 | } |
ChangYuHsuan | 0:bf4612a77210 | 49 | } |