61 lines
1.3 KiB
C
61 lines
1.3 KiB
C
/*
|
|
* 程序清单:闪灯例程
|
|
*
|
|
* 跑马灯大概是最简单的例子,就类似于每种编程语言中程序员接触的第一个程序
|
|
* Hello World 一样,所以这个例子就从跑马灯开始。创建一个线程,让它定时地对
|
|
* LED 进行更新(关或灭)
|
|
*/
|
|
|
|
#include <rtdevice.h>
|
|
#include <rtthread.h>
|
|
|
|
#define LD2_PIN 21
|
|
|
|
ALIGN(RT_ALIGN_SIZE)
|
|
static rt_uint8_t led_stack[512];
|
|
/* 线程的 TCB 控制块 */
|
|
static struct rt_thread led_thread;
|
|
|
|
void rt_hw_led_init(void)
|
|
{
|
|
rt_pin_mode(LD2_PIN, PIN_MODE_OUTPUT);
|
|
}
|
|
|
|
static void led_thread_entry(void* parameter)
|
|
{
|
|
unsigned int count = 0;
|
|
|
|
rt_hw_led_init();
|
|
|
|
while (1) {
|
|
/* led1 on */
|
|
rt_pin_write(LD2_PIN, 1);
|
|
rt_thread_delay(RT_TICK_PER_SECOND ); /* sleep 1 second and switch to other thread */
|
|
|
|
/* led1 off */
|
|
rt_pin_write(LD2_PIN, 0);
|
|
rt_thread_delay(RT_TICK_PER_SECOND / 2);
|
|
}
|
|
}
|
|
|
|
int led_sample_init(void)
|
|
{
|
|
rt_err_t result;
|
|
|
|
/* init led thread */
|
|
result = rt_thread_init(&led_thread,
|
|
"led",
|
|
led_thread_entry,
|
|
RT_NULL,
|
|
(rt_uint8_t*)&led_stack[0],
|
|
sizeof(led_stack),
|
|
20,
|
|
5);
|
|
if (result == RT_EOK) {
|
|
rt_thread_startup(&led_thread);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
INIT_APP_EXPORT(led_sample_init);
|