22
Electronic music box 9/05/2019 rev2 Rev2: added 4,7uF on LM386. Correction error R7 = 8.2k and not 6.8k. R17 220kio A long story. I was a child when a parent back from Italy had offered me a music box, a gondola with his gondolier of course and a pretty dancer in a pink dress who was hopping to the sound of crystal clear music. A spring was being pulled up and the device was playing a melody extracted from a ten-second song that was repeated at first quickly and then slowed down until the spring's energy was exhausted. Not very satisfactory but it pleased me a lot, better than the crank mill that was DiGueDon, DiGueDon ... Alas ephemeral pleasure, the spring back too often or too strong eventually broke. As a teenager, I found the gondola and decided to dismount it; I pulled out the spring and managed to make him blush on the gas to soak it, pin it and hang it on its picot. All the mechanics put back in place I pulled up the spring and released the wind brake, Catastrophe, the spring was relaxed in less than a quarter of a second the spiked drum turned at full speed and broke three quarters of the blades vibrant. Definitely irreparable! Several decades later, yes! time passes but not childhood memories. One evening, the television gave us a nice gift, an opera "The magic flute" by Mozart, a masterpiece in which we hear a music box, (bells) one of two instruments enchanted with the flute that give their owners powers to defeat the villains who want to thwart their plans. Here is a memory reactivated. We still find nowadays mechanical box music but without spring, it is necessary to turn a crank, so unfit to repair my musical gondola. Good! Let's abstain from nostalgia! In the meantime my professional life has been spent in electronics and computers. Starting from the premise that in electronics and with the help of a microcontroller and a little judge you can do everything on the condition of remaining modest, I decided to embark on the manufacture of an electronic music box (no risk of breaking the spring) with specifications a little more fleshed out than those of these small Swiss mechanics:

Electronic music box - pagesperso-orange.frbernard.chabbert.pagesperso-orange.fr/ElectronicMusicB…  · Web viewDo not write a reb (d flat) that does not exist, write do # which

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Electronic music box - pagesperso-orange.frbernard.chabbert.pagesperso-orange.fr/ElectronicMusicB…  · Web viewDo not write a reb (d flat) that does not exist, write do # which

Electronic music box 9/05/2019 rev2

Rev2: added 4,7uF on LM386. Correction error R7 = 8.2k and not 6.8k. R17 220kioA long story. I was a child when a parent back from Italy had offered me a music box, a gondola with his gondolier of course and a pretty dancer in a pink dress who was hopping to the sound of crystal clear music. A spring was being pulled up and the device was playing a melody extracted from a ten-second song that was repeated at first quickly and then slowed down until the spring's energy was exhausted. Not very satisfactory but it pleased me a lot, better than the crank mill that was DiGueDon, DiGueDon ... Alas ephemeral pleasure, the spring back too often or too strong eventually broke. As a teenager, I found the gondola and decided to dismount it; I pulled out the spring and managed to make him blush on the gas to soak it, pin it and hang it on its picot. All the mechanics put back in place I pulled up the spring and released the wind brake, Catastrophe, the spring was relaxed in less than a quarter of a second the spiked drum turned at full speed and broke three quarters of the blades vibrant. Definitely irreparable! Several decades later, yes! time passes but not childhood memories. One evening, the television gave us a nice gift, an opera "The magic flute" by Mozart, a masterpiece in which we hear a music box, (bells) one of two instruments enchanted with the flute that give their owners powers to defeat the villains who want to thwart their plans. Here is a memory reactivated. We still find nowadays mechanical box music but without spring, it is necessary to turn a crank, so unfit to repair my musical gondola. Good! Let's abstain from nostalgia! In the meantime my professional life has been spent in electronics and computers. Starting from the premise that in electronics and with the help of a microcontroller and a little judge you can do everything on the condition of remaining modest, I decided to embark on the manufacture of an electronic music box (no risk of breaking the spring) with specifications a little more fleshed out than those of these small Swiss mechanics: The music should not be repetitive anyway no more than a symphony of Beethoven (it is not a criticism), it must not slow down and end when dying of fatigue. The range of the musical range will be not 19 notes but 44.

From the Do1 at a frequency of 261.63 periods persecond to La4 at 3520pps.Here are the note names:do, do #, re, re #, mi, fa, fa #, sol, sol #, la, sib, si.These notes should be followed by a subscript (1 to 4)specifying which range they belong to.

For example: do1 (the most low note), do # 2, la4 (the highest note) All the notes have their diésée note except the mi and the si, of course, and since always an exception to the rule there is no la# but a sib. Do not write a reb (d flat) that does not exist, write do # which has the same frequency in the temperate range (we say that do # and reb are enharmonic). The tunes presented must be complete. My music box will be able to nuances (piano, mezzo forte, forte) the tempo can be modified (accelerando deccelerando); it will broadcast several tunes all to last several minutes. Especially the user can introduce the tunes of his choice. However, provided you have some notions of solfeggio and have the ability to edit reassemble (MPASMWIN) and re-record the PIC microcontroller program. I know that electronicians who know music are not a popular genre, but I'm sure there are some. The program does not have to be modified just write the notes in the place provided following prerecorded songs that can also be erased and replaced by others. The pre-recorded tracks have been transposed

Page 2: Electronic music box - pagesperso-orange.frbernard.chabbert.pagesperso-orange.fr/ElectronicMusicB…  · Web viewDo not write a reb (d flat) that does not exist, write do # which

and simplified for satisfied with the operation of this Music Box. A restriction: my Music Box will not include a dancer. Although this is entirely possible: Imagine a medium power relay type electromagnet whose paddle would give the vertical impulse to make the dancer jump, the rotation is done naturally. This relay would be controlled in the sequence "Bip1t" by the port A3 systematically as are controlled the charges of the capacitors. This would require a more consistent power supply incompatible with the use of a 9 volts battery.

Technically the electronic part must be simple: The least possible components, easy to obtain, inexpensive and easy to manufacture by an amateur with limited tools. All components are through; no cms; single-sided printed circuit board. The first tests (in 2010) made with a PIC 16F84 were quickly abandoned (not fast enough, not enough memory set instructions too restricted) other failure with a PIC 16F876. Finally I opted for a PIC 18F2520 Microchip here why: The 16F have a 14-bit instruction word that allows a set of 36 instructions. RISC architecture (Reduced Instruction Set Computer) is good, easy to remember, but it's still better with a set a little less reduced. The 18Fs have a 16-bit instruction word that allows some 80 instructions. Do not ask me why the 14 bits are called 16F, the F means "flash memory so erasable and rewritable at least 100,000 times", and the 16 bits are called 18F ... So new instructions very useful that simplify programming: Ex BTG 'bit toggle' which inverts a bit of a port or byte in memory by a single instruction; I use it a lot. New conditional connections (positive, negative, zero ...) that avoid going to test the word Status; The 'Movff' which transfers one byte into another without going through the working register Wreg; we can manipulate the stack (Pop, Push) I use it to shorten an air. During an interruption the registers: Status, Wreg, Bsr are saved without loss of time systematically and the "fast retfie 1" (Return from interrupt enable, 1) restore all without increasing the number of cycles (2). In addition the 18F2520 has a flash memory of 32 kilobytes, (The songs are recorded in the program memory). Another advantage, this chip can be controlled by an internal oscillator up to 8Mhz and a PLL multiplies this frequency by 4. I do so pedaling at 32Mhz internally thus saving a quartz, 2 ceramic capacitors, 1resistor and room on the PCB. This chip costs less than 7 $. The DS39631E manual can be downloaded free of charge from Microchip, a 410-page English-language Pdf. All this fully satisfies my specifications.

Let's get into the heart of the matter 1st problem: Imitate as much as possible the sound of a mechanical music box with vibrating blades. In April 2010 Elektor released my achievement featuring a code lock that allowed for the bonus of performing the "Foreign Legion March" (There were only 4 possible notes and only one at a time), the principle is the following: Charge a capacitor through a port A of the microcontroller. This capacitor feeds a transistor collector which receives at its base a slot calculated by the computer and emitted by another port output. This results in a damped wave on the collector (strong attack and progressive decrease of the sound according to the well-known curve of discharge of a capacitor in a resistor). This first, rather satisfying test has been improved. 2nd problem: My music box is able today to broadcast 3 simultaneous notes. These 3 notes can be 1 black, 1 eighth, 1 sixteenth note, each emitted by a different port and transistor. We can play in a time 1 black + 2 eighths + 4 sixteenth notes. The big problem is the calculation of all this, the PIC program does it. there is no generator of notes: that would be too easy !, too expensive and too cumbersome (a quartz a generator chip of 12 notes and a multitude of divisors by 2 not counting the passive components). Fortunately the Software can significantly reduce the Hardware!

Page 3: Electronic music box - pagesperso-orange.frbernard.chabbert.pagesperso-orange.fr/ElectronicMusicB…  · Web viewDo not write a reb (d flat) that does not exist, write do # which

Step is the basis of writing. A musical air is composed of measures; a simple measure contains 2, 3, 4, steps, a composite measure (ternary) 6, 9, 12 steps. (The base unit is the round, the black is the quarter of a round, the eighth is the eighth of a round). In a measure 3/4 (3 quarts of round) for example the measure contains 3 blacks. In a 6/8 measure (2 steps) the measure contains 2 black dots (black and half) or 6 eighth notes (2 triplets). In a simple measure a black can be divided into 2 eighths or 4 16ths in all possible combinations. Thus a step can contain up to 7 notes: 1 black, 2 eighth notes and 4 sixteenth notes. Silences do not have to be codified, just leave the place empty. In my system the music is written and executed step by step. The range of sounds is nearly 4 octaves from Do1 to La4. The most low range (from Do1 to Si1) is the one containing the 'La440' (International reference 440 alternates per second). This is the note that you hear when you pick up a phone; it is also the 1st note of the firefighters' or police 'pimpon'. In an orchestra it is the oboe that gives the La because it is the only instrument that is non-adjustable. All ratings are calculated from this La440 for a temperate range. The length and frequency of the notes are calculated from the oscillator 32 Mhz ie 8 million instructions per second since a simple instruction takes 4 periods (some instructions last 2 cycles of 4 periods). The main difficulty of the program is to take this into account in order to evolve the counters at the right speed whatever the tempo, the number, the height and the length of the notes issued simultaneously. Some notes of the range 4 very acute, are a little out of tolerance due to the rounding of the number of loops to the nearest unit; only a very exercised ear can detect this small shift; these notes are used very little. The internal oscillator RC 8Mhz is likely to slip slightly depending on the voltage and temperature. The regulated 5 volt voltage varies very little and the temperature moves very slowly. A music box is not intended (as they say now) to play with other instruments that also vary with the temperature! I have never noticed an annoying effect. Purists can however add a quartz of 8Mhz (the PLL is only a multiplier and can not drag the frequency). We can then go up to 40Mhz, with a quartz of 10 Mhz. I think it's an unnecessary expense and work. It would then be necessary to modify the config of the chip as well as the values of the frequencies of the notes; change the board ... The internal RC oscillator can be set precisely by changing bits in the OSCTUNE register (By default the frequency is centered at 32Mhz) I did not feel the need.

Let's go to the electronic diagram.Extremely simple, the board contains 1 Pic Microchip 18F2520 DIL 28 legs, 4 transistors BC548, 1chip 8 legs LM386 amplifier BF, why bored with transistors when there is a chip costing <1 $? A small 70mm 16 ohm HP, a 10k potentiometer, 1 push button, 16 resistors, 11 capacitors, a microswitch. The type of transistor (BC548b) is of little importance (it is a 'TUN' NPN universal transistor as recalled by Mr. Clemens Valens in an article 'ELPP' Elektor Labs Preffered Parts of September 2014) because it always works either blocked or saturated (all or nothing). It must be an NPN. Except for the T4 that adjusts the sound intensity a BC548b or other TUN of a Beta to 250 (hfe). If it is not satisfactory act on his Res. collector (4.7k); increasing it will reduce the changes in the power of the sound (less nuances). The 9v. battery (or 2 x 4.5v batteries in series) powers a 5v regulator for powering the Pic. 9v. battery directly powers the BF amplifier. Consumption in 9 v. is at most 35 mA. in the fortissimi. Everything is installed in a wooden box; opening the lid puts the device under power by a microswitch attached to a wall of the box. The micro PIC is initialized then, recruited with fatigue, it falls into SLEEP. Consumption is very low, since a year of development I use the same battery without noticing any weakening. Apart from the music, open cabinet, the computer is still in sleep (dormant oscillator stopped).

Page 4: Electronic music box - pagesperso-orange.frbernard.chabbert.pagesperso-orange.fr/ElectronicMusicB…  · Web viewDo not write a reb (d flat) that does not exist, write do # which

We see on the left of diagram 3 very simple circuits, identical except the values of the components. A circuit for the black, one for the eighth and one for the sixteenth note. I must specify here that the step is divided into 16 parts (4 parts corresponding to a 16th note, 8 parts for a eighth note and 16 parts for the black). A counter called Cint counts the interrupts produced by the overflowing zero timer. Cint counts from 0 to 16 and is reset to 0 as soon as it reaches 16. This is the tempo of the music. The capacitors are charged between cint 0 and 1 or between 4 and 5 etc ... The charging time is the same for all the notes, see further CintGraf. This is why the capacitors and their load resistances are different. The black is issued from 0 to 16, the eighth notes are issued from 0 to 8 and / or from 8 to 16, the sixteenth notes are issued from 0 to 4 and / or from 4 to 8, 8 to 12, 12 to 16. The program only loads the capacitors of the notes present in a step.

Let's study, for example, the eighth note circuit. At cint0 or cint8 begins the charging of the capacitor C2; the load stops at cint1 or cint9. the charge is produced by port A1 leg 3 of the Pic. A diode prevents the capacitor from discharging into the port when the output voltage of port A1 drops back to 0v. The voltage drop in the diode is about 0.7v. At the same time as the load port B2 tab 23 sends the creneau to the base of the transistor by the resistor R9 the transistor becomes on during the positive phases of the slot (5v.). The resistor that charges the capacitor is 1kilohm; the collector resistance R6 is 8.2K but the current only circulates there half of the time during the positive periods of the crenels emitted by the port. These components are calculated so that the transistor produces a damped wave for 8 parts out of 16 of the step. The capacitor charge is approximately 95% of 4.3v.or 4v. The wave stops at 8 or 16. Note: The calculation of the charging time by the formula Theta = R x C gives 10 millisec. but at this point the load is only 66% and considering that the wave appears at the beginning

Page 5: Electronic music box - pagesperso-orange.frbernard.chabbert.pagesperso-orange.fr/ElectronicMusicB…  · Web viewDo not write a reb (d flat) that does not exist, write do # which

of the charge it takes at least 6 Theta's to charge at 95%. The charging time is fixed by Cint, a step is worth on average (according to the tempo) 60 millisec. V = 4 * (e ^ (- T / CR). V in volt T in sec.; C in microFarad R in MegOhm. At the moment of discharge: For the black we have 0.28v after 1 second and 0.01v after 2sec. For the crooked 0.2v after 0.5 sec. and 0.01v after 1 sec. For the sixteenth note 0.17v after 0.25 sec. and 0.01 after 0.5 sec.The 3 notes or maybe 7 are mixed by 3 resistors of 100k R11, 12, 13 which end on the potentiometer P1 of 10k. set adjustment the sound, depending on the speaker used. Before the first power up, set this potentio. about 1 Kilo Ohm between the slider and the ground point so as not to saturate the HP; Later refine the setting by listening to the music. At the top of this potentiometer the height of the sum of the 3 notes is at most 1v. It's too much to enter the LM386; The P1 lowers this voltage to a value suitable for good listening according to the quality of the HP 8 or 16 ohms. A capacitor of 10 nanoF. rounded the angles of the crenellations. A transistor T4 in series with a resistance of 4.7k partially bypasses P1 as a function of what it receives on its base, a set of 2 resistors giving a scale of 4 current values. These resistances are at 5v. or 0v. by the ports B4 leg 25 and / or B5 leg 26 which allows the program to qualify the power of the sound. The resulting voltage is spread over time by C8.. The small LM386 in its extreme stripping gives an amplification of 20 dB so a power output 100 times the input power (2 is the log dec 100 and 2 Bell's = 20 decibells). A capacitor between legs 1 and 8 can mount it up to 26DB (400times) but it's way too much for a music box and a 1/4 watt HP. Revision2: I have abortused this amplifier without capacitor; but the dynamics of this Music Box is very great between the Pianissimi and the Forte. In use I realized that 3 notes simultaneously sent Strong saturated the input of the amp. which resulted in unpleasant sounds output. It was therefore necessary to lower the input level by the 10Kohm Potentiometer And in this case the notes Pianissimo became almost inaudible. So I mounted the amp to 26 DB by a capacitor 4.7misroF between the legs 1 and 8.Note: It is imperative to keep P1 10 KiloOhm otherwise the shades would be disturbed. Did you notice that there was no capacitor in series between the Pic and the BF amplifier? Continuous link.

Page 6: Electronic music box - pagesperso-orange.frbernard.chabbert.pagesperso-orange.fr/ElectronicMusicB…  · Web viewDo not write a reb (d flat) that does not exist, write do # which

The songs follow each other after a silence of 3 seconds. Pressing the button while a song is running aborts the song and starts the next one. A press during the last song plugs the program to "departure" and the microprocessor in sleep awaits a new pressingt to restart the song. You can stop broadcasting the music at any time simply by closing the box. After use close the lid which cuts off the power supply and saves the battery. The program is written in assembler language essential to know exactly how many cycles are held in a sequence. (To generate a note the time of the crenel must be very precise). The ratio of the slot is always 50%. To program I use MPLAB IDE version 8.91 downloadable for free at Microchip. To write the file 'BoiteAmus.hex' in the microproc. I use PICPgm programmer and a very old programmer: POK508 which connects to the serial port of the PC.

Page 7: Electronic music box - pagesperso-orange.frbernard.chabbert.pagesperso-orange.fr/ElectronicMusicB…  · Web viewDo not write a reb (d flat) that does not exist, write do # which

The program is in one piece, there is no "include". The configuration bits are written under the 'Heading' frame (CONFIG directives) they are in the HEX file (At the very bottom of the .HEX file the line starting with 03) there is nothing to add; however, the flash code is not locked. Open system. The possible notes are listed as constants: Name of the note and its assigned frequency. Example: (La4 equ 0xED) ED is a hexadecimal number = 237 decimal. The complement of this number at 256 is 19. 19 is the number of loops that will have to be executed for half a slot; The bit is then inverted and the counter restored for the next half slot and so on until the end of the time that the note must last. The loop in question will be seen further. See the appendix EXCEL sheet which gives the frequency of the notes, the time of half a slot in microseconds and the number of loops (iterations) only for detailed information because these values are at the top of the program. To play a note it is necessary to write its name and the index of its range, its length will be defined by the case of the macro 'temps' in which the note is written. ; <<<< Preview for 1 beat ... a glimpse to execute one step >>>> cint = 0 cint is incremented by interrupt due to timer0 over run dc1 bra $ + 2 ; To complete time bra $ + 2 ; bra $ + 2 = 2 cycles .25 microsec.to jump bra $ + 2 ; to the next line bra $ + 2 bra $ + 2 btfss bitest, 1 ; Is there a double quaver 1 bra $ + 8 ; No note absent infsnz countdc , f ; Yes increment counter bra $ + 10' ; count = 0 go to movff then toggle port B3 bra $ + 6 ; not zero out with time compensation nop ;complete time. . . = 1cycle (0.125 uSec.) bra $ + 2 bra c1 ; Continue with crooked. Movff frdc1, countdc ; refresh counter DC btg portb, 3 ; toggle B3 doublecroche 1 (invert polarity) ; 18cycles = 2.25 microseconds) c1 bra $ + 2 ;The same for C1 and Black (some differences for black Due to the pedal)cint = 1 nop btfsc cint, 0 ; cint bit 0 present? (cint = 1?) clrf porta ; yes clear port A stop load capa 's btfss cint, 2 ; cint = 4? 1/4 of time bra dc1 ; no continue iteration until cint = 4 << This sequence from 'dc1' to 'bra ‘dc1’ always 60 cycles = 7.5 micro sec. >> cint = 4 ; Yes btfss bitest, 4 ; is there a eighth note 2 bra $ + 4 ; no skip loading capa for dc bsf porta,2 ;yes start loading capa& DC ;only a DC note is possible at this timeDc2 Same sequence for a quarter of time (cint 4 to 8)dc3 . . .” “ “ “ (cint 8 to 12 dc4 . . .” “ “ “ (cint 12 to 16) cint = 0 next step …For more details read the Pic program "musicbox.asm" in notepad or better in MPLAB IDE Microchip (in color easier to read).

Page 8: Electronic music box - pagesperso-orange.frbernard.chabbert.pagesperso-orange.fr/ElectronicMusicB…  · Web viewDo not write a reb (d flat) that does not exist, write do # which

A macro called precisely "temps" makes up a set of 8 bytes which is the arrangement: Byte 0: receives the tempo (a number from 1 to 255) which gives the speed of this step and thefollowing as long as it is not changed. Write eg d’150 ' (150 decimal) instead of n whichmeans Null (same tempo as before or no note at this point.)

Byte 1: case for the 1st 16th note (DC1). . . . . .

Byte 2: case for the 1st eighth (C1). . . . . . Byte 3: case for the black (N). . . . . . Byte 4: case for the DC2 Byte 5: case for the DC3 Byte 6: case for the C2 Byte 7: case for the DC4 The boxes are separated by a comma. A box without a note must contain a lowercase n

Exemple : Writing one step that contains 6 notes not a blackThen a second step containing only a black note. 2 steps to write in sol key.

Mf ; (This macro sets port B5 to 1 and B4 to 0) = Mezzo-fortelocate: ; tempo dc1 c1 black dc2 dc3 c2 dc4 this line in comment label1 temps d’105' ,do1 ,do2 , n , mi1 , sol1 , sol2 , re2 ; 1st step label2 temps n , n , n , do2 , n , n , n , n ; 2nd step Same tempo this step contains just one black do2. label3 temps n , n , n , n , n , n , n , n ; ... continue .The tempo at 105 gives a black 0.43 seconds or 138 Black per minute. « « 1 « « 0.004. " 15000 " " Unusable! 60 0.25 sec. 240 100 0, 4 150 200 0.82 sec 73 255 1 sec. 60On a score we give rather the number of black per minute, which can only be appreciated using a metronome. The desired tempo for a black is obtained by dividing the time by 0.004096. For example: Tempo for a black of 0,8seconds ... 0.8 / 0.004096 = 195 (one uses only 100 to 255) The nuances are written between the steps. (4 shades: Pp, Piano, Mf, Forte) and are activated on the following steps. Attention to capital letters, the assembler distinguishes them from lowercase letters. See at the end of the program how to write an air. First of all have the score; doubtless to transpose it higher so that it fits within the range of possible notes; simplify 3-note chords; delete or raise them an octave, very low notes that have nothing to do in a music box. The lowest possible note is the do1 on the line just below the span range; it is therefore interesting to convert the key of Fa into a key of Sol. Mark the times and write them one by one in the form of "temps" lines like the example above. Put a label on each line (it does not cost anything in memory) example: c4m2.3: song 4, measure 2, 3rd step. Not mandatory but practical to navigate, it is necessary to number also measures on the score. We can in a song do repeats. For this purpose 2 macros: "sectim label of the second time measurements". To be placed right in front of the measures of 1st time. Macro "repris label of start of recovery". To be placed between 1st and 2nd steps measurements. See example in song7 in the ASM program. (Repeats save bytes of memory).For nested overlays another pair of macros sectim2 and repis2 macros are available.See example in song4: a cover and at the end a Da capo (sectim2 and repris2), during this Da capo one does not make the cover.

Page 9: Electronic music box - pagesperso-orange.frbernard.chabbert.pagesperso-orange.fr/ElectronicMusicB…  · Web viewDo not write a reb (d flat) that does not exist, write do # which

Warning: only the labels are written in column1.The instructions or macros are written after the label and at least one space or tab. If no label make a Tab. Ex: c7m7.2 temps …Ex .: c7m7.2 temps ... sectim c7m9.1 c7m8.1 temps ... 1st time c7m8.2 temps ... 1st time repris c7m1.1 resume at the beginning of the song c7m9.1 temps ... 2nd time ...continue.Result: Measures 1 to 8 will be executed, then measures 1 to 7 measure 8 will be skipped, then measure 9 executed and then continue. See this example in the PIC program at the beginning of the song7 and in the appendix the first page of the song7. For these occasions a label is mandatory of course, for example: verse1, chorus, or: c7m9.1 ... (c7 = song 7 because we can not give 2 similar labels in a program). The word "temps" is mandatory is the name of the macro, (see the macro's just before the beginning of the program) it is she who collects the notes and writes their numerical values in 7 variables n1 to n7. Also the value of the tempo in n0.Note: Avoid playing 2 notes of the same frequency at the same time eg. 'Black do2' and a 'quaver do2' both having exactly the same frequency, in opposition of phase they choke the time of the shortest. On a piano it's impossible to play the same note twice at the same time. There is no limit to the size of a song other than the remaining memory. A song must end with a "Return" statement which returns the program to the Start sequence. Resonance Pedal: A black can continue to resonate in the following step if no black present. For this write just before the time when a black must continue: pedon. It is a macro that is written as an instruction. This pedal is automatically canceled at the end of the step; it only acts on a black. Use with caution, prolonged black should not rub at very close notes which would produce unpleasant dissonances, except at the end of step 'dc4' here the sound of the black is very attenuated. Let's go back to the program. After the name of the notes and their values are the addresses of the processor registers and variables used by the program; then the macros and finally the beginning of the program that starts with the usual initialization in any program. Following the init2 the processor falls dormant (Sleep, very low consumption) then we find the label "Depart". The proc in sleep waits for the button to be pressed, which produces the external interrupt 0 which wakes up the proc and the interrupt routine goes up bit 0 of the variable "newsong", the program continues after "depart" and forward the music! At address 8 of the flash memory begins the interrupt service routine. "Introut" which manages the 2 kinds of interrupts possible in this program: the pressing on the button and the overflow of the timer 0. The timer 0 of 16 bits thus 2 bytes (0 to 65535) is used to count the tempo. It receives the frequency of 8Mhz which is input divided by 8 by the prescaler. The high byte is pre-loaded by the number given in box 0 of the ‘temps’ macro. When this timer overflows (goes through 0) it produces an interrupt that hooks the program to introut which increments Cint. At the end of the macro ‘temps’ there is a call of the sequence "loadfr" which loads the counters with the numbers representing the notes: the value of the Noire in frn, the value of the C1 in frc1 the value of the Dc1 in frdc1. this sequence also loads the tempo if it has to be changed and constitutes a byte "bitest", each bit of which indicates the presence of a note: bit 1 = 1 if presence of a DC1 otherwise the bit 1 = 0. Bit 2 for C1 etc. in the same order as the bytes of the temps macro. The end of this sequence calls "bip1t" which according to the word "bitest" loads the capacitors of the notes present at the beginning of step (Black, C1, DC1) by raising the corresponding port A. (At this moment Cint is = 0). The capacitors start charging, the load will stop by resetting port A to Cint1, Cint5, Cint9, Cint13.

Page 10: Electronic music box - pagesperso-orange.frbernard.chabbert.pagesperso-orange.fr/ElectronicMusicB…  · Web viewDo not write a reb (d flat) that does not exist, write do # which

Let's finally get into the big loop ‘Exec’. This loop, to execute a step, has 4 parts corresponding to 4 sixteenth notes; they have for label: dc1; dc2; dc3; dc4. Each of these loops lasts 4/16 of time. The loop dc1 from cint0 to cint4; loop dc2 from cint4 to cint8 etc ... At cint16 is the instruction "return" to go to the next step. These 4 loops are identical except the name of the counters. A loop of 4/16 of time is itself divided into three parts that follow each other, the first (dc1) for the count of the sixteenth note, the second (c1) for the eighth, the third for the black. (The black and eighth counters must evolve in the 4 loops) Let's see the loop “dc1”. "Bra $ + 2" branching to the next line, (1 instruction takes 2 bytes) are there to complete the time of the loop which must always last 7.5 microsec. between the label dc1 and the return to this label. The "bra $ + 2" are always made and therefore take 2 cycles we also see "nop" (no operation) that last only 1 cycle. This time is calculated to get the do1 with the maximum number of loops of 7.5usec is 255. See the Excel sheet. The min value of the counters is 1 because in the macro "temps", 0 means no note. At each iteration the 3 counters pre loaded by the values of the notes to play: countdc; countc, and countn are incremented (+1) if the note is present until they increase to 256 or 0 what is detected by the program which then inverts the output voltage of the port of this note (Toggle) and restore the counter to the value of the current note. This loop dc1 - bra dc1 turns until cint reaches 4, we go to the next loop dc2. During the course of the loop each turn the program reads the cint to stop the charge of the capacitors when the bit1 is present (1, 5, 9,13) and to pass to the next loop when cint arrives at 4 or 8 or 12 or 16. After this quadruple loop "Exec" is the subprogram Delai itself called by the macro delais (used to introduce dead time). "Delais 1" = 50 millisec. Then just below begin the songs in the order: song1, song2, Etc. After the last song played the program puts the processor in 'SLEEP' waiting for a press on the button for a new audition or the closing of the music box which cuts the power supply.

The pre-recorded songs Chant1: Adorable music Mozart (The Magic Flute) 1196 bytes 44 seconds. Chant2: At the clear fountain 1028 " " 1 min. 2sec. Chant3: The song of spring F. Mendelssohn 6024 " " 2 m.15 sec. Chant4: The love of me ... Author unknown 15th century 2330 " " 1 m. 45 sec. Chant5: Waltz No. 15 J. Brahms 4268 " " 1 m. 30 sec. Chant6: Entertainment Scott Joplin 6780 " " 4 m. 40 sec. Chant7: Study in Fa in form of sonatinette for 8562 " " 3 min. music box only From my composition, we hear in the 2nd movement 2/4 'larghetto' blacks, eighths, sixteenth notes, but also white or 2 black bound, and triple quarter notes! Syncopated measures! The third movement ‘vivace’ is a Sicilian, a ternary measure in 6/8. To show everything you can do with 3 notes. Sometimes you have to juggle the tempo to get the impossible! The time that a song lasts is not proportional to its number of bytes because of repeats and tempo. There are 1535 bytes left to record your favorite song or composition (85 steps, one takes 18 bytes), if you are satisfied with your first try You can erase any pre-recorded vocals (in full) to free memory space. The assembler automatically resets and the free bytes are always at the end after the last song. But we can also for example clear the song2 and write another instead without worrying about the amount of memory used. There will be no gap between singing2 and singing3.1/4 hours of music to fall asleep ... Or learn to love music and want to hear or play these simplified songs yourself.

Page 11: Electronic music box - pagesperso-orange.frbernard.chabbert.pagesperso-orange.fr/ElectronicMusicB…  · Web viewDo not write a reb (d flat) that does not exist, write do # which

Single-sided printed circuit board seen copper side 4 x 9 cm

Rev.2: For those who have already realized the Pcb old version just drill 2holes End LM386 to install the capacitor 4.7 micro between the legs 1 and 8.

Establishment of the components Component side view

A single strap st1 spans the circuit coming from the lug 1 of the micro PIC18F2520 U1 The printed circuit is seen by transparency. The drawing is enlarged for clarity. Push button SW1 welded in 4 holes connected two by two can be deported to a more accessible place of the box. Weld a two-wire in place of sw1 in 2 unrelated holes to the right of the plate. The switch bringing the 9v. is not represented, it is not on the PCB see below.

Page 12: Electronic music box - pagesperso-orange.frbernard.chabbert.pagesperso-orange.fr/ElectronicMusicB…  · Web viewDo not write a reb (d flat) that does not exist, write do # which

My Music box

The circuit is fixed by 3 screws on a piece of triangular wood itself glued to the bottom of the box.

At the top left of the box the micro-switch is fixed by 2 screws so that the contact opens by closing the box.

Bottom left is the 9-volt battery immobilized by a glued wall and a piece of bubble plastic.

On the right the red button to start the music or shorten a song.

The speaker takes place in the lid.

A wooden bar on the back of the box keeps the lid vertical.

Attention: With the box open, the board is powered up. To remove the Pic and put back in place it is essential to disconnect the battery (remove the connector at the bottom right).Green wire = 0 volt Yellow wire = + 9 volts after switch.

List of components:Resistors 1/8 watt R1 470 ohm R2 1 kohmR3 1.5 k ohm.R4; R8; R9; R10 10 kohmR5; R6; R7 8.2 kohm R11; R12; R13; R14 100kohm R15 47 kohm R16 56 kohmR17 220kohm Pot.1 10 kohm vertical pitch 2.54mm 20 turns Capacitors 16v. not 2.54mm C1; C4 22 microfarads C2; C5; C10 10uF C3; C8; 4.7uF C6 10nFC7 220µFC11 8µFC9 100nF

Page 13: Electronic music box - pagesperso-orange.frbernard.chabbert.pagesperso-orange.fr/ElectronicMusicB…  · Web viewDo not write a reb (d flat) that does not exist, write do # which

Actifs T1; T2; T3; T4 BC548b or other NPNs. For T4 see text. Microcontroller: PIC18F2520-I / SPDIP28 ou 18F25K42 + Tulip Holder Dl 28 Amplifier BF LM386 N1 DIP8 + (support DIL8 not required) 1 Regulator 5v. 78L05 100mA

Various1 speaker 16 or 8 ohms 70mm 1/4 watt from 200 to 3500 pps 1 microswitch with lever 1 contact Ex: (Conrad 709450) 1 miniature push button for PCB (Conrad 700479) 1 circuit board according to supplied drawing 4 x 9 cm . Mono Face 1 strap or 0 ohm resistor Connectors: 2 straight pins 2 pins at 2.54 mm pitch (breaker bar) 2 female plugs with 2.54 mm pitch cable Pay attention to the 9 volts polarity. No sense of connection for the HP. Housing ad libitum (speaker housed in the lid, depth of the lid: allow 2.5cm, otherwise use an inverted motor HP.) 1 battery 9v. + 1 pressure contact for 9v battery. (Conrad 624691)

Internet Bernard.chabbert.pagesperso-orange.fr Microchip.com/mplab Bernard Chabbert Villers-lès-Nancy. France