LED seven-segment indicators. Arduino and four-digit seven-segment indicator Seven-segment indicator 4-digit pinout

New articles

● Project 7: 4-digit matrix of 7-segment indicators. Making a dynamic display

In this experiment we will look at the operation of Arduino with a 4-bit seven-segment matrix. Let's get an idea of ​​dynamic display, which allows you to use the same Arduino pins when displaying information on several seven-segment indicators.

Required components:

The 4-digit matrix of seven-segment indicators consists of four seven-segment indicators and is designed to simultaneously display 4 digits on the matrix; it is also possible to display a decimal point. The circuit of a 4-bit matrix on 7-segment indicators is shown in Fig. 7.1.

Rice. 7.1. Scheme of a 4-bit matrix on 7-segment indicators

To output a number, you need to light the necessary LEDs on pins A-G and DP and select the desired matrix by applying LOW to pin 6, 8, 9 or 12.
Let's connect the matrix contacts to the Arduino board and output numbers to various bits of the matrix. To connect we need 12 Arduino pins. The connection diagram for connecting a 4-bit matrix to the Arduino board is shown in Fig. 7.2. When connecting contacts, 510 Ohm limiting resistors are used.

Rice. 7.2. Connection diagram for 4-bit matrix to Arduino

Let's write a sketch of sequential output of numbers (0-9) to an arbitrary register of the matrix. To select a random value from the range, we will use the random() function. The numbers array stores values ​​corresponding to the data for displaying digits 0-9 (the most significant bit of the byte corresponds to the label of segment A of the indicator, and the low-order one to segment G), the pins array contains the contact values ​​for segments A-G and DP, the pindigits array contains the contact values ​​for selecting a matrix digit. The contents of the sketch are shown in Listing 7.1.

// variable to store the value of the current digit int number=0 ; // seven-segment indicator int digit=0 ; void setup()( for (int i=0 ;i<8 ;i++) pinMode(pins[i],OUTPUT); for (int i=0 ;i<4 ;i++) {pinMode(pindigits[i],OUTPUT); digitalWrite(pindigits[i],HIGH); } } void loop()( number=(number+1 )%10 ; showNumber(number); // DS for (int i=0 ;i<4 ;i++) digitalWrite(pindigits[i],HIGH); digit=random(0 ,4 ); digitalWrite(pindigits,LOW); delay(3000 ); } void showNumber( int num)( for (int i=0 ;i<7 ;i++) { if (bitRead(numbers,7 -i)==HIGH) // light the segment // extinguish the segment digitalWrite(pins[i],LOW); ) )
Connection order:

1. Connect a seven-segment indicator according to the diagram in Fig. 7.3.
2. Load the sketch from Listing 7.2 onto the Arduino board.

// list of Arduino pins to connect to bits a-g // seven-segment indicator int pins=(9 ,13 ,4 ,6 ,7 ,10 ,3 ,5 ); // values ​​to display numbers 0-9 byte numbers = ( B11111100, B01100000, B11011010, B11110010, B01100110, B10110110, B10111110, B11100000, B11111110, B11110110); // variable for storing and processing the current value int number=0 ; int number1=0 ; int number2=0 ; // seven-segment indicator int pindigits=(2 ,8 ,11 ,12 ); // variable to store the current digit int digit=0 ; // to measure 100 ms unsigned long millis1=0 ; // mode 1 - stopwatch is running mode=0 ; const int BUTTON=14 ; // Pin 14(A0) for connecting the button int tekButton = LOW; // Variable to save the current state of the button int prevButton = LOW; // Variable to save the previous state// to the buttons boolean ledOn = false ; // Current state of the LED (on/off) void setup(){ // Configure the button pin as an input pinMode(BUTTON, INPUT); // Configure pins as outputs for (int i=0 ;i<8 ;i++) pinMode(pins[i],OUTPUT); for (int i=0 ;i<4 ;i++) {pinMode(pindigits[i],OUTPUT); digitalWrite(pindigits[i],HIGH); } } void loop()( tekButton = debounce(prevButton); if (prevButton == LOW && tekButton == HIGH) // if pressed... ( mode=1 -mode; // change mode if (mode==1 ) number=0 ; ) if (millis()-millis1>=100 && mode==1 ) (millis1=millis1+100 ; number=number+1 ; if (number==10000 ) number=0 ; ) number1=number; for (int i=0 ;i<4 ;i++) { number2=number1%10 ; number1=number1/10 ; showNumber(number2,i); for (int j=0 ;j<4 ;j++) digitalWrite(pindigits[j],HIGH); digitalWrite(pindigits[i],LOW); delay(1 ); } } // function for displaying numbers on a seven-segment indicator void showNumber( int num,int dig)( for (int i=0 ;i<8 ;i++) { if (bitRead(numbers,7 -i)==HIGH) // light the segment digitalWrite(pins[i],HIGH); else // extinguish the segment digitalWrite(pins[i],LOW); ) if (dig==1 ) // decimal point for second digit digitalWrite(pins,HIGH); ) // Bounce smoothing function. Accepts as // argument the previous state of the button and returns the actual one. boolean debounce ( boolean last)( boolean current = digitalRead(BUTTON); // Read the state of the button, if (last != current) // if it has changed...( d elay ( 5 ) ; // let's dem 5 m s current = digitalRead(BUTTON); // read the button state return current; // return the button state } }

3. By pressing the button we start or stop the stopwatch.

In today's article we will talk about 7-segment indicators and how to “make friends” with Arduino. There are several options. The easiest one, of course, is to go to and buy a ready-made indicator with an integrated shield (this is what the matching card is called), but we are not looking for easy ways, so we will take a slightly more difficult path. Beginners - don’t be alarmed, this article, like my previous articles ( And ) just for you. Let the gurus write for the same experienced gurus, and I am a beginner - I write for beginners.

Why a 7-segment indicator? After all, there are so many different screens, with a large number of characters, lines, various diagonals and resolutions, black and white and color, the most affordable of which cost a couple of dollars... And here: the “old” one, outrageously simple, but requiring a huge number of pins 7- segment indicator, but still this “old man” also has an advantage. The fact is that using the sketches given here you can revive not only an indicator with a digit height of 14 mm, but also more serious (albeit homemade) projects, and meter digits in this case are far from the limit. This may not be so interesting for residents of the capitals, but the population of Novokatsapetovka or Nizhnyaya Kedrovka will be very happy if a clock appears at a club or village council that can also display the date and temperature, and they will talk about the creator of this clock for a very long time. But such watches are the topic of a separate article: visitors will want - I’ll write. Everything written above can be considered an introduction. Like my last article, this article will consist of parts, this time in two. In the first part we will simply “manage” the indicator, and in the second we will try to adapt it for something at least a little useful. So let's continue:

Part one. Experimental - educational

The basis for this project is the ARDUINO UNO, which is already well known to us from previous articles. Let me remind you that the easiest way to purchase it is here: or here: , in addition, you will need a 4-digit, 7-segment indicator. I have, in particular, GNQ-5641BG-11. Why this one? Yes, simply because 5 years ago I bought it by mistake, I was too lazy to go change it, so it was lying around all this time, waiting in the wings. I think that anyone with a common anode will do (and with a common cathode it is possible, but you will have to invert the array data and other port values ​​- that is, change them to the opposite ones), as long as it is not too powerful so as not to burn the Arduino. In addition, 4 current-limiting resistors, approximately 100 Ohms each, and a piece of cable (10 cm was enough for me) for 12 pins (cores) can be “torn off” from the wider one, which is what I did. Or you can even solder them with separate wires, there will be no problems. You will also need pins for the board (11 pieces), although if you are careful you can do without them. A sketch of the indicator can be seen in Figure 1, and its diagram in Figure 2. I will also note that it is better to supply no more than 2.1V to each segment of this indicator (limited by 100-Ohm resistors), and in this case it will consume no more than 20 mA. If the number “8” lights up, the consumption will not exceed 7x20=140 mA, which is quite acceptable for Arduino outputs. An inquisitive reader will ask the question: “But 4 discharges of 140 mA each is already 4x140 = 560 mA, and this is already too much!” I will answer - there will be 140 left. How? Read on! The location of the pins on the indicator can be seen in Figure 3. And we make the connection according to Table 1.


Rice. 1 - Indicator sketch


Rice. 2 - Indicator circuit


Rice. 3 - Pin location

Table 1

Pin Arduino Uno

Indicator pin

Note

Segment G

Segment F

Segment E

Segment D

Segment C

Segment B

Segment A

The common anode of segment No. 1, connect through a 100 Ohm resistor.

The common anode of segment No. 2, connect through a 100 Ohm resistor.

The common anode of segment No. 3, connect through a 100 Ohm resistor.

The common anode of segment No. 6, connect through a 100 Ohm resistor.



We fill in a simple sketch, which is a simple “counting table” from 0 to 9:


Now for some clarification. DDRD is a register of port D (DDRB - respectively, port B) behind the “scary” word “register” there is just a “hidden” function that indicates whether the port will read something with its pin (receive information), or vice versa it will be possible to do something there then write (give information). In this case, the line DDRD=B11111111; indicates that all pins of port D are output, i.e. information will come out of them. The letter “B” means that a binary number is written to the register. An impatient reader will immediately ask: “Is decimal possible!?!” I hasten to reassure you that it is possible, but more on that a little later. If we wanted to use half of the port for input, and half for output, we could specify it like this: DDRD=B11110000; ones show those pins that will give out information, and zeros show those that will receive this information. The main convenience of the register also lies in the fact that you do not need to register all the pins 8 times, i.e. we save 7 lines in the program. Now let's look at the following line:

PORTB=B001000; // set pin 11 of port B high

PORTB is the port B data register, i.e. By writing a number into it, we indicate which pin of the port will have a one and which will have a zero. In addition to the comment, I will say that if you take the Arduino Uno in such a way that you can see the controller and the digital pins are on top, the entry into the register will be clear, i.e. which “zero” (or “one”) corresponds to which pin, i.e. the rightmost zero of port B is responsible for the 8th pin, and the leftmost one is for the 13th (which has a built-in LED). For port D, respectively, the right one is for pin 0, the left one is for pin 7.
I hope after such detailed explanations everything is clear, but since it is clear, I propose to return to the decimal number system known to us and beloved since childhood. And one more thing - a sketch of 25 lines may seem small, but for a beginner it is still somewhat cumbersome. We'll reduce it.

Let’s fill in an even simpler sketch, the same “counting table”:


Video 1.
Only 11 lines! This is our way, “the newbie way”! Please note that instead of binary numbers, decimal numbers are written in the registers. Naturally, for decimal numbers no letters are needed in front. I think it wouldn’t hurt to put all the numbers into tables.

Table 2. Correspondence of the displayed character to the port data

Common anode

Common cathode

Binary system

Decimal system

Binary system

Decimal system

Table 3. Correspondence of the displayed digit to the port data

Common anode

Common cathode

Binary system

Decimal system

Binary system

Decimal system



Attention! The data in tables 2 and 3 is valid only when wired according to table 1.
Now let’s upload a sketch with a “counting table” from 0 to 9999:




Rice. 4 - Counting table

You can see the sketch in action atVideo 2.

There are more comments in this sketch than there is code itself. There shouldn't be any questions... Besides one thing, what kind of “flicker cycle” is this, what, in fact, flickers there and why? And there is also some kind of variable for this...
And the whole point is that the segments of the same name of all four categories are connected at one point. A1, A2, A3 and A4 have a common cathode; A1, B1,…..G1 common anode. So, by simultaneously applying “1234” to the 4-digit indicator, we will get “8888” and will be very surprised at this. To prevent this from happening, you must first light “1” in your category, then turn it off, light “2” in yours, etc. If you do this very quickly, the flickering of the numbers will merge, like frames on a film, and the eye will practically not notice it. And the maximum value of the flickering variable in this case controls the speed of changing numbers on the indicator. By the way, it is thanks to this “flickering” that the maximum current consumption is only 140 mA, instead of 560. Now I suggest moving on to something more useful.

Part two. At least a little useful

In this part, we will output characters from a personal computer to a 7-segment indicator using ARDUINO MEGA. Why did the idea of ​​“switching horses at the crossing” suddenly arise? There are two reasons: first, I had never considered ARDUINO MEGA in my articles before; and secondly, in ARDUINO UNO I still haven’t figured out how I can dynamically swap the COM port and port D. But I’m a newbie - I can be forgiven. Naturally, you can purchase this controller here: . To implement the plan, I had to take a soldering iron and re-solder the cable from the Arduino side, and also write a new sketch. You can see how the cable is soldered in Figure 5. The thing is that ARDUINO MEGA and ARDUINO UNO have different port pinouts, and Mega has many more ports. The correspondence of the pins used can be seen from Table 4.



Rice. 5 - New cable wiring

Table 4

Port Mega


Attention! This table is valid only for this project!

You should also note that port C of the Arduino Mega “starts” from pin 37 and then in descending order, and port A starts from pin 22 and then in ascending order.



Rice. 6 - General view



Small implementation features: we will output 4 characters. The characters must be numbers. If you entered “1234” and we will see “1234”, if you entered “123456” we will still see “1234”, if you entered “ytsuk”, “fyva1234”, “otiog485909oapom” - we will not see anything. If you entered “pp2345mm” we will see “23” i.e. small, built-in “foolproofing”.

The sketch itself:



You can see how this program works atVideo 3.



Review prepared by Pavel Sergeev

Seven-segment LED indicators are very popular among digital value display devices and are used in the front panels of microwave ovens, washing machines, digital clocks, counters, timers, etc. Compared with LCD indicators, LED indicator segments glow brightly and are visible over long distances and in wide viewing angle. To connect a seven-segment 4-bit indicator to a microcontroller, at least 12 I/O lines will be required. Therefore, it is almost impossible to use these indicators with microcontrollers with a small number of pins, for example, series from the company. Of course, you can use different multiplexing methods (a description of which can be found on the website in the “Schemes” section), but even in this case there are certain limitations for each method, and they often use complex software algorithms.

We will look at the method of connecting an indicator via the SPI interface, which will require only 3 I/O lines of the microcontroller. At the same time, control of all indicator segments will remain.

To connect a 4-bit indicator to a microcontroller via the SPI bus, a specialized driver chip produced by the company is used. The microcircuit is capable of driving eight seven-segment indicators with a common cathode and includes a BCD decoder, segment drivers, a multiplexing circuit and static RAM for storing digit values.

The current through the indicator segments is set using only one external resistor. Additionally, the chip supports control of indicator brightness (16 brightness levels) using built-in PWM.

The circuit discussed in the article is a display module circuit with an SPI interface that can be used in amateur radio designs. And we are more interested not in the circuit itself, but in working with the microcircuit via the SPI interface. The +5 V module power is supplied to the Vcc pin, the MOSI, CLK and CS signal lines are intended for communication between the master device (microcontroller) and the slave (MAX7219 chip).

The microcircuit is used in a standard connection; the only external components needed are a resistor that sets the current through the segments, a protective diode for the power supply, and a filter capacitor for the power supply.

Data is transferred to the chip in 16-bit packets (two bytes), which are placed in the built-in 16-bit shift register on each rising edge of the CLK signal. We denote a 16-bit packet as D0-D15, where bits D0-D7 contain data, D8-D11 contain the register address, bits D12-D15 have no meaning. Bit D15 is the most significant bit and is the first bit received. Although the chip is capable of controlling eight indicators, we will consider working with only four. They are controlled by the outputs DIG0 - DIG3, located in sequence from right to left, the 4-bit addresses (D8-D11) that correspond to them are 0x01, 0x02, 0x03 and 0x04 (hexadecimal format). The digit register is implemented using on-chip RAM with an 8x8 organization and is directly addressable so that each individual digit on the display can be updated at any time. The following table shows the addressable digits and control registers of the MAX7219 chip.

Register

Address

HEX value

No operation

Decoding Mode

Number of indicators

Shutdown

Indicator test

Control registers

The MAX1792 chip has 5 control registers: decoding mode (Decode-Mode), indicator brightness control (Intensity), register of the number of connected indicators (Scan Limit), on/off control (Shutdown), test mode (Display Test).

Turning the chip on and off

When power is applied to the chip, all registers are reset and it goes into Shutdown mode. In this mode the display is turned off. To switch to normal operation mode, bit D0 of the Shutdown register (address 0Сh) must be set. This bit can be cleared at any time to force the driver to turn off, leaving the contents of all registers unchanged. This mode can be used to save energy or in alarm mode by flashing the indicator (sequential activation and deactivation of the Shutdown mode).

The microcircuit is switched to Shutdown mode by sequentially transmitting the address (0Сh) and data (00h), and transferring 0Ch (address) and then 01h (data) returns to normal operation.

Decoding Mode

Using the decoding mode selection register (address 09h), you can use BCD code B decoding (display characters 0-9, E, H, L, P, -) or without decoding for each digit. Each bit in the register corresponds to one digit, setting a logical one corresponds to turning on the decoder for this bit, setting 0 means the decoder is disabled. If a BCD decoder is used, then only the lowest nibble of data in the digit registers (D3-D0) is taken into account, bits D4-D6 are ignored, bit D7 does not depend on the BCD decoder and is responsible for turning on the decimal point on the indicator if D7 = 1. For example, when bytes 02h and 05h are sent in sequence, the DIG1 indicator (second digit from the right) will display the number 5. Similarly, when sending 01h and 89h, the DIG0 indicator will display the number 9 with the decimal point included. The table below provides a complete list of characters displayed when using the IC's BCD decoder.

Symbol

Data in registers

Enabled segments = 1

Empty

*The decimal point is set by bit D7=1

When the BCD decoder is excluded from operation, data bits D7-D0 correspond to the segment lines (A-G and DP) of the indicator.

Indicator brightness control

The chip allows you to programmatically control the brightness of the indicators using the built-in PWM. The PWM output is controlled by the low-order nibble (D3-D0) of the Intensity register (address 0Ah), which allows you to set one of 16 brightness levels. When all bits of a nibble are set to 1, the maximum brightness of the indicator is selected.

Number of connected indicators

The Scan-Limit register (address 0Bh) sets the value of the number of bits serviced by the microcircuit (1 ... 8). For our 4-bit version, the value 03h should be written to the register.

Indicator test

The register responsible for this mode is located at address 0Fh. By setting the D0 bit in the register, the user turns on all indicator segments, while the contents of the control and data registers do not change. To disable Display-Test mode, bit D0 must be 0.

Interface with microcontroller

The indicator module can be connected to any microcontroller that has three free I/O lines. If the microcontroller has a built-in SPI hardware module, then the indicator module can be connected as a slave device on the bus. In this case, the SPI signal lines SDO (serial data out), SCLK (serial clock) and SS (slave select) of the microcontroller can be directly connected to the MOSI, CLK and CS pins of the MAX7219 chip (module), the CS signal is active low.

If the microcontroller does not have hardware SPI, the interface can be organized in software. Communication with the MAX7219 begins by pulling and holding the CS line low, then sending 16 bits of data sequentially (MSB first) on the MOSI line on the rising edge of the CLK signal. Upon completion of the transmission, the CS line goes high again.

In the downloads section, users can download the source text of the test program and the HEX file of the firmware, which implements a conventional 4-bit counter with display of values ​​on an indicator module with an SPI interface. The microcontroller used is an interface implemented in software, the signal lines CS, MOSI and CLK of the indicator module are connected to ports GP0, GP1 and GP2, respectively. The mikroC compiler for PIC microcontrollers is used (), but the code can be modified for other high-level compilers. The microcontroller operates at a clock frequency of 4 MHz from the built-in RC oscillator, the MCLR output is disabled.

This module can also be connected to the Arduino platform. To work with it, you will need the LedControl library, available for download on the Arduino website.

Downloads

Source code of the test program and HEX file for flashing the microcontroller firmware -

  • THIS IS UNFAIR!!! It would be more correct to name the topic “connecting an LED matrix with an intelligent controller via a low-wire interface.” You can build such a little town yourself - put on the indicator something from PIC12-PIC16 with the appropriate protocol (microLAN, SPI, I2C, rs232 or something else homemade - just synchronous USART mode). Now there are a sufficient number of different families of MKs - it’s time to move on to working with circuits made up of several MKs, each of which performs its own task, and not try to load everything “on one head”.
  • This is an article for the damned bourgeoisie! Chips from Maxim are too expensive. There is a much simpler solution - serial-parallel shift registers. True, you will need more wires - switch the common terminals of the indicators. Or, as a colleague correctly noted, use two MKs. It's still cheaper than Max chips... Z.Y. It is possible, however, to create a universal circuit using two registers. Then you can get by with four wires: clock, data, record and sign/place. The number of acquaintances will be limited only by the bit capacity of the registers.
  • I started with shift registers myself. But then he refused. The reason is simple. Significant CPU time is required for the display. Suitable for simple programs. As software volume increases, brightness decreases. The indicator current also cannot be increased to values ​​exceeding the constant current of the segment. The program may hang. A separate processor is also not an option. Processor resistors and board dimensions and wiring will cost 2/3 of the cost of the MAX7219 on the board. I mean 8-digit display. I repeatedly left Terraelectronics with a bunch of something clutched in my hand. And why did you give 6000-10000 wooden ones for? And when you then hand over the device to the customer, you remember and think how many problems it saved me from. And they are worth it. Over time you will change your point of view.
  • Let me disagree;) The minimum set for an indicator is 4 positions * 8 segments: pic16f628a or attiny2313 (eats much more) in the “raster” scanning mode, the brightness is really not too high, but there is a minimum of details. In most solutions with a fairly significant segment current and increased (+11 - +27 Volts unstabilized DC) voltage, only the “upper” switches create a problem (regardless of what we feed with +U - the segment or the anode of the indicator matrix). Standard set: pic16f628a/attiny2313/, pic16f676 uln2803 tpic6b595 (hc595hc595 + uln2803) and a set of npn transistors (according to the emitter switch circuit) as “upper” switches as active current sources - standard solution on LM317L (LM317T). With a known and stable level of voltage supplying the anodes, the calculation of the base resistors of the upper keys is quite simple. Some problems arise when powered by an increased, unstabilized constant voltage...:mad: A solution is possible with the help of specialized microcircuits - but they are quite expensive, so a “trick” was invented, the speed of which is quite sufficient, and the details are very popular - a set of several resistors , 4N33 and a powerful n-p-n transistor (see diagram at http://radiokot.ru/forum/download/file.php?id=93485):)
  • In some cases, shift registers are justified. Well, I don’t take on cheap designs. If, for example, I make an electronic theodolite for 80. That's a four-line LCD for 1 thousand rubles. need to buy. It's just a shame to waste time on display. Solder the keys, waste processor time - it is the most expensive. And the customer is usually picky. The brightness should be normal. Yes, you forgot to calculate the cost of a set of parts and do not forget to include the difference in the cost of the printed circuit board (it will be higher) and installation time. And one more thing. This is the specifics of the work. For example, PIC hung. It is possible to understand the reason. You can see the latest data before the failure. Here is a recent example of a very rare and incomprehensible glitch in the program for 3 months. I didn't know where to look. Here the worker’s fingers were also crushed. And when I saw the last data before the failure, I understood the reason.
  • The difference between professional equipment and amateur homemade products has always been, is and will be - the developed circuit was “coolly” handed over to the Chinese, and they will generally build it on a “droplet” basis :) A LED primitive is not a competitor to a monoblock on an LCD (though with rare exceptions). But for an example of a typical application of MK in removable indicators, you don’t need to look far - you should pay attention to the solution from the so-called fiscal registrars (client display) - there, one device can use any option (luminescent / LCD / LED) - as long as the communication protocol was supported and the client liked it (I’m ready to shell out money for it)... What about the start of development on the principle “the client can pay more”... so the one who has a lot of money buys something ready-made from companies, and turns to a home-made worker either “out of poverty ", or complete rednecks who know how to find any excuse for subsequent scams...:mad: For myself, whatever is currently available will do (and not always the freshest - I haven’t seen a Ramtron watch on sale for 12 years now :) ). Besides everything else, practically the majority of “specialized” LSIs are created on the basis of the same MKs with a mask program. ;)

Let's connect a seven-segment LED indicator to the Arduino board and learn how to control it using the Led4Digits.h library.

The previous lesson described in detail microcontrollers. Let's connect such an indicator to the Arduino board.

The diagram for connecting the indicator to the Arduino board looks like this.

I assembled it on a circuit board.

To manage indicators, I wrote the Led4Digits.h library:

And pay.

The library allows you to manage seven-segment indicators:

  • up to four digits in size;
  • with any variants of control pulse polarities (all);
  • works in a parallel process;
  • allows you to display on the indicator:
    • segments of each category;
    • the digit of each digit;
    • integer 0 ... 9999;
  • to output an integer, the number of digits can be specified;
  • There is a mode for suppressing insignificant digits.

You can download the Led4Digits.h library from this link:

And pay. Only 25 rub. per month for access to all site resources!

How to install is written in .

I will not provide the source texts. You can look them up in the library files. As always, there are plenty of comments there. I will describe in detail, with examples, how to use the library.

LED control library for Arduino Led4Digits.

Here is the class description. I only provided public methods and properties.

class Led4Digits (
public:
byte digit; // bit segment control codes
void regen(); // regeneration, the method must be called regularly
void tetradToSegCod(byte dig, byte tetrad); // converting tetrad to segment codes
boolean print(unsigned int value, byte digitNum, byte blank); // integer output



} ;

Constructor.

Led4Digits (byte typeLed, byte digitPin0, byte digitPin1, byte digitPin2, byte digitPin3,
byte segPinA, byte segPinB, byte segPinC, byte segPinD,
byte segPinE, byte segPinF, byte segPinG, byte segPinH);

typeLed Sets control pulse polarities for bit and segment selection signals. Supports any connection schemes ().

typeLed Selection of category Segment selection Circuit type
0 -_- -_- Common anode with discharge selection keys
1 _-_ -_- Common anode
2 -_- _-_ Common cathode
3 _-_ _-_ Common cathode with discharge selection keys

digitPin0...digitPin3– outputs for selecting digits. If digitPin = 255, then the digit is disabled. This allows you to connect indicators with fewer digits. digitPin0 – low (right) digit.

segPinA...segPinH– segment control outputs.

For example,

means: indicator type 1; discharge outputs 5,4,3,2; outputs of segments 6,7,8,9,10,11,12,13.

void regen() method

The method must be called regularly in a parallel process. It regenerates the image on the indicators. The regeneration cycle time is equal to the method call period multiplied by the number of bits.

For example,

// interrupt handler 2 ms
void timerInterrupt() (
disp.regen(); // indicator regeneration
}

Byte digit array

Contains the state of the segments. digit is the least significant bit, the least significant bit of digit is the “A” segment of the least significant bit. A bit status of 1 means the segment is lit.

For example,

digit = B0000101;

means that in the second digit, segments “A” and “C” are lit.

An example of a program that sequentially lights all segments of each digit.

// running segments
#include
#include

//
Led4Digits disp(1, 5,4,3,2, 6,7,8,9,10,11,12,13);

void setup() (
timer interrupt 2 ms
MsTimer2::start(); // interrupt enable
}

void loop() (
for (int i = 0; i< 32; i++) {
if (i == 0) disp.digit= 1;
else if (i == 8) disp.digit= 1;
else if (i == 16) disp.digit= 1;
else if (i == 24) disp.digit= 1;
else(
disp.digit = disp.digit<< 1;
disp.digit = disp.digit<< 1;
disp.digit = disp.digit<< 1;
disp.digit = disp.digit<< 1;
}
delay(250);
}
}

//interrupt handler 2 ms
void timerInterrupt() (
disp.regen(); // indicator regeneration
}

In the digit array, 1 is shifted and the indicators display this.

Method void tetradToSegCod(byte dig, byte tetrad)

The method allows you to display numbers and letters of hexadecimal code in individual digits. Has arguments:

  • dig – digit number 0 ... 3;
  • tetrad – decimal character code. Code 0 will display the number “0”, code 1 - the number “1”, code 14 - the letter “E”.

For example,

tetrad(2, 7);

will display the number “7” in the third digit.

An example of a program that changes characters in each digit in turn.

// numbers one by one
#include
#include

// indicator type 1; discharge outputs 5,4,3,2; segment outputs 6,7,8,9,10,11,12,13
Led4Digits disp(1, 5,4,3,2, 6,7,8,9,10,11,12,13);

void setup() (
MsTimer2::set(2, timerInterrupt); // timer interrupt 2 ms
MsTimer2::start(); // interrupt enable
}

void loop() (
for (int i = 0; i< 64; i++) {
disp.tetradToSegCod(i>>4, i);
delay(250);
}
}

// interrupt handler 2 ms
void timerInterrupt() (
disp.regen(); // indicator regeneration
}

Method boolean print(unsigned int value, byte digitNum, byte blank)

The method displays an integer on the indicators. It converts the binary number into BCD for each digit. Has arguments:

  • value – the number that is displayed on the indicator.
  • digitNum – number of digits for the number. This should not be confused with the number of indicator digits. You may want to display a number on 2 digits, and display characters on the other two using digit.
  • blank – a sign of suppression of insignificant digits. blank=0 means the number should be displayed with all zeros. The number "7" will look like "0007". If blank is different from 0, insignificant zeros will be suppressed.

If the number value exceeds the allowed number for the selected number of digits (digitNum), then the function will display “---” on the indicator and return false.

An example of a number output program.

// output number
#include
#include

// indicator type 1; discharge outputs 5,4,3,2; segment outputs 6,7,8,9,10,11,12,13
Led4Digits disp(1, 5,4,3,2, 6,7,8,9,10,11,12,13);

void setup() (
MsTimer2::set(2, timerInterrupt); // timer interrupt 2 ms
MsTimer2::start(); // interrupt enable
}

void loop() (
for (int i = 0; i< 12000; i++) {
disp.print(i, 4, 1);
delay(50);
}
}

// interrupt handler 2 ms
void timerInterrupt() (
disp.regen(); // indicator regeneration
}

The last two methods do not change the state of the “H” segment – ​​the decimal point. To change the state of a point, you can use the commands:

digit |= 0x80; // light the decimal point
digit &= 0x7f; // extinguish the decimal point

Output to indicators of negative numbers (int).

Negative numbers can be output as follows:

  • Check the sign of the number.
  • If the number is negative, then print a minus sign at the most significant digit and change the sign of the number to positive in the print() function.
  • If the number is positive, then turn off the sign bit and print the number using the print() function.

Here is a program that demonstrates this method. It outputs numbers from -999 to 999.

// output negative numbers
#include
#include

// indicator type 1; discharge outputs 5,4,3,2; segment outputs 6,7,8,9,10,11,12,13
Led4Digits disp(1, 5,4,3,2, 6,7,8,9,10,11,12,13);

void setup() (
MsTimer2::set(2, timerInterrupt); // timer interrupt 2 ms
MsTimer2::start(); // interrupt enable
}

void loop() (

for (int i = -999; i< 1000; i++) {

if (i< 0) {
// the number is negative
disp.digit= B01000000; // sign -
disp.print(i * -1, 3, 1);
}
else(
disp.digit= B00000000; // clear the sign
disp.print(i, 3, 1);
}

delay(50);
}
}

// interrupt handler 2 ms
void timerInterrupt() (
disp.regen(); // indicator regeneration
}

Output to indicators of fractional numbers, float format.

There are many ways to display floating point numbers (floats) using standard C language functions. This is, first of all, the sprint() function. It works very slowly, requires additional conversions of character codes to binary decimal codes, you need to extract a point from a string. Same problems with other functions.

I use a different method for displaying the values ​​of float variables on indicators. The method is simple, reliable, fast. Reduces to the following operations:

  • The floating point number is multiplied by 10 to the power corresponding to the required number of decimal places. If you need to display 1 decimal place on indicators, multiply by 10, if 2, then multiply by 100, 3 decimal places by 1000.
  • Next, the floating point number is explicitly converted to an integer (int) and displayed on the indicators using the print() function.
  • A dot is placed in the required digit.

For example, the following lines will output a float variable with two decimal places to the seven-segment LEDs.

float x = 2.12345;

disp.digit |= 0x80; //

We multiply the number by 100, and by placing a dot in the third digit, we divide the result by 100.

Here is a program that displays floating point numbers from 0.00 to 99.99 on the indicators.

// floating point output
#include
#include

// indicator type 1; discharge outputs 5,4,3,2; segment outputs 6,7,8,9,10,11,12,13
Led4Digits disp(1, 5,4,3,2, 6,7,8,9,10,11,12,13);

void setup() (
MsTimer2::set(2, timerInterrupt); // timer interrupt 2 ms
MsTimer2::start(); // interrupt enable
}

void loop() (
float x = 0;

for (int i = 0; i< 10000; i++) {
x += 0.01;

disp.print((int)(x * 100.), 4, 1);
disp.digit |= 0x80; // light up the third level point

delay(50);
}
}

//interrupt handler 2 ms
void timerInterrupt() (
disp.regen(); // indicator regeneration
}

As you can see, the Led4Digits.h library greatly simplifies working with seven-segment light-emitting diode (LED) indicators connected to the Arduino board. I have not found an analogue of such a library.

There are libraries for working with LED displays via a shift register. Someone wrote to me that they found a library that works with an LED display directly connected to the Arduino board. But when using it, the indicator digits glow unevenly and wink.

Unlike its analogues, the Led4Digits.h library:

  • Runs as a parallel process. In the main loop, the program loads data into certain variables, which are automatically displayed on the display. Information output and indicator regeneration occur in a timer interrupt, invisible to the main program.
  • The display numbers glow evenly, without blinking. This property is ensured by the fact that regeneration occurs in a cycle strictly defined by a timer interrupt.
  • The library has compact code, executes quickly, and minimally loads the controller.

In the next lesson, we will connect an LED indicator and a button matrix to the Arduino board at the same time. Let's write a library for such a design.

Category: . You can bookmark it.

This article continues the series of my publications about the organization of dynamic indication on PIC microcontrollers and LED indicators. Here are links to previous posts:

Table of operation of the proposed algorithm (an indicator with a common cathode is used, the first column shows the register outputs combined with the indicator digits) according to the connection diagram given below.

In each of the interrupts with an interval of 2 ms (in this case from the TMR0 timer), one stage of dynamic indication (DI) is prepared according to an algorithm that consists of five phases of register and indicator control.

2nd phase: The positive edge at pin 12 of the register (ST_CP) writes the zero state of the register to the output latch. Here and further, before the start of the indication, the indicator is extinguished by zero potential on the segments.

3rd phase: by controlling register pins 14 (DS - data) and 11 (SH_CP - clock), the code for controlling the segments is written into it.

4th phase: with a positive drop at pin 12 of the register, data from the register is written to the output latch, and, due to the positive levels on the bits, the indicator remains off.

5th phase: here the required code is supplied to the outputs of the indicator digits, and then the actual indication occurs.

If the circuit uses one 4-digit indicator, then for proper operation it must be set to OK. If you need to control 8 bits, then 8 ports of the MK are used, while the remaining 4 ports simply control the bits (in phase 4 they should have a high level). It is worth noting that in this case it is possible to use indicators with both OK and OA, connecting segments or digits to the register, respectively (for the reasons stated below, in the first case it is preferable to organize DI segment-by-segment, and in the second - bit-by-bit).

Using this method, you can connect two four-bit indicators to the PIC16F676 MCU using one shift register, while leaving as many as four free ports for use. For example, for such a connection, people used the combination of DI and analog input functions in some MK ports (in my opinion, an extremely dubious decision), which led to a significant complication of the circuit and to some limitations, which the authors warn about. Using my connection diagram, everything would be solved simply and beautifully - separate inputs, separate indications, plus two more ports (including MCLR) for buttons.

To test this control method, the following simple circuit is proposed on the PIC12F629 MCU and the FYQ3641A indicator, which alternately displays the word “test” and the number 1234 on the indicator.

Here it was decided to use a segment-by-segment DI (one segment is turned on at each moment, and there is a code on the bit pins, where in each bit: 0 - if this segment should be lit in a given bit and 1 - otherwise), in which peak currents are transferred to the register . Why? There are two reasons for this: the first is the maximum load capacity of the 74HC595 outputs 35 mA versus 25 mA for PIC controllers; the second and main thing is that a current close to the limit through the output port of the MK can theoretically raise its output potential to the level of switching the register inputs, which would lead to errors in operation. And so, currents of 6-7 mA flow into the MK ports and the potentials at the outputs certainly do not exceed TTL levels.

As mentioned above, the interruption interval is 2 ms, which corresponds to the indicator refresh rate of 64 Hz and its glow is quite comfortable for the eye.

This DI method, among other things, made it possible to halve the number of current-limiting resistors (R2-R5).

The device is assembled on a so-called “solderless” breadboard.

The indicator can be replaced with any of the 3641A series.

The circuit is powered by a stabilized 5 V source. I used a special stabilizer board designed for use with the breadboard mentioned above.

The MK control program is written in C language and translated in the environment.

Code in MikroC, project, HEX file in the application.

To use this connection method in commercial developments, please contact me.

List of radioelements

Designation Type Denomination Quantity NoteShopMy notepad
DD1 MK PIC 8-bit

PIC12F629

1 To notepad
DD2 Register74HC5951 To notepad
H.L. IndicatorFYQ36411 To notepad
R1 Resistor

30 kOhm

1 To notepad
R2 Resistor

430 Ohm

1 To notepad
R3 Resistor

430 Ohm

1
If you find an error, please select a piece of text and press Ctrl+Enter.