Author Topic: 7 Seg. and 74HC595  (Read 2671 times)

0 Members and 1 Guest are viewing this topic.

Offline BrownTopic starter

  • Contributor
  • Posts: 45
7 Seg. and 74HC595
« 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);
  }
}
 

Offline Rerouter

  • Super Contributor
  • ***
  • Posts: 4694
  • Country: au
  • Question Everything... Except This Statement
Re: 7 Seg. and 74HC595
« Reply #1 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
 

Offline sachleen

  • Contributor
  • Posts: 39
  • Country: us
    • My Site
Re: 7 Seg. and 74HC595
« Reply #2 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.
« Last Edit: January 04, 2014, 07:01:38 am by sachleen »
 

Offline sleemanj

  • Super Contributor
  • ***
  • Posts: 3024
  • Country: nz
  • Professional tightwad.
    • The electronics hobby components I sell.
Re: 7 Seg. and 74HC595
« Reply #3 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
« Last Edit: January 04, 2014, 07:22:26 am by sleemanj »
~~~
EEVBlog Members - get yourself 10% discount off all my electronic components for sale just use the Buy Direct links and use Coupon Code "eevblog" during checkout.  Shipping from New Zealand, international orders welcome :-)
 

Offline BrownTopic starter

  • Contributor
  • Posts: 45
Re: 7 Seg. and 74HC595
« Reply #4 on: January 10, 2014, 07:56:08 pm »
Thanks guys! Sorry for the late reply. Thanks!
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf