[ ST NUCLEO-U575ZI-Q 测评] FREERTOS - 结构体多参数传递
<div class='showpostmsg'><p>这次的例程是创建两个LED闪烁的程序,闪烁时间各不相同,如何做到同一程序,传入不同参数来实现功能呢?</p><p>先写一个任务创建函数,用指针来接收结构体数据。</p>
<pre>
<code>void ledFlash(void *pt){
LEDFLASH *ptLedFlash = (LEDFLASH *)pt;
byte pin = ptLedFlash->pin;
int delayTime = ptLedFlash->delayTime;
pinMode(pin,OUTPUT);
while(1){
digitalWrite(pin,!digitalRead(pin));
vTaskDelay(delayTime);
}
}
</code></pre>
<p>为了给任务创建时传递多个参数,这里用结构体来传递多个参数。</p>
<pre>
<code>typedef struct {
byte pin;
int delayTime;
} LEDFLASH;</code></pre>
<p>setup函数:</p>
<pre>
<code>void setup() {
Serial.begin(115200);
led1.pin = PB7;
led1.delayTime = 200;
led2.pin = PC7;
led2.delayTime = 500;
if(xTaskCreate(ledFlash,
"FLASH LED",
1024,
(void *)&led1,
6,
NULL) == pdPASS)
Serial.println("led1,flash task Created.");
if (xTaskCreate(ledFlash,
"Flash LED.",
1024,
(void *)&led2,
6,
NULL) == pdPASS)
Serial.println("led2 flash task Created.");
vTaskStartScheduler();
}</code></pre>
<p>这样就实现了同一个任务接收不一样的参数,实现不同的功能,整体程序如下:</p>
<pre>
<code>#include <Arduino.h>
#include <STM32FreeRTOS.h>
typedef struct {
byte pin;
int delayTime;
} LEDFLASH;
void ledFlash(void *pt){
LEDFLASH *ptLedFlash = (LEDFLASH *)pt;
byte pin = ptLedFlash->pin;
int delayTime = ptLedFlash->delayTime;
pinMode(pin,OUTPUT);
while(1){
digitalWrite(pin,!digitalRead(pin));
vTaskDelay(delayTime);
}
}
LEDFLASH led1,led2;
void setup() {
Serial.begin(115200);
led1.pin = PB7;
led1.delayTime = 200;
led2.pin = PC7;
led2.delayTime = 500;
if(xTaskCreate(ledFlash,
"FLASH LED",
1024,
(void *)&led1,
6,
NULL) == pdPASS)
Serial.println("led1,flash task Created.");
if (xTaskCreate(ledFlash,
"Flash LED.",
1024,
(void *)&led2,
6,
NULL) == pdPASS)
Serial.println("led2 flash task Created.");
vTaskStartScheduler();
}
void loop() {
// put your main code here, to run repeatedly:
}</code></pre>
<p>上传给开发板后实现两个LED按不同的频率闪烁。</p>
<p> 6d355bf40df237d0ec39c9f9500a90c7<br />
</p>
<p> </p>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script> <p>arduino真好<img height="48" src="https://bbs.eeworld.com.cn/static/editor/plugins/hkemoji/sticker/facebook/wanwan88.gif" width="59" /></p>
damiaa 发表于 2022-12-13 19:36
arduino真好
<p>谢谢!</p>
<p>测评汇总:免费申请|ST NUCLEO-U575ZI-Q https://bbs.eeworld.com.cn/thread-1228653-1-1.html</p>
页:
[1]