12919浏览
查看: 12919|回复: 12

Arduino多线程库文件,标题只能这么详细了

[复制链接]
如题,这个是一个很强大的多线程库文件。
具体可以百度下,或者点击这:点我
例程里跑了2个while(1)
很好很强大
很好很强大(我是回音,是回音,回音,音)


下载附件ProtoThreads.zip

要是下载了不顶一个可不是好孩子啊

Ricky  NPC

发表于 2014-3-26 14:12:39

给多个样例,看看怎么玩的。
回复

使用道具 举报

Holiday  初级技匠
 楼主|

发表于 2014-3-26 18:26:59

Ricky 发表于 2014-3-26 14:12
给多个样例,看看怎么玩的。


好吧,程序如下:

  1. #include <pt.h>//ProtoThreads必须包含的头文件
  2. static int counter1,counter2,state1=0,state2=0; //counter为定时计数器,state为每个灯的状态
  3. static int protothread1(struct pt *pt) //线程1,控制灯1
  4. {  
  5.   PT_BEGIN(pt);  //线程开始
  6.   while(1) //每个线程都不会死
  7.   {  
  8.     PT_WAIT_UNTIL(pt, counter1==1); //如果时间满了1秒,则继续执行,否则记录运行点,退出线程1
  9.     digitalWrite(12,state1);
  10.     state1=!state1;//灯状态反转
  11.     counter1=0; //计数器置零
  12.   }
  13.   PT_END(pt); //线程结束
  14. }
  15. static int protothread2(struct pt *pt) //线程2,控制灯2
  16. {
  17.   PT_BEGIN(pt); //线程开始
  18.   while(1) {    //每个线程都不会死
  19.     PT_WAIT_UNTIL(pt, counter2==5); //如果时间满了5秒,则继续执行,否则记录运行点,退出线程2
  20.     counter2=0;  //计数清零
  21.     digitalWrite(13,state2);
  22.     state2=!state2; //灯状态反转
  23.   }
  24.   PT_END(pt);  //线程结束
  25. }
  26. static struct pt pt1, pt2;
  27. void setup()
  28. {
  29.   pinMode(12,OUTPUT);
  30.   pinMode(13,OUTPUT);
  31.   PT_INIT(&pt1);  //线程1初始化
  32.   PT_INIT(&pt2);  //线程2初始化
  33. }
  34. void loop () //这就是进行线程调度的地方
  35. {
  36.     protothread1(&pt1);  //执行线程1
  37.     protothread2(&pt2);  //执行线程2
  38.     delay(1000);  //时间片,每片1秒,可根据具体应用设置大小
  39.     counter1++;
  40.     counter2++;
  41.   }
复制代码
回复

使用道具 举报

Grey  中级技匠

发表于 2014-3-26 18:28:12

学习一下,看看怎么实现的
回复

使用道具 举报

Angelo  初级技匠

发表于 2014-3-26 22:40:53

  1. typedef unsigned short lc_t;
  2. #define LC_INIT(s) s = 0;
  3. #define LC_RESUME(s) switch(s) { case 0:
  4. #define LC_SET(s) s = __LINE__; case __LINE__:
  5. #define LC_END(s) }
复制代码




哈哈,如果写7万行的代码这个库就不能用了~~~;P
回复

使用道具 举报

Holiday  初级技匠
 楼主|

发表于 2014-3-27 09:50:16

Angelo 发表于 2014-3-26 22:40
哈哈,如果写7万行的代码这个库就不能用了~~~

{:3_45:}  7万。。。。好庞大
回复

使用道具 举报

Youyou  初级技匠

发表于 2014-3-27 10:08:24

神器,写程序再也不用怕卡了!越看越像RTX Kernel的用法了。
回复

使用道具 举报

Holiday  初级技匠
 楼主|

发表于 2014-3-27 11:19:10

Youyou 发表于 2014-3-27 10:08
神器,写程序再也不用怕卡了!越看越像RTX Kernel的用法了。

这个也不能用delay的,不过有代替delay的子函数,还是很赞的
回复

使用道具 举报

Angelo  初级技匠

发表于 2014-3-28 11:40:34

要仔细研究一下这个库才会发现它的NB之处啊~
回复

使用道具 举报

pizg  学徒

发表于 2014-4-9 22:21:07

這麼好的東東
不頂一下不行
回复

使用道具 举报

Eric  初级技神

发表于 2014-12-14 12:12:40

无意中看到了这个应用~不错啊~简单实用,前提要看懂库文件哦,哈哈哈,超赞~~~顶起来,加精啊
回复

使用道具 举报

1214032625  见习技师

发表于 2018-4-10 10:30:23

用这个库,复制代码,报错了是什么原因?Arduino:1.6.6 (Windows 10), 开发板:"Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

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]

  报告将会包含更多的信息
  "在编译时,输出详细信息"
  在 文件>首选项 中启用
回复

使用道具 举报

100万.py  学徒

发表于 2021-7-23 15:54:07

怎么把程序下载到开发板?打开都没找到main???
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail