Arduino多线程库文件,标题只能这么详细了
2014-03-262.5万
AI 快速预览详细
ProtoThreads是一个强大的Arduino多线程库文件,支持在Arduino项目中实现多任务处理。通过使用该库文件,可以在同一个项目中同时运行多个while(1)循环,从而提升项目的并发处理能力。下载并使用ProtoThreads库文件,可以显著提高项目的效率和灵活性。
|
如题,这个是一个很强大的多线程库文件。 具体可以百度下,或者点击这:点我 例程里跑了2个while(1) 很好很强大 ProtoThreads.zip要是下载了不顶一个可不是好孩子啊 |





C:\Users\Administrator\Documents\Arduino\sketch_apr10b\sketch_apr10b.ino: In function 'int protothread1(pt*)':
sketch_apr10b:5: error: 'int protothread1(pt*)' was declared 'extern' and later 'static' [-fpermissive]
static int protothread1(struct pt *pt) //绾跨▼1锛屾帶鍒剁伅1
^
sketch_apr10b:5: error: previous declaration of 'int protothread1(pt*)' [-fpermissive]
static int protothread1(struct pt *pt) //绾跨▼1锛屾帶鍒剁伅1
^
C:\Users\Administrator\Documents\Arduino\sketch_apr10b\sketch_apr10b.ino: In function 'int protothread2(pt*)':
sketch_apr10b:19: error: 'int protothread2(pt*)' was declared 'extern' and later 'static' [-fpermissive]
static int protothread2(struct pt *pt) //绾跨▼2锛屾帶鍒剁伅2
^
sketch_apr10b:6: error: previous declaration of 'int protothread2(pt*)' [-fpermissive]
{
^
exit status 1
'int protothread1(pt*)' was declared 'extern' and later 'static' [-fpermissive]
报告将会包含更多的信息
"在编译时,输出详细信息"
在 文件>首选项 中启用
不頂一下不行
#define LC_INIT(s) s = 0;
#define LC_RESUME(s) switch(s) { case 0:
#define LC_SET(s) s = __LINE__; case __LINE__:
#define LC_END(s) }[/code]
哈哈,如果写7万行的代码这个库就不能用了~~~;P
好吧,程序如下:
[code]#include
static int counter1,counter2,state1=0,state2=0; //counter为定时计数器,state为每个灯的状态
static int protothread1(struct pt *pt) //线程1,控制灯1
{
PT_BEGIN(pt); //线程开始
while(1) //每个线程都不会死
{
PT_WAIT_UNTIL(pt, counter1==1); //如果时间满了1秒,则继续执行,否则记录运行点,退出线程1
digitalWrite(12,state1);
state1=!state1;//灯状态反转
counter1=0; //计数器置零
}
PT_END(pt); //线程结束
}
static int protothread2(struct pt *pt) //线程2,控制灯2
{
PT_BEGIN(pt); //线程开始
while(1) { //每个线程都不会死
PT_WAIT_UNTIL(pt, counter2==5); //如果时间满了5秒,则继续执行,否则记录运行点,退出线程2
counter2=0; //计数清零
digitalWrite(13,state2);
state2=!state2; //灯状态反转
}
PT_END(pt); //线程结束
}
static struct pt pt1, pt2;
void setup()
{
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
PT_INIT(&pt1); //线程1初始化
PT_INIT(&pt2); //线程2初始化
}
void loop () //这就是进行线程调度的地方
{
protothread1(&pt1); //执行线程1
protothread2(&pt2); //执行线程2
delay(1000); //时间片,每片1秒,可根据具体应用设置大小
counter1++;
counter2++;
} [/code]