初级会员

- 积分
- 56
- 金钱
- 56
- 注册时间
- 2016-7-20
- 在线时间
- 13 小时
|
本人也新手一个,有个项目用到一个四位数码管,可是管脚并不连续,思考了半天,用了最笨的方法,不过还挺实用,分享给大家,一起学习。数码管为四位共阴数码管,用了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;
}
}
}
|
|