We are not looking for easy ways.

Previous, it is my first
publication caused a resonance among users of Habr. Decided not to stop. We continue to squeeze the impossible out of ATtiny13. Immediately I warn you, the solutions described are again non-standard, and someone may be outraged and cognitive dissonantly (“And what’s the point of the article? Show that elements can be connected?”). Moreover, such a decision is also really inexpedient, as I will write in more detail below. But it so happened that standard solutions have long been known, and it is not always interesting to read about them, and writing is ungrateful.
I really like this baby-ATtiny13. He has enough brains to solve many household tasks (turn on the light, ventilation,
run to the store for a beer ). And the price is just ridiculous. Here are just a few legs, and no pens at all. Therefore, you have to go for all sorts of tricks to solve the problem of lack of legs.
')
In the process of studying the programming of microcontrollers (in the Arduino environment, just do not tell anyone) I, like many, went through the stage of connecting an ultrasonic distance sensor, something like this:

Since the method of transmitting information from the sensor to the controller is simple to disgrace, the ATtiny13 does it easily. Then it was necessary to display information on a seven-segment indicator using shift registers. That is, the diagram of the display part was several times larger than the controller itself. At that moment I played and moved on.
Recently, I wondered what else overwhelming task to assign to Tinku? What she did not cope with in the examples described. The first thing I remembered about the display. Some time ago I was looking for information on a similar topic. Then I found such an
interesting option.21 segment from 5 feet controller. Great! I don’t need that much, two signs will suffice, plus a dot, a total of 15 segments. And if four legs? Then get a maximum of 13 segments, not enough. At the sight of the scheme, there immediately appeared a desire to assemble and try, although the work algorithm is not easy to make. But with a closer look, you understand that it will not work out, these seven-segmentors do not exist in nature (most likely). You can make, of course, but this is another level. Then the idea was postponed until better times.
Offtopic: Why are there no seven-segment indicators with built-in logic? Where are the developers looking? How convenient it is to install and manage two power legs and 3 (1, 2) data legs. And in fact they were even in the USSR: 490IP1, 490IP2. Inside the most mediocre indicator for 2 ... 4 digits is full of space for placing the chip on the chip, and the price of the shift register is 0.064 cu with the body. Anyway.
So I again thought about how to reduce the number of legs to work with the seven-segment indicator? The outputs of the controller can take three states (actually 4, but this is not important now). Is it possible to use it somehow? If the two states in relation to the LED can be interpreted only as shining, not shining, then with three is a little more interesting. I have not yet figured out how to use it, but such a scheme occurred to me:

If the controller output is in the zero state, the LEDs do not glow (which is obvious).
If the output is in the unit state, then the LEDs are lit, which is also understandable.
But if the output is not an output at all, but is connected to the input, then a current flowing through the circuit of two resistors and the HL1 LED creates a voltage drop of about (5-1.7) / (2.2 + 1.5) at the connection point of the resistors * 1.5 + 1.7 = 3.0 V. This is not enough for the current to flow through the VD1_R3_HL2 circuit (approximately 3.4 V is needed). VD1 is an additional LED used as a zener diode (the stabistor is more correct), so we will not consider it an LED to avoid confusion. It does not matter if the pull-up resistor is turned on inside the microcontroller, its resistance (20 kΩ) practically does not affect the situation. I didn’t come to such nominal values ​​immediately, before I tried it with a normal diode as VD1, it also works quite well with the same resistors R1 and R2. But it is better that R2 is about one and a half times more than R1. And I almost forgot the most important thing: everything described is possible only when using
red LEDs and in the indicator, and additional ones. In a pinch,
either the indicator
or additional LEDs can be applied green. And at a supply voltage of 4.5 V to 5 V.
What do we have in the end? Three states: no LED (0) shines, HL1 (1) shines, or HL1 and HL2 (2) shines. Very reminiscent of the ternary system. But we can not light HL2 without HL1, this must be remembered. But now with the help of four microcontroller legs we can control eight LEDs (I wanted to think so).
Then I tried to break the indicator segments into pairs (just like in kindergarten: boy-girl). The main condition is that in each pair one of the segments cannot shine on their own, such is discrimination. Here's what I got:

Four pairs of segments, each capital letter indicates the dominant segment that can work itself, the second can only work with it. It can be noted that the segment “a” is muddied with two at once, and in general nobody got the poor point. How it looks like life!
But these pairs can display (almost) all the numbers:
Each pair is painted with its own color. The attentive viewer noticed that something was wrong with the two. We will not focus on this attention yet. I tried several more options for grouping segments, I didn’t think of it better. Maybe someone will offer. Perhaps the neural network would have coped.
In the second stage of the experiments, I had to use an indicator with a common anode. Therefore, the final scheme was as follows:
Someone might ask: where are the 100 ohm resistors? It has long been known (and actively used) that with properly made dynamic indications, it is possible to do without current-limiting resistors. Even if from the controller output to two consecutively connected LEDs, by mistake, constantly supply voltage, the microcontroller and the LEDs normally withstand this, the current is limited by the resistance of the transition inside the MC. And more about resistors. The maximum current through HL1 in the previous scheme is about 2 mA, and through HL2 it reaches 25 ... 40 mA (presumably, later I will tell where these figures came from). This means that the light output of different segments will be different. But since the dynamic display will be used, this can be easily solved due to the different segment display times.
I conducted all experiments on Arduino Nano in the Arduino IDE environment. Excellent board for prototyping, it becomes good in the breadboard, stitched via USB without problems. Something did not work? Corrected the sketch and after a minute filled in new firmware. And when you have debugged the code, you can go to the firmware in ATtiny13, it still takes a little more gestures.
By the way, I am also flashing with Arduino and in the Arduino environment, this practically excludes the possibility of locking MK with the wrong fuses, and much easier.
Here is an example of displaying the number 4 in the code:
pinMode(f_a, INPUT);
In principle, everything should be clear even to those who are not familiar with Arduino, but understand a little in controllers. Figures 150 microseconds and 2 milliseconds are chosen experimentally for the brightness of the segments. In the final code, they need to be put into separate variables so that they can be changed during debugging. From these figures it is possible to approximately determine the order of the difference of currents through two segments in one pair. Since the G segment glows about 13 times less time than the others, and provides the same brightness, it can be assumed that the current through this segment is 13 times more than through others. In fact, the dependence of brightness on the current is non-linear, so the current can be more than 25 times, that is, 50 mA. With such a duty ratio is quite safe to exit the MK. By the way, this difference in the currents played into the hands of solving the problem of the number 2. As I wrote above, the G segment can only be lit together with the C segment. But if you apply 0 on the MK leg, responsible for C and G, for 150 μsec, and after 2 ms to hold 1 on it, then the G segment will “work out” at full brightness, and the C segment will only be slightly highlighted during the same 150 microseconds. We get almost a full two. Thus, I managed to break the rule, which I myself set. What can not be done out of despair.
So, we lit the number with four legs of the MK. Actually, I missed this stage for myself, immediately deduced two signs. To do this, disconnect the output of the common anode of one of the bits of the indicator from the power supply, and connect to another output of the MC, and the anode of the other discharge to the next output (already 6 legs). Now, in turn, put 1 on the low-order anode, display the low-order digit, then 1 at the high-order anode, and display the high-order digit, and so on in a circle. I conducted this experiment with Arduino Nano, she has enough legs. All code debugged on it, not the first time. And it all worked as it should.
Since the anodes are connected in turn, with the help of a simple refinement, you can release another conclusion of the MC. Here is the final diagram:
Total use 5 feet MK to display a two-digit number. At this stage, you can already try with baby ATtiny. What I did. But not at once. The sketch compiled in Arduino for ATtiny13 took about 1.7 KB of memory with 1 KB available. To reduce the size, I had to contact the ports directly, which I was going to do later on. By the way, on the Arduino I used the same ports that I was going to use on ATtiny, very convenient. They are already marked on the last diagram. After processing, the code has lost weight per kilobyte.
Here is the final code for ATtiny13:
The above code will allow your ATtiny13 to count from 0 to 99. It would be more correct to provide for the possibility of reassigning the legs of the MK. Programming gurus could reduce the code several times (
Where is the minimum Hello World limit on the AVR? ).
You can add the desired function to the code so that the MC displays something conscious. True, Tinky already has all her legs. There is also a reset leg, which can be used as an I / O port. But it turned out to be harder to use than I thought. Therefore, for myself I will leave “for later”. But there is an interesting feature about which not everyone knows. The ADC0 analog input is on the same leg, and it works! However, when the voltage on it is less than 1/4 of the supply voltage, the MK switches to the reset mode. But from 1/4 to the supply voltage it is quite possible to measure the voltage at the input. I took advantage of this:
Experience shows that the indicator can be reduced to as much as 21, only then the MC goes into reset mode, and begins to work when it returns to about 25 and above. So you can make a very wrong "display" to indicate voltage from 25 to 99 volts, of course, with a divider at the measuring input.
Now about the practical application. The initial idea to display data from a distance sensor was postponed until better times due to the lack of one digital input. For what else can you apply the scheme until the ideas come. Another caveat: about any economy can be no question. Even if all the segments are extinguished, a current of 2.5 mA will flow through the resistor R2 (according to the first scheme), a total of 10 mA per indicator, plus control of the transistor will add about 5 mA more. I did not mention, the transistor is almost any pnp modern.
About expediency. The cheapest version of the seven-segment output is ATtiny13 plus 74HC595. Two SMD cases will cost me approximately 0.50 USD. The simplest is ATmega8 (and everything, no resistors, nothing more), this is 0.68 cu And the variant described above is the cost of an ATtiny, 9 resistors, 4 LEDs, a transistor (all SMD) is about 0.46 cu, though the piece is more expensive at times. In addition, to collect everything in a bunch more difficult than in previous versions.
Actually, the only option that I see is if you have full ATtiny13, and for ATmega you still need to go to the store. Well, if the seven-segment indicator is the main decoration of your device, I would not advise this scheme, the display is not perfect, in some combinations the unnecessary segments are slightly highlighted. It happens that the display is needed occasionally when setting up, then the place itself.
In general, a few days I spent in vain.
In addition to criticism, I am waiting for suggestions to improve the code and simplify the scheme. Or improve functionality without complication. What interests me the most is how to ignite a point, it would expand the scope. But if I were to release one conclusion out of 5 involved, then it would be possible to roam.
I would be glad if my non-standard solution will benefit anyone, not now, so someday.
UPD: I know about Charliplexing! A third-party scheme, which I cited at the beginning of the article, this is Charliplexing. And in the same place I wrote why Charliplexing does not fit.
Please do not write comments if you read the article diagonally.