int i;
int j;
int k;
int ledStatus[10];
const int numCols=4;
const int numRows=4;
const int debounceDelay=20;
const char num[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
const int seg7[]={6,7,8,9,10,11,12};
const int col[]={A0,A1,A2,A3};//類比輸入當數位腳(行)
const int row[]={2,3,4,5};//{9,8,7,6};
const int keyMap[numRows][numCols]=
{
{1, 2, 3, 12},
{4, 5, 6, 13},
{7, 8, 9, 14},
{10,0,11, 15}
};
void setup()
{
for(i=0;i<numRows;i++)
{
pinMode(row[i],OUTPUT);
digitalWrite(row[i],HIGH);
}
for(j=0;j<numCols;j++)
{
pinMode(col[j],INPUT);
digitalWrite(col[j],HIGH);
}
for(k=0;k<7;k++)
{
pinMode(seg7[k],OUTPUT);
digitalWrite(seg7[k],LOW);
}
}
void loop()
{
int key=getKey();
if(key>=0 && key<=9)
{
output(num[key]);
}
}
int getKey()
{
int i;
int j;
int key=-1;
for(i=0;i<numRows;i++)
{
digitalWrite(row[i],LOW);
for(j=0;j<numCols;j++)
{
if(digitalRead(col[j])==LOW)
{
delay(debounceDelay);
while(digitalRead(col[j])==LOW)
;
key=keyMap[i][j];
}
}
digitalWrite(row[i],HIGH);
}
return(key);
}
void output(byte data)
{
for(i=0;i<7;i++)
digitalWrite(seg7[i],bitRead(~data,i));
}
沒有留言:
張貼留言