I recently wrote some code to drive a 4 digit seven segment display using the arduino and 74HC595 shift register chip. The display is a seven segment common anode display with decimal dots. The pin configuration can be found here. I have connected a DS18S20 temparature sensor to read the temparature and display the reading on the seven segment display.
Here is how it looks:

Here is the arduino sketch:
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress insideThermometer;
//]\const int ledPin = 13;// LED connected to digital pin 13
const int latchPin = 5;//Pin connected to ST_CP of 74HC595
const int clockPin = 6;//Pin connected to SH_CP of 74HC595
const int dataPin = 7;//Pin connected to DS of 74HC595
const int digitPins[4] = {
8,9,10,11}; //pins to control the 4 common anode pins of the display
const byte digit[10] = //seven segment digit bits
{
B00111111, //0
B00000110, //1
B01011011, //2
B01001111, //3
B01100110, //4
B01001111, //5
B01111101, //6
B00000111, //7
B01111111, //8
B01101111 //9
};
int digitBuffer[4] = {
0};
int digitScan = 0;
int soft_scaler = 0;
float tempC, tempF;
int tmp;
void setup() {
TCCR2A = 0;
TCCR2B = (1<//pinMode(ledPin, OUTPUT);
for(int i=0;i<4;i++)
{
pinMode(digitPins[i],OUTPUT);
}
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
sensors.begin();
sensors.getAddress(insideThermometer, 0);
}
ISR(TIMER2_OVF_vect) {
soft_scaler++;
if(soft_scaler==15)
{
refreshDisplay();
soft_scaler = 0;
}
};
void refreshDisplay()
{
for(byte k=0;k<4;k++)
{
digitalWrite(digitPins[k], LOW);
}
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, B11111111);
digitalWrite(latchPin, HIGH);
delayMicroseconds(400);
digitalWrite(digitPins[digitScan], HIGH);
digitalWrite(latchPin, LOW);
if(digitScan==2)
{
shiftOut(dataPin, clockPin, MSBFIRST, ~(digit[digitBuffer[digitScan]] | B10000000)); //inserting the dot
}
else
{
shiftOut(dataPin, clockPin, MSBFIRST, ~digit[digitBuffer[digitScan]]);
}
digitalWrite(latchPin, HIGH);
digitScan++;
if(digitScan>3) digitScan=0;
}
void loop()
{
//digitalWrite(ledPin, HIGH);
sensors.requestTemperatures();
tempC = sensors.getTempC(insideThermometer);
tempF = DallasTemperature::toFahrenheit(tempC);
tmp = int(tempF*100);
digitBuffer[3] = tmp/1000;
digitBuffer[2] = (tmp%1000)/100;
digitBuffer[1] = (tmp%100)/10;
digitBuffer[0] = (tmp%100)%10;
//digitalWrite(ledPin, LOW);
delay(20);
}
Download the sketch from the link below:
SevenSegmentTest.pde (2.27 kb)
If you have any questions, please feel free to ask me via the contact link.