#define key2 A1
#define key3 A2
#define key4 A3
char table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
byte seg7[]={9,8,7,6,5,4,3};
int i,j;
int mode=-1;
void setup()
{
// put your setup code here, to run once:
for(j=0;j<7;j++)
{
pinMode(seg7[j],OUTPUT);
}
pinMode(key1,INPUT);
pinMode(key2,INPUT);
pinMode(key3,INPUT);
pinMode(key4,INPUT);
}
void loop()
{
// put your main code here, to run repeatedly:
if(digitalRead(key1)==0 && mode !=1) {mode=1;outport(~table[0]);}
if(digitalRead(key2)==0 && mode !=2) {mode=2;outport(~table[1]);}
if(digitalRead(key3)==0 && mode !=3) {mode=3;outport(~table[2]);}
if(digitalRead(key4)==0 && mode !=4) {mode=4;outport(~table[3]);}
}
void outport(byte data)
{
for(i=0;i<7;i++)
{
digitalWrite(seg7[i],bitRead(data,i));
}
}
例2:按6、7、8、9
#define key6 A0
#define key7 A1
#define key8 A2
#define key9 A3
char seg7Table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
byte seg7Pin[]={9,8,7,6,5,4,3};
int i,j;
int mode=-1;
void setup()
{
for(i=0;i<7;i++)
{
pinMode(seg7Pin[i],OUTPUT);
}
pinMode(key6,INPUT);
pinMode(key7,INPUT);
pinMode(key8,INPUT);
pinMode(key9,INPUT);
}
void loop()
{
if(digitalRead(key6)==0 && mode !=6)
{
mode=6;
output(~seg7Table[6]);
}
if(digitalRead(key7)==0 && mode !=7)
{
mode=7;
output(~seg7Table[7]);
}
if(digitalRead(key8)==0 && mode !=8)
{
mode=8;
output(~seg7Table[8]);
}
if(digitalRead(key9)==0 && mode !=9)
{
mode=9;
output(~seg7Table[9]);
}
}
void output(byte data)
{
for(i=0;i<7;i++)
{
digitalWrite(seg7Pin[i],bitRead(data,i));
}
}
沒有留言:
張貼留言