EEVblog Electronics Community Forum

Electronics => Beginners => Topic started by: Brown on January 04, 2014, 06:24:48 am

Title: 7 Seg. and 74HC595
Post by: Brown on January 04, 2014, 06:24:48 am
 This is some Arduino code to cycle 0-9 on a 7 seg. display. Can someone explain how this works in a dumb down version, I get the first part up to pinMode(faderPin, OUTPUT); but not the rest. Thanks!





const byte ledCharSet[128] =
{
     // 00-0F: Hex digits
     B01111110, B00110000, B01101101, B01111001,   // 0123
     B00110011, B01011011, B01011111, B01110000,   // 4567
     B01111111, B01111011,   // 89

};
 
//// Pin connected to latch pin (ST_CP) of 74HC595
const int latchPin = 8;
//// Pin connected to clock pin (SH_CP) of 74HC595
const int clockPin = 12;
//// Pin connected to Data in (DS) of 74HC595
const int dataPin  = 11;
//// Pin connected to display's common annode
const int faderPin = 10;
 
/////////////////////////////////////////////////////////////////////////////////
//
void setup()
{
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin,  OUTPUT);
  pinMode(faderPin, OUTPUT);
}
 
/////////////////////////////////////////////////////////////////////////////////
//
void loop()
{
  for(int i=0; i <= 128; ++i)
  {
    byte bitsToSend = ledCharSet;
 
    // invert bitmas - we're using a common ANODE display.
    // for common cathode, comment out the following line.
    bitsToSend = bitsToSend ^ B11111111;
 
    // turn off the output so the pins don't light up
    // while you're shifting bits:
    digitalWrite(latchPin, LOW);
 
    // shift the bits out:
    shiftOut(dataPin, clockPin, LSBFIRST, bitsToSend);
 
    // turn on the output so the LEDs can light up:
    digitalWrite(latchPin, HIGH);
 
    fadeInOut();
  }
}
 
/////////////////////////////////////////////////////////////////////////////////
//
void fadeInOut()
{
  for(int i = 0; i < 255; ++i)
  {
    analogWrite(faderPin, i);
    delay(01);
  }
  for(int i = 255; i > 0; --i)
  {
    analogWrite(faderPin, i);
    delay(01);
  }
}
Title: Re: 7 Seg. and 74HC595
Post by: Rerouter on January 04, 2014, 06:57:02 am
you can treat the faderpin as contrast, its writing a PWM voltage onto that pin, and as that is the common anode it lowers or increases the voltage across the leds,

really it is a dumb idea as the arduino pins can only handle 40mA max, and it would not be hard to exceed this.

i also agree the for loop is a bit weird, and off the top of my head i am not really seeing how it could work
Title: Re: 7 Seg. and 74HC595
Post by: sachleen on January 04, 2014, 06:59:56 am
I think that's supposed to be

Code: [Select]
byte bitsToSend = ledCharSet[i];
and then the loop should only be 0 to 9, of course.
Title: Re: 7 Seg. and 74HC595
Post by: sleemanj on January 04, 2014, 07:18:34 am
NB: When pastign code you should wrap it in the code forum BB tags (see the # icon in the forum post editor), because you didn't part of your code was not visible (the array indexing).

Code: [Select]
const byte ledCharSet[128] =
{
  // 00-0F: Hex digits
  B01111110, B00110000, B01101101, B01111001, // 0123
  B00110011, B01011011, B01011111, B01110000, // 4567
  B01111111, B01111011, // 89

};

This is an array of bits to be set on the shift register output in order to display each character (note that they are inverted later on, 1's become 0's and vice-versa), the array is defined as 128 bytes, they are only using 10, the "Bxxxxxxxx" is a way to enter a byte as a binary string specifying each bit separately


Code: [Select]
void loop()
 {
   for(int i=0; i <= 128; ++i)
   {
       byte bitsToSend = ledCharSet[i];

For each of the bytes in the array already defined


Code: [Select]
     // invert bitmas - we're using a common ANODE display.
     // for common cathode, comment out the following line.
     bitsToSend = bitsToSend ^ B11111111;

Invert the bits, eg 01111110 becomes 10000001


Code: [Select]
     // turn off the output so the pins don't light up
     // while you're shifting bits:
     digitalWrite(latchPin, LOW);
 
     // shift the bits out:
     shiftOut(dataPin, clockPin, LSBFIRST, bitsToSend);
 
     // turn on the output so the LEDs can light up:
     digitalWrite(latchPin, HIGH);

Turn the outputs of the shift register off , shift the 8 bits into the register, and turn it on again
Should have read the code not the comment, they are putting latch low, shifting in data, and putting latch high.  The outputs do not switch off (but they retain the previously latched data until it's relatched).

Code: [Select]

     fadeInOut();

Fade the display in, and then out again
Title: Re: 7 Seg. and 74HC595
Post by: Brown on January 10, 2014, 07:56:08 pm
Thanks guys! Sorry for the late reply. Thanks!