该项目的连线图如下所示:
代码如下:
int latchPin=8;
int clockPin=12;
int dataPin=11;
void setup() {
// put your setup code here, to run once:
pinMode(latchPin,OUTPUT);
pinMode(clockPin,OUTPUT);
pinMode(dataPin,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
for(int i=0;i<256;i++)
{
digitalWrite(latchPin,LOW);
ShiftOut(i);
digitalWrite(latchPin,HIGH);
delay(500);
}
}
void ShiftOut(byte dataOut)
{
boolean pinState;
digitalWrite(dataPin,LOW);
digitalWrite(clockPin,LOW);
for(int i=0;i<=7;i++)
{
digitalWrite(clockPin,LOW);
if(dataOut & (1<<i))
{
pinState=HIGH;
}
else
{
pinState=LOW;
}
digitalWrite(dataPin,pinState);
digitalWrite(clockPin,HIGH);
}
digitalWrite(clockPin,LOW);
}
代码的效果可以到下面的网址观看: