电子网首页 > 开源与设计

【FRDMMCXW71|Zephyr】2打印更多信息

2026-03-10 11:32 | 来源:电子世界报

【前提】

【FRDMMCXW71|Zephyr】1、快速实现helloworld-电子产品世界论坛

在这篇工程的基础上添加更多的打印信息。

【实现目的】

修改 hello_world 例程,让它输出你的名字和当前系统运行时间,并每秒循环输出一次。

【详细步骤】

1、使用记事本,打开main.c修改为以下内容:

#include <zephyr/kernel.h>

int main(void)
{
    uint32_t count = 0;

    printk("=== Zephyr Hello World ===\n");
    printk("Board: %s\n", CONFIG_BOARD);
    printk("Author: 刘工\n");  // 替换为你的名字
    printk("==========================\n\n");

    while (1) {
        // 获取系统运行时间(毫秒)
        uint64_t uptime_ms = k_uptime_get();

        // 转换为秒
        uint32_t uptime_sec = uptime_ms / 1000;

        printk("[%u] Uptime: %u seconds (Count: %u)\n", 
               uptime_sec, uptime_sec, count);

        count++;

        // 休眠 1 秒
        k_sleep(K_MSEC(1000));
    }

    return 0;
}

image.png

2、编译:

执行west build -b frdm_mcxw71

image.png

3、下载  

执行:west flash

image.png

【实现效果】

image.png

【知识点总结】¶

通过这个任务,你学会了:

  • 使用 k_uptime_get() 获取系统运行时间

  • 使用 k_sleep() 实现延时

  • 使用 K_MSEC() 宏将毫秒转换为内核时间单位

  • 在 Zephyr 中实现循环任务

非常简单吧,感兴趣可以的来体检一把哦!


阅读全文

推荐技术

返回顶部