Pages: 1 2 [3]   Go Down
Author Topic: Interfacing Arduino with 74HC595 and a 7-Seg  (Read 7819 times)
0 Members and 2 Guests are viewing this topic.
Dallas, Texas
Offline Offline
God Member
*****
Karma: 0
Posts: 861
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You're welcome. Keep us posted.
Logged

Niagara on the Lake, Ontario
Offline Offline
Full Member
***
Karma: 0
Posts: 144
Arduino rocks!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Finally got 'er done! Even had it working on the first try. Oh boy, am I happy!

Code:
#include <DallasTemperature.h>

int bottomDataPin = 5;
int bottomLatchPin = 6;
int bottomClockPin = 7;
int topLatchPin = 3;
int topClockPin = 4;
int topDataPin = 2;
int j = 1;
int toShift_Top = 0;
int toShift_Bottom = 0;
char m[4];
byte data;
byte dataArray[10];

DallasTemperature tempSensor0;
DallasTemperature tempSensor1;

void setup() {
  pinMode(topLatchPin, OUTPUT);
  pinMode(bottomLatchPin, OUTPUT);
  Serial.begin(9600);
  tempSensor0.begin(11);
  tempSensor1.begin(12);

  dataArray[0] = 0x3F; // 0
  dataArray[1] = 0x06; // 1
  dataArray[2] = 0x5B; // 2
  dataArray[3] = 0x4F; // 3
  dataArray[4] = 0x66; // 4
  dataArray[5] = 0x6D; // 5
  dataArray[6] = 0x7C; // 6
  dataArray[7] = 0x07; // 7
  dataArray[8] = 0x7F; // 8
  dataArray[9] = 0x67; // 9

  // clear both displays with 0's
  // top
  digitalWrite(topLatchPin, 0);
  shiftOut(topDataPin, topClockPin, dataArray[0]);
  shiftOut(topDataPin, topClockPin, dataArray[0]);
  shiftOut(topDataPin, topClockPin, dataArray[0]);
  shiftOut(topDataPin, topClockPin, dataArray[0]);
  digitalWrite(topLatchPin, 1);
  //bottom
  digitalWrite(bottomLatchPin, 0);
  shiftOut(bottomDataPin, bottomClockPin, dataArray[0]);
  shiftOut(bottomDataPin, bottomClockPin, dataArray[0]);
  shiftOut(bottomDataPin, bottomClockPin, dataArray[0]);
  shiftOut(bottomDataPin, bottomClockPin, dataArray[0]);
  digitalWrite(bottomLatchPin, 1);
}

void loop()
{
  int k = 1000;
  toShift_Top = (tempSensor0.getTemperature() * 100);

  // the following code is by EmilyJane of arduino.cc forums
  int temp = toShift_Top;
  for (int i = 3; i >= 0; i--)
  {
    m[i] = temp / k;
    temp = temp - (m[i] * k);
    k = k / 10;
  }
  // end
  digitalWrite(topLatchPin, 0);
  shiftOut(topDataPin, topClockPin, dataArray[m[0]]);  
  shiftOut(topDataPin, topClockPin, dataArray[m[1]]);  
  shiftOut(topDataPin, topClockPin, dataArray[m[2]]);  
  shiftOut(topDataPin, topClockPin, dataArray[m[3]]);
  digitalWrite(topLatchPin, 1);
  
  k = 1000;
  toShift_Bottom = (tempSensor1.getTemperature() * 100);
  // again, the following code is by EmilyJane of arduino.cc forums
  temp = toShift_Bottom;
  for (int i = 3; i >= 0; i--)
  {
    m[i] = temp / k;
    temp = temp - (m[i] * k);
    k = k / 10;
  }
  // end
  digitalWrite(bottomLatchPin, 0);
  shiftOut(bottomDataPin, bottomClockPin, dataArray[m[0]]);  
  shiftOut(bottomDataPin, bottomClockPin, dataArray[m[1]]);  
  shiftOut(bottomDataPin, bottomClockPin, dataArray[m[2]]);  
  shiftOut(bottomDataPin, bottomClockPin, dataArray[m[3]]);
  digitalWrite(bottomLatchPin, 1);
}

// the heart of the program
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?
  //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);

}



« Last Edit: July 23, 2009, 09:02:19 pm by SpikedCola » Logged

Dallas, Texas
Offline Offline
God Member
*****
Karma: 0
Posts: 861
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Wow! Beautiful job. Congratulations!
Logged

Pages: 1 2 [3]   Go Up
Jump to: