一階邏輯開根號計算,使用 ticker 每隔二秒執行一次 LED 閃爍,直到計算完成後恆亮
main.cpp@0:41518d1f552d, 2015-02-14 (annotated)
- Committer:
- mbedschool
- Date:
- Sat Feb 14 04:56:34 2015 +0000
- Revision:
- 0:41518d1f552d
ticker
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mbedschool | 0:41518d1f552d | 1 | #include "mbed.h" |
| mbedschool | 0:41518d1f552d | 2 | |
| mbedschool | 0:41518d1f552d | 3 | Ticker flipper; |
| mbedschool | 0:41518d1f552d | 4 | DigitalOut led1(LED1); |
| mbedschool | 0:41518d1f552d | 5 | bool c=false; |
| mbedschool | 0:41518d1f552d | 6 | void flip() { |
| mbedschool | 0:41518d1f552d | 7 | while (!c) { |
| mbedschool | 0:41518d1f552d | 8 | led1 = !led1; |
| mbedschool | 0:41518d1f552d | 9 | } |
| mbedschool | 0:41518d1f552d | 10 | led1=1; |
| mbedschool | 0:41518d1f552d | 11 | } |
| mbedschool | 0:41518d1f552d | 12 | |
| mbedschool | 0:41518d1f552d | 13 | int main() { |
| mbedschool | 0:41518d1f552d | 14 | |
| mbedschool | 0:41518d1f552d | 15 | flipper.attach(&flip, 2.0); // the address of the function to be attached (flip) and the interval (2 seconds) |
| mbedschool | 0:41518d1f552d | 16 | // spin in a main loop. flipper will interrupt it to call flip |
| mbedschool | 0:41518d1f552d | 17 | while(1) { |
| mbedschool | 0:41518d1f552d | 18 | unsigned long n=100000000; |
| mbedschool | 0:41518d1f552d | 19 | float x=0; |
| mbedschool | 0:41518d1f552d | 20 | while(x*x <n) |
| mbedschool | 0:41518d1f552d | 21 | x = x + 0.01 ; |
| mbedschool | 0:41518d1f552d | 22 | c=true; |
| mbedschool | 0:41518d1f552d | 23 | } |
| mbedschool | 0:41518d1f552d | 24 | } |