int led[]={14,15,16,17};
int seg7[]={2,3,4,5,6,7,8,9};
int dip_sw[]={13,12,11,10};
int weight[]={8,4,2,1};
int num;
int value;
char table_led[]=
{
0x0,0x1,0x2,0x3,
0x4,0x5,0x6,0x7,
0x8,0xf9,0xfa,0xb,
0xc,0xd,0xe,0xf
};
char table_7seg[]=
{
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0Xc6,0xa1,0x86,0x8E
};
//--------------------------------------------------
void setup()
{
// put your setup code here, to run once:
for(int i=0;i<4;i++)
{
pinMode(led[i],OUTPUT);
digitalWrite(led[i],LOW);
}
for(int i=0;i<8;i++)
{
pinMode(seg7[i],OUTPUT);
}
for(int i=0;i<4;i++)
{
pinMode(dip_sw[i],INPUT);
digitalWrite(dip_sw[i],HIGH);
}
}
//---------------------------------------------------------
void tx1(byte data1)
{
for(int j=0;j<8;j++)
{
if(data1%2==1)
{
digitalWrite(seg7[j],LOW);
}
else
{
digitalWrite(seg7[j],HIGH);
}
data1=data1/2;
}
}
//---------------------------------------------------------------
void tx2(byte data2)//A0~A3只有四位而已
{
for(int h=0;h<4;h++)//你寫8會錯,因為你並沒有給他8支腳位scan
{
if(data2%2==1)
{
digitalWrite(led[h],HIGH);//高電位led就亮
}
else
{
digitalWrite(led[h],LOW);
}
data2=data2/2;//16進制(電腦會自動轉十進制來算)用除2會當十進制
}
}
//--------------------------------------------------------
void loop()
{
num=0;
for(int k=0;k<4;k++)
{
value=digitalRead(dip_sw[k]);
if(value==HIGH)
num=num+ weight[k];
}
tx1(table_7seg[num]);
tx2(table_led[num]);
}
沒有留言:
張貼留言