三木社区

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 316|回复: 0
打印 上一主题 下一主题

Protothreads实现STM32多线程处理

[复制链接]

1657

主题

1684

帖子

5684

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
5684
跳转到指定楼层
楼主
发表于 2022-12-29 21:33:49 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1.准备pt1.4源码。

2.准备stm32工程模板
第一,基于MDK5.36创建一个stm32f103的工程。
第二,添加串口驱动程序,使其输出信息。
第三,添加基本文件
  1. lc.h
  2. pt.h
  3. lc-switch.h
复制代码
第四,添加主要代码,实现两个线程函数互相切换功能。
  1. //C标准头文件
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. //STM32F103相关头文件
  5. #include "stm32f10x.h"

  6. #include "debug-uart.h"
  7. #include "pt.h"   //线程库头文件
复制代码
添加线程变量
  1. static int protothread1_flag, protothread2_flag; //线程标志位
  2. static struct pt pt1, pt2;//结构,保存线程信息
复制代码
添加线程1代码
  1. static int protothread1(struct pt *pt)
  2. {
  3.   
  4.   PT_BEGIN(pt);


  5.   while(1) {
  6.    
  7.     PT_WAIT_UNTIL(pt, protothread2_flag != 0);
  8.     printf("Protothread 1 running\n");

  9.     protothread2_flag = 0;
  10.     protothread1_flag = 1;

  11.   
  12.   }

  13.   PT_END(pt);
  14. }
复制代码
添加线程2代码
  1. static int protothread2(struct pt *pt)
  2. {
  3.   PT_BEGIN(pt);

  4.   while(1) {
  5.    
  6.     protothread2_flag = 1;

  7.    
  8.     PT_WAIT_UNTIL(pt, protothread1_flag != 0);
  9.     printf("Protothread 2 running\n");
  10.    
  11.    
  12.     protothread1_flag = 0;


  13.   }
  14.   PT_END(pt);
  15. }
复制代码
添加主函数:
  1. int main(void)
  2. {
  3.     USART_Config();                                    //串口参数配置

  4.     printf("setting the usart successfully!!\r\n");    //如果输出信息证明串口启动正常
  5.           PT_INIT(&pt1);//线程1结构
  6.     PT_INIT(&pt2);//线程2结构
  7.         
  8.     while(1)
  9.     {
  10.       protothread1(&pt1);//执行线程1
  11.       protothread2(&pt2);//执行线程2
  12.     }               
  13. }

复制代码
3.stm32线程添加以后MDK5.36工程源码文件:

4.移植完成以后程序运行结果如下图所示:





本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

Archiver|手机版|小黑屋|三木电子社区 ( 辽ICP备11000133号-4 )

辽公网安备 21021702000620号

GMT+8, 2025-6-18 23:53 , Processed in 0.130560 second(s), 23 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

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