Library para led de 7-Segment com Shift Register (Library for 7-Segment LED with Shift register)

(english version below)

Depois de criar a biblioteca para tratar um led de 7-Segmentos, estou disponibilizando agora uma library para controlar esses LEDs quando usando shift registers.

1. Biblioteca com o uso do Shift Register (ligação dos pinos do arduino no shift register e deste para o led)

Você pode baixar aqui: https://github.com/oleomachado/LM7SegShift.

Descompacte o arquivo e siga as instruções no arquivo Installation.txt

Um exemplo utilização (no arquivo ir em File/Examples/LM7SegShift/ para encontrar exemplos)

#include <LM7SegShift.h>

int SER_Pin = 11;      // Ligação do 74HC595 
int RCLK_Pin = 12;     // Ligação do 74HC595
int SRCLK_Pin = 13;    // Ligação do 74HC595 

int digitQty = 4;     // Quantidade de digitos do LED
int currentValue = 0;
int currentMillis = 0;

// Inicializa a library
LM7SegShift disp(SER_Pin, RCLK_Pin, SRCLK_Pin, 2); 

void setup()  { 
  Serial.begin(9600);

  //----------------------------------------------------------------------------------------
  // Define the pin in Shift Register for each one of the 7 segments
  // The last parameter if a boolean value indicating if the Led is anode or cathode
  // Definition
  //    defineSegments(segA, segB, segC, segD, segE, segF, segG, segDot, isAnode);
  disp.defineSegments(0, 1, 2, 3, 4, 5, 6, 7, 1);

  //----------------------------------------------------------------------------------------
  // Define the pin in Shift Register for each one of the digits of the 7 segment LED
  // The first parameter is the quantity of pins you LED have
  // For now up to 4 digits are allowed. Pass 0 for the ones that you dont have
  // Definition
  //    defineDigits(digits, dig1, dig2, dig3, dig4)
  disp.defineDigits(digitQty, 8, 9, 10, 11);
} 

void loop()  { 
   if (Serial.available())
      currentValue = Serial.parseInt();

  disp.valueWrite(currentValue);

  //----------------------------------------------------------------------------------------
  // For each digit 5 ms are spent to show, so if you multiply the quantity of 
  // digits for 5 you have the amount of time ellapsed in the call
  currentMillis = currentMillis + (5 * digitQty);

  if (currentMillis > 1000)
  {
    currentValue++;
    currentMillis = 0;
  }
}

Veja que agora basta instanciar a classe LM7SegShift  e usá-la para acionar o LED. Repare que na criação da instância, todos os pinos dos segmentos devem ser informados, bem como se o LED é do tipo anodo comum ou catodo comum.

Voce pode controlar LEDs do tipo Anodo Comum ou Catodo Comum, porem eu só testei o Anodo Comum pois não tenho LEDs Catodo Comum.

Posteriormente, na função Setup, deve ser definido a pinagem de cada dígito do LED de acordo com o datasheet de cada componente.

Qualquer dúvida é só entrar em contato 🙂

(ENGLISH)

Looking at the code used to control 7-Segment LEDs, I have the idea to write a library to make it easier.

I build two libraries: one to control the component with Shift Register and another to control it without  Shift register.

Now I will present the library with shift registers.

You can download the library here: https://github.com/oleomachado/LM7SegShift.

Unzip and follow instructions in the file Installation.txt

Now I will show a little example of how to use it (more examples in File/Examples/LM7SegShift/ in arduino IDE)

#include <LM7SegShift.h>

int SER_Pin = 11;      // Pins of 74HC595 
int RCLK_Pin = 12;     // Pins of 74HC595
int SRCLK_Pin = 13;    // Pins of 74HC595 

int digitQty = 4;     // How many digits in the LED
int currentValue = 0;   // Start number to show in LED
int currentMillis = 0;

// Inicializa a library
LM7SegShift disp(SER_Pin, RCLK_Pin, SRCLK_Pin, 2); 

void setup()  { 
  Serial.begin(9600);

  //----------------------------------------------------------------------------------------
  // Define the pin in Shift Register for each one of the 7 segments
  // The last parameter if a boolean value indicating if the Led is anode or cathode
  // Definition
  //    defineSegments(segA, segB, segC, segD, segE, segF, segG, segDot, isAnode);
  disp.defineSegments(0, 1, 2, 3, 4, 5, 6, 7, 1);

  //----------------------------------------------------------------------------------------
  // Define the pin in Shift Register for each one of the digits of the 7 segment LED
  // The first parameter is the quantity of pins you LED have
  // For now up to 4 digits are allowed. Pass 0 for the ones that you dont have
  // Definition
  //    defineDigits(digits, dig1, dig2, dig3, dig4)
  disp.defineDigits(digitQty, 8, 9, 10, 11);
} 

void loop()  { 
   if (Serial.available())
      currentValue = Serial.parseInt();

  disp.valueWrite(currentValue);

  //----------------------------------------------------------------------------------------
  // For each digit 5 ms are spent to show, so if you multiply the quantity of 
  // digits for 5 you have the amount of time ellapsed in the call
  currentMillis = currentMillis + (5 * digitQty);

  if (currentMillis > 1000)
  {
    currentValue++;
    currentMillis = 0;
  }
}

As you can see, now you just need to create an instance of  LM7SegShift and use it to control the led.

You can control common anode or common cathode leds here, but I just have tested it with common Anode (I dont have common cathodes).

In the Setup function, you must call defineDigits and pass each digit pin number as in datasheet.

In the example, if you want to control a 4 digit led, you just need to change  digitQty to  4 and pass the correct pins to  defineDigits.

If you have any questions, just send an email 🙂

2 thoughts on “Library para led de 7-Segment com Shift Register (Library for 7-Segment LED with Shift register)

  1. Hello!
    Great library, thank you!
    I am currently trying to use it, but I am confused as how to wire elements up.
    A sketch of wiring for the example above would be most helpful!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s