(8)mbed实战——最简单的多任务运行和外部中断
时间:10-02
整理:3721RD
点击:
我们平时说的定时器中断,在mbed中称为断续装置可以通过Ticker来定义断续装置
函数api:
attach:附加断续装置要调用的函数,以s为间隔,支持浮点数据,比如0.01s;
attach_us:功能同上,间隔为us
detach:分离函数
比如我写了一个反转LED的函数
- DigitalOut myled(LED1);
- void LED_fun(void)
- {myled != myled;}
- Ticker s1hz_task;
- s1hz_task.attach(&LED_fun, 1);
- #include<mbed.h>
- void LED_fun(void);
- Ticker s1hz_tash;
- DigitalOut myled;
- void LED_fun(void)
- {myled !=myled;}
- int main{
- s1hztask.attach(&LED_fun, 1);
- while(1);
- }
- #include "mbed.h"
- #include "1602_iic_sw.h"
- #define GREEN_LED PB_0
- #define BLUE_LED PB_7
- #define RED_LED PB_14
- //DigitalOut myled1(GREEN_LED);
- //DigitalOut myled2(BLUE_LED);
- //DigitalOut myled3(RED_LED);
- PwmOut PWM1(GREEN_LED);
- PwmOut PWM2(BLUE_LED);
- PwmOut PWM3(RED_LED);
- // I2C_SCL = PB_8,
- // I2C_SDA = PB_9,
- IIC_1602 my1602(I2C_SDA, I2C_SCL);
- float gValue = 0;
- char data = 0;
- Ticker s100hz_tick;
- Ticker s1hz_tick;
- Serial pc(PA_2,PA_3);//tx rx
- #define UP 0 //上
- #define Down 1 //下
- #define Enter 2 //确定
- #define Esc 3 //退出
- void task1(void);
- void NullSubs(void) //空函数
- { }
- int main() {
- PWM1.period(0.01);
- PWM2.period(0.01);
- PWM3.period(0.01);
- // my1602.lcdPrint("Hacker");
- printf("Init ok\n\n");
- s1hz_tick.attach(&task1, 1);
- while(1) {
- }
- }
- void task1(void)
- {
- static u16 i = 0;
- PWM1 = gValue;
- PWM2 = 1 - gValue;
- PWM3 = gValue;
- gValue = gValue + 0.001;
- wait(0.001); // 1 sec
-
- printf("Loop = %d\n", i);
- i = (i+1)%1000;
- if(gValue >= 1)
- {
- static char ack_value = 0;
- gValue = 0;
-
- printf("ACK = %d\n", ack_value);
- }
- }