2016/2/29 LED Yu-Hsuan edited

Dependencies:   mbed

Committer:
winstonkuo
Date:
Thu Mar 03 05:35:47 2016 +0000
Revision:
3:b81cc31bfcba
Parent:
2:9e5b526db3aa
2016/3/3; LED; YuHsuan edited

Who changed what in which revision?

UserRevisionLine numberNew 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
winstonkuo 3:b81cc31bfcba 41 if(led_1 == 0)
winstonkuo 2:9e5b526db3aa 42 led_1 = 1;
winstonkuo 2:9e5b526db3aa 43 else
winstonkuo 2:9e5b526db3aa 44 led_1 = 0;
ChangYuHsuan 0:bf4612a77210 45
ChangYuHsuan 0:bf4612a77210 46 // reset timer_1_counter to zero
ChangYuHsuan 0:bf4612a77210 47 timer_1_counter = 0;
ChangYuHsuan 0:bf4612a77210 48 }
ChangYuHsuan 0:bf4612a77210 49 }
ChangYuHsuan 0:bf4612a77210 50 }