Arduino学习手记三:八位数码管

8位数码管

上图是八位数码管里各二极管和引脚的关系。

一共两个正极(接+5v),剩下的8个脚,哪个是低电平时管就亮,于是就可以凑出来数字。这个小数点可以用来区分正反。

数字0数字1引脚对程序upload的影响 如果你将数码管接到了数字0和数字1上,那很遗憾,每次upload的时候,你都要拔下来才行。因为数据交互借用了这两个脚做事情。

如果你的IDE出现avrdude: stk500_recv(): programmer is not responding的提示的时候,注意看看你的数字0和数字1是不是插着东西。

八位数码管的连线 数字:
a-12
b-13
c-2
d-3
e-4
f-5
g-6
DP-7

剩下的两个正极同时接到+5v上。

这里a和b故意让开0和1,方便开发过程,不需要拔来拔去的。

显示的代码

void num9()
{ digitalWrite(pin0,LOW); //显示9
digitalWrite(pin1,LOW);
digitalWrite(pin2,LOW);
digitalWrite(pin3,LOW);
digitalWrite(pin4,HIGH);
digitalWrite(pin5,LOW);
digitalWrite(pin6,LOW);
digitalWrite(pin7,HIGH);
}

以上是一个数字9的显示。

完整的代码 以下是在第二节里的代码和连线基础上的加强版本 http://www.54chen.com/_linux_/arduino-starter-2-led.html
代码完成了在摇控器上按数字几,在八位管上显示数字几。

连线需要先按照第二节中的连线之后,再加上本节的连线。

效果图: 八位数码管

#include

#define LED 11
#define BIZZER 10
#define IR_IN 8 //红外接收

IRrecv irrecv(IR_IN); // 定义IRrecv 对象来接收红外线信号
decode_results results; //解码结果放在decode_results构造的对象results里

int pin0=12; //设置蜂鸣器数字接口
int pin1=13;
int pin2=2;
int pin3=3;
int pin4=4;
int pin5=5;
int pin6=6;
int pin7=7;
void setup()
{ pinMode(BIZZER,OUTPUT);
pinMode(LED,OUTPUT);
pinMode(pin0,OUTPUT);
pinMode(pin1,OUTPUT);
pinMode(pin2,OUTPUT);
pinMode(pin3,OUTPUT);
pinMode(pin4,OUTPUT);
pinMode(pin5,OUTPUT);
pinMode(pin6,OUTPUT);
pinMode(pin7,OUTPUT);
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
numd();
}

void loop()
{
if (irrecv.decode(&results))
{
switch(results.value)
{
case 0xFD00FF:
numd();
break;
case 0xFD30CF:
num0();
break;
case 0xFD08F7:
num1();
break;
case 0xFD8877:
num2();
break;
case 0xFD48B7:
num3();
break;
case 0xFD28D7:
num4();
break;
case 0xFDA857:
num5();
break;
case 0xFD6897:
num6();
break;
case 0xFD18E7:
num7();
break;
case 0xFD9867:
num8();
break;
case 0xFD58A7:
num9();
break;
case 0xFD40BF:
digitalWrite(BIZZER,HIGH);
delay(50);
digitalWrite(BIZZER,LOW);
break;
default:
Serial.println(results.value, HEX);
digitalWrite(LED,HIGH);
delay(50);
digitalWrite(LED,LOW);
}
irrecv.resume(); // Receive the next value
}
}

void num0()
{ digitalWrite(pin0,LOW); //显示0
digitalWrite(pin1,LOW);
digitalWrite(pin2,LOW);
digitalWrite(pin3,LOW);
digitalWrite(pin4,LOW);
digitalWrite(pin5,LOW);
digitalWrite(pin6,HIGH);
digitalWrite(pin7,HIGH);
} void num1()
{ digitalWrite(pin0,HIGH); //显示1
digitalWrite(pin1,LOW);
digitalWrite(pin2,LOW);
digitalWrite(pin3,HIGH);
digitalWrite(pin4,HIGH);
digitalWrite(pin5,HIGH);
digitalWrite(pin6,HIGH);
digitalWrite(pin7,HIGH);
digitalWrite(pin0,HIGH);
} void num2()
{ digitalWrite(pin0,LOW); //显示2
digitalWrite(pin1,LOW);
digitalWrite(pin2,HIGH);
digitalWrite(pin3,LOW);
digitalWrite(pin4,LOW);
digitalWrite(pin5,HIGH);
digitalWrite(pin6,LOW);
digitalWrite(pin7,HIGH);
} void num3()
{ digitalWrite(pin0,LOW); //显示3
digitalWrite(pin1,LOW);
digitalWrite(pin2,LOW);
digitalWrite(pin3,LOW);
digitalWrite(pin4,HIGH);
digitalWrite(pin5,HIGH);
digitalWrite(pin6,LOW);
digitalWrite(pin7,HIGH);
} void num4()
{ digitalWrite(pin0,HIGH); //显示4
digitalWrite(pin1,LOW);
digitalWrite(pin2,LOW);
digitalWrite(pin3,HIGH);
digitalWrite(pin4,HIGH);
digitalWrite(pin5,LOW);
digitalWrite(pin6,LOW);
digitalWrite(pin7,HIGH);
}
void num5()
{ digitalWrite(pin0,LOW); //显示5
digitalWrite(pin1,HIGH);
digitalWrite(pin2,LOW);
digitalWrite(pin3,LOW);
digitalWrite(pin4,HIGH);
digitalWrite(pin5,LOW);
digitalWrite(pin6,LOW);
digitalWrite(pin7,HIGH);
}
void num6()
{ digitalWrite(pin0,LOW); //显示6
digitalWrite(pin1,HIGH);
digitalWrite(pin2,LOW);
digitalWrite(pin3,LOW);
digitalWrite(pin4,LOW);
digitalWrite(pin5,LOW);
digitalWrite(pin6,LOW);
digitalWrite(pin7,HIGH);
}
void num7()
{ digitalWrite(pin0,LOW); //显示7
digitalWrite(pin1,LOW);
digitalWrite(pin2,LOW);
digitalWrite(pin3,HIGH);
digitalWrite(pin4,HIGH);
digitalWrite(pin5,HIGH);
digitalWrite(pin6,HIGH);
digitalWrite(pin7,HIGH);
}
void num8()
{ digitalWrite(pin0,LOW); //显示8
digitalWrite(pin1,LOW);
digitalWrite(pin2,LOW);
digitalWrite(pin3,LOW);
digitalWrite(pin4,LOW);
digitalWrite(pin5,LOW);
digitalWrite(pin6,LOW);
digitalWrite(pin7,HIGH);
}
void num9()
{ digitalWrite(pin0,LOW); //显示9
digitalWrite(pin1,LOW);
digitalWrite(pin2,LOW);
digitalWrite(pin3,LOW);
digitalWrite(pin4,HIGH);
digitalWrite(pin5,LOW);
digitalWrite(pin6,LOW);
digitalWrite(pin7,HIGH);
}
void numd()
{ digitalWrite(pin0,HIGH); //显示点DP
digitalWrite(pin1,HIGH);
digitalWrite(pin2,HIGH);
digitalWrite(pin3,HIGH);
digitalWrite(pin4,HIGH);
digitalWrite(pin5,HIGH);
digitalWrite(pin6,HIGH);
digitalWrite(pin7,LOW);
}


原创文章如转载,请注明:转载自五四陈科学院[http://www.54chen.com]

捐款订阅54chen
捐赠说明