|
1.准备pt1.4源码。
2.准备stm32工程模板
第一,基于MDK5.36创建一个stm32f103的工程。
第二,添加串口驱动程序,使其输出信息。
第三,添加基本文件
第四,添加主要代码,实现两个线程函数互相切换功能。
- //C标准头文件
- #include <stdint.h>
- #include <stdio.h>
- //STM32F103相关头文件
- #include "stm32f10x.h"
- #include "debug-uart.h"
- #include "pt.h" //线程库头文件
复制代码 添加线程变量
- static int protothread1_flag, protothread2_flag; //线程标志位
- static struct pt pt1, pt2;//结构,保存线程信息
复制代码 添加线程1代码
- static int protothread1(struct pt *pt)
- {
-
- PT_BEGIN(pt);
-
- while(1) {
-
- PT_WAIT_UNTIL(pt, protothread2_flag != 0);
- printf("Protothread 1 running\n");
- protothread2_flag = 0;
- protothread1_flag = 1;
-
- }
- PT_END(pt);
- }
复制代码 添加线程2代码
- static int protothread2(struct pt *pt)
- {
- PT_BEGIN(pt);
- while(1) {
-
- protothread2_flag = 1;
-
- PT_WAIT_UNTIL(pt, protothread1_flag != 0);
- printf("Protothread 2 running\n");
-
-
- protothread1_flag = 0;
- }
- PT_END(pt);
- }
复制代码 添加主函数:
- int main(void)
- {
- USART_Config(); //串口参数配置
- printf("setting the usart successfully!!\r\n"); //如果输出信息证明串口启动正常
- PT_INIT(&pt1);//线程1结构
- PT_INIT(&pt2);//线程2结构
-
- while(1)
- {
- protothread1(&pt1);//执行线程1
- protothread2(&pt2);//执行线程2
- }
- }
复制代码 3.stm32线程添加以后MDK5.36工程源码文件:
4.移植完成以后程序运行结果如下图所示:
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|