概述
本篇文章主要介紹如何使用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;
SM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SE;
SMG_F_CLOSE;
SMG_G_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文章
23134瀏覽量
658405 -
數碼管
+關注
關注
32文章
1874瀏覽量
90939 -
keil
+關注
關注
68文章
1211瀏覽量
166677 -
電子時鐘
+關注
關注
11文章
197瀏覽量
24496
發布評論請先 登錄
相關推薦
評論