Chen Huan
/
ticker
中断的使用
main.cpp@0:342ef23c88ad, 2018-04-11 (annotated)
- Committer:
- heroistired
- Date:
- Wed Apr 11 07:06:27 2018 +0000
- Revision:
- 0:342ef23c88ad
?????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
heroistired | 0:342ef23c88ad | 1 | #include "mbed.h" |
heroistired | 0:342ef23c88ad | 2 | |
heroistired | 0:342ef23c88ad | 3 | //演示定时器中断如何使用 |
heroistired | 0:342ef23c88ad | 4 | |
heroistired | 0:342ef23c88ad | 5 | Ticker toggle_led_ticker; //声明一个 Ticker 对象 |
heroistired | 0:342ef23c88ad | 6 | |
heroistired | 0:342ef23c88ad | 7 | DigitalOut led1(LED1); |
heroistired | 0:342ef23c88ad | 8 | |
heroistired | 0:342ef23c88ad | 9 | void toggle_led() { //中断子程序,在平衡车和无人机的项目中,控制算法就写在中断子程序中:获取陀螺仪数据,根据陀螺仪数据通过PID算法得到电机的控制量(占空比和方向),在输出到单片机管脚 |
heroistired | 0:342ef23c88ad | 10 | led1 = !led1; |
heroistired | 0:342ef23c88ad | 11 | } |
heroistired | 0:342ef23c88ad | 12 | |
heroistired | 0:342ef23c88ad | 13 | int main() { |
heroistired | 0:342ef23c88ad | 14 | // Init the ticker with the address of the function (toggle_led) to be attached and the interval (100 ms) |
heroistired | 0:342ef23c88ad | 15 | toggle_led_ticker.attach(&toggle_led, 0.1); //指定Ticker 对象的回调函数也就是中断子程序以及 中断的时间间隔即每隔多久执行一次,一般0.02或0.01即可 |
heroistired | 0:342ef23c88ad | 16 | while (true) { |
heroistired | 0:342ef23c88ad | 17 | // Do other things... |
heroistired | 0:342ef23c88ad | 18 | } |
heroistired | 0:342ef23c88ad | 19 | } |