打印
[开发板与模块]

【ESK32-30519 + ESK32-21001测评】 动态数码管驱动

[复制链接]
439|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
tlled|  楼主 | 2022-9-19 23:38 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
开发板上有一个4位显示的LED数码管,下面来驱动数码管动态显示。

一、数码管电路图

1.1、数码管引脚的定义


1.2、数码管连接开发板对应的引脚


二、驱动程序

2.1、ledc.c
#include "ht32.h"
#include "ht32_board.h"
#include "delay.h"


const  u8 disp_nodp_dat[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
const  u8 disp_dp_dat[10]={0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF};

const  u8 disp_hello_dat[22]={0x00,0x00,0x00,0x00,0x76,0x79,0x38,0x38,0x3f,0x00,0x76,0x4f,0x5b,0x71,0x6d,0x66,0x5b,0x4f,0x00,0x00,0x00,0x00};

u8 disp[4]={0x00,0x00,0x00,0x00};

//u8 disp[4]={0x3F,0x06,0x5B,0x4F};

u8 ledc_tim_cn=0;
u8 ledc_tim_flag=0;
       
void init_ledc(void)
{
       
         { /* Enable peripheral clock                                                                              */
    CKCU_PeripClockConfig_TypeDef CKCUClock = {{ 0 }};
    CKCUClock.Bit.AFIO = 1;
    CKCUClock.Bit.PA = 1;
                CKCUClock.Bit.PC = 1;
                CKCUClock.Bit.PD = 1;
    CKCU_PeripClockConfig(CKCUClock, ENABLE);
  }
         
        { /* Configure GPIO as output mode                                                                        */

    /* Configure AFIO mode as GPIO                                                                          */
    //AFIO_GPxConfig(GPIO_PB, AFIO_PIN_1, AFIO_FUN_GPIO);

    /* Configure GPIO pull resistor                                                                         */
    GPIO_PullResistorConfig(HT_GPIOA, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_7, GPIO_PR_UP);
                GPIO_PullResistorConfig(HT_GPIOC, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7, GPIO_PR_UP);
                GPIO_PullResistorConfig(HT_GPIOD, GPIO_PIN_2|GPIO_PIN_4|GPIO_PIN_5, GPIO_PR_UP);

    /* Default value RESET/SET                                                                              */
    GPIO_WriteOutBits(HT_GPIOA, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_7, RESET);
                GPIO_WriteOutBits(HT_GPIOC, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7, RESET);
                GPIO_WriteOutBits(HT_GPIOD, GPIO_PIN_2|GPIO_PIN_4|GPIO_PIN_5, RESET);

    /* Configure GPIO direction as output                                                                   */
    GPIO_DirectionConfig(HT_GPIOA, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_7, GPIO_DIR_OUT);
                GPIO_DirectionConfig(HT_GPIOC, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7, GPIO_DIR_OUT);
                GPIO_DirectionConfig(HT_GPIOD, GPIO_PIN_2|GPIO_PIN_4|GPIO_PIN_5, GPIO_DIR_OUT);
  }
       
}



void ledc_dig(u8 dig)
{
       
        if(dig&0x01)
        {
                GPIO_ClearOutBits(HT_GPIOC, GPIO_PIN_4); // GPIO = LOW
        }
        else
        {
                GPIO_SetOutBits(HT_GPIOC, GPIO_PIN_4); // GPIO = HIGH
        }
       
        if(dig&0x02)
        {
                GPIO_ClearOutBits(HT_GPIOC, GPIO_PIN_5); // GPIO = LOW
        }
        else
        {
                GPIO_SetOutBits(HT_GPIOC, GPIO_PIN_5); // GPIO = HIGH
        }
       
        if(dig&0x04)
        {
                GPIO_ClearOutBits(HT_GPIOC, GPIO_PIN_6); // GPIO = LOW
        }
        else
        {
                GPIO_SetOutBits(HT_GPIOC, GPIO_PIN_6); // GPIO = HIGH
        }
       
        if(dig&0x08)
        {
                GPIO_ClearOutBits(HT_GPIOC, GPIO_PIN_7); // GPIO = LOW
        }
        else
        {
                GPIO_SetOutBits(HT_GPIOC, GPIO_PIN_7); // GPIO = HIGH
        }
}

void ledc_dat(u8 dat)
{
        //A
        if(dat&0x01)
        {
                GPIO_SetOutBits(HT_GPIOD, GPIO_PIN_4); // GPIO = HIGH
        }
        else
        {
                GPIO_ClearOutBits(HT_GPIOD, GPIO_PIN_4); // GPIO = LOW
        }
       
        //B
        if(dat&0x02){
                GPIO_SetOutBits(HT_GPIOA, GPIO_PIN_7);  
        }        else        {
                GPIO_ClearOutBits(HT_GPIOA, GPIO_PIN_7);  
        }
       
        //C
        if(dat&0x04){
                GPIO_SetOutBits(HT_GPIOA, GPIO_PIN_2);  
        }        else        {
                GPIO_ClearOutBits(HT_GPIOA, GPIO_PIN_2);  
        }
       
        //D
        if(dat&0x08){
                GPIO_SetOutBits(HT_GPIOD, GPIO_PIN_5);  
        }        else        {
                GPIO_ClearOutBits(HT_GPIOD, GPIO_PIN_5);  
        }
       
        //E
        if(dat&0x10){
                GPIO_SetOutBits(HT_GPIOA, GPIO_PIN_3);  
        }        else        {
                GPIO_ClearOutBits(HT_GPIOA, GPIO_PIN_3);  
        }
       
        //F
        if(dat&0x20){
                GPIO_SetOutBits(HT_GPIOA, GPIO_PIN_0);  
        }        else        {
                GPIO_ClearOutBits(HT_GPIOA, GPIO_PIN_0);  
        }
       
        //G
        if(dat&0x40){
                GPIO_SetOutBits(HT_GPIOD, GPIO_PIN_2);  
        }        else        {
                GPIO_ClearOutBits(HT_GPIOD, GPIO_PIN_2);  
        }
       
        //DP
        if(dat&0x80){
                GPIO_SetOutBits(HT_GPIOA, GPIO_PIN_1);  
        }        else        {
                GPIO_ClearOutBits(HT_GPIOA, GPIO_PIN_1);  
        }
}


u8 i=0;
u8 flag=1;


void ledc_hello(void)
{
        while(flag)
        {
                disp[0]= disp_hello_dat[i];
                disp[1]= disp_hello_dat[i+1];
                disp[2]= disp_hello_dat[i+2];
                disp[3]= disp_hello_dat[i+3];
                i++;
                if(i>18)
                {
                        i=0;
                        flag=0;
                }
                delay_ms(500);
        }
}

u16 cn=0;
void  ledc_test(void)
{
        ledc_hello();
        cn++;
        if(cn>1000)
        {
                cn=0;
        }
        disp[0]= disp_nodp_dat[cn/1000];
        disp[1]=disp_nodp_dat[(cn%1000)/100];
        disp[2]=disp_nodp_dat[((cn%1000)%100)/10];
        disp[3]=disp_nodp_dat[((cn%1000)%100)%10];
        delay_ms(200);
}

void  ledc_test1(void)
{
        ledc_dig(0x0F);
        if(flag==0)
        {
                ledc_dat(disp_nodp_dat[i]);
                delay_ms(500);
               
                i++;
                if(i>9)
                {
                        i=0;
                        flag=1;
                }
        }
       
        else
        {
                ledc_dat(disp_dp_dat[i]);
                delay_ms(500);
               
                i++;
                if(i>9)
                {
                        i=0;
                        flag=0;
                }
        }
       
}


void ledc_tim_hdl(void)
{
                ledc_tim_flag=0;
                ledc_tim_cn++;
                if(ledc_tim_cn>4)
                {
                        ledc_tim_cn=1;
                }
                switch (ledc_tim_cn)
                {
                        case 1:
                                ledc_dig(0x00);
                                ledc_dat(disp[0]);
                                ledc_dig(0x01);
                                break;
                        case 2:
                                ledc_dig(0x00);
                                ledc_dat(disp[1]);
                                ledc_dig(0x02);
                                break;
                        case 3:
                                ledc_dig(0x00);
                                ledc_dat(disp[2]);
                                ledc_dig(0x04);
                                break;
                        case 4:
                                ledc_dig(0x00);
                                ledc_dat(disp[3]);
                                ledc_dig(0x08);
                                break;
                        default:
                                break;
                }
}



2.2、ledc.h
#ifndef __LEDC_H
#define __LEDC_H

#include "ht32.h"

extern u8 ledc_tim_flag;

void init_ledc(void);
void  ledc_test(void);

void ledc_tim_hdl(void);

#endif


2.3、main.c
int main(void)
{
  s32 input;

  NVIC_Configuration();               /* NVIC configuration                                                 */
  CKCU_Configuration();               /* System Related configuration                                       */
  GPIO_Configuration();               /* GPIO Related configuration                                         */
  RETARGET_Configuration();           /* Retarget Related configuration                                     */
       
        init_ledc();
        delay_init();
        init_timer();
       

  HT32F_DVB_LEDInit(HT_LED1);
  HT32F_DVB_LEDInit(HT_LED2);
  HT32F_DVB_LEDInit(HT_LED3);
  HT32F_DVB_LEDOff(HT_LED1);
  HT32F_DVB_LEDOff(HT_LED2);
  HT32F_DVB_LEDOff(HT_LED3);
       
       
        while(1)
        {
                HT32F_DVB_LEDToggle(HT_LED1);
                delay_ms(1);
                //HT32F_DVB_LEDOff(HT_LED1);
                //delay_ms(1);
                ledc_test();
        }



三、程序运行

上电后,滚动显示“HELLO H32F5423”,然后从0开始计数


使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

125

主题

689

帖子

6

粉丝