【Silicon Labs Development Kit Review】Sample Code Testing and Analysis
[Copy link]
This post was last edited by Beifang on 2021-8-11 12:57
Example code testing and analysis
1. Install SDKs. Install needs to be fully installed before creating a new program. Due to network reasons, if the download is not successful, then the new program cannot be created. This bug also delayed a lot of time.
2. Start by selecting hardware from the home page.
Then create from the example program
The program was created successfully.
3. Start debugging and select build
After passing, since it is a sample program, it must pass, then debug, and then flash it into the board
The program can light up the board normally.
4. Code Analysis
4.1 Silab has three framework programs, which define engineering, project, and pins respectively. You can observe them in the bottom three.
The program design is also relatively standardized, starting from the main.c standard entry.
#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
}
Then reference 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();
}
The final application code is called from app.c. In order to design the breathing effect, the array lut[] is defined to show the light and dark relationship of the gradual breathing. It is very serious. However, it is also better to use a loop.
#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);
}
}
}
|