[开发板与模块] 【ESK32-30519 + ESK32-21001测评】 动态数码管驱动

[复制链接]
 楼主| tlled 发表于 2022-9-19 23:38 | 显示全部楼层 |阅读模式
<
开发板上有一个4位显示的LED数码管,下面来驱动数码管动态显示。

一、数码管电路图

1.1、数码管引脚的定义
20220919232240.png

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

二、驱动程序

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


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

  6. 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};

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

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

  9. u8 ledc_tim_cn=0;
  10. u8 ledc_tim_flag=0;
  11.        
  12. void init_ledc(void)
  13. {
  14.        
  15.          { /* Enable peripheral clock                                                                              */
  16.     CKCU_PeripClockConfig_TypeDef CKCUClock = {{ 0 }};
  17.     CKCUClock.Bit.AFIO = 1;
  18.     CKCUClock.Bit.PA = 1;
  19.                 CKCUClock.Bit.PC = 1;
  20.                 CKCUClock.Bit.PD = 1;
  21.     CKCU_PeripClockConfig(CKCUClock, ENABLE);
  22.   }
  23.          
  24.         { /* Configure GPIO as output mode                                                                        */

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

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

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

  35.     /* Configure GPIO direction as output                                                                   */
  36.     GPIO_DirectionConfig(HT_GPIOA, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_7, GPIO_DIR_OUT);
  37.                 GPIO_DirectionConfig(HT_GPIOC, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7, GPIO_DIR_OUT);
  38.                 GPIO_DirectionConfig(HT_GPIOD, GPIO_PIN_2|GPIO_PIN_4|GPIO_PIN_5, GPIO_DIR_OUT);
  39.   }
  40.        
  41. }



  42. void ledc_dig(u8 dig)
  43. {
  44.        
  45.         if(dig&0x01)
  46.         {
  47.                 GPIO_ClearOutBits(HT_GPIOC, GPIO_PIN_4); // GPIO = LOW
  48.         }
  49.         else
  50.         {
  51.                 GPIO_SetOutBits(HT_GPIOC, GPIO_PIN_4); // GPIO = HIGH
  52.         }
  53.        
  54.         if(dig&0x02)
  55.         {
  56.                 GPIO_ClearOutBits(HT_GPIOC, GPIO_PIN_5); // GPIO = LOW
  57.         }
  58.         else
  59.         {
  60.                 GPIO_SetOutBits(HT_GPIOC, GPIO_PIN_5); // GPIO = HIGH
  61.         }
  62.        
  63.         if(dig&0x04)
  64.         {
  65.                 GPIO_ClearOutBits(HT_GPIOC, GPIO_PIN_6); // GPIO = LOW
  66.         }
  67.         else
  68.         {
  69.                 GPIO_SetOutBits(HT_GPIOC, GPIO_PIN_6); // GPIO = HIGH
  70.         }
  71.        
  72.         if(dig&0x08)
  73.         {
  74.                 GPIO_ClearOutBits(HT_GPIOC, GPIO_PIN_7); // GPIO = LOW
  75.         }
  76.         else
  77.         {
  78.                 GPIO_SetOutBits(HT_GPIOC, GPIO_PIN_7); // GPIO = HIGH
  79.         }
  80. }

  81. void ledc_dat(u8 dat)
  82. {
  83.         //A
  84.         if(dat&0x01)
  85.         {
  86.                 GPIO_SetOutBits(HT_GPIOD, GPIO_PIN_4); // GPIO = HIGH
  87.         }
  88.         else
  89.         {
  90.                 GPIO_ClearOutBits(HT_GPIOD, GPIO_PIN_4); // GPIO = LOW
  91.         }
  92.        
  93.         //B
  94.         if(dat&0x02){
  95.                 GPIO_SetOutBits(HT_GPIOA, GPIO_PIN_7);  
  96.         }        else        {
  97.                 GPIO_ClearOutBits(HT_GPIOA, GPIO_PIN_7);  
  98.         }
  99.        
  100.         //C
  101.         if(dat&0x04){
  102.                 GPIO_SetOutBits(HT_GPIOA, GPIO_PIN_2);  
  103.         }        else        {
  104.                 GPIO_ClearOutBits(HT_GPIOA, GPIO_PIN_2);  
  105.         }
  106.        
  107.         //D
  108.         if(dat&0x08){
  109.                 GPIO_SetOutBits(HT_GPIOD, GPIO_PIN_5);  
  110.         }        else        {
  111.                 GPIO_ClearOutBits(HT_GPIOD, GPIO_PIN_5);  
  112.         }
  113.        
  114.         //E
  115.         if(dat&0x10){
  116.                 GPIO_SetOutBits(HT_GPIOA, GPIO_PIN_3);  
  117.         }        else        {
  118.                 GPIO_ClearOutBits(HT_GPIOA, GPIO_PIN_3);  
  119.         }
  120.        
  121.         //F
  122.         if(dat&0x20){
  123.                 GPIO_SetOutBits(HT_GPIOA, GPIO_PIN_0);  
  124.         }        else        {
  125.                 GPIO_ClearOutBits(HT_GPIOA, GPIO_PIN_0);  
  126.         }
  127.        
  128.         //G
  129.         if(dat&0x40){
  130.                 GPIO_SetOutBits(HT_GPIOD, GPIO_PIN_2);  
  131.         }        else        {
  132.                 GPIO_ClearOutBits(HT_GPIOD, GPIO_PIN_2);  
  133.         }
  134.        
  135.         //DP
  136.         if(dat&0x80){
  137.                 GPIO_SetOutBits(HT_GPIOA, GPIO_PIN_1);  
  138.         }        else        {
  139.                 GPIO_ClearOutBits(HT_GPIOA, GPIO_PIN_1);  
  140.         }
  141. }


  142. u8 i=0;
  143. u8 flag=1;


  144. void ledc_hello(void)
  145. {
  146.         while(flag)
  147.         {
  148.                 disp[0]= disp_hello_dat[i];
  149.                 disp[1]= disp_hello_dat[i+1];
  150.                 disp[2]= disp_hello_dat[i+2];
  151.                 disp[3]= disp_hello_dat[i+3];
  152.                 i++;
  153.                 if(i>18)
  154.                 {
  155.                         i=0;
  156.                         flag=0;
  157.                 }
  158.                 delay_ms(500);
  159.         }
  160. }

  161. u16 cn=0;
  162. void  ledc_test(void)
  163. {
  164.         ledc_hello();
  165.         cn++;
  166.         if(cn>1000)
  167.         {
  168.                 cn=0;
  169.         }
  170.         disp[0]= disp_nodp_dat[cn/1000];
  171.         disp[1]=disp_nodp_dat[(cn%1000)/100];
  172.         disp[2]=disp_nodp_dat[((cn%1000)%100)/10];
  173.         disp[3]=disp_nodp_dat[((cn%1000)%100)%10];
  174.         delay_ms(200);
  175. }

  176. void  ledc_test1(void)
  177. {
  178.         ledc_dig(0x0F);
  179.         if(flag==0)
  180.         {
  181.                 ledc_dat(disp_nodp_dat[i]);
  182.                 delay_ms(500);
  183.                
  184.                 i++;
  185.                 if(i>9)
  186.                 {
  187.                         i=0;
  188.                         flag=1;
  189.                 }
  190.         }
  191.        
  192.         else
  193.         {
  194.                 ledc_dat(disp_dp_dat[i]);
  195.                 delay_ms(500);
  196.                
  197.                 i++;
  198.                 if(i>9)
  199.                 {
  200.                         i=0;
  201.                         flag=0;
  202.                 }
  203.         }
  204.        
  205. }


  206. void ledc_tim_hdl(void)
  207. {
  208.                 ledc_tim_flag=0;
  209.                 ledc_tim_cn++;
  210.                 if(ledc_tim_cn>4)
  211.                 {
  212.                         ledc_tim_cn=1;
  213.                 }
  214.                 switch (ledc_tim_cn)
  215.                 {
  216.                         case 1:
  217.                                 ledc_dig(0x00);
  218.                                 ledc_dat(disp[0]);
  219.                                 ledc_dig(0x01);
  220.                                 break;
  221.                         case 2:
  222.                                 ledc_dig(0x00);
  223.                                 ledc_dat(disp[1]);
  224.                                 ledc_dig(0x02);
  225.                                 break;
  226.                         case 3:
  227.                                 ledc_dig(0x00);
  228.                                 ledc_dat(disp[2]);
  229.                                 ledc_dig(0x04);
  230.                                 break;
  231.                         case 4:
  232.                                 ledc_dig(0x00);
  233.                                 ledc_dat(disp[3]);
  234.                                 ledc_dig(0x08);
  235.                                 break;
  236.                         default:
  237.                                 break;
  238.                 }
  239. }



2.2、ledc.h
  1. #ifndef __LEDC_H
  2. #define __LEDC_H

  3. #include "ht32.h"

  4. extern u8 ledc_tim_flag;

  5. void init_ledc(void);
  6. void  ledc_test(void);

  7. void ledc_tim_hdl(void);

  8. #endif


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

  4.   NVIC_Configuration();               /* NVIC configuration                                                 */
  5.   CKCU_Configuration();               /* System Related configuration                                       */
  6.   GPIO_Configuration();               /* GPIO Related configuration                                         */
  7.   RETARGET_Configuration();           /* Retarget Related configuration                                     */
  8.        
  9.         init_ledc();
  10.         delay_init();
  11.         init_timer();
  12.        

  13.   HT32F_DVB_LEDInit(HT_LED1);
  14.   HT32F_DVB_LEDInit(HT_LED2);
  15.   HT32F_DVB_LEDInit(HT_LED3);
  16.   HT32F_DVB_LEDOff(HT_LED1);
  17.   HT32F_DVB_LEDOff(HT_LED2);
  18.   HT32F_DVB_LEDOff(HT_LED3);
  19.        
  20.        
  21.         while(1)
  22.         {
  23.                 HT32F_DVB_LEDToggle(HT_LED1);
  24.                 delay_ms(1);
  25.                 //HT32F_DVB_LEDOff(HT_LED1);
  26.                 //delay_ms(1);
  27.                 ledc_test();
  28.         }



三、程序运行

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

200.gif
您需要登录后才可以回帖 登录 | 注册

本版积分规则

132

主题

701

帖子

7

粉丝
快速回复 在线客服 返回列表 返回顶部