197_FreeRToS队列

34 篇文章 0 订阅
1 #include <stdio.h>                                                                                                                                                                                                                         2 #include <string.h>                                                                                                                                                                                                                        3 #include <stdlib.h>                                                                                                                                                                                                                        4 #include "freertos/FreeRTOS.h"                                                                                                                                                                                                             5 #include "freertos/task.h"                                                                                                                                                                                                                 6 #include "freertos/queue.h"                                                                                                                                                                                                                7 #include "driver/gpio.h"                                                                                                                                                                                                                   8                                                                                                                                                                                                                                            9 static xQueueHandle p_queue_test = NULL;                                                                                                                                                                                                  10                                                                                                                                                                                                                                           11 struct  Student{                                                                                                                                                                                                                          12         char name[16];                                                                                                                                                                                                                    13         uint8_t num;                                                                                                                                                                                                                      14         uint8_t score;                                                                                                                                                                                                                    15                                                                                                                                                                                                                                           16 };                                                                                                                                                                                                                                        17                                                                                                                                                                                                                                           18 static void task1(void* arg)                                                                                                                                                                                                              19 {                                                                                                                                                                                                                                         20     portBASE_TYPE xStatus;                                                                                                                                                                                                                21     static struct Student Student_1;                                                                                                                                                                                                      22     Student_1.num = 10;                                                                                                                                                                                                                   23                                                                                                                                                                                                                                           24     for(;;)                                                                                                                                                                                                                               25     {                                                                                                                                                                                                                                     26         if(Student_1.num <= 15)                                                                                                                                                                                                           27         {                                                                                                                                                                                                                                 28             xStatus = xQueueSend(p_queue_test, &Student_1, 0);                                                                                                                                                                            29             if(xStatus != pdPASS)                                                                                                                                                                                                         30             {                                                                                                                                                                                                                             31                 printf("ERR!  Cannt send \n");                                                                                                                                                                                            32                 return;                                                                                                                                                                                                                   33             }                                                                                                                                                                                                                             34             Student_1.num ++;                                                                                                                                                                                                             35         }                                                                                                                                                                                                                                 36         else                                                                                                                                                                                                                              37         {                                                                                                                                                                                                                                 38             Student_1.num = 10;                                                                                                                                                                                                           39         }                                                                                                                                                                                                                                 40         vTaskDelay(2000 / portTICK_RATE_MS);                                                                                                                                                                                              41     }                                                                                                                                                                                                                                     42 }                                                                                                                                                                                                                                         43                                                                                                                                                                                                                                           44 static void task2(void* arg)                                                                                                                                                                                                              45 {                                                                                                                                                                                                                                         46     portBASE_TYPE xStatus;                                                                                                                                                                                                                47     static struct Student Student_1;                                                                                                                                                                                                      48     Student_1.num = 100;                                                                                                                                                                                                                  49                                                                                                                                                                                                                                           50     for(;;)                                                                                                                                                                                                                               51     {                                                                                                                                                                                                                                     52         if(Student_1.num <= 105)                                                                                                                                                                                                          53         {                                                                                                                                                                                                                                 54             xStatus = xQueueSend(p_queue_test, &Student_1, 0);                                                                                                                                                                            55             if(xStatus != pdPASS)                                                                                                                                                                                                         56             {                                                                                                                                                                                                                             57                 printf("ERR!  Cannt send \n");                                                                                                                                                                                            58                 return;                                                                                                                                                                                                                   59             }                                                                                                                                                                                                                             60             Student_1.num ++;                                                 
61         }
62         else
63         {
64             Student_1.num = 100;
65         }
66         vTaskDelay(2000 / portTICK_RATE_MS);
67     }
68 }
69
70 static void task3(void* arg)
71 {
72     portBASE_TYPE xStatus;
73     static struct Student Student_1;
74     Student_1.num = 200;
75
76     for(;;)
77     {
78         if(Student_1.num <= 205)
79         {
80             xStatus = xQueueSend(p_queue_test, &Student_1, 0);
81             if(xStatus != pdPASS)
82             {
83                 printf("ERR!  Cannt send \n");
84                 return;
85             }
86             Student_1.num ++;
87         }
88         else
89         {
90             Student_1.num = 200;
91         }
92         vTaskDelay(2000 / portTICK_RATE_MS);
93     }
94 }
95
96
97 static void task4(void* arg)
98 {
99     portBASE_TYPE xStatus;
100     static struct Student Student_4;
101     const portTickType xTicksToWait = 100 / portTICK_RATE_MS;
102
103     for(;;)
104     {
105         if(uxQueueMessagesWaiting(p_queue_test))
106         {
107             xStatus = xQueueReceive(p_queue_test, &Student_4, xTicksToWait);
108             if(xStatus == pdPASS)
109             {
110                 printf("Success!  num: %d \n", Student_4.num);
111             }
112             else
113             {
114                 printf("ERR!  Cannt receive \n");
115                 return;
116             }
117         }
118         vTaskDelay(10 / portTICK_RATE_MS);
119     }
120 }
121
122 void app_main()
122 {
123    p_queue_test = xQueueCreate(20, sizeof(struct Student));
124    if(!p_queue_test)
125    {
126         printf("Creat failed \n");
127         return;
128    }
129    xTaskCreate(task1, "task_1", 2048, NULL, 5, NULL);
130    xTaskCreate(task2, "task_2", 2048, NULL, 5, NULL);
131    xTaskCreate(task3, "task_3", 2048, NULL, 7, NULL);
132    xTaskCreate(task4, "task_4", 2048, NULL, 5, NULL);
133 }
134

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值