【匠芯创D133CBS】--2.创建任务点亮LED
<div class='showpostmsg'> 本帖最后由 dirty 于 2024-8-17 09:28 编辑<p> 本篇讲述 匠芯创D133CBV-QFN88-V1-2开发板创建任务,点亮LED闪烁。</p>
<p><span style="color:#0000ff;"><strong>一.硬件原理与准备</strong></span></p>
<p> 观察开发板,发现I2C0接口处方便作为IO口外接LED,这里使用I2C_SCL对应MCU的PD0脚作为IO控制脚。硬件连接准备如下:</p>
<div style="text-align: center;"></div>
<div style="text-align: center;">图1:连接LED模块</div>
<p> 连接好后,插上USB转串口工具及USB Type-C,给开发板上电。</p>
<p> </p>
<p><strong><span style="color:#0000ff;">二.代码准备</span></strong></p>
<p>1.在SDK工程创建源文件及脚本结构如下,构建工程</p>
<div style="text-align: center;"></div>
<div style="text-align: center;">图2:创建工程框架</div>
<p>2.LED引脚宏定义及初始化,并将驱动初始化放在app_peripheral_init函数里。</p>
<pre>
<code>#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();
}</code></pre>
<p>3.创建任务,在任务里取反点亮LED</p>
<pre>
<code>static rt_thread_tled_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");
}
}
</code></pre>
<p>4.main函数如下:</p>
<pre>
<code>int main(void)
{
#ifdef AIC_AB_SYSTEM_INTERFACE
char target = { 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;
}
</code></pre>
<p> </p>
<p><strong><span style="color:#0000ff;">三.测验</span></strong></p>
<p> 编译烧录,可看到LED闪烁如下视频,串口日志如下:</p>
<div style="text-align: center;"></div>
<div style="text-align: center;">图3:串口日志</div>
<p> 至此,实现创建任务与LED驱动。</p>
<p>.05623de39cc9ebe17ede818afd697854<br />
</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>
页:
[1]