1.新建工程
工程建立和之前的类似,可以参考上一章。
![](https://img-blog.csdnimg.cn/img_convert/0217dee42eef89a70c8bb65e2d52b43d.png)
2.代码添加,逻辑控制
其默认模版在ra_gen\common_data.c中定义了IO驱动实例。
/* generated common source file - do not edit */
#include "common_data.h"
ioport_instance_ctrl_t g_ioport_ctrl;
const ioport_instance_t g_ioport =
{ .p_api = &g_ioport_on_ioport, .p_ctrl = &g_ioport_ctrl, .p_cfg = &g_bsp_pin_cfg, };
void g_common_init(void)
{
}
这里主要用api中pin调用write功能。
板载的led由IO口控制,此定义在ra\board\ra2e1_cpk文件夹中,包括board_leds.h和board_leds.c两个文件。
其中.h头文件控制灯的入口,选择默认的pwr灯控制用户的两个灯led1和led2,分别是红灯和蓝灯
原理图在下面。
![](https://img-blog.csdnimg.cn/img_convert/7764d87de048423cad2ee39c2cad0433.png)
头文件代码
typedef struct st_bsp_leds
{
uint16_t led_count; ///< The number of LEDs on this board
uint16_t const * p_leds; ///< Pointer to an array of IOPORT pins for controlling LEDs
} bsp_leds_t;
/** Available user-controllable LEDs on this board. These enums can be can be used to index into the array of LED pins
* found in the bsp_leds_t structure. */
typedef enum e_bsp_led
{
BSP_LED_LED3 = 0, ///< TB LED - Red
} bsp_led_t;
c文件
static const uint16_t g_bsp_prv_leds[] =
{
(uint16_t) BSP_IO_PORT_05_PIN_02, ///< USER LED1
(uint16_t) BSP_IO_PORT_05_PIN_01, ///< USER LED2
};
/***********************************************************************************************************************
* Exported global variables (to be accessed by other files)
**********************************************************************************************************************/
/** Structure with LED information for this board. */
const bsp_leds_t g_bsp_leds =
{
.led_count = (uint16_t) ((sizeof(g_bsp_prv_leds) / sizeof(g_bsp_prv_leds[1]))),
.p_leds = &g_bsp_prv_leds[1]
/*.led_count = (uint16_t) ((sizeof(g_bsp_prv_leds) / sizeof(g_bsp_prv_leds[0]))),
.p_leds = &g_bsp_prv_leds[0]*/
};
led灯是一个结构体,包括两个灯,0和1分别控制不同灯。我们需要先控制灯的电平,再进行延时。
总体代码如下,我们在hal_entry.c文件中添加以下代码。
extern bsp_leds_t g_bsp_leds;
bsp_leds_t Leds = g_bsp_leds;
while (1)
{
g_ioport.p_api->pinWrite(&g_ioport_ctrl,
Leds.p_leds[BSP_LED_LED3],
BSP_IO_LEVEL_LOW);
R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_SECONDS);
g_ioport.p_api->pinWrite(&g_ioport_ctrl,
Leds.p_leds[BSP_LED_LED3],
BSP_IO_LEVEL_HIGH);
R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_SECONDS);
}
最后编译,点击bulid。
![](https://img-blog.csdnimg.cn/img_convert/97e1e35293d7ef2b31bd8aec9a21c679.png)
下载代码并调试,首次下载,指定编译选项。
![](https://img-blog.csdnimg.cn/img_convert/9a01adf6db23150d639b758d9266de68.png)
最后进行调试,可以直接调试,也可以不用调试,在板端进行观察。
![](https://img-blog.csdnimg.cn/img_convert/64a0d883b2d5e7f607d3223eb13f54c6.png)
3.现象
我这里分别进行蓝色灯以及红色灯的控制。
![](https://img-blog.csdnimg.cn/img_convert/d044173450c6faee1870c5a7b758c71e.gif)
![](https://img-blog.csdnimg.cn/img_convert/072143261124ff246d3ea2105a9a150a.gif)
学习真的不易,一起加油进步!