基于单片机的Led跑马灯实现

2.1 原理图分析查看ek-ra6m4的原理图,如下图所示,该开发板上有3个led指示灯。
根据原理图可知,这三个led的控制逻辑为:
led灯led3led2led1
颜色 红色 绿色 蓝色
引脚 p400 p404 p415
电平 高电平亮、低电平灭 高电平亮、低电平灭 高电平亮、低电平灭
2.2 创建工程在开始菜单的fsp v4.1.0里点击运行e2studio。然后点击 file -> switch workspace -> other... ,然后选择刚才创建的工作路径并点击 launch启动。
接下来点击 file -> c/c++ project 创建 renesas ra c/c++ project 。接下来设置我们的项目名为 myboard 。
在接下来的配置中,我们定制开发我们的开发板,board选择“custom user board (any device) ”,device选择r7fa6m4af3cfb,以及烧录调试器的类型。下一步我们就选择 flat (non-trustzone) project 。
接下来的操作系统类型,选择 no rtos 。
项目模板选择 bare metal -minimal
接下来,创建好的项目如下图所示。
2.3 pins引脚配置接下来我们打开项目中的fsp配置文件,配置三个led灯的引脚为gpio模式,并分别配置它们的别名为ledred(p400)、ledgreen(p404)、ledblue(p415)。
通过 symbolic name 字段可以修改该gpio的名称为 ledred,它将在ide自动生成的文件 ra_cfg/fsp_cfg/bsp/bsp_pin_cfg.h 中定义;通过 mode 字段可以修改gpio口的工作模式,它们将在ide自动生成的文件 ra_gen/pin_data.c 中定义;然后再配置使能外部晶振,并重新生成代码。
2.4 stack配置e2studio生成的项目,默认添加的gpio的stack,这里我们不需要进行任何的修改。
2.5 源码修改接下来,我们使用添加 src/bsp_led.h 头文件,在里面添加led的相关定义及操作函数声明如下:
#ifndef bsp_led_h_#define bsp_led_h_/** information on how many leds and what pins they are on. */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_ledblue, ///< led1 bsp_ledgreen, ///< led2 bsp_ledred, ///< led3} bsp_led_t;/** available user-controllable leds on this board. these enums can be used to turn on/off led. */typedef enum e_bsp_led_status{ bsp_ledoff, ///< turn off led bsp_ledon, ///= g_bsp_leds.led_count ) { return ; } if( bsp_ledoff == status ) { r_bsp_pinwrite(g_bsp_leds.p_leds[which], bsp_io_level_low); } else { r_bsp_pinwrite(g_bsp_leds.p_leds[which], bsp_io_level_high); }}接下来,我们可以修改 src/hal_entry.c 源文件,在里面添加毫秒级延时宏函数 delay_ms() 以及 led 跑马灯的控制代码。
#include bsp_led.h#define delay_ms(ms) r_bsp_softwaredelay(ms, bsp_delay_units_milliseconds)... ...void hal_entry(void){ /* todo: add your own code here */#if bsp_tz_secure_build /* enter non-secure code */ r_bsp_nonsecureenter();#endif r_bsp_pinaccessenable(); /* enable access to the pfs registers. */ while (1) { turn_led(bsp_ledblue, bsp_ledon); delay_ms(250); turn_led(bsp_ledblue, bsp_ledoff); turn_led(bsp_ledgreen, bsp_ledon); delay_ms(250); turn_led(bsp_ledgreen, bsp_ledoff); turn_led(bsp_ledred, bsp_ledon); delay_ms(250); turn_led(bsp_ledred, bsp_ledoff); delay_ms(350); }}r_bsp_pinaccessenable() 函数必须使能,否则不能操作gpio的寄存器;2.5 编译运行代码修改完成后,在开发板上编译运行,会发现三个led跑马灯运行。

电子荧光灯的照明电路图
ADI推出首款商用模拟三轴MEMS加速度计
LED灯带的两种生产工艺
平板市场风险大 RIM回头是岸
[图文]单片单放机IC KA22136
基于单片机的Led跑马灯实现
GTC23 | 在 NVIDIA GTC 大会,探索工业元宇宙和数字化领域新突破
2018半导体人的春天
C语言为何用/* */ 注释?
数字中国的建设,已经进入了一个崭新时代
电工知识—SIMATIC S7-1500 PLC的分类
变电站接地系统的结构、用途及应用
快讯:康佳集团与盐城组建100亿元基金,投资半导体、电子信息等领域
XP Power 正式宣布推出机板型DC-DC转换器JTD15 和 JTD20系列
双极步进马达加速和减速过程应用
德州仪器宣布推出业界功耗最低的MCU 可使电池寿命长达10年
小鹏汽车40辆G3车型全部装机NCA三元动力电池,联动天翼啥来头?
云网融合赋能5G新娱乐
万众期待的IOTE2020深圳国 际物联网展成功举办!
骨传导黑科技耳机,是户外运动耳机的最佳选择