⬆️ ⬇️

Christmas music card

New Year and Christmas holidays are on the nose, and many are concerned about giving to friends and family.

Actually, I was concerned about the same question. And I got such an idea - to make a music card. Of course, the idea is not new - our Chinese brothers are making them in large quantities and at a low price. But I was not going to compete with the Chinese, I just wanted to have something of my own. And, as it seems to me, a good result was obtained:









TK


To begin with, I formulated such requirements:

In the open state, the card should play a melody in a circle.

I chose Shchedryk or Carol Of The Bells as the melody.

It is desirable that the melody was polyphonic.

The scheme should be as simple as possible, contain a minimum of parts, easy to repeat, and be inexpensive. And most importantly - fit in the card.

')

Analysis


I had long been going to do microcontrollers, and for the experiments I bought several pieces of the cheapest - attiny13, so I decided to make a circuit based on these microcontrollers.

To begin with, I figured out whether it will be possible to realize what was planned on this MK, and if so, in what way. For this, I found and downloaded notes , and began to analyze.

The melody in the memory of the MC can be written in many ways, 2 options came to my mind: 1) as a note number in each time interval corresponding to the minimum note duration, and 2) in the format duration — note. The first option is naturally easier to implement. I think: in the song there are 25 measures, 16 of which are repeated twice, i.e. total 41 beats. The minimum note length is 1/8, and the measure size is 3/4, therefore, in one measure, 6 notes of the minimum duration, and the whole melody will contain 6 (notes in measure) * 41 (measure) * 3 (voices) = 738 notes. If spending one byte on each note leaves a bit too much, since attiny13 contains 1024 bytes of flash memory in total (and you need to place another program and a table of frequencies). Therefore, it is worth trying the second method. I think: only 263 notes (including pauses) is already acceptable, even if you use a byte for the duration, and a byte for the height of the note, you get 526 bytes.

I decided to write in assembler - firstly, in order to better understand the MK itself, and secondly - to fit in the remaining 700 bytes.

MK tiny13 has 2 PWM on board, with which you can make a two-part tune. But for this melody you need 3 voices, so you can’t use PWM alone to generate sound. For simplicity, I decided to abandon the use of PWM - at least in the first version of the device.



Scheme


At first I decided to display each voice of the melody on a separate leg of the MK. Therefore, initially the scheme was as follows:







Later it turned out that with this switch on the music sounds very quiet. To solve this problem, all the voices had to be mixed into one channel, and the result was applied to the two MK outputs in antiphase - thanks to this, the voltage change on the piezoelectric element turned out to be 2 times the supply voltage. The scheme was even simpler:







Algorithm


In order to get the sound of a given frequency at the output of the MC, it is necessary to divide its clock frequency into a number-coefficient equal to the frequency of the MC / specified frequency. Since the division operations (and the calculation of the remainder by module) are absent in the AVR command list, we will divide by the counter. Initially, the division factor is assigned to the counter, and 1 is subtracted at the next cycle. If the counter is zero, then the signal has changed at the divider, and the value on a specific MK leg must be changed, and the value-coefficient will be written back to the counter.

Divisor counters need 4 pieces: one for each voice, and one more for counting notes (and determining the tempo).

The algorithm works as follows:



1) initialize variables

2) if the count-divider of notes is zero, then for each voice, check whether the current note has finished playing, and if it has finished playing, load the next one from memory.

3) for each voice, reduce the counter by 1, if it is zero, reassign the division factor to it, and change the value at the output.

4) transition to 2



In order for this algorithm to work, it is necessary that each program cycle be executed in the same number of cycles. To do this, it was necessary to calculate all the branches of the branches, and add a nop or empty loop for too “nimble” sections.



At first, when the sound was output to the three outputs of the MC, the waveform was in the form of a meander. Then, when I had to mix all this up and out through 2 pins out of phase (i.e. in fact, through one pin), there was a problem how to mix these three voices. Therefore, it was decided to make a waveform in the form of short pulses, and sum them with a simple or `a.



Program code


The source code of the program can be found here.



Iron


When implementing the final version, the following tasks arose:

1) food

2) on-off

Task 1 I solved with the help of a cr2016 lithium battery, which according to this article should have a capacity of approximately 90 mAh. I placed the battery in a self-made compartment from a paper clip, as I did not find an acceptable size and price for the industrial compartment. The circuit consumes about 2 mA, so this battery should be enough for 40 hours, which is more than enough.

Task 2, I initially thought to solve using the same clips, but then abandoned this idea in favor of the reed switch and magnet. The principle of operation is visible on the video.



Firmware


I compiled and flashed in AVR Studio 4, with firmware in fusion, I changed only the clock frequency to 4.8 MHz and turned off the division into 8 frequencies of the master oscillator.



Budget


Microcontroller - 8 UAH

Piezo element - 2 UAH

Battery - 4 UAH

Reed switch - 2 UAH

Development, debugging of the program - 6 hours

Development, testing scheme - 3 hours

Production of one postcard (in the presence of an etched board) - 1 hour



A photo




First prototype





Second prototype





Battery mount





Finished device - view from the details





Finished device - view from the back





First episode



PS


Happy New Year and Merry Christmas!

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



All Articles