hc32l13x数码管示例代码
时间: 2023-07-02 20:10:06 浏览: 106
以下是HC32L13x单片机使用数码管的示例代码:
```c
#include "hc32l13x.h"
#include "hc32l13x_clk.h"
#include "hc32l13x_gpio.h"
#define SEG_A_PIN (GPIO_PIN_0)
#define SEG_B_PIN (GPIO_PIN_1)
#define SEG_C_PIN (GPIO_PIN_2)
#define SEG_D_PIN (GPIO_PIN_3)
#define SEG_E_PIN (GPIO_PIN_4)
#define SEG_F_PIN (GPIO_PIN_5)
#define SEG_G_PIN (GPIO_PIN_6)
#define SEG_DP_PIN (GPIO_PIN_7)
#define DIGIT1_PIN (GPIO_PIN_13)
#define DIGIT2_PIN (GPIO_PIN_14)
#define DIGIT3_PIN (GPIO_PIN_15)
/* 数码管段选对应的GPIO端口和引脚 */
static stc_gpio_t* const SEG_PORT[8] = {GPIOB, GPIOB, GPIOB, GPIOB, GPIOB, GPIOB, GPIOB, GPIOB};
static uint32_t const SEG_PIN[8] = {SEG_A_PIN, SEG_B_PIN, SEG_C_PIN, SEG_D_PIN, SEG_E_PIN, SEG_F_PIN, SEG_G_PIN, SEG_DP_PIN};
/* 数码管位选对应的GPIO端口和引脚 */
static stc_gpio_t* const DIGIT_PORT[3] = {GPIOC, GPIOC, GPIOC};
static uint32_t const DIGIT_PIN[3] = {DIGIT1_PIN, DIGIT2_PIN, DIGIT3_PIN};
/* 数码管编码表,从0到F */
static uint8_t const SEG_CODE[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71};
/**
* @brief 数码管初始化
*/
static void Seg_Init(void)
{
uint8_t i;
/* 设置段选引脚为输出模式 */
for(i = 0; i < 8; i++)
{
GPIO_SetFunc(SEG_PORT[i], SEG_PIN[i], GPIO_FUNC_0_SEG);
GPIO_SetOutput(SEG_PORT[i], SEG_PIN[i]);
}
/* 设置位选引脚为输出模式 */
for(i = 0; i < 3; i++)
{
GPIO_SetFunc(DIGIT_PORT[i], DIGIT_PIN[i], GPIO_FUNC_0_SEG);
GPIO_SetOutput(DIGIT_PORT[i], DIGIT_PIN[i]);
}
}
/**
* @brief 显示数字
* @param [in] digit 要显示的数字,0到15
* @param [in] pos 数码管位置,从1到3
*/
static void Seg_ShowDigit(uint8_t digit, uint8_t pos)
{
uint8_t code;
/* 数码管编码 */
code = SEG_CODE[digit];
/* 设置段选引脚输出 */
GPIO_SetOutput(SEG_PORT[0], SEG_A_PIN, (code & 0x01) ? 0 : 1);
GPIO_SetOutput(SEG_PORT[1], SEG_B_PIN, (code & 0x02) ? 0 : 1);
GPIO_SetOutput(SEG_PORT[2], SEG_C_PIN, (code & 0x04) ? 0 : 1);
GPIO_SetOutput(SEG_PORT[3], SEG_D_PIN, (code & 0x08) ? 0 : 1);
GPIO_SetOutput(SEG_PORT[4], SEG_E_PIN, (code & 0x10) ? 0 : 1);
GPIO_SetOutput(SEG_PORT[5], SEG_F_PIN, (code & 0x20) ? 0 : 1);
GPIO_SetOutput(SEG_PORT[6], SEG_G_PIN, (code & 0x40) ? 0 : 1);
GPIO_SetOutput(SEG_PORT[7], SEG_DP_PIN, (code & 0x80) ? 0 : 1);
/* 设置位选引脚输出 */
switch(pos)
{
case 1:
GPIO_SetOutput(DIGIT_PORT[0], DIGIT_PIN[0], 0);
GPIO_SetOutput(DIGIT_PORT[1], DIGIT_PIN[1], 1);
GPIO_SetOutput(DIGIT_PORT[2], DIGIT_PIN[2], 1);
break;
case 2:
GPIO_SetOutput(DIGIT_PORT[0], DIGIT_PIN[0], 1);
GPIO_SetOutput(DIGIT_PORT[1], DIGIT_PIN[1], 0);
GPIO_SetOutput(DIGIT_PORT[2], DIGIT_PIN[2], 1);
break;
case 3:
GPIO_SetOutput(DIGIT_PORT[0], DIGIT_PIN[0], 1);
GPIO_SetOutput(DIGIT_PORT[1], DIGIT_PIN[1], 1);
GPIO_SetOutput(DIGIT_PORT[2], DIGIT_PIN[2], 0);
break;
default:
break;
}
}
int main(void)
{
uint8_t cnt = 0;
/* 系统时钟初始化 */
CLK_HSECmd(CLK_HSE_ON);
CLK_WaitClockReady(CLK_FLAG_HSESTB);
CLK_SetSysclkDiv(CLK_SYSCLK_DIV1);
CLK_SetSysclkSrc(CLK_SYSCLKSOURCE_HSE);
CLK_SetHCLKDiv(CLK_SYSCLK_DIV1);
CLK_SetPCLKDiv(CLK_SYSCLK_DIV1);
/* 数码管初始化 */
Seg_Init();
while(1)
{
/* 显示数字 */
Seg_ShowDigit(cnt % 16, 1);
Seg_ShowDigit((cnt / 16) % 16, 2);
Seg_ShowDigit((cnt / 256) % 16, 3);
cnt++;
}
}
```
在这个例子中,我们通过HC32L13x单片机控制了三个位数码管显示一个计数器的值。在`main`函数中,我们循环计数器`cnt`并调用`Seg_ShowDigit`函数显示不同的位数。`Seg_ShowDigit`函数根据要显示的数字和数码管位置设置相应的GPIO引脚输出状态。
阅读全文