Hey guys,
Wow, I posted this a while back and didn't think any responses were coming. Thanks to everyone who contributed!
I've managed to resolve my issues with this display and it's working nicely now.
I also have this working with an 8-bit shift register (http://www.sparkfun.com/datasheets/IC/SN74HC595.pdf also from SparkFun). So this might be of interest for you guys if you wanna use less arduino pins.
Please note: since my project involves temperature readings, this code is specifically for displaying temperature so digit 4 can be a 'C' or an 'F' and there are no decimals.
Here is the code along with pin descriptions. I welcome any suggestions/questions/comments so please feel free to post:
/*
4 Digit 7 Segment display from Sparkfun
http://www.sparkfun.com/commerce/product_info.php?products_id=9480
1: Digit 1 16: B
2: Digit 2 15: G
3: D 14: A
4: Colon Anode 13: C
5: E 12: Colon Cathode
6: Digit 3 11: F
7: Decimal Point 10: Apostrophe Anode
8: Digit 4 9: Apostrophe Cathode
8 Bit Shift Register
1: display's B 16: 5V
2: display's C 15: display's A
3: display's D 14: arduino's dataPin
4: display's E 13: Gnd
5: display's F 12: arduino's latchPin
6: display's G 11: arduino's clockPin
7: display's DP 10: 5V
8: Gnd 9: none
*************
Display's Cathode goes to ground via resistor
Display's Anode goes to digital out
Digit pins go to digital out via resistor
Segment pins (A-G) go to digital out or shift register out (0 is on)
original shift reg code:
http://arduino.cc/en/Tutorial/ShftOut13
helpful schematic:
http://www.modxhost.com/595and4021.jpg
*/
int latchPin = 8; //Pin connected to ST_CP of 74HC595 (aka RCLK)
int clockPin = 12; //Pin connected to SH_CP of 74HC595 (aka SRCLK)
int dataPin = 11; //Pin connected to DS of 74HC595 (aka SER)
int digit1Pin = 5; //can't use pin 1 since it's TX?
int digit2Pin = 2;
int digit3Pin = 3;
int digit4Pin = 4;
byte data;
byte dataArray[13];
const int MINUS_IDX = 10;
const int CELCIUS_IDX = 11;
const int FARENHEIT_IDX = 12;
void setup(){
pinMode(digit1Pin, OUTPUT);
pinMode(digit2Pin, OUTPUT);
pinMode(digit3Pin, OUTPUT);
pinMode(digit4Pin, OUTPUT);
pinMode(latchPin, OUTPUT);
Serial.begin(9600);
// A
// F B
// G
// E C
// D dp (H)
//
// In binary representation, right most digit is A
dataArray[0] = B11000000;
dataArray[1] = B11111001;
dataArray[2] = B10100100;
dataArray[3] = B10110000;
dataArray[4] = B10011001;
dataArray[5] = B10010010;
dataArray[6] = B10000010;
dataArray[7] = B11111000;
dataArray[8] = B10000000;
dataArray[9] = B10010000;
//temperature specific characters
dataArray[MINUS_IDX] = B10111111; // minus sign
dataArray[CELCIUS_IDX] = B11000110; // C
dataArray[FARENHEIT_IDX] = B10001110; // F
}
void loop(){
setTemp(-23, 'C');
//setDigit(digit1Pin, 3);
//setDigit(digit2Pin, 4);
//setDigit(digit3Pin, 5);
//setDigit(digit4Pin, 6);
}
void setTemp(int temp, char scale){
//temp must be between -99 and 999 in either scale to fit the display
//put in a check here later
boolean negative = false;
if (temp < 0)
negative = true;
temp = abs(temp);
if (scale == 'F'){
setDigit(digit4Pin, FARENHEIT_IDX);
} else if (scale == 'C'){
setDigit(digit4Pin, CELCIUS_IDX);
}
setDigit(digit3Pin, temp % 10);
temp /= 10;
if (temp >= 1){
setDigit(digit2Pin, temp % 10);
temp /= 10;
if (temp >= 1){
setDigit(digit1Pin, temp % 10);
}
}
if (negative){
setDigit(digit1Pin, MINUS_IDX);
}
}
void setDigit(int digitPin, int value){
digitalWrite(latchPin, 0);
shiftOut(dataPin, clockPin, dataArray[value]);
digitalWrite(latchPin, 1);
digitalWrite(digitPin, HIGH);
delay(1);
digitalWrite(digitPin, LOW);
}
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
// This shifts 8 bits out MSB first,
//on the rising edge of the clock,
//clock idles low
//internal function setup
int i=0;
int pinState;
pinMode(myClockPin, OUTPUT);
pinMode(myDataPin, OUTPUT);
//clear everything out just in case to
//prepare shift register for bit shifting
digitalWrite(myDataPin, 0);
digitalWrite(myClockPin, 0);
//for each bit in the byte myDataOut[ch65533]
//NOTICE THAT WE ARE COUNTING DOWN in our for loop
//This means that %00000001 or "1" will go through such
//that it will be pin Q0 that lights.
for (i=7; i>=0; i--) {
digitalWrite(myClockPin, 0);
//if the value passed to myDataOut and a bitmask result
// true then... so if we are at i=6 and our value is
// %11010100 it would the code compares it to %01000000
// and proceeds to set pinState to 1.
if ( myDataOut & (1<<i) ) {
pinState= 1;
}
else {
pinState= 0;
}
//Sets the pin to HIGH or LOW depending on pinState
digitalWrite(myDataPin, pinState);
//register shifts bits on upstroke of clock pin
digitalWrite(myClockPin, 1);
//zero the data pin after shift to prevent bleed through
digitalWrite(myDataPin, 0);
}
//stop shifting
digitalWrite(myClockPin, 0);
}