在本练习中,我们将练习使用用户友好的 printk() 函数将字符串打印到控制台。
我们将以第 2 课练习 2 为基础并对其进行修改,以便在按下按钮 1(nRF54 系列 DK 上为按钮 0)时,计算 1 到 10 之间数字的阶乘,并将其打印到控制台,如下所示。我们使用的是 UART 控制台,这是板卡配置文件中的默认设置。
*** Booting nRF Connect SDK ***
*** Using Zephyr OS ***
nRF Connect SDK Fundamentals - Lesson 4 - Exercise 1
Calculating the factorials of numbers from 1 to 10:
The factorial of 1 = 1
The factorial of 2 = 2
The factorial of 3 = 6
The factorial of 4 = 24
The factorial of 5 = 120
The factorial of 6 = 720
The factorial of 7 = 5040
The factorial of 8 = 40320
The factorial of 9 = 362880
The factorial of 10 = 3628800
_______________________________________________________Terminal1. 在本课程的 GitHub 仓库中,使用此练习的基础代码,该代码位于 l4/l4_e1。
2. 使用 nRF Connect for VS Code 中的 Open an existing application 选项打开本练习的基础代码,就像我们在第 2 课和第 3 课中所做的那样。

3. 确保您的开发套件(DK)已通电并连接到计算机。
4. 在您的机器上配置终端仿真器。这已在第 3 课练习 1 中介绍过。
5. 打开 l4_e1/src 内部的 main.c 文件。
6. 在 STEP 6 下,通过添加以下行来包含 printk() 函数的头文件:
#include <zephyr/sys/printk.h>C控制台驱动程序 (CONFIG_CONSOLE) 和控制台类型(例如 CONFIG_UART_CONSOLE)配置选项已在板卡配置文件中启用,该文件位于 <sdk_folder>/<sdk_version>/zephyr/boards/arm/<your_board>/*_defconfig。因此,我们不需要在应用程序配置文件 (prj.conf) 中进行任何操作。
7. 让我们打印出第一条消息,一个显示 nRF Connect SDK Fundamentals - Lesson 4 - Exercise 1 的简单标题。搜索 STEP 7 并添加以下行:
printk("nRF Connect SDK Fundamentals - Lesson 4 - Exercise 1\n");C添加构建配置,编译并将应用程序烧录到您的开发板上,就像我们在之前的课程中所做的那样,您应该会在控制台上看到打印的这条消息:
*** Booting nRF Connect SDK ***
*** Using Zephyr OS ***
nRF Connect SDK Fundamentals - Lesson 4 - Exercise 1Terminal8. 现在让我们将按钮回调函数 (ISR) 替换为生成数字阶乘的代码:
8.1 定义宏 MAX_NUMBER_FACT,表示要计算其阶乘的最大数字,并将其值设置为 10。
#define MAX_NUMBER_FACT 10C由于 ISR 中的代码以高优先级运行(高于所有系统和用户线程),因此在编写时应考虑时间因素。过于冗长或复杂的任务不应由 ISR 执行,而应推迟到线程中处理。这将在第 7 课中深入讲解。
8.2 在 main.c 中,将回调函数 button_pressed() 替换为以下内容:
void button_pressed(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
{
int i;
long int factorial = 1;
printk("Calculating the factorials of numbers from 1 to %d:\n", MAX_NUMBER_FACT);
for (i = 1; i <= MAX_NUMBER_FACT; i++) {
factorial = factorial * i;
printk("The factorial of %2d = %ld\n", i, factorial);
}
printk("_______________________________________________________\n");
/*Important note!
Code in ISR runs at a high priority, therefore, it should be written with timing in mind.
Too lengthy or too complex tasks should not be performed by an ISR, they should be deferred
to a thread.
*/
}C复制上面的代码并使用它替换 main.c 中的 button_pressed() 函数,如下面的屏幕截图所示:

button_pressed() 函数的内容在这段代码中,我们在三个不同的位置使用了 printk()。
第一次调用是打印一个包含一个整数 %d 的格式化字符串。
printk("Calculating the factorials of numbers from 1 to %d:\n",MAX_NUMBER_FACT);C第二次调用是打印一个格式化字符串,其中包含一个宽度说明符为 %2d 的整数和一个长整数 %ld。该语句放置在循环内部,因此它将被执行多次。
printk("The factorial of %2d = %ld \n",i,factorial);C第三次也是最后一次对 printk() 的调用是打印一个包含一条长下划线的简单字符串。
printk("_______________________________________________________\n");C9. 构建练习,并将其烧录到开发板上,就像我们在之前的练习中所做的那样。
观察当按下板上的按钮 1(nRF54 系列 DK 上为按钮 0)时,将计算 1 到 10 之间数字的阶乘并打印在控制台上,如下所示:
*** Booting nRF Connect SDK ***
*** Using Zephyr OS ***
nRF Connect SDK Fundamentals - Lesson 4 - Exercise 1
Calculating the factorials of numbers from 1 to 10:
The factorial of 1 = 1
The factorial of 2 = 2
The factorial of 3 = 6
The factorial of 4 = 24
The factorial of 5 = 120
The factorial of 6 = 720
The factorial of 7 = 5040
The factorial of 8 = 40320
The factorial of 9 = 362880
The factorial of 10 = 3628800
_______________________________________________________Terminal本练习的解决方案可以在 GitHub 仓库 l4/l4_e1_sol 中找到。