本帖最后由 eltonchang2001 于 2022-11-9 13:53 编辑
【ESK32-30519 + ESK32-21001测评】使用4位数码管显示数值
在ESK32-30519中自带了4位数码管,并且官方已经配置好了库,直接调用即可。
一、移植LEDC
我们的LEDC的文件存放在.\example\LEDC\7-SegmentDigitalDisplay。
/*********************************************************************************************************//**
* [url=home.php?mod=space&uid=247401]@brief[/url] Configure the LEDC.
* @retval None
***********************************************************************************************************/
void LEDC_Configuration(void)
{
LEDC_InitTypeDef LEDC_InitStruct;
/* Turn on the APB clock of LEDC. */
CKCU_PeripClockConfig_TypeDef CKCUClock = {{0}};
CKCUClock.Bit.LEDC = 1;
CKCU_PeripClockConfig(CKCUClock, ENABLE);
CKCUClock.Bit.AFIO = 1;
CKCU_PeripClockConfig(CKCUClock, ENABLE);
/* SEG0~7 */
AFIO_GPxConfig(HTCFG_LEDC_SEG0_GPIO_ID, HTCFG_LEDC_SEG0_AFIO_PIN, AFIO_FUN_LEDC);
AFIO_GPxConfig(HTCFG_LEDC_SEG1_GPIO_ID, HTCFG_LEDC_SEG1_AFIO_PIN, AFIO_FUN_LEDC);
AFIO_GPxConfig(HTCFG_LEDC_SEG2_GPIO_ID, HTCFG_LEDC_SEG2_AFIO_PIN, AFIO_FUN_LEDC);
AFIO_GPxConfig(HTCFG_LEDC_SEG3_GPIO_ID, HTCFG_LEDC_SEG3_AFIO_PIN, AFIO_FUN_LEDC);
AFIO_GPxConfig(HTCFG_LEDC_SEG4_GPIO_ID, HTCFG_LEDC_SEG4_AFIO_PIN, AFIO_FUN_LEDC);
AFIO_GPxConfig(HTCFG_LEDC_SEG5_GPIO_ID, HTCFG_LEDC_SEG5_AFIO_PIN, AFIO_FUN_LEDC);
AFIO_GPxConfig(HTCFG_LEDC_SEG6_GPIO_ID, HTCFG_LEDC_SEG6_AFIO_PIN, AFIO_FUN_LEDC);
AFIO_GPxConfig(HTCFG_LEDC_SEG7_GPIO_ID, HTCFG_LEDC_SEG7_AFIO_PIN, AFIO_FUN_LEDC);
/* COM0~4 */
AFIO_GPxConfig(HTCFG_LEDC_COM0_GPIO_ID, HTCFG_LEDC_COM0_AFIO_PIN, AFIO_FUN_LEDC);
AFIO_GPxConfig(HTCFG_LEDC_COM1_GPIO_ID, HTCFG_LEDC_COM1_AFIO_PIN, AFIO_FUN_LEDC);
AFIO_GPxConfig(HTCFG_LEDC_COM2_GPIO_ID, HTCFG_LEDC_COM2_AFIO_PIN, AFIO_FUN_LEDC);
AFIO_GPxConfig(HTCFG_LEDC_COM3_GPIO_ID, HTCFG_LEDC_COM3_AFIO_PIN, AFIO_FUN_LEDC);
/* Frame = fCK_LED / (LEDC_DutyClockNumber * COM Number) */
/* = 1600 / (8*4) = 50Hz */
//LEDC_InitStruct.LEDC_ClockSource = LEDC_SRC_LSI; /* LSI = 32Khz */
LEDC_InitStruct.LEDC_ClockSource = LEDC_SRC_PCLK; /*PLL = 60Mhz*/
LEDC_InitStruct.LEDC_ClockPrescaler = HTCFG_LEDC_CLOCK_PRESCAL - 1; /* fCK_LED =3MHz */
LEDC_InitStruct.LEDC_DutyClockNumber = HTCFG_LEDC_DUTY_CLOCK_NUMBER; /* Duty CLock Number = 8 Clock */
LEDC_InitStruct.LEDC_COMxEN = HTCFG_LEDC_COM1EN | HTCFG_LEDC_COM2EN |\
HTCFG_LEDC_COM3EN | HTCFG_LEDC_COM4EN; /* Enable COMx */
/* Change the brightness, the parameter range is 0~Duty (CLock Number-1.) */
/* 0: the brightest, 7: the darkest. */
LEDC_InitStruct.LEDC_DeadTime = 0;
LEDC_Init(&LEDC_InitStruct); /* LEC initialization. */
/* Set the LEDC mode to COMMON_CATHODE. */
LEDC_SetPolarityMode(HTCFG_LEDC_COM1POL | HTCFG_LEDC_COM2POL | HTCFG_LEDC_COM3POL | HTCFG_LEDC_COM4POL,
LEDC_SEG0POL | LEDC_SEG1POL | LEDC_SEG2POL | LEDC_SEG3POL |\
LEDC_SEG4POL | LEDC_SEG5POL | LEDC_SEG6POL | LEDC_SEG7POL,
COMMON_CATHODE);
LEDC_IntConfig(ENABLE); /* Enable frame interrupt. */
NVIC_EnableIRQ(LEDC_IRQn); /* Enable LECD IRQ. */
LEDC_Cmd(ENABLE); /* LEDC Start. */
}
还有在ht32f5xxxxx_01_it.c中添加
/* Global variables ----------------------------------------------------------------------------------------*/
bool gIsUpdateChar;
/* Private variables ---------------------------------------------------------------------------------------*/
int gCounter = -1;
/*********************************************************************************************************//**
* [url=home.php?mod=space&uid=247401]@brief[/url] This function handles LEDC Handler.
* @retval None
************************************************************************************************************/
void LEDC_IRQHandler(void)
{
if(LEDC_GetFlagStatus())
LEDC_ClearFlagStatus();
/* Frame = LSI_VALUE/HTCFG_LEDC_CLOCK_PRESCAL/_HTCFG_LEDC_CLOCK_NUMBER/4
= 32K / HTCFG_LEDC_CLOCK_PRESCAL/_HTCFG_LEDC_CLOCK_NUMBER/4
= 40 Hz*/
if(gCounter<(LSI_VALUE/HTCFG_LEDC_CLOCK_PRESCAL/_HTCFG_LEDC_CLOCK_NUMBER/4))
{
/* gCounter < 40*/
gCounter++;
}
else
{
/* gCounter >= 40, The "gCounter" greater than 40 means that the character has been displayed for 1 second,
and the new character will be replaced.*/
gCounter = 0;
gIsUpdateChar = TRUE;
}
}
二、数据处理
void Uint_List(u16 num,u8* dig1,u8* dig2,u8* dig3,u8* dig4)
{
if(num>=9999)
{
*dig1=9;
*dig2=9;
*dig3=9;
*dig4=9;
}else{
*dig1 = num/1000;
*dig2 = num/100%10;
*dig3 = num/10%10;
*dig4 = num%10;
}
}
void LEDC_Uint_Display(u16 num)
{
u8 dig1=0,dig2=0,dig3=0,dig4=0,dp1=0,dp2=0,dp3=0,dp4=0;
if(gIsUpdateChar)
{
Uint_List(num,&dig1,&dig2,&dig3,&dig4);
LEDC_SetData(HTCFG_LEDC_COM_D1, NumberChar[dig1]);
LEDC_SetData(HTCFG_LEDC_COM_D2, NumberChar[dig2]);
LEDC_SetData(HTCFG_LEDC_COM_D3, NumberChar[dig3]);
LEDC_SetData(HTCFG_LEDC_COM_D4, NumberChar[dig4]);
gIsUpdateChar = FALSE;
}
}
void Float_List(float num,u8* dig1,u8* dig2,u8* dig3,u8* dig4,u8* dp1,u8* dp2,u8* dp3,u8* dp4)
{
if(num>0.0f&&num<10.0f)
{
*dp1=10;
*dp2=11;
*dp3=11;
*dp4=11;
*dig1 = (u8)num;
*dig2 = (u8)(num*10)%10;
*dig3 = (u8)((u16)(num*100)%10);
*dig4 = (u8)((u16)(num*1000)%10);
}else if(num>=10.0f&&num<100.0f)
{
*dp1=11;
*dp2=10;
*dp3=11;
*dp4=11;
*dig1 = (u8)num/10;
*dig2 = (u8)(num)%10;
*dig3 = (u8)((u16)(num*10)%10);
*dig4 = (u8)((u16)(num*100)%10);
}else if(num>=100.0f&&num<1000.0f)
{
*dp1=11;
*dp2=11;
*dp3=10;
*dp4=11;
*dig1 = (u8)num/100;
*dig2 = (u8)(num)/10%10;
*dig3 = (u8)((u16)(num)%10);
*dig4 = (u8)((u16)(num*10)%10);
}else{
*dp1=11;
*dp2=11;
*dp3=11;
*dp4=10;
Uint_List((u16)num,dig1,dig2,dig3,dig4);
}
}
void LEDC_Float_Display(float num)
{
u8 dig1=0,dig2=0,dig3=0,dig4=0,dp1=0,dp2=0,dp3=0,dp4=0;
if(gIsUpdateChar)
{
Float_List(num,&dig1,&dig2,&dig3,&dig4,&dp1,&dp2,&dp3,&dp4);
LEDC_SetData(HTCFG_LEDC_COM_D1, NumberChar[dig1]+NumberChar[dp1]);
LEDC_SetData(HTCFG_LEDC_COM_D2, NumberChar[dig2]+NumberChar[dp2]);
LEDC_SetData(HTCFG_LEDC_COM_D3, NumberChar[dig3]+NumberChar[dp3]);
LEDC_SetData(HTCFG_LEDC_COM_D4, NumberChar[dig4]+NumberChar[dp4]);
gIsUpdateChar = FALSE;
}
}
暂时还没有对负号进行处理
|