STM32 controls the output count of 4-bit common anode digital tube

Publisher:RadiantGlowLatest update time:2017-09-12 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

A total of 12 pins of GPIO port PA0~PA11 are used

First, the schematic diagram

Because it is a common anode, 12, 9, 8, and 6 are power inputs, and the other pins are grounded. So for the chip, 12, 9, 8, and 6 are high outputs, and the others can be set to low.


  1. //Set the GPIO port  

  2. void GPIO_Num_Init(void)  

  3. {  

  4.     GPIO_InitTypeDef GPIO_InitStructure;  

  5.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);  

  6.     GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE);  

  7.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;  

  8.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;   

  9.     GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;  

  10.     GPIO_Init(GPIOA,&GPIO_InitStructure);  

  11. }  


There is one thing to note here. According to the schematic diagram, the digital tube can only output one bit of array at a time, so if it is multi-bit, it must be output in a scanning manner. By shortening the switching interval, a non-flickering effect can be achieved.


  1. //Output package  

  2. void DisPlayNum(int n)  

  3. {  

  4.     if (n < 9999)  

  5.     { int i;  

  6.         int s;    

  7.         s = n;  

  8.         i=0;  

  9.         for (i=0;i<4;i++)  

  10.         {  

  11.             GPIO_ResetBits(GPIOA,GPIO_Pin_All);  

  12.             switch (i)  

  13.             {  

  14.                                 case 0:  

  15.                     GPIO_SetBits(GPIOA,GPIO_Pin_5);  

  16.                     break;  

  17.                 case 1:  

  18.                     GPIO_SetBits(GPIOA,GPIO_Pin_7);  

  19.                     break;  

  20.                 case 2:  

  21.                     GPIO_SetBits(GPIOA,GPIO_Pin_8);  

  22.                     break;  

  23.                 case 3:  

  24.                     GPIO_SetBits(GPIOA,GPIO_Pin_11);  

  25.                     break;  

  26.                         }  

  27.           

  28.             switch (s % 10)  

  29.             {  

  30.                 case 0:  

  31.                     GPIO_SetBits(GPIOA,GPIO_Pin_2|GPIO_Pin_4);   

  32.                     break;  

  33.                 case 1:  

  34.                     GPIO_SetBits(GPIOA,GPIO_Pin_2|GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_4|GPIO_Pin_9|GPIO_Pin_10);  

  35.                     break;  

  36.                 case 2:  

  37.                     GPIO_SetBits(GPIOA,GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_9);  

  38.                     break;  

  39.                 case 3:  

  40.                     GPIO_SetBits(GPIOA,GPIO_Pin_2|GPIO_Pin_0|GPIO_Pin_9);  

  41.                     break;  

  42.                 case 4:  

  43.                     GPIO_SetBits(GPIOA,GPIO_Pin_2|GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_10);  

  44.                     break;  

  45.                 case 5:  

  46.                     GPIO_SetBits(GPIOA,GPIO_Pin_2|GPIO_Pin_0|GPIO_Pin_6);  

  47.                     break;  

  48.                 case 6:  

  49.                     GPIO_SetBits(GPIOA,GPIO_Pin_2|GPIO_Pin_6);  

  50.                     break;  

  51.                 case 7:  

  52.                     GPIO_SetBits(GPIOA,GPIO_Pin_2|GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_4|GPIO_Pin_9);  

  53.                     break;  

  54.                 case 8:  

  55.                     GPIO_SetBits(GPIOA,GPIO_Pin_2);  

  56.                     break;  

  57.                 case 9:  

  58.                     GPIO_SetBits(GPIOA,GPIO_Pin_2|GPIO_Pin_0);  

  59.                     break;  

  60.             }     

  61.             s = s / 10;  

  62.             delay_ms(1);  

  63.             if (s==0)  

  64.                 break;  

  65.         }      

  66.     }  

  67. }  


Finally, the main test code


  1. int count;  

  2. int TimeCheck;  

  3.   

  4. int main(void)  

  5. {  

  6.     GPIO_Num_Init();  

  7.     count =0;  

  8.     TimeCheck = 0;  

  9.     while(1)  

  10.     {  

  11.         DisPlayNum(count);  

  12.         TimeCheck++;  

  13.         delay_ms(5);  

  14.         if (TimeCheck > 100){ //+1 every 500 milliseconds  

  15.             TimeCheck=0;  

  16.             count++;  

  17.         }  

  18.     }  

  19. }  


Keywords:STM32 Reference address:STM32 controls the output count of 4-bit common anode digital tube

Previous article:STM32 uses PWM to control the LED breathing light effect
Next article:ADC multi-channel acquisition program for STM32f103 digital electrical acquisition circuit

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号