北方 发表于 2021-8-11 12:44

【Silicon Labs 开发套件评测】范例代码测试和分析

本帖最后由 北方 于 2021-8-11 12:57 编辑

<p style="text-align: center;"><span style="font-size:26px;">范例代码测试和分析</span></p>

<p style="text-align: center;">&nbsp;</p>

<p><span style="font-size:20px;">1、安装SDKs,Install需要完整安装好后才能新建程序,由于网络原因,没有成功下载,那么就不能创建新的程序。这个bug也耽误了不少时间。</span></p>

<p><span style="font-size:20px;">2、从首页开始选择硬件,</span></p>

<p><span style="font-size:20px;"></span></p>

<p><span style="font-size:20px;">然后从范例程序创建</span></p>

<p><span style="font-size:20px;"></span></p>

<p><span style="font-size:20px;">程序就创建成功了。</span></p>

<p></p>

<p><span style="font-size:20px;">3、开始调试,选择build</span></p>

<p>通过后,因为是范例程序,一定可以pass,然后debugg,就flash到板子里</p>

<p></p>

<p></p>

<p>程序就可以正常的在板子上亮灯了。</p>

<p></p>

<p>4、代码分析</p>

<p>4.1 Silab的框架程序有三个,分别定义工程,项目,以及引脚,在最下面的3个可以分别观察,</p>

<p></p>

<p>&nbsp;</p>

<p></p>

<p>程序设计也比较规范,从main.c 标准入口,</p>

<pre>
<code class="language-cpp">#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
}</code></pre>

<p>然后引用app.c</p>

<pre>
<code class="language-cpp">#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();
}
</code></pre>

<p>从app.c 调用最终的应用代码,其中为了设计呼吸的效果,还定了了数组lut[],显示逐步呼吸的明暗关系,很认真滴。不过也,用个循环好不好。</p>

<pre>
<code class="language-cpp">#include &quot;sl_pwm.h&quot;
#include &quot;sl_pwm_instances.h&quot;
#include &quot;sl_sleeptimer.h&quot;

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(&amp;sl_pwm_led0);
}

void blink_pwm_process_action(void)
{
for (uint8_t i = 0; i &lt; 100; i++) {
    sl_pwm_set_duty_cycle(&amp;sl_pwm_led0, pwm_lut<i>);
    sl_sleeptimer_delay_millisecond(6);
    if (i == 0) {
      sl_sleeptimer_delay_millisecond(190);
    }
}
for (uint8_t i = 100; i &gt; 0; i--) {
    sl_pwm_set_duty_cycle(&amp;sl_pwm_led0, pwm_lut<i>);
    sl_sleeptimer_delay_millisecond(6);
    if (i == 100) {
      sl_sleeptimer_delay_millisecond(190);
    }
}
}</i></i></code></pre>

<p><i><i>&nbsp;</i></i></p>

freebsder 发表于 2021-8-11 16:47

<p>谢谢分享,期待后续!</p>

soso 发表于 2021-8-11 18:06

<p>网络足够好,成功下载是关键。</p>
页: [1]
查看完整版本: 【Silicon Labs 开发套件评测】范例代码测试和分析