📜 ⬆️ ⬇️

Knight rider. We program smooth animation on the microcontroller

Good day.

Today I want to tell you how to make a smooth animation on the microcontroller using LEDs. The basis is taken from Atmel's attiny2313 microcircuit, which was exactly what I found. I will not go into the details of the firmware, the programmer, it was all in the middle. Here and under Linux

Most people remember the movie, and then the series “Knight Rider” . Who in childhood did not dream of such a car? Well, for example, I decided to make an animation, such as in front of the car itself.
')
image



Soldered circuit according to the following principle.
scheme

The bar with LEDs took from the TV "Electron", just there were 8 pieces. There were 3 volt LEDs, so in my version, in series with diodes, I added 200 ohm resistors.

Theory



A trivial thing, if you write a unit to the output port, then the diode will shine, if zero, it goes out. In order for it to be partially luminous (luminaries, but not at full power), it is necessary to emulate PWM on each port.

The accuracy of the formation of the pulse decided to spread by 10 iterations. That is, perhaps 10 different options for the power of the diode. If all the iterations at the output will be 1, then this is the maximum luminescence, if 5 out of 10 - to turn out half. That is, when at output 1 it will light up, but 0 goes further, and it dies away. Such a PWM signal has a few hundred kilohertz, and the inertia of the diode itself gives us the desired effect.

Implementation



I put the whole animation into the matrix, it gives us the opportunity to easily and easily program the animation itself.

const char mat[] =
{
10,0,0,0,0,0,0,0,
6,10,0,0,0,0,0,0,
2,6,10,0,0,0,0,0,
0,2,6,10,0,0,0,0,
0,0,2,6,10,0,0,0,
0,0,0,2,6,10,0,0,
0,0,0,0,2,6,10,0,
0,0,0,0,0,2,6,10,
0,0,0,0,0,0,2,6,
0,0,0,0,0,0,0,2,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,10,
0,0,0,0,0,0,10,6,
0,0,0,0,0,10,6,2,
0,0,0,0,10,6,2,0,
0,0,0,10,6,2,0,0,
0,0,10,6,2,0,0,0,
0,10,6,2,0,0,0,0,
10,6,2,0,0,0,0,0,
6,2,0,0,0,0,0,0,
2,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0
};

short timeAnim = 3000;


* This source code was highlighted with Source Code Highlighter .


And so timeAnim is the time of the whole animation. Each line is 10 iterations of one frame, where each cell is the “light power” of the diode.

It remains only to calculate the animation time for one frame (one line of the matrix), generate the result and output it to the port.

//
sPeriodTime = timeAnim * 4 / sizeof (mat) * 8;
iCycle = 0;

//
iMaxCycle = sizeof (mat) / 8;

while (1)
{
PORTB = 0;
TCNT1 = 0;
iCycle = 0;
while (iCycle < iMaxCycle)
{
while (TCNT1 < (iCycle + 1)*sPeriodTime) //,
{
for (i = 0; i < 10; i++) // 10
{
iTemp = 0;
for (j = 0; j < 8; j++) //
{
iTemp <<= 1;
iTemp |= (mat[iCycle*8 + j] - i) > 0;
}
PORTB = iTemp; //
}
};
iCycle++;
}
};

* This source code was highlighted with Source Code Highlighter .


Result







Well, who cares, the source

According to the traditions of Habr, this is my first article, do not kick much.

Successes.

Source: https://habr.com/ru/post/79481/


All Articles