概述
本篇文章主要介紹如何使用e2studio對(duì)瑞薩RA2E1開發(fā)板進(jìn)行數(shù)碼管的驅(qū)動(dòng)。
硬件準(zhǔn)備
首先需要準(zhǔn)備一個(gè)開發(fā)板,這里我準(zhǔn)備的是芯片型號(hào)R7FA2E1A72DFL的開發(fā)板:
視頻教程
https://www.bilibili.com/video/BV1pV411K78Y
數(shù)碼管說(shuō)明
查看手冊(cè)可以得知,該數(shù)碼管位共陰極。
同時(shí)查看原理圖,可以看到數(shù)碼管連接到MCU對(duì)應(yīng)的管腳。
配置這些IO都為輸出口,當(dāng)紅色為高電平,藍(lán)色為低電平時(shí)候,LED亮起。
配置IO口
配置與數(shù)碼管連接的IO都為Output mode (Initial Low)模式。
#數(shù)碼管顯示庫(kù) 數(shù)碼管的控制管教如下所示。
下圖列出了數(shù)碼管顯示0到F時(shí)點(diǎn)亮的段。例如,在顯示數(shù)字0的時(shí)候,除了中間的G段外其他的段都被點(diǎn)亮了。而數(shù)字1只點(diǎn)亮了B段和C段。
這里的項(xiàng)目位電子時(shí)鐘,所以需要使用的字庫(kù)為0-9。后續(xù)在添加溫濕度的字庫(kù)。 新建smg.c和smg.h文件,用于保存數(shù)碼管的驅(qū)動(dòng)程序。
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_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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;
SMG_F_OPEN;
SMG_G_OPEN;
SMG_DP_CLOSE;
}
void smg_1_close(void)
{
SMG_1_OPEN;
SMG_2_CLOSE;
SMG_3_CLOSE;
SMG_4_CLOSE;
SMG_A_CLOSE;
SMG_B_CLOSE;
SMG_C_CLOSE;
SMG_D_CLOSE;
SMG_E_CLOSE;
SMG_F_CLOSE;
SMG_G_CLOSE;
SMG_DP_CLOSE;
}
void smg_2_close(void)
{
SMG_1_CLOSE;
SMG_2_OPEN;
SMG_3_CLOSE;
SMG_4_CLOSE;
SMG_A_CLOSE;
SMG_B_CLOSE;
SMG_C_CLOSE;
SMG_D_CLOSE;
SMG_E_CLOSE;
SMG_F_CLOSE;
SMG_G_CLOSE;
SMG_DP_CLOSE;
}
void smg_3_close(void)
{
SMG_1_CLOSE;
SMG_2_CLOSE;
SMG_3_OPEN;
SMG_4_CLOSE;
SMG_A_CLOSE;
SMG_B_CLOSE;
SMG_C_CLOSE;
SMG_D_CLOSE;
SMG_E_CLOSE;
SMG_F_CLOSE;
SMG_G_CLOSE;
SMG_DP_CLOSE;
}
void smg_4_close(void)
{
SMG_1_CLOSE;
SMG_2_CLOSE;
SMG_3_CLOSE;
SMG_4_OPEN;
SMG_A_CLOSE;
SMG_B_CLOSE;
SMG_C_CLOSE;
SMG_D_CLOSE;
SMG_E_CLOSE;
SMG_F_CLOSE;
SMG_G_CLOSE;
SMG_DP_CLOSE;
}
void smg_maohao_open(int num)
{
SMG_1_CLOSE;
SMG_2_CLOSE;
if(num)//開啟冒號(hào)
{
SMG_3_OPEN;
SMG_4_OPEN;
SMG_A_CLOSE;
SMG_B_CLOSE;
SMG_C_CLOSE;
SMG_D_CLOSE;
SMG_E_CLOSE;
SMG_F_CLOSE;
SMG_G_CLOSE;
SMG_DP_OPEN;
}
else
{
SMG_3_CLOSE;
SMG_4_CLOSE;
SMG_A_CLOSE;
SMG_B_CLOSE;
SMG_C_CLOSE;
SMG_D_CLOSE;
SMG_E_CLOSE;
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
/*
* smg.h
*
* 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_ */
添加完畢之后需要在主程序中加入對(duì)于的頭文件。
#include "smg.h"
添加測(cè)試程序。
主程序
#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 */
/**********************串口設(shè)置***************************************/
/* Open the transfer instance with initial configuration. */
err = R_SCI_UART_Open(&g_uart9_ctrl, &g_uart9_cfg);
assert(FSP_SUCCESS == err);
/**********************數(shù)碼管測(cè)試***************************************/
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
}
加入對(duì)于的數(shù)碼管測(cè)試程序。
審核編輯:湯梓紅
-
led
+關(guān)注
關(guān)注
240文章
23062瀏覽量
657037 -
瑞薩
+關(guān)注
關(guān)注
34文章
22282瀏覽量
85938 -
數(shù)碼管
+關(guān)注
關(guān)注
32文章
1873瀏覽量
90865 -
電子時(shí)鐘
+關(guān)注
關(guān)注
11文章
197瀏覽量
24449
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論