Chapter 18 74HC595 & 7-segment display.
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
int main( void)
{
int i;
if(wiringPiSetup() == -1){ //when initialize wiring failed,print messageto screen
printf("setup wiringPi failed !");
return 1;
}
pinMode(dataPin,OUTPUT); //set the pin connected to74HC595 for output mode
pinMode(latchPin,OUTPUT);
pinMode(clockPin,OUTPUT);
//set the pin connected to 7-segment display common end to output mode
for(i=0;i<4;i++){
pinMode(digitPin[i],OUTPUT);
digitalWrite(digitPin[i],HIGH);
}
signal(SIGALRM,timer); //configure the timer
alarm( 1); //set the time of timer to 1s
w hile(1){
display(counter); //display the number counter
}
r eturn 0;
}
First, define the pin of 74HC595 and 7-segment display common end, character encoding and a variable
"counter" to be displayed counter.
#define dataPin 5 //DS Pin of 74HC595(Pin14)
#define latchPin 4 //ST_CP Pin of 74HC595(Pin12)
#define clockPin 1 //CH_CP Pin of 74HC595(Pin11)
const int digitPin[]={0,2,3,12}; //Define the pin of 7-segment display common end
// character 0-9 code of common anode 7-segment display
unsigned char num[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
int counter = 0; //variable counter, the number will be displayed by 7-segment display
Subfunction selectDigit (int digit) function is used to open one of the 7-segment display and close the other
7-segment display, where the parameter digit value can be 1,2,4,8. Using "|" can open a number of 7-segment
display.
void selectDigit(int digit){
digitalWrite(digitPin[0],((digit&0x08) == 0x08) ? LOW : HIGH);
digitalWrite(digitPin[1],((digit&0x04) == 0x04) ? LOW : HIGH);
digitalWrite(digitPin[2],((digit&0x02) == 0x02) ? LOW : HIGH);
digitalWrite(digitPin[3],((digit&0x01) == 0x01) ? LOW : HIGH);
}