Greetings to you, colleagues, comrades accompanying and just interested! The other day, I made a device that can be used as a basis for an original gift, a souvenir, or just made for my own pleasure, if it gives you a fuss with modern microelectronics. Immediately video demonstration of the device: Unfortunately, the camera can not convey the shades and modulations of the glow of the RGB-LED, it looks much nicer live. However, if you are nevertheless interested, then there are a lot of letters and pictures further. The prerequisites for the creation of the device were an irresistible craving for everything and everyone filling in microcontrollers, an article by Comrade Ariman ( We are creating an original gift with the help of chemistry, physics and electronics ) - for which he once again has a separate respect and respect, and a desire to do something interesting with his own hands. First, I bring to your attention a quick assembly manual, and then, for the curious, the details of the internal structure and performance characteristics. So, we will need: 1. Atmel Attiny13 microcontroller - 1 pc. 2. RGB LED - 1 pc. 3. Development board type CRS 044 - 1 pc. 4. Resistor 1 MΩ, smd - 1 pc. 5. Copper wire (lived from a twisted pair) - 30 cm. 6. CR2032 battery - 1 pc. 7. Clip - 1 pc. It also assumes the presence of a soldering iron, a simple tool for working with electronics and straight arms. First of all, we cut off a piece of about 3x3 cm from the breadboard. The size is determined by the dimensions of the battery, which should fit on the marked piece, taking into account the gap for the wire sensor along the sides. After cutting out a piece of board, unbend the clip, which we will use as the contact-holder of the battery. The clip is bent as necessary and fastened in the holes of the board. We push the battery and turn it in, so that it would hold tight. Also from the copper wire make a second contact for the negative pole of the battery. Both contacts are soldered to the breadboard. Once again, insert the battery and check the reliability of its attachment. It is better to tighten the paperclip tightly, try until the battery keeps well. And now we will sew. But not a microcontroller, but recall the skills of sewing with thread and needle. Stitch by stitch, around the perimeter, by copper wire we go around our kerchief, and we draw the ends in the designated place. This will be our sensor. One moment, at the corners of the main diagonal wire, we snack and fix. Therefore, as it turned out later ("... experience, son of difficult errors ...") such a large sensor is poorly calibrated and leads to poor-quality detection of touch. So, the role of the sensor remains only behind the two sides of the square of our board, the rest is decorative (in the photo the sensor is not consumed, as it all turned out after the trial tests. But the cut sensor can be seen in the last photo of the finished device). Well, we got to our microcontroller. What a wonderful thing it is! On a tiny platform with only 8 legs and you have an ADC, and a timer, and hardware PWM, and it has a built-in generator and you only show him the electricity, and he is already working. What else is needed for happiness, especially since the price of this pleasure is only 75 American kopecks. There are of course some flaws, but - well, the world is not perfect. Okay, enough of the lyrics, we continue. So, we solder our tin to the prepared site. STOP! .. I almost forgot. Now we will sew. Download the finished kosher firmware and fill it in any convenient way, I personally used the simplest LPT programmer, though I did it a bit for myself, but this is not the point - the circuit is as simple as a stool, it works reliably like a hammer. Fyuzy not touch. Now we solder our tin to the prepared site: 4th pin (ground, minus source) to the wire contact from the center of the battery, 8th pin (power, plus source) to the clip, 2nd pin to the wire from the sensor. From the side we catch the smd resistor (1 MΩ), between pins 2 and 3. If the smd resistor is not at hand, the usual one can be crammed. The case remains for small - RGB-LED. RGB-LED we need one that with four pins (there are, however, oddly enough, and with 2 - do not take them). Three of them are anodes of red, green and blue diodes, one conclusion is common. The LED lens must first be melted with a transparent silicone hot melt. If this is not done, then it will not glow very nicely, because emitters give beams that almost do not mix and in general do not look ice. So we heat the thermogun and thickly apply molten silicone to the head of the LED, then you can show your sculptural abilities by trying to portray a flower, a bug, or whatever. Then we solder the LED to the mikruh, common on the 4th leg (ground), red to the 5th, blue to the 6th, green to the 7th (the color arrangement matters, because for the green the software brightness correction is used) . Well that's all! Boldly and smartly insert the battery - 3 colors will light up one by one, each for 1 second. During this time, you must remove your hands from the device, because then the sensor calibration starts immediately, which is signaled to us by a flashing blink for about 4 seconds. During this period, the sensor state is recorded when it is not touched. After the illumination is over, you can take the firefly in your hands and watch the fascinating colors, the play of light and halftones.
If you are still not running to the nearest electronics store for details, then I would like to give you a clue to the focus, i.e. how it all works. Also, perhaps, something will seem interesting to those who are in the subject. As I already wrote, the basis of the design was the above article. Therefore, I refer you to the description of the fundamentals and the work of the sensor, everything is very well chewed there. My improvement is connecting to the RGB and refactoring code. Let me explain in more detail: the 13th tinkle is, of course, beautiful, but only the PWM hardware channels have only 2 channels, and for controlling the RGB LED, as Cap suggests, you need at least 3 channels. This raises the question: where to get another PWM channel? When asked if there is an answer to a simple one, we won’t look for another PWM, and we’re not going to use hardware shim at all, but write down our own program PWM - Orthodox, just like a channel! From loud slogans we turn to real practice. I hope that such PWM, duty cycle, no need to explain? (Rhetorical question). So, given: 1. The frequency of the PWM channel - at least 100 Hz, in fact, the eye and from 50 blink practically does not notice, but the real guys less than 100 do not look. So pulse duration with a duty cycle of 100% should be 10 ms. In these 10 ms we have to push the brightness adjustment, at least 100 levels. Then we find that the minimum pulse duration is 0.1 ms (at a duty cycle of 1%). 2. The frequency of the microcontroller's internal timer depends on a) the frequency of the clock generator, which, since we use internal, maybe 4.8 or 9.6 MHz; b) clock generator divider; c) the divider of the timer itself. Using the timer, we should be able to count down the intervals of 0.1 ms, i.e. handle events at a frequency of at least 10 kHz. In short, as a result of theoretical and empirical research, such results were obtained. The clock frequency of the controller is 9.6 MHz, the timer works with a division of 1/256 = at a frequency of 37.5 KHz. In order to be able to handle events at this frequency, we set the timer operation mode by triggering by coincidence. Those. as soon as the timer counter coincides with the value set in the ORCA0 register, an interrupt occurs - and here we are. In this case, the set value of the ORCA0 register, as some of you have already guessed, should be 0. Brightness will not be adjusted by 100, but by 256 levels (the controller is 8-bit, so it is more convenient, and more shades), then we get the frequency PWM 37500/256 = 146 Hz, which is more than satisfactory. Consider the interrupt processing in more detail:
The brightness of each LED is set by global variables red_bs, green_bs, blue_bs and is changed by the main program according to the special effects plan. The wave_counter counter rotates in a circle. When the brightness of the LED is greater than the value of the counter, it lights up, otherwise it is extinguished. So if the brightness is 255, then this corresponds to a 100% duty cycle. I did the division by 2 in the green LED in order to increase its brightness, because its visual luminosity turned out to be less than the others, apparently the specificity of the spectral characteristics. In general, gourmets and interested can appreciate the entire source . Much more could be written about adjusting the overall brightness, processing the sensor, the size of the program and the memory (oh, darling, here's another sore point), setting the watchdog timer and low power mode (by the way, as shown by , in hibernation, the circuit eats 0.06 mA) ... It would be possible to attach oscillograms to increase visibility and clarity for beginners, discuss the use of the rand () function and debate about contrasting the advantages of programming in assembler with convenience and power tion CodeVisionAVR compiler code. But, as the unforgettable Kozma Prutkov used to say: “You can’t grasp the immensity,” and popular wisdom says, “Have a good time.” I hope I will still have the opportunity to share with you delights in this area, which I also wish you, professional, creative and every success. Present ladies - with the coming!