/* **********************************************************************
* 7-SegmentDisplay - v1.0
* Uses Common cathode 7-segment display,
* Uses 74HC595 shift register IC
*
*
* Description:
* This sketch illustrates the controlling a 7-segment display with
* a 74HC595 shift register.
*
* See spreadsheet file 74HC595_SegmentDisplay.ods
* Define the LED digit patterns, from 0 - 9
* 1 = LED on, 0 = LED off, common cathode, in this order:
* 74HC595 pin Q7,Q6,Q5,Q4,Q3,Q2,Q1,Q0
* Mapping to dp,g,f,e,d,c,b,a of Seven-Segment LED
*
* NOTES:
* Add 330 ohm resistors to each segment pin of 7-segment display.
* Add a NPN transistor (2N2222) to each digit line of 74HC595. The collector goes to the display,
* the base goes to the shift register and the emitter goes to ground.
* ********************************************************* */
/* Hardware Definitions */
int latchPin = 11; // connect to the ST_CP of 74HC595 (pin 12,latch pin)
int clockPin = 9; // connect to the SH_CP of 74HC595 (pin 11, clock pin)
int dataPin = 12; // connect to the DS of 74HC595 (pin 14, data pin)
#define DIGITS 2
byte sevenSegDigits[10] = { B00111111 , // = 0
B00000110 , // = 1
B01011011 , // = 2
B01001111 , // = 3
B01100110 , // = 4
B01101101 , // = 5
B01111101 , // = 6
B00000111 , // = 7
B01111111 , // = 8
B01101111 // = 9
};
byte sevenSegDP = B10000000; // = DP
int digit1, digit2, digit3, digit4;
/* Function Declaration */
void sevenSegWrite(byte digit);
void sevenSegSegment(byte digit);
void sevenSegBlank();
void showNumber(long, long);
int intLength(long);
void setup() {
Serial.begin(9600);
// Set latchPin, clockPin, dataPin as output
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
// really needed????
digitalWrite(latchPin, LOW);
digitalWrite(clockPin, LOW);
digitalWrite(dataPin, LOW);
}
void loop() {
for (int i = 0; i < 10; i++) {
int digitBits = B11111101;
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, digitBits);
shiftOut(dataPin, clockPin, MSBFIRST, sevenSegDigits[i]);
digitalWrite(latchPin, HIGH);
delay(500);
for (int j = 0; j < 10; j++) {
int digitBits = B11111011;
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, digitBits);
shiftOut(dataPin, clockPin, MSBFIRST, sevenSegDigits[j]);
digitalWrite(latchPin, HIGH);
delay(500);
}
}
/*
// This selects which digit to turn on. Common cathode uses a 0 to turn on a digit.
// Below digit 1 is turned on, all other digits are turned off.
int digitBits = B11111110;
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, digitBits);
shiftOut(dataPin, clockPin, MSBFIRST, sevenSegDigits[0]);
digitalWrite(latchPin, HIGH);
// Serial.print("Showing: ");Serial.println(digit1);
delay(1);
digitBits = B11111101;
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, digitBits);
shiftOut(dataPin, clockPin, MSBFIRST, sevenSegDigits[1]);
digitalWrite(latchPin, HIGH);
// Serial.print("Showing: ");Serial.println(digit2);
delay(1);
digitBits = B11111011;
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, digitBits);
shiftOut(dataPin, clockPin, MSBFIRST, sevenSegDigits[2]);
digitalWrite(latchPin, HIGH);
// Serial.print("Showing: ");Serial.println(digit2);
delay(1);
*/
/*
// count from 9 to 0
for (int digit = 9; digit >= 0; digit--) {
sevenSegWrite(digit);
delay(800);
}
// suspend 1 second
sevenSegBlank();
delay(800);
// count from 0 to 9
for (int digit = 0; digit <= 9; digit++) {
sevenSegWrite(digit);
delay(800);
}
// suspend 1 second
sevenSegBlank();
delay(800);
for (int i = 0; i < 6; i++) {
byte z = 0b00000000 + (1 << i);
sevenSegSegment(z);
delay(100);
}
for (int i = 0; i < 6; i++) {
byte z = 0b00000000 + (1 << i);
sevenSegSegment(z);
delay(100);
}
for (int i = 0; i < 6; i++) {
byte z = 0b00000000 + (1 << i);
sevenSegSegment(z);
delay(100);
}
for (int i = 0; i < 6; i++) {
sevenSegSegment(sevenSegDP);
delay(300);
sevenSegBlank();
delay(300);
}
// suspend 1 second
sevenSegBlank();
delay(1000);
*/
}
/* ***********************************************************
* Functions *
* ********************************************************* */
// display a alpha, binary value, or number on the digital segment display
void sevenSegWrite(byte digit) {
// digit = array pointer or binary value, as a byte
digitalWrite(latchPin, LOW); // set latchPin low, before sending data
shiftOut(dataPin, clockPin, MSBFIRST, sevenSegDigits[digit]);
digitalWrite(latchPin, HIGH); // set latchPin high, after sending data
}
void sevenSegSegment(byte digit) {
// digit = array pointer or binary value, as a byte
digitalWrite(latchPin, LOW); // set latchPin low, before sending data
shiftOut(dataPin, clockPin, MSBFIRST, digit);
digitalWrite(latchPin, HIGH); // set latchPin high, after sending data
}
void sevenSegBlank(){
digitalWrite(latchPin, LOW); // set latchPin low, before sending data
shiftOut(dataPin, clockPin, MSBFIRST, B00000000);
digitalWrite(latchPin, HIGH); // set latchPin high, after sending data
}