OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 3094|回复: 4

分享一个可以任意设置管脚的4位数码管动态显示程序

[复制链接]

3

主题

15

帖子

0

精华

初级会员

Rank: 2

积分
56
金钱
56
注册时间
2016-7-20
在线时间
13 小时
发表于 2016-7-20 15:43:24 | 显示全部楼层 |阅读模式
本人也新手一个,有个项目用到一个四位数码管,可是管脚并不连续,思考了半天,用了最笨的方法,不过还挺实用,分享给大家,一起学习。数码管为四位共阴数码管,用了switch分支语句。

#include "seg.h"
#include "delay.h"
#define zero    GPIO_SetBits(GPIOC,GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11)
#define zero1   GPIO_ResetBits(GPIOC,GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11)
#define one     GPIO_SetBits(GPIOC,GPIO_Pin_7|GPIO_Pin_8)
#define one1    GPIO_ResetBits(GPIOC,GPIO_Pin_7|GPIO_Pin_8)
#define two     GPIO_SetBits(GPIOC,GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_12)
#define two1    GPIO_ResetBits(GPIOC,GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_12)
#define three   GPIO_SetBits(GPIOC,GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12)
#define three1  GPIO_ResetBits(GPIOC,GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12)
#define four    GPIO_SetBits(GPIOC,GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_11|GPIO_Pin_12)
#define four1   GPIO_ResetBits(GPIOC,GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_11|GPIO_Pin_12)
#define five    GPIO_SetBits(GPIOC,GPIO_Pin_6|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_11|GPIO_Pin_12)
#define five1   GPIO_ResetBits(GPIOC,GPIO_Pin_6|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_11|GPIO_Pin_12)
#define six     GPIO_SetBits(GPIOC,GPIO_Pin_6|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12)
#define six1    GPIO_ResetBits(GPIOC,GPIO_Pin_6|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12)
#define seven   GPIO_SetBits(GPIOC,GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8)
#define seven1  GPIO_ResetBits(GPIOC,GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8)
#define eight   GPIO_SetBits(GPIOC,GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12)
#define eight1  GPIO_ResetBits(GPIOC,GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12)
#define nine    GPIO_SetBits(GPIOC,GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_11|GPIO_Pin_12)
#define nine1   GPIO_ResetBits(GPIOC,GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_11|GPIO_Pin_12)
u8 Thou,Hund,Ten;
/**********************************************           
             4位共阴数码管GPIO配置
             GPIOC Pin6~12为数码管的a\b\c\d\e\f\g没有小数点
                                                 GPIOA Pin 4\5\6\7为位选
***********************************************/


void SEG_GPIO_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC,ENABLE);
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12;
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_Init(GPIOC,&GPIO_InitStructure);
       
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_Init(GPIOA,&GPIO_InitStructure);
       
  GPIO_ResetBits(GPIOC,GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12);
        GPIO_SetBits(GPIOA,GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);
}

/**********************************************           
             4位共阴数码管显示
             单位为L
***********************************************/

void Display(void)
{
        switch(Thou/10)
        {
                case 0 :
                {
                zero;
                GPIO_ResetBits(GPIOA,GPIO_Pin_4);//
                        delay_ms(5);
                zero1;
                GPIO_SetBits(GPIOA,GPIO_Pin_4);//
                        break;
                }
       
               
                case 1 :
                {
                one;
                GPIO_ResetBits(GPIOA,GPIO_Pin_4);//2
                        delay_ms(5);
                one1;
                GPIO_SetBits(GPIOA,GPIO_Pin_4);//
                        break;
                }
         
               
                case 2 :
                {
                two;
                GPIO_ResetBits(GPIOA,GPIO_Pin_4);//
                        delay_ms(5);
                two1;
                GPIO_SetBits(GPIOA,GPIO_Pin_4);//
                        break;
                }
               
                case 3 :
                {
                three;
                GPIO_ResetBits(GPIOA,GPIO_Pin_4);//
                        delay_ms(5);
                three1;
                GPIO_SetBits(GPIOA,GPIO_Pin_4);//
                        break;
                }
               
                case 4 :
                {
                four;
                GPIO_ResetBits(GPIOA,GPIO_Pin_4);//
                        delay_ms(5);
                four1;
                GPIO_SetBits(GPIOA,GPIO_Pin_4);//
                        break;
                }
       
               
                case 5 :
                {
                five;
                GPIO_ResetBits(GPIOA,GPIO_Pin_4);//2
                        delay_ms(5);
                five1;
                GPIO_SetBits(GPIOA,GPIO_Pin_4);//
                        break;
                }
         
               
                case 6 :
                {
                six;
                GPIO_ResetBits(GPIOA,GPIO_Pin_4);//
                        delay_ms(5);
                six1;
                GPIO_SetBits(GPIOA,GPIO_Pin_4);//
                        break;
                }
               
                case 7 :
                {
                seven;
                GPIO_ResetBits(GPIOA,GPIO_Pin_4);//
                        delay_ms(5);
                seven1;
                GPIO_SetBits(GPIOA,GPIO_Pin_4);//
                        break;
                }
               
               
                case 8 :
                {
                eight;
                GPIO_ResetBits(GPIOA,GPIO_Pin_4);//
                        delay_ms(5);
                eight1;
                GPIO_SetBits(GPIOA,GPIO_Pin_4);//
                        break;
                }
       
               
                case 9 :
                {
                nine;
                GPIO_ResetBits(GPIOA,GPIO_Pin_4);//2
                        delay_ms(5);
                nine1;
                GPIO_SetBits(GPIOA,GPIO_Pin_4);//
                        break;
                }
               
        }
               
               
               
                switch(Thou%10)
        {
                case 0 :
                {
                zero;
                GPIO_ResetBits(GPIOA,GPIO_Pin_5);//
                        delay_ms(5);
                zero1;
                GPIO_SetBits(GPIOA,GPIO_Pin_5);//
                        break;
                }
       
               
                case 1 :
                {
                one;
                GPIO_ResetBits(GPIOA,GPIO_Pin_5);//2
                        delay_ms(5);
                one1;
                GPIO_SetBits(GPIOA,GPIO_Pin_5);//
                        break;
                }
         
               
                case 2 :
                {
                two;
                GPIO_ResetBits(GPIOA,GPIO_Pin_5);//
                        delay_ms(5);
                two1;
                GPIO_SetBits(GPIOA,GPIO_Pin_5);//
                        break;
                }
               
                case 3 :
                {
                three;
                GPIO_ResetBits(GPIOA,GPIO_Pin_5);//
                        delay_ms(5);
                three1;
                GPIO_SetBits(GPIOA,GPIO_Pin_5);//
                        break;
                }
               
                case 4 :
                {
                four;
                GPIO_ResetBits(GPIOA,GPIO_Pin_5);//
                        delay_ms(5);
                four1;
                GPIO_SetBits(GPIOA,GPIO_Pin_5);//
                        break;
                }
       
               
                case 5 :
                {
                five;
                GPIO_ResetBits(GPIOA,GPIO_Pin_5);//2
                        delay_ms(5);
                five1;
                GPIO_SetBits(GPIOA,GPIO_Pin_5);//
                        break;
                }
         
               
                case 6 :
                {
                six;
                GPIO_ResetBits(GPIOA,GPIO_Pin_5);//
                        delay_ms(5);
                six1;
                GPIO_SetBits(GPIOA,GPIO_Pin_5);//
                        break;
                }
               
                case 7 :
                {
                seven;
                GPIO_ResetBits(GPIOA,GPIO_Pin_5);//
                        delay_ms(5);
                seven1;
                GPIO_SetBits(GPIOA,GPIO_Pin_5);//
                        break;
                }
               
               
                case 8 :
                {
                eight;
                GPIO_ResetBits(GPIOA,GPIO_Pin_5);//
                        delay_ms(5);
                eight1;
                GPIO_SetBits(GPIOA,GPIO_Pin_5);//
                        break;
                }
       
               
                case 9 :
                {
                nine;
                GPIO_ResetBits(GPIOA,GPIO_Pin_5);//2
                        delay_ms(5);
                nine1;
                GPIO_SetBits(GPIOA,GPIO_Pin_5);//
                        break;
                }
               
               
        }       
               
                switch(Hund/10)
        {
                case 0 :
                {
                zero;
                GPIO_ResetBits(GPIOA,GPIO_Pin_6);//
                        delay_ms(5);
                zero1;
                GPIO_SetBits(GPIOA,GPIO_Pin_6);//
                        break;
                }
       
               
                case 1 :
                {
                one;
                GPIO_ResetBits(GPIOA,GPIO_Pin_6);//2
                        delay_ms(5);
                one1;
                GPIO_SetBits(GPIOA,GPIO_Pin_6);//
                        break;
                }
         
               
                case 2 :
                {
                two;
                GPIO_ResetBits(GPIOA,GPIO_Pin_6);//
                        delay_ms(5);
                two1;
                GPIO_SetBits(GPIOA,GPIO_Pin_6);//
                        break;
                }
               
                case 3 :
                {
                three;
                GPIO_ResetBits(GPIOA,GPIO_Pin_6);//
                        delay_ms(5);
                three1;
                GPIO_SetBits(GPIOA,GPIO_Pin_6);//
                        break;
                }
               
                case 4 :
                {
                four;
                GPIO_ResetBits(GPIOA,GPIO_Pin_6);//
                        delay_ms(5);
                four1;
                GPIO_SetBits(GPIOA,GPIO_Pin_6);//
                        break;
                }
       
               
                case 5 :
                {
                five;
                GPIO_ResetBits(GPIOA,GPIO_Pin_6);//2
                        delay_ms(5);
                five1;
                GPIO_SetBits(GPIOA,GPIO_Pin_6);//
                        break;
                }
         
               
                case 6 :
                {
                six;
                GPIO_ResetBits(GPIOA,GPIO_Pin_6);//
                        delay_ms(5);
                six1;
                GPIO_SetBits(GPIOA,GPIO_Pin_6);//
                        break;
                }
               
                case 7 :
                {
                seven;
                GPIO_ResetBits(GPIOA,GPIO_Pin_6);//
                        delay_ms(5);
                seven1;
                GPIO_SetBits(GPIOA,GPIO_Pin_6);//
                        break;
                }
               
               
                case 8 :
                {
                eight;
                GPIO_ResetBits(GPIOA,GPIO_Pin_6);//
                        delay_ms(5);
                eight1;
                GPIO_SetBits(GPIOA,GPIO_Pin_6);//
                        break;
                }
       
               
                case 9 :
                {
                nine;
                GPIO_ResetBits(GPIOA,GPIO_Pin_6);//2
                        delay_ms(5);
                nine1;
                GPIO_SetBits(GPIOA,GPIO_Pin_6);//
                        break;
                }
               
               
        }       
               
               
                switch(Hund%10)
        {
                case 0 :
                {
                zero;
                GPIO_ResetBits(GPIOA,GPIO_Pin_7);//
                        delay_ms(5);
                zero1;
                GPIO_SetBits(GPIOA,GPIO_Pin_7);//
                        break;
                }
       
               
                case 1 :
                {
                one;
                GPIO_ResetBits(GPIOA,GPIO_Pin_7);//2
                        delay_ms(5);
                one1;
                GPIO_SetBits(GPIOA,GPIO_Pin_7);//
                        break;
                }
         
               
                case 2 :
                {
                two;
                GPIO_ResetBits(GPIOA,GPIO_Pin_7);//
                        delay_ms(5);
                two1;
                GPIO_SetBits(GPIOA,GPIO_Pin_7);//
                        break;
                }
               
                case 3 :
                {
                three;
                GPIO_ResetBits(GPIOA,GPIO_Pin_7);//
                        delay_ms(5);
                three1;
                GPIO_SetBits(GPIOA,GPIO_Pin_7);//
                        break;
                }
               
                case 4 :
                {
                four;
                GPIO_ResetBits(GPIOA,GPIO_Pin_7);//
                        delay_ms(5);
                four1;
                GPIO_SetBits(GPIOA,GPIO_Pin_7);//
                        break;
                }
       
               
                case 5 :
                {
                five;
                GPIO_ResetBits(GPIOA,GPIO_Pin_7);//2
                        delay_ms(5);
                five1;
                GPIO_SetBits(GPIOA,GPIO_Pin_7);//
                        break;
                }
         
               
                case 6 :
                {
                six;
                GPIO_ResetBits(GPIOA,GPIO_Pin_7);//
                        delay_ms(5);
                six1;
                GPIO_SetBits(GPIOA,GPIO_Pin_7);//
                        break;
                }
               
                case 7 :
                {
                seven;
                GPIO_ResetBits(GPIOA,GPIO_Pin_7);//
                        delay_ms(5);
                seven1;
                GPIO_SetBits(GPIOA,GPIO_Pin_7);//
                        break;
                }
               
               
                case 8 :
                {
                eight;
                GPIO_ResetBits(GPIOA,GPIO_Pin_7);//
                        delay_ms(5);
                eight1;
                GPIO_SetBits(GPIOA,GPIO_Pin_7);//
                        break;
                }
       
               
                case 9 :
                {
                nine;
                GPIO_ResetBits(GPIOA,GPIO_Pin_7);//2
                        delay_ms(5);
                nine1;
                GPIO_SetBits(GPIOA,GPIO_Pin_7);//
                        break;
                }
       
        }
       
}



正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

3

主题

15

帖子

0

精华

初级会员

Rank: 2

积分
56
金钱
56
注册时间
2016-7-20
在线时间
13 小时
 楼主| 发表于 2016-7-20 16:12:34 | 显示全部楼层
回复 支持 1 反对 0

使用道具 举报

3

主题

15

帖子

0

精华

初级会员

Rank: 2

积分
56
金钱
56
注册时间
2016-7-20
在线时间
13 小时
 楼主| 发表于 2016-7-20 15:45:46 | 显示全部楼层
附件不见了···
回复 支持 反对

使用道具 举报

3

主题

15

帖子

0

精华

初级会员

Rank: 2

积分
56
金钱
56
注册时间
2016-7-20
在线时间
13 小时
 楼主| 发表于 2016-7-20 15:49:07 | 显示全部楼层
重新上传附件

STM32数码管动态显示.zip

5.91 MB, 下载次数: 183

回复 支持 反对

使用道具 举报

6

主题

21

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
377
金钱
377
注册时间
2013-5-17
在线时间
43 小时
发表于 2016-7-20 16:07:00 | 显示全部楼层
真刘备
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-7-5 13:01

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表