Chapter 17 74HC595 & LEDBar Graph 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
    if(wiringPiSetup() == -1){ //when initialize wiring failed,print messageto screen  
        printf("setup wiringPi failed !"); 
        return 1;  
    }  
    pinMode(dataPin,OUTPUT); 
    pinMode(latchPin,OUTPUT); 
    pinMode(clockPin,OUTPUT); 
    w hile(1){ 
        x= 0x01; 
        for(i=0;i<8;i++){ 
            digitalWrite(latchPin,LOW);     // Output low level to latchPin 
            _shiftOut(dataPin,clockPin,LSBFIRST,x);// Send serial data to 74HC595 
            digitalWrite(latchPin,HIGH); // Output high level to latchPin, and 74HC595 
will update the data to the parallel output port.  
            x<<=1; // make the variable move one bit to left once, then the bright LED 
move one step to the left once. 
            delay(100); 
        } 
        x= 0x80; 
        for(i=0;i<8;i++){ 
            digitalWrite(latchPin,LOW); 
            _shiftOut(dataPin,clockPin,LSBFIRST,x); 
            digitalWrite(latchPin,HIGH); 
            x>>=1; 
            delay(100); 
        } 
    }  
    r eturn 0; 
} 
In the code, we configure three pins to control the 74HC595. And define a one-byte variable to control the 
state of 8 LEDs through the 8 bits of the variable. The LED lights on when the corresponding bit is 1. If the 
variable is assigned to 0x01, that is 00000001  in binary, there will be only one LED on.   
In the “while” cycle of main function, use “for” cycle to send x to 74HC595  output pin to control the LED. In 
“for” cycle, x will be shift one bit to left in one cycle, then in the next round when data of x is sent to 74HC595, 
the LED turned on will move one bit to left once. 
        for(i=0;i<8;i++){ 
            digitalWrite(latchPin,LOW);     // Output low level to latchPin 
            _shiftOut(dataPin,clockPin,LSBFIRST,x);// Send serial data to 74HC595 
            digitalWrite(latchPin,HIGH); // Output high level to latchPin, and 74HC595 
will update the data to the parallel output port.  
            x<<=1; // make the variable move one bit to left once, then the bright LED 
move one step to the left once. 
            delay(100);