范例代码测试和分析
1、安装SDKs,Install需要完整安装好后才能新建程序,由于网络原因,没有成功下载,那么就不能创建新的程序。这个bug也耽误了不少时间。
2、从首页开始选择硬件,
然后从范例程序创建
程序就创建成功了。
3、开始调试,选择build
通过后,因为是范例程序,一定可以pass,然后debugg,就flash到板子里
程序就可以正常的在板子上亮灯了。
4、代码分析
4.1 Silab的框架程序有三个,分别定义工程,项目,以及引脚,在最下面的3个可以分别观察,
程序设计也比较规范,从main.c 标准入口,
#include "sl_component_catalog.h"
#include "sl_system_init.h"
#include "app.h"
#if defined(SL_CATALOG_POWER_MANAGER_PRESENT)
#include "sl_power_manager.h"
#endif
#if defined(SL_CATALOG_KERNEL_PRESENT)
#include "sl_system_kernel.h"
#else // SL_CATALOG_KERNEL_PRESENT
#include "sl_system_process_action.h"
#endif // SL_CATALOG_KERNEL_PRESENT
int main(void)
{
// Initialize Silicon Labs device, system, service(s) and protocol stack(s).
// Note that if the kernel is present, processing task(s) will be created by
// this call.
sl_system_init();
// Initialize the application. For example, create periodic timer(s) or
// task(s) if the kernel is present.
app_init();
#if defined(SL_CATALOG_KERNEL_PRESENT)
// Start the kernel. Task(s) created in app_init() will start running.
sl_system_kernel_start();
#else // SL_CATALOG_KERNEL_PRESENT
while (1) {
// Do not remove this call: Silicon Labs components process action routine
// must be called from the super loop.
sl_system_process_action();
// Application process.
app_process_action();
#if defined(SL_CATALOG_POWER_MANAGER_PRESENT)
// Let the CPU go to sleep if the system allows it.
sl_power_manager_sleep();
#endif
}
#endif // SL_CATALOG_KERNEL_PRESENT
}
然后引用app.c
#include "blink_pwm_app.h"
/***************************************************************************//**
* Initialize application.
******************************************************************************/
void app_init(void)
{
blink_pwm_init();
}
/***************************************************************************//**
* App ticking function.
******************************************************************************/
void app_process_action(void)
{
blink_pwm_process_action();
}
从app.c 调用最终的应用代码,其中为了设计呼吸的效果,还定了了数组lut[],显示逐步呼吸的明暗关系,很认真滴。不过也,用个循环好不好。
#include "sl_pwm.h"
#include "sl_pwm_instances.h"
#include "sl_sleeptimer.h"
uint8_t pwm_lut[] = {
0, 1, 1, 1, 2, 2, 2, 2, 2, 2,
2, 3, 3, 3, 3, 3, 4, 4, 4, 4,
5, 5, 5, 5, 6, 6, 6, 7, 7, 7,
8, 8, 8, 9, 9, 10, 10, 10, 11, 11,
12, 12, 13, 13, 14, 15, 15, 16, 17, 17,
18, 19, 19, 20, 21, 22, 23, 23, 24, 25,
26, 27, 28, 29, 30, 31, 32, 34, 35, 36,
37, 39, 40, 41, 43, 44, 46, 48, 49, 51,
53, 54, 56, 58, 60, 62, 64, 66, 68, 71,
73, 75, 78, 80, 83, 85, 88, 91, 94, 97,
100,
};
void blink_pwm_init(void)
{
// Enable PWM output
sl_pwm_start(&sl_pwm_led0);
}
void blink_pwm_process_action(void)
{
for (uint8_t i = 0; i < 100; i++) {
sl_pwm_set_duty_cycle(&sl_pwm_led0, pwm_lut);
sl_sleeptimer_delay_millisecond(6);
if (i == 0) {
sl_sleeptimer_delay_millisecond(190);
}
}
for (uint8_t i = 100; i > 0; i--) {
sl_pwm_set_duty_cycle(&sl_pwm_led0, pwm_lut);
sl_sleeptimer_delay_millisecond(6);
if (i == 100) {
sl_sleeptimer_delay_millisecond(190);
}
}
}
本帖最后由 北方 于 2021-8-11 12:57 编辑