【致敬未来的攻城狮计划】 + 点亮led灯

1.新建工程

工程建立和之前的类似,可以参考上一章。

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,分别是红灯和蓝灯

原理图在下面。

头文件代码

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。

下载代码并调试,首次下载,指定编译选项。

最后进行调试,可以直接调试,也可以不用调试,在板端进行观察。

3.现象

我这里分别进行蓝色灯以及红色灯的控制。

学习真的不易,一起加油进步!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值