// 正极
int y5=6;
int y4=5;
int y3=4;
int y2=3;
int y1=2;
//负极
int ds=8;
int stcp=0;
int shcp=1;
int i = 0;
int counter = 0;
int hello[] ={B11111,B11111,B11111,B11111,B11111,
B01110,B01110,B00000,B01110,B01110,//H
B11111,B11111,
B10001,B10111,B10001,B10111,B10001,//E
B11111,B11111,
B10111,B10111,B10111,B10111,B10001,//L
B11111,B11111,
B10111,B10111,B10111,B10111,B10001,//L
B11111,B11111,
B10001,B01110,B01110,B01110,B10001,//O
B11111,B11111,B11111,B11111,B11111,};
/*int iloveu[]={B11111,B11111,B11111,B11111,B11111,
B10001,B11011,B11011,B11011,B10001,
B11111,
B10101,B00000,B00000,B10001,B11011,
B01110,B01110,B01110,B01110,B10001,
B11111,B11111,B11111,B11111,B11111};*/
//int heart[]={B10101,B00000,B00000,B10001,B11011};
//int letterI[]={B10001,B11011,B11011,B11011,B10001};
//int letterU[]={B01110,B01110,B01110,B01110,B00000};
void setup()
{
pinMode(stcp,OUTPUT);
pinMode(shcp,OUTPUT);
pinMode(ds,OUTPUT);
pinMode(y1,OUTPUT);
pinMode(y2,OUTPUT);
pinMode(y3,OUTPUT);
pinMode(y4,OUTPUT);
pinMode(y5,OUTPUT);
// Serial.begin(9600);
}
void keepchar(int dat)
{
int m;
for(m = 0; m < 8; m++)
{
digitalWrite(shcp,LOW);
digitalWrite(ds,dat & 0x80);
int val = digitalRead(ds);
digitalWrite(shcp,HIGH);
dat = dat<<1;
// Serial.print(val);
}
// Serial.print('\n');
}
void sendchar()
{
digitalWrite(stcp,LOW);
digitalWrite(stcp,HIGH);
}
void refresh_matrix()
{
static int j=0;//Serial.println("refresh");
switch(j)
{
case 0:digitalWrite(y5,1);digitalWrite(y4,0);digitalWrite(y3,0);digitalWrite(y2,0);digitalWrite(y1,0); break;
case 1:digitalWrite(y4,1);digitalWrite(y5,0);digitalWrite(y3,0);digitalWrite(y2,0);digitalWrite(y1,0); break;
case 2:digitalWrite(y3,1);digitalWrite(y5,0);digitalWrite(y4,0);digitalWrite(y2,0);digitalWrite(y1,0); break;
case 3:digitalWrite(y2,1);digitalWrite(y4,0);digitalWrite(y3,0);digitalWrite(y5,0);digitalWrite(y1,0); break;
case 4:digitalWrite(y1,1);digitalWrite(y4,0);digitalWrite(y3,0);digitalWrite(y2,0);digitalWrite(y5,0); break;
default:break;
}
//Serial mornitoring
/* Serial.print(f1);
Serial.print(f2);
Serial.print(f3);
Serial.print(f4);
Serial.println(f5); */
keepchar(hello[j+i]);
sendchar();
delay(2);
keepchar(0xff);
sendchar();
if(4==j++) j=0;
}
/*void loop()
{
counter++;
if(counter == 100)
{
counter = 0;
i++;
if(i == 39) i = 0;
}
refresh_matrix();
}*/
void displaymatrix()
{
counter++;
if(counter == 100)
{
counter = 0;
i++;//Serial.println("display");
if(i == 39) i = 0;
}
refresh_matrix();
}
void loop()
{
displaymatrix();
}
**********以上是利用595的。如果直接利用IO口的话。只需在第一个switch以后这样写:
int f1=heart[j]&B1;
int f2=(heart[j]&B10) >> 1;
int f3=(heart[j]&B100) >> 2;
int f4=(heart[j]&B1000) >> 3;
int f5=(heart[j]&B10000) >> 4;
digitalWrite(x1,f1);
digitalWrite(x2,f2);
digitalWrite(x3,f3);
digitalWrite(x4,f4);
digitalWrite(x5,f5);
delay(2);
switch(j)
{
case 0:digitalWrite(x5,1);digitalWrite(x4,1);digitalWrite(x3,1);digitalWrite(x2,1);digitalWrite(x1,1);break;
case 1:digitalWrite(x5,1);digitalWrite(x4,1);digitalWrite(x3,1);digitalWrite(x2,1);digitalWrite(x1,1);break;
case 2:digitalWrite(x5,1);digitalWrite(x4,1);digitalWrite(x3,1);digitalWrite(x2,1);digitalWrite(x1,1);break;
case 3:digitalWrite(x5,1);digitalWrite(x4,1);digitalWrite(x3,1);digitalWrite(x2,1);digitalWrite(x1,1);break;
case 4:digitalWrite(x5,1);digitalWrite(x4,1);digitalWrite(x3,1);digitalWrite(x2,1);digitalWrite(x1,1);break;
}
连线都是一样的。再次还要感谢“珜羽”的热心解答。 |