const byte colPins[4] = {9, 8, 7, 6}; // 設定「行」腳位
const byte rowPins[4] = {13, 12, 11, 10}; // 設定「列」腳位
const int keymap[4][4] = { // 設定按鍵的「行、列」代表值
{0xc0,0xf9,0xa4,0xb0},
{0x99,0x92,0x82,0xf8},
{0x80,0x90,0xa0,0x83},
{0xa7,0xa1,0x84,0x8e}
};
byte i, j,num; // 暫存迴圈的索引數字
byte scanVal; // 暫存掃描到的按鍵值
void setup()
{
Serial.begin(9600);
for (i = 0; i <= 3; i++)
{
pinMode(rowPins[i], INPUT);
pinMode(colPins[i], OUTPUT);
digitalWrite(colPins[i], HIGH);
digitalWrite(rowPins[i], HIGH);
}
for(j=0;j<6;j++)
{
pinMode(seg7[j],OUTPUT);
}
pinMode(2,OUTPUT);
}
void outport(byte data)
{
for(num=0;num<7;num++)
{
if(data % 2==1)digitalWrite(seg7[num],HIGH);
else digitalWrite(seg7[num],LOW);
data=data/2;
}
}
void loop()
{
for (i = 0; i <= 3; i++) {
for (j = 0; j <= 3; j++) {
digitalWrite(colPins[j], LOW);
scanVal = digitalRead(rowPins[i]);
if (scanVal == LOW) { // 如果輸入值是「低電位」…
Serial.println(keymap[i][j]); // 輸出按鍵代表的字元
outport(~keymap[i][j]);
delay(200); // 掃描按鍵的間隔時間
digitalWrite(colPins[j], HIGH);
break; // 跳出迴圈
}
digitalWrite(colPins[j], HIGH);
}
}
}
沒有留言:
張貼留言