五、测试代码
#include "stdio.h"
#include <NuMicro.h>
#include <rtthread.h>
//配置优先级,栈大小,时间片,设置不对没法运行的。
#define THREAD_PRIORITY 5
#define THREAD_STACK_SIZE 256
#define THREAD_TIMESLICE 10
void led(void *parameter)
{
rt_kprintf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %d Hz\n", SystemCoreClock);
rt_kprintf("+-------------------------------------------------+\n");
rt_kprintf("| PB14(Output) Sample Code |\n");
rt_kprintf("+-------------------------------------------------+\n\n");
rt_kprintf("Hello RTT_NANO\n");
while(1)
{
PB14=0;
rt_thread_mdelay(2000);
rt_kprintf("\nLED1 is ON\n");
PB14=1;
rt_thread_mdelay(2000);
rt_kprintf("\nLED1 is OFF\n");
}
}
/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(led, RT-Thread first led sample);
void led2(void *parameter)
{
rt_kprintf("Hello RTT_NANO\n");
while(1)
{
PB14=0;
rt_thread_mdelay(3000);
rt_kprintf("\nLED2 is ON\n");
PB14=1;
rt_thread_mdelay(3000);
rt_kprintf("\nLED2 is OFF\n");
}
}
MSH_CMD_EXPORT(led2, RT-Thread second led sample);
int led_sample(void)
{
static rt_thread_t tid = RT_NULL;
static rt_thread_t tid2 = RT_NULL;
/* 创建线程1 */
tid = rt_thread_create("thread1",
led, RT_NULL,
THREAD_STACK_SIZE,
THREAD_PRIORITY, THREAD_TIMESLICE);
if (tid != RT_NULL)
rt_thread_startup(tid);
/* 创建线程2 */
tid2 = rt_thread_create("thread2",
led2, RT_NULL,
THREAD_STACK_SIZE,
THREAD_PRIORITY, THREAD_TIMESLICE);
if (tid2 != RT_NULL)
rt_thread_startup(tid2);
//该例子共用PB14端口,所以一并在创建线程时候初始化为输出模式
GPIO_SetMode(PB, BIT14, GPIO_MODE_OUTPUT);
return 0;
}
MSH_CMD_EXPORT(led_sample, RT-Thread sample);
/*---------------------------------------------------------------------------------------------------------*/
/* Main Function */
/*---------------------------------------------------------------------------------------------------------*/
int32_t main(void)
{
led_sample();
return 0;
}
|
———————————————— 版权声明:本文为CSDN博主「纵向深耕」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/dmjkun/article/details/123852266