int DIP[] = { 10,11,12,13 };
int Weight[] = { 8, 4, 2, 1};
int seg7[] = {2,3,4,5,6,7,8,9};
char seg7Table[]=
{
B11000000, //0
B11111001, //1
B10100100, //2
B10110000, //3
B10011001, //4
B10010010, //5
B10000010, //6
B11111000, //7
B10000000, //8
B10010000, //9
B10001000, //A
B10000011, //B
B11000110, //C
B10100001, //D
B10000110, //E
B10001110 //F
};
int i,j; // 迴圈用的變數
int num;
void OutPort(byte dat)
{
for(j=0;j<8;j++)
{
if(bitRead(dat,j))
digitalWrite(seg7[j],LOW);
else
digitalWrite(seg7[j],HIGH);
}
}
void setup() {
for (i=0; i<7; i++) {
pinMode(seg7[i], OUTPUT);
}
for (i=0; i<4; i++) {
// 設定 DIP 為輸入腳位,且致能內部提昇電阻
pinMode(DIP[i], INPUT);
digitalWrite(DIP[i], HIGH);
}
}
void loop()
{
num=0;
for (i=0; i<4; i++)
{
int reading = digitalRead(DIP[i]); // 讀取指撥開關狀態
if (reading == HIGH) num = num + Weight[i];
}
OutPort(seg7Table[num]); // 取出陣列中對應的數字編碼表
}
沒有留言:
張貼留言