Chapter 18 74HC595 & 7-segment display.
Subfunction outData (int8_t data) is used to make the 74HC595 output a 8-bit data immediately.
void outData(int8_t data){ // function used to output data for 74HC595
digitalWrite(latchPin,LOW);
shiftOut(dataPin,clockPin,MSBFIRST,data);
digitalWrite(latchPin,HIGH);
}
Subfunction display (int dec) is used to make 4-Digit 7-segment display a 4-bit integer. First open the
common end of first 7-segment display and close to the other three, at this time, it can be used as 1-Digit 7-
segment display. The first is used for displaying single digit of "dec", the second for tens digit, third for
hundreds digit and fourth for thousands digit respectively. Each digit will be displayed for a period of time
through using delay (). The time in this code is set very short, so you will see different digit is in a mess. If the
time is set long enough, you will see that every digit is display independent.
void display(int dec){ //display function for 7-segment display
selectDigit(0x01); //select the first, and display the single digit
outData(num[dec%10]);
delay(1); //display duration
selectDigit(0x02); //Select the second, and display the tens digit
outData(num[dec%100/10]);
delay(1);
selectDigit(0x04); //Select the third, and display the hundreds digit
outData(num[dec%1000/100]);
delay(1);
selectDigit(0x08); //Select the fourth, and display the thousands digit
outData(num[dec%10000/1000]);
delay(1);
}
Subfunction timer (int sig) is the timer function, wich will set a alarm signal. This function wil be ececuted once
at set intervals. Accompanied by the execution, the variable counter will be added 1, and then reset the time
of timer to 1s.
void timer(int sig){ //timer function
if(sig == SIGALRM){ //If the signal is SIGALRM, the value of counter plus 1, and
update the number displayed by 7-segment display
counter ++;
alarm(1); //set the next timer time
}
}