本帖最后由 dirty 于 2024-8-17 09:28 编辑
本篇讲述 匠芯创D133CBV-QFN88-V1-2开发板创建任务,点亮LED闪烁。
一.硬件原理与准备
观察开发板,发现I2C0接口处方便作为IO口外接LED,这里使用I2C_SCL对应MCU的PD0脚作为IO控制脚。硬件连接准备如下:
图1:连接LED模块
连接好后,插上USB转串口工具及USB Type-C,给开发板上电。
二.代码准备
1.在SDK工程创建源文件及脚本结构如下,构建工程
图2:创建工程框架
2.LED引脚宏定义及初始化,并将驱动初始化放在app_peripheral_init函数里。
#define LED_PIN "PD.0"
void app_led_init(void)
{
// 1.get pin number
static u32 pin = 0;
pin = rt_pin_get(LED_PIN);
printf("Led pin%d\n",pin);
// 2.set pin mode to Output
rt_pin_mode(pin, PIN_MODE_OUTPUT);
}
void app_peripheral_init(void)
{
app_led_init();
}
3.创建任务,在任务里取反点亮LED
static rt_thread_t led_thread = RT_NULL;
void led_thread_entry(void *parameter)
{
int pin;
bool value;
while(1)
{
pin = rt_pin_get(LED_PIN);
value=!value;
/* 写入相反的值 */
rt_pin_write(pin, value);
rt_kprintf("Run Write LED value:%d\r\n",!value);
rt_thread_mdelay(1000);
}
}
void app_rtos_init(void)
{
led_thread = rt_thread_create("led_thread",
led_thread_entry, RT_NULL,
1024,
5, 10);
if (led_thread != RT_NULL)
{
rt_thread_startup(led_thread);
}
else
{
rt_kprintf("led_thread create fail!\n");
}
}
4.main函数如下:
int main(void)
{
#ifdef AIC_AB_SYSTEM_INTERFACE
char target[32] = { 0 };
enum boot_device boot_dev = aic_get_boot_device();
if (boot_dev != BD_SDMC0) {
aic_ota_status_update();
aic_get_rodata_to_mount(target);
printf("Mount APP in blk %s\n", target);
if (dfs_mount(target, "/rodata", "elm", 0, 0) < 0)
printf("Failed to mount elm\n");
memset(target, 0, sizeof(target));
aic_get_data_to_mount(target);
printf("Mount APP in blk %s\n", target);
if (dfs_mount(target, "/data", "elm", 0, 0) < 0)
printf("Failed to mount elm\n");
}
#endif
#ifdef ULOG_USING_FILTER
ulog_global_filter_lvl_set(ULOG_OUTPUT_LVL);
#endif
rt_kprintf("Run User Project\r\n");
app_peripheral_init();
app_rtos_init();
return 0;
}
三.测验
编译烧录,可看到LED闪烁如下视频,串口日志如下:
图3:串口日志
至此,实现创建任务与LED驱动。
.
Led_Display