创建线程是RT_Thread最基本的操作之一。 1、在man.c主程序下面创建静态线程: - /* 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[512];
- /* 线程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[0], 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;
- }
复制代码编译下载后,可以看到LED3与LED1同时以500ms的频率闪烁。 在终端查看任务,看到led任务在运行中:
转载自:社区用户:lugl发布
|