找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1967|回复: 1
打印 上一主题 下一主题
收起左侧

51单片机的一个多线程编程模型

[复制链接]
跳转到指定楼层
楼主
Protothread 一个非常巧妙的轮询式多线程模型,也是 contiki 系统的内核,uip 和 lwip 作者 adam 的杰作

proteus8.12.SP0 居然都支持 STC 51 单片机和 STM32 模拟了。。可以去百度下载

常见问题:
一、proteus 8.12_sp0和proteus_8.9sp2存在相同的问题,如果安装在非C盘目录下,打开后则会显示密钥错误和未注册的灰色字体;点击帮助后也是未注册。
这是因为破解器默认是你安装在C盘。
解决方法如下:
[1]、卸载后重装在C盘
[2]、1.先在C盘安装,破解.
        2.打开软件,发现有注册信息,则注册成功
        3.此时软件复制根目录的bin文件夹。
        4.卸载刚才安装的proteus
        注意:不要用第三方的卸载软件,在控制面版里卸载就行,更不要用everything之类的把残余文件删的干干净净
        5.重新安装,选择D盘。破解
        6.把刚才复制bin文件夹覆盖到D盘软件的根目录下,选择全部替换
        7.再打开proteus就显示注册成功了
-- 虽然有点脱裤子放屁的感觉,但能用就行


Protesu 仿真测试,两个线程控制 LED 闪烁,另外还有 mutex timer 等,可以参考 contiki 的内核实现。



示例工程:
STCxx_Projects.rar (169.57 KB, 下载次数: 17)

单片机源程序如下:
  1. /**
  2.   ******************************************************************************
  3.   * @file    main.c
  4.   * @author  Iron
  5.   * @date    2021-01-01
  6.   * @version v1.0
  7.   * @brief   main c file
  8.   */

  9. /* Private includes ----------------------------------------------------------*/
  10. #include "board.h"
  11. #include "pt.h"
  12. #include "delay.h"

  13. /* Private define ------------------------------------------------------------*/
  14. /* Private macro -------------------------------------------------------------*/
  15. /* Private typedef -----------------------------------------------------------*/
  16. /* Private variables ---------------------------------------------------------*/

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

  19. /* Private function prototypes -----------------------------------------------*/


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

  33.     /* We loop forever here. */
  34.     while (1)
  35.     {
  36.         /* Wait until the other protothread has set its flag. */
  37.         PT_WAIT_UNTIL(pt, protothread2_flag != 0);

  38.         /* thread code... */
  39.         led_togger(LED0);
  40.         delay_ms(100);

  41.         /* We then reset the other protothread's flag, and set our own
  42.            flag so that the other protothread can run. */
  43.         protothread2_flag = 0;
  44.         protothread1_flag = 1;

  45.         /* And we loop. */
  46.     }

  47.     /* All protothread functions must end with PT_END() which takes a
  48.        pointer to a struct pt. */
  49.     PT_END(pt);
  50. }

  51. /**
  52. * The second protothread function. This is almost the same as the
  53. * first one.
  54. */
  55. static int protothread2(struct pt *pt)
  56. {
  57.     PT_BEGIN(pt);

  58.     while (1)
  59.     {
  60.         /* Let the other protothread run. */
  61.         protothread2_flag = 1;

  62.         /* Wait until the other protothread has set its flag. */
  63.         PT_WAIT_UNTIL(pt, protothread1_flag != 0);

  64.         /* thread code... */
  65.         led_togger(LED1);
  66.         delay_ms(100);

  67.         /* We then reset the other protothread's flag. */
  68.         protothread1_flag = 0;

  69.         /* And we loop. */
  70.     }
  71.     PT_END(pt);
  72. }


  73. /**
  74. * Finally, we have the main loop. Here is where the protothreads are
  75. * initialized and scheduled. First, however, we define the
  76. * protothread state variables pt1 and pt2, which hold the state of
  77. * the two protothreads.
  78. */
  79. static struct pt pt1, pt2;

  80. int main(void)
  81. {
  82.     board_init();

  83.     /* Initialize the protothread state variables with PT_INIT(). */
  84.     PT_INIT(&pt1);
  85.     PT_INIT(&pt2);

  86.     /*
  87.      * Then we schedule the two protothreads by repeatedly calling their
  88.      * protothread functions and passing a pointer to the protothread
  89.      * state variables as arguments.
  90.      */
  91.     while (1)
  92.     {
  93.         protothread1(&pt1);
  94.         protothread2(&pt2);
  95.     }
  96. }

  97. /**
  98.   * @}
  99.   */

  100. /******************* (C)COPYRIGHT 2021 ***** END OF FILE *********************/

复制代码



评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏4 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:646477 发表于 2021-10-29 18:39 | 只看该作者
赞一个,Protothread 用了几年了,大大小小的项目,感觉都比较不错。除非有抢占式的需求,才会用上RTOS,总体来说,还是对它比较亲切
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表