[ ST NUCLEO-U575ZI-Q 测评] RT—Thread 2 创建第一个线程
<div class='showpostmsg'><p>创建线程是RT_Thread最基本的操作之一。</p><p>1、在man.c主程序下面创建静态线程:</p>
<pre>
<code>/* defined the LED1 pin: PC7 */
/* defined the LED0 pin: PG2 */
#define LED0_PIN GET_PIN(G, 2)
#define LED1_PIN GET_PIN(C, 7)
#define THREAD_PRIORITY 25
#define THREAD_STACK_SIZE 512
#define THREAD_TIMESLICE 5
/* 线程led 的对象和运行时用到的栈 */
static struct rt_thread thread_led;
static rt_uint8_t thread_led_stack;
/*线程led 入口 */
void thread_led_entry(void* parameter)
{
rt_pin_mode(LED1_PIN, PIN_MODE_OUTPUT);
while(1)
{
rt_pin_write(LED1_PIN,PIN_HIGH);
rt_thread_mdelay(500);
rt_pin_write(LED1_PIN, PIN_LOW);
rt_thread_mdelay(500);
}
}
/* 线程LED1 初始化 */
int thread_sample(void)
{
rt_thread_t threadled_ptr;
rt_err_t result;
/*
* 初始化 线程LED1
* 线程的入口是 thread_led_entry, 参数是RT_NULLL
* 线程栈是thread_led_stack
* 优先级是25,时间片是5 OS Tick
*/
result = rt_thread_init(&thread_led,
"thread led",
thread_led_entry, RT_NULL,
&thread_led_stack, sizeof(thread_led_stack),
THREAD_PRIORITY-1,THREAD_TIMESLICE);
/*启动线程 */
if(result == RT_EOK) rt_thread_startup(&thread_led);
return 0;
}
int main(void)
{
int count = 1;
/* set LED0 pin mode to output */
rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT);
thread_sample();
while (count++)
{
rt_pin_write(LED0_PIN, PIN_HIGH);
rt_thread_mdelay(500);
rt_pin_write(LED0_PIN, PIN_LOW);
rt_thread_mdelay(500);
}
return RT_EOK;
}</code></pre>
<p>编译下载后,可以看到LED3与LED1同时以500ms的频率闪烁。</p>
<p>在终端查看任务,看到led任务在运行中:</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>测评汇总:免费申请|ST NUCLEO-U575ZI-Q https://bbs.eeworld.com.cn/thread-1228653-1-1.html</p>
页:
[1]