概述
本篇文章主要介绍如何使用e2studio对瑞萨RA2E1开发板进行数码管的驱动。
硬件准备
首先需要准备一个开发板,这里我准备的是芯片型号R7FA2E1A72DFL的开发板:
视频教程
https://www.bilibili.com/video/BV1Bx4y197iy/
数码管说明
查看手册可以得知,该数码管位共阴极。
同时查看原理图,可以看到数码管连接到MCU对应的管脚。
配置这些IO都为输出口,当红色为高电平,蓝色为低电平时候,LED亮起。
配置IO口
配置与数码管连接的IO都为Output mode (Initial Low)模式。
数码管显示库
数码管的控制管教如下所示。
下图列出了数码管显示0到F时点亮的段。例如,在显示数字0的时候,除了中间的G段外其他的段都被点亮了。而数字1只点亮了B段和C段。
这里的项目位电子时钟,所以需要使用的字库为0-9。后续在添加温湿度的字库。
新建smg.c和smg.h文件,用于保存数码管的驱动程序。
都保存到SRC文件夹下面。
smg.c
/*
* smg.c
*
* Created on: 2023年6月29日
* Author: a8456
*/
#include "smg.h"
#include "hal_data.h"
void smg_num(int num)
{
switch(num)
{
case 0:
{
SMG_A_OPEN;
SMG_B_OPEN;
SMG_C_OPEN;
SMG_D_OPEN;
SMG_E_OPEN;
SMG_F_OPEN;
SMG_G_CLOSE;
SMG_DP_CLOSE;
break;
}
case 1:
{
SMG_A_CLOSE;
SMG_B_OPEN;
SMG_C_OPEN;
SMG_D_CLOSE;
SMG_E_CLOSE;
SMG_F_CLOSE;
SMG_G_CLOSE;
SMG_DP_CLOSE;
break;
}
case 2:
{
SMG_A_OPEN;
SMG_B_OPEN;
SMG_C_CLOSE;
SMG_D_OPEN;
SMG_E_OPEN;
SMG_F_CLOSE;
SMG_G_OPEN;
SMG_DP_CLOSE;
break;
}
case 3:
{
SMG_A_OPEN;
SMG_B_OPEN;
SMG_C_OPEN;
SMG_D_OPEN;
SMG_E_CLOSE;
SMG_F_CLOSE;
SMG_G_OPEN;
SMG_DP_CLOSE;
break;
}
case 4:
{
SMG_A_CLOSE;
SMG_B_OPEN;
SMG_C_OPEN;
SMG_D_CLOSE;
SMG_E_CLOSE;
SMG_F_OPEN;
SMG_G_OPEN;
SMG_DP_CLOSE;
break;
}
case 5:
{
SMG_A_OPEN;
SMG_B_CLOSE;
SMG_C_OPEN;
SMG_D_OPEN;
SMG_E_CLOSE;
SMG_F_OPEN;
SMG_G_OPEN;
SMG_DP_CLOSE;
break;
}
case 6:
{
SMG_A_OPEN;
SMG_B_CLOSE;
SMG_C_OPEN;
SMG_D_OPEN;
SMG_E_OPEN;
SMG_F_OPEN;
SMG_G_OPEN;
SMG_DP_CLOSE;
break;
}
case 7:
{
SMG_A_OPEN;
SMG_B_OPEN;
SMG_C_OPEN;
SMG_D_CLOSE;
SMG_E_CLOSE;
SMG_F_CLOSE;
SMG_G_CLOSE;
SMG_DP_CLOSE;
break;
}
case 8:
{
SMG_A_OPEN;
SMG_B_OPEN;
SMG_C_OPEN;
SMG_D_OPEN;
SMG_E_OPEN;
SMG_F_OPEN;
SMG_G_OPEN;
SMG_DP_CLOSE;
break;
}
case 9:
{
SMG_A_OPEN;
SMG_B_OPEN;
SMG_C_OPEN;
SMG_D_OPEN;
SMG_E_CLOSE;
SMG_F_OPEN;
SMG_G_OPEN;
SMG_DP_CLOSE;
break;
}
}
}
void smg_1(int num)
{
SMG_1_OPEN;
SMG_2_CLOSE;
SMG_3_CLOSE;
SMG_4_CLOSE;
smg_num(num);
}
void smg_2(int num)
{
SMG_1_CLOSE;
SMG_2_OPEN;
SMG_3_CLOSE;
SMG_4_CLOSE;
smg_num(num);
}
void smg_3(int num)
{
SMG_1_CLOSE;
SMG_2_CLOSE;
SMG_3_OPEN;
SMG_4_CLOSE;
smg_num(num);
}
void smg_4(int num)
{
SMG_1_CLOSE;
SMG_2_CLOSE;
SMG_3_CLOSE;
SMG_4_OPEN;
smg_num(num);
}
void smg_1_p(void)
{
SMG_1_OPEN;
SMG_2_CLOSE;
SMG_3_CLOSE;
SMG_4_CLOSE;
SMG_A_OPEN;
SMG_B_OPEN;
SMG_C_CLOSE;
SMG_D_CLOSE;
SMG_E_OPEN;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CLOSE;
SMG_DP_CLOSE;
}
}
void ceshi_smg(void)
{
for(int i=0;i< 40;i++)
{
if(i< 10)
{ smg_1(i);}
else if(i >=10&&i< 20)
{ smg_2(i-10);}
else if(i >=20&&i< 30)
{ smg_3(i-20);}
else if(i >=30&&i< 40)
{ smg_4(i-30);}
R_BSP_SoftwareDelay(100U, BSP_DELAY_UNITS_MILLISECONDS);
}
smg_maohao_open(1);
}
smg.h
/*aa
*
* Created on: 2023年6月29日
* Author: a8456
*/
#ifndef SMG_H_
#define SMG_H_
#define SMG_A_OPEN R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_01_PIN_03, BSP_IO_LEVEL_HIGH)
#define SMG_B_OPEN R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_05_PIN_00, BSP_IO_LEVEL_HIGH)
#define SMG_C_OPEN R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_02_PIN_13, BSP_IO_LEVEL_HIGH)
#define SMG_D_OPEN R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_08, BSP_IO_LEVEL_HIGH)
#define SMG_E_OPEN R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_07, BSP_IO_LEVEL_HIGH)
#define SMG_F_OPEN R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_01_PIN_02, BSP_IO_LEVEL_HIGH)
//旧版PCB
//#define SMG_G_OPEN R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_00, BSP_IO_LEVEL_HIGH)
//新版PCB
#define SMG_G_OPEN R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_00, BSP_IO_LEVEL_HIGH)
#define SMG_DP_OPEN R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_02_PIN_12, BSP_IO_LEVEL_HIGH)
#define SMG_A_CLOSE R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_01_PIN_03, BSP_IO_LEVEL_LOW)
#define SMG_B_CLOSE R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_05_PIN_00, BSP_IO_LEVEL_LOW)
#define SMG_C_CLOSE R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_02_PIN_13, BSP_IO_LEVEL_LOW)
#define SMG_D_CLOSE R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_08, BSP_IO_LEVEL_LOW)
#define SMG_E_CLOSE R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_07, BSP_IO_LEVEL_LOW)
#define SMG_F_CLOSE R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_01_PIN_02, BSP_IO_LEVEL_LOW)
//旧版PCB
//#define SMG_G_CLOSE R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_00, BSP_IO_LEVEL_LOW)
//新版PCB
#define SMG_G_CLOSE R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_00, BSP_IO_LEVEL_LOW)
#define SMG_DP_CLOSE R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_02_PIN_12, BSP_IO_LEVEL_LOW)
#define SMG_1_OPEN R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_01_PIN_04, BSP_IO_LEVEL_LOW)
#define SMG_2_OPEN R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_01_PIN_01, BSP_IO_LEVEL_LOW)
#define SMG_3_OPEN R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_01_PIN_00, BSP_IO_LEVEL_LOW)
//旧版PCB
//#define SMG_4_OPEN R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_00, BSP_IO_LEVEL_LOW)
//新版PCB
#define SMG_4_OPEN R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_00, BSP_IO_LEVEL_LOW)
#define SMG_1_CLOSE R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_01_PIN_04, BSP_IO_LEVEL_HIGH)
#define SMG_2_CLOSE R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_01_PIN_01, BSP_IO_LEVEL_HIGH)
#define SMG_3_CLOSE R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_01_PIN_00, BSP_IO_LEVEL_HIGH)
//旧版PCB
//#define SMG_4_CLOSE R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_00_PIN_00, BSP_IO_LEVEL_HIGH)
//新版PCB
#define SMG_4_CLOSE R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_00, BSP_IO_LEVEL_HIGH)
void smg_num(int num);
void smg_1(int num);
void smg_2(int num);
void smg_3(int num);
void smg_4(int num);
void smg_1_p(void);
void smg_1_close(void);
void smg_2_close(void);
void smg_3_close(void);
void smg_4_close(void);
void smg_maohao_open(int num);
void ceshi_smg(void);
#endif /* SMG_H_ */
将刚刚的文件导入进来。
添加完毕之后需要在主程序中加入对于的头文件。
#include "smg.h"
添加测试程序。
主程序
#include "hal_data.h"
#include < stdio.h >
#include "smg.h"
FSP_CPP_HEADER
void R_BSP_WarmStart(bsp_warm_start_event_t event);
FSP_CPP_FOOTER
fsp_err_t err = FSP_SUCCESS;
volatile bool uart_send_complete_flag = false;
/* Callback function */
void user_uart_callback(uart_callback_args_t *p_args)
{
/* TODO: add your own code here */
if(p_args- >event == UART_EVENT_TX_COMPLETE)
{
uart_send_complete_flag = true;
}
}
#ifdef __GNUC__ //串口重定向
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
PUTCHAR_PROTOTYPE
{
err = R_SCI_UART_Write(&g_uart9_ctrl, (uint8_t *)&ch, 1);
if(FSP_SUCCESS != err) __BKPT();
while(uart_send_complete_flag == false){}
uart_send_complete_flag = false;
return ch;
}
int _write(int fd,char *pBuffer,int size)
{
for(int i=0;i< size;i++)
{
__io_putchar(*pBuffer++);
}
return size;
}
/*******************************************************************************************************************//**
* main() is generated by the RA Configuration editor and is used to generate threads if an RTOS is used. This function
* is called by main() when no RTOS is used.
**********************************************************************************************************************/
void hal_entry(void)
{
/* TODO: add your own code here */
/**********************串口设置***************************************/
/* Open the transfer instance with initial configuration. */
err = R_SCI_UART_Open(&g_uart9_ctrl, &g_uart9_cfg);
assert(FSP_SUCCESS == err);
/**********************数码管测试***************************************/
ceshi_smg();
while(1)
{
printf("hello world!n");
R_BSP_SoftwareDelay(1000U, BSP_DELAY_UNITS_MILLISECONDS);
}
#if BSP_TZ_SECURE_BUILD
/* Enter non-secure code */
R_BSP_NonSecureEnter();
#endif
}
加入对于的数码管测试程序。
审核编辑:汤梓红
-
led
+关注
关注
240文章
23128浏览量
658243 -
数码管
+关注
关注
32文章
1874浏览量
90919 -
keil
+关注
关注
68文章
1211浏览量
166659 -
电子时钟
+关注
关注
11文章
197浏览量
24492
发布评论请先 登录
相关推荐
评论