• 程序名称:数码管静态显示
  • 程序功能:四位数码管显示
  • 程序版本:Ver 1.0
  • 编写日期:2020/07/30
  • 作者信息:天津科技大学创新实验室-JC
/******************************************************************
* 程序名称:数码管静态显示
* 程序功能:四位数码管显示
* 程序版本:Ver 1.0
* 编写日期:2020/07/30
* 作者信息:天津科技大学创新实验室-JC
******************************************************************/

#define A1 39
#define B1 43
#define C1 35
#define D1 A15
#define E1 A13
#define F1 41
#define G1 37
#define DP1 33
#define wei11 3
#define wei12 0
#define wei13 1
#define wei14 2

void setup() {
// put your setup code here, to run once:
// 第 1 个数码管引脚
pinMode(A1,OUTPUT);pinMode(B1,OUTPUT);pinMode(C1,OUTPUT);
pinMode(D1,OUTPUT);pinMode(E1,OUTPUT);pinMode(F1,OUTPUT);
pinMode(G1,OUTPUT);pinMode(DP1,OUTPUT);
pinMode(wei11,OUTPUT);pinMode(wei12,OUTPUT);
pinMode(wei13,OUTPUT);pinMode(wei14,OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
dispyTest();
}

/******************************************************************
* 函数名称:dispy1()
* 函数功能:指定第 1 个数码管的某一位显示某个数字
* 使用示例:dispy1(6,1,1,0,1) //使数码管第三位显示数字6
******************************************************************/
void dispy1(char num, int wei4, int wei3, int wei2, int wei1) {
digitalWrite(wei11, wei1);
digitalWrite(wei12, wei2);
digitalWrite(wei13, wei3);
digitalWrite(wei14, wei4);
switch (num) {
case 1: digitalWrite(A1,1);digitalWrite(B1,0);digitalWrite(C1,0);digitalWrite(D1,1); digitalWrite(E1,1);digitalWrite(F1,1);digitalWrite(G1,1);digitalWrite(DP1,1);break;
case 2: digitalWrite(A1,0);digitalWrite(B1,0);digitalWrite(C1,1);digitalWrite(D1,0); digitalWrite(E1,0);digitalWrite(F1,1);digitalWrite(G1,0);digitalWrite(DP1,1);break;
case 3: digitalWrite(A1,0);digitalWrite(B1,0);digitalWrite(C1,0);digitalWrite(D1,0); digitalWrite(E1,1);digitalWrite(F1,1);digitalWrite(G1,0);digitalWrite(DP1,1);break;
case 4: digitalWrite(A1,1);digitalWrite(B1,0);digitalWrite(C1,0);digitalWrite(D1,1); digitalWrite(E1,1);digitalWrite(F1,0);digitalWrite(G1,0);digitalWrite(DP1,1);break;
case 5: digitalWrite(A1,0);digitalWrite(B1,1);digitalWrite(C1,0);digitalWrite(D1,0); digitalWrite(E1,1);digitalWrite(F1,0);digitalWrite(G1,0);digitalWrite(DP1,1);break;
case 6: digitalWrite(A1,0);digitalWrite(B1,1);digitalWrite(C1,0);digitalWrite(D1,0); digitalWrite(E1,0);digitalWrite(F1,0);digitalWrite(G1,0);digitalWrite(DP1,1);break;
case 7: digitalWrite(A1,0);digitalWrite(B1,0);digitalWrite(C1,0);digitalWrite(D1,1); digitalWrite(E1,1);digitalWrite(F1,1);digitalWrite(G1,1);digitalWrite(DP1,1);break;
case 8: digitalWrite(A1,0);digitalWrite(B1,0);digitalWrite(C1,0);digitalWrite(D1,0); digitalWrite(E1,0);digitalWrite(F1,0);digitalWrite(G1,0);digitalWrite(DP1,1);break;
case 9: digitalWrite(A1,0);digitalWrite(B1,0);digitalWrite(C1,0);digitalWrite(D1,0); digitalWrite(E1,1);digitalWrite(F1,0);digitalWrite(G1,0);digitalWrite(DP1,1);break;
default: digitalWrite(A1,0);digitalWrite(B1,0);digitalWrite(C1,0);digitalWrite(D1,0); digitalWrite(E1,0);digitalWrite(F1,0);digitalWrite(G1,1);digitalWrite(DP1,1);break;
}
delayMicroseconds(500);
}

/******************************************************************
* 函数名称:dispyClose()
* 函数功能:关闭所有数码管的显示,此时位选无效
******************************************************************/
void dispyClose() {
digitalWrite(A1,1);digitalWrite(B1,1);digitalWrite(C1,1);digitalWrite(D1,1); digitalWrite(E1,1);digitalWrite(F1,1);digitalWrite(G1,1);digitalWrite(DP1,1);
delayMicroseconds(500);
}

/******************************************************************
* 函数名称:dispyTest()
* 函数功能:数码管测试函数
******************************************************************/
void dispyTest() {
dispy1(1,0,0,0,1);delay(500);dispyClose();
dispy1(2,0,0,1,0);delay(500);dispyClose();
dispy1(3,0,1,0,0);delay(500);dispyClose();
dispy1(4,1,0,0,0);delay(500);dispyClose();
dispy1(5,1,0,0,0);delay(500);dispyClose();
dispy1(6,0,1,0,0);delay(500);dispyClose();
dispy1(7,0,0,1,0);delay(500);dispyClose();
dispy1(8,0,0,0,1);delay(500);dispyClose();
dispy1(9,1,1,1,1);delay(500);dispyClose();
dispy1(0,1,1,1,1);delay(500);dispyClose();
}

Ref

  1. Arduino 让数码管显示数字
  2. 如何实现数码管动态显示带有小数点的数据