[其它] Arduino多线程ProtoThread测试

[复制链接]
6948|11
 楼主| hbzjt2011 发表于 2016-12-2 10:55 | 显示全部楼层 |阅读模式
本帖最后由 hbzjt2011 于 2016-12-2 10:59 编辑

Arduino Nano是Arduino USB接口的微型版本,最大的不同是没有电源插座以及USB接口是Mini-B型插座。Arduino Nano是尺寸非常小的而且可以直接插在面包板上使用。其处理器核心ATmega328(Nano3.0),同时具有14路数字输入/输出口(其中6路可作为PWM输出),8路模拟输入,一个16MHz晶体振荡器,一个mini-B USB口,一个ICSP header和一个复位按钮。
正面照:                                            反面照:
628535825_副本.jpg                1428114206_副本.jpg
Arduino Nano供电方式
  • mini-B USB接口供电
  • pin27 +5V接外部直流5V电源
存储器
ATmega328包括了片上32KB Flash,其中2KB用于Bootloader。同时还有2KB SRAM和1KB EEPROM。
输入输出
  • 14路数字输入输出口:工作电压为5V,每一路能输出和接入最大电流为40mA。每一路配置了20-50K欧姆内部上拉电阻(默认不连接)。除此之外,有些引脚有特定的功能
    • 串口信号RX(0号)、TX(1号): 提供TTL电压水平的串口接收信号,与FT232Rl的相应引脚相连。
    • 外部中断(2号和3号):触发中断引脚,可设成上升沿、下降沿或同时触发。
    • 脉冲宽度调制PWM(3、5、6、9、10 、11):提供6路8位PWM输出。
    • SPI(10(SS),11(MOSI),12(MISO),13(SCK)):SPI通信接口。
    • LED(13号):Arduino专门用于测试LED的保留接口,输出为高时点亮LED,反之输出为低时LED熄灭。
  • 6路模拟输入A0到A5:每一路具有10位的分辨率(即输入有1024个不同值),默认输入信号范围为0到5V,可以通过AREF调整输入上限。除此之外,有些引脚有特定功能
    • TWI接口(SDA A4和SCL A5):支持通信接口(兼容I2C总线)。
  • AREF:模拟输入信号的参考电压。
  • Reset:信号为低时复位单片机芯片。
通信接口
  • 串口:ATmega328内置的UART可以通过数字口0(RX)和1(TX)与外部实现串口通信;ATmega16U2可以访问数字口实现USB上的虚拟串口。
  • TWI(兼容I2C)接口:
  • SPI 接口:
下载程序
  • Arduino Nano上的MCU已经预置了bootloader程序,因此可以通过Arduino软件直接下载程序。
  • 可以直接通过Nano上ICSP header直接下载程序到MCU。
电路图:
ArduinoNano30Schematic.pdf (60.54 KB, 下载次数: 33)

介绍完Arduino片上资源后,下面介绍主题:ProtoThread的应用,先上代码:
  1. /**
  2. * This is a very small example that shows how to use
  3. * protothreads. The program consists of two protothreads that wait
  4. * for each other to toggle a variable.
  5. */

  6. /* We must always include pt.h in our protothreads code. */
  7. #include "pt.h"

  8. /* Two flags that the two protothread functions use. */
  9. static int protothread1_flag, protothread2_flag;

  10. /* protothread state variables, one for each ptorothread. */
  11. static struct pt pt1, pt2;

  12. // constants won't change. Used here to set a pin number :
  13. const int ledPin =  13;      // the number of the LED pin

  14. /**
  15. * The first protothread function. A protothread function must always
  16. * return an integer, but must never explicitly return - returning is
  17. * performed inside the protothread statements.
  18. *
  19. * The protothread function is driven by the main loop further down in
  20. * the code.
  21. */
  22. static int protothread1(struct pt *pt)
  23. {
  24.   /* A protothread function must begin with PT_BEGIN() which takes a
  25.      pointer to a struct pt. */
  26.   PT_BEGIN(pt);

  27.   /* We loop forever here. */
  28.   while(1) {
  29.     /* Wait until the other protothread has set its flag. */
  30.     PT_WAIT_UNTIL(pt, protothread2_flag != 0);
  31.     Serial.println("Protothread 1 running");
  32.    
  33.     digitalWrite(ledPin,LOW);
  34.    
  35.     /* We then reset the other protothread's flag, and set our own
  36.        flag so that the other protothread can run. */
  37.     protothread2_flag = 0;
  38.     protothread1_flag = 1;

  39.     /* And we loop. */
  40.   }

  41.   /* All protothread functions must end with PT_END() which takes a
  42.      pointer to a struct pt. */
  43.   PT_END(pt);
  44. }

  45. /**
  46. * The second protothread function.
  47. * This one is almost the same as the first one.
  48. */
  49. static int protothread2(struct pt *pt)
  50. {
  51.   PT_BEGIN(pt);

  52.   while(1) {
  53.     /* Let the other protothread run. */
  54.     protothread2_flag = 1;

  55.     /* Wait until the other protothread has set its flag. */
  56.     PT_WAIT_UNTIL(pt, protothread1_flag != 0);
  57.     Serial.println("Protothread 2 running");
  58.    
  59.     digitalWrite(ledPin,HIGH);
  60.    
  61.     /* We then reset the other protothread's flag. */
  62.     protothread1_flag = 0;

  63.     /* And we loop. */
  64.   }
  65.   PT_END(pt);
  66. }

  67. /**
  68. * setup() is where the protothreads are initialized.
  69. * The state variables pt1 and pt2 holdthe state of the two protothreads.
  70. */
  71. void setup()
  72. {
  73.   /* Initialize the protothread state variables with PT_INIT(). */
  74.   PT_INIT(&pt1);
  75.   PT_INIT(&pt2);

  76.   Serial.begin(9600);
  77.   
  78.   // set the digital pin as output:
  79.   pinMode(ledPin, OUTPUT);
  80. }

  81. /**
  82. * Finally, we have the main loop.
  83. * This is where the protothreads are scheduled.
  84. */
  85. void loop()
  86. {
  87.   /*
  88.    * We schedule the two protothreads by repeatedly calling their
  89.    * protothread functions and passing a pointer to the protothread
  90.    * state variables as arguments.
  91.    */
  92.   protothread1(&pt1);
  93.   protothread2(&pt2);
  94. }

运行效果:

135908by1ssyoy6zzjtg7z.gif
     ProtoThread是专为资源有限的系统设计的一种耗费资源特别少并且不使用堆栈的线程模型,相比于嵌入式操作系统,其有如下优点:
      1. 以纯C语言实现,无硬件依靠性; 因此不存在移植的困难。
      2. 极少的资源需求,每个Protothread仅需要2个额外的字节;
      3. 支持阻塞操纵且没有栈的切换。

  使用Protothread实现多任务的最主要的好处在于它的轻量级。每个Protothread不需要拥有自已的堆栈,所有的Protothread共享同一个堆栈空间,这一点对于RAM资源有限的系统尤为有利。相对于操作系统下的多任务而言,每个任务都有自已的堆栈空间,这将消耗大量的RAM资源,而每个Protothread仅使用一个整型值保存当前状态。


    所有的“线程”都是(也允许不是)一个死循环,不多占用堆栈,不能被抢占,所以中途必须“退出”,不然怎么会有其他“线程”被调度的机会?退出有两种结果:下一次被调度后从头运行或从退出的位置继续向后运行。Protothreads使用C语言中的“switch”结构或goto语句来实现。既然能够从上一次退出的位置继续向后运行,那么就需要一个变量来记录这个“位置”信息,这个变量就是线程唯一占用的ram变量。作者提供的数据类型是短整型或字符串类型。我认为这就是Protothreads的精髓,也就是说Protothreads线程不是被其他程序“中断”的,而是自己主动退出,但退出之前记录当前位置,以便下次从此位置继续运行。
    由于线程是反复被调用的,因此,写程序的时候不能像写一般的函数一样使用局部变量,因为每次重新调用都会把变量初始化了,如果要保持变量,可以把它定义为static的
  在pt.h中定义了很多功能:
PT_INIT(pt)   初始化任务变量,只在初始化函数中执行一次就行
PT_BEGIN(pt)   启动任务处理,放在函数开始处
PT_END(pt)   结束任务,放在函数的最后
PT_WAIT_UNTIL(pt, condition) 等待某个条件(条件可以为时钟或其它变量,IO等)成立,否则直接退出本函数,下一次进入本函数就直接跳到这个地方判断
PT_WAIT_WHILE(pt, condition)  和上面一个一样,只是条件取反了
PT_WAIT_THREAD(pt, thread) 等待一个子任务执行完成
PT_SPAWN(pt, child, thread) 新建一个子任务,并等待其执行完退出
PT_RESTART(pt)   重新启动某个任务执行
PT_EXIT(pt)   任务后面的部分不执行,直接退出重新执行
PT_YIELD(pt)   锁死任务
PT_YIELD_UNTIL(pt, cond) 锁死任务并在等待条件成立,恢复执行
在pt中一共定义四种线程状态,在任务函数退出到上一级函数时返回其状态
PT_WAITING  等待
PT_EXITED  退出
PT_ENDED  结束
PT_YIELDED  锁死

下面文件是对ProtoThread源码的分析:
轻量级线程库protothreads介绍.pdf (434.9 KB, 下载次数: 95)

我们需要做的就是保证每个线程的完整性,与事件处理程序的编写,接下来可以结合其他硬件进行更为复杂系统的开发。

PT1.4源码:
pt-1.4.zip (256.5 KB, 下载次数: 63)




mcu5i51 发表于 2016-12-2 14:35 | 显示全部楼层
挺好,简单好用
as4781 发表于 2016-12-19 15:31 | 显示全部楼层
挺经典的一个例子了,不错
colin2135 发表于 2016-12-21 11:13 | 显示全部楼层
不错,学习了。
6881087 发表于 2017-3-22 08:10 | 显示全部楼层
不错,挺好的,学习了。
zxy2266 发表于 2017-5-24 21:42 | 显示全部楼层
OS确实很方便,但俺是软件小白,看不懂啊
wdliming 发表于 2022-6-16 09:41 | 显示全部楼层
谢谢分享
gaochy1126 发表于 2022-6-29 16:46 | 显示全部楼层
Arduino还可以开多线程?这个确实可以啊。            
gaochy1126 发表于 2022-6-29 16:47 | 显示全部楼层
Arduino Nano运行操作系统感觉就剩不下多少内存了吧。      
gaochy1126 发表于 2022-6-29 16:50 | 显示全部楼层
ATmega328的频率也比较低,不如esp32的速度快了。         
xu@xupt 发表于 2022-12-13 21:25 | 显示全部楼层
学习啦
gaochy1126 发表于 2022-12-29 20:10 | 显示全部楼层
使用ESP32,这个确实是多进程的操作,性能很给力的。                           
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:欢迎参与LabVIEW版块的讨论学习! 点我一键即达

323

主题

2908

帖子

45

粉丝
快速回复 在线客服 返回列表 返回顶部