La-la-la-la-laa!

You know those ICs playing melodies? You'd like to use one of them, but you'd like to play a certain song that none of the existing melody ICs has? This project comes handy in that situation. Take one 8051-compatible microcontroller and this software instead. Type in your preferred song, compile, burn into µC and you're almost done. The µC requires a few external passive components like xtal, a couple of transistors for power switching and if you want some other waveform instead of square wave, a 4-bit DAC constructed of resistors and a LM386 as an audio amplifier for loudspeaker. All circuits are powered with a battery of 4.5 or 6 volts.

The software is coded in assembly and it's for MCS-51 family. I'm using as31 as compiler. as31 exists for the Amiga, Linux and MS-DOS. It appeared to me, that the DOS version does not work in DOS prompt of WinNT4 or Win2k, as you can see. On MS-DOS 6.22 it works just fine.

Assembly source: The Melody Playing routine for MCS-51. version 33.

Compiled binary for quick testing: The Routine with Deck the Halls compiled in. Compiled to 0x0000 for burning into AT89C2051.

Another compiled binary, compiled to 0x8000 for testing in certain 8052 CPU card. Upload, connect a speaker between VCC and P1.4 via a 470 ohm resistor, run and perhaps enjoy.

Schematic: Basic electronics for quick testing. AT, xtal, caps, dec cap, resistor and spkr.

Schematic: Basic electronics for square wave output and auto power off.

Schematic: Enhanced electronics for "any" waveform output and auto power off.

There are some traditional xmas songs that I've typed in.
Deck the Halls, the same, that is in the melody player's source code
Joy to the World
More to come until Xmas.


Modifications to come

Since AT89C2051 seems to be hard to get, replace it with 8031-74HC573-EPROM.

Move poweroff-pin from P3.7 to some not-so-fatal pin.


Instructions for typing in the melodies.

Example: Simple scale from A to G.
        .db    AA|L8,BB|L8,CC|L8,D|L8,E|L8,F|L8,G|L8,EOP

To save space, both note's pitch and length are encoded into one byte, thus upper four bits are the note and lower four bits its length. The "|" character acts as OR operation in assembly, so, if AA=0x20 and L8=0x05, AA|L8 makes 0x25.

The length value is 1 for whole note, 2 for 1/2 note, 4 for 1/4 note and so on. Dotted notes are marked with "d", so 1/4 dotted note, that is played as 3/8 note, is typed in as "|L4d".

Let's continue from our scale example. We have to set the speed of the music. The command to use is SPD and its syntax is
        SPD|n, in which "n" is desired speed. The smaller the value, the higher the speed. n can vary from 1 to 15.

How'bout if we'd have to lengthen the scale to another octave? We'll select the octave with command OCT.
        OCT|n, in which n selects the octave. n may vary from 0 to 2. Don't select an octave beyond 2, or something strange will happen.

If there's not a note but a rest in the music? Then, instead of the note type
        REST|L8 to get a rest of 1/8 note, for example.

To change instrument to use type
        EFF_INSTRn, in which n is number of desired instrument. Instruments are included in the Source and only two exist in this version: sine wave and triangle wave. Others may be added, the instructions are in the Source.

All the notes to use are

A A#/Bb B C C#/Db D D#/Eb E F F#/Gb G G#/Ab
that are encoded as
AA Au/BBb BB CC Cu/Dbb D Du/Eb E F Fu/Gb G Gu/Abb

"C" is presented as "CC", because "C" is Carry flag, "B" is "BB", "B" is B register and "A" is "AA", "A" is A register.

        EOP is End Of Pattern, the end mark.

That's what is inside a pattern. Now let's see the Melody structure.

        .dw    Scale_INIT,Scale_1,Scale_END,EOM

Melody structure consists of words, that are addresses to patterns. If a pattern has to be played more than once in a melody, aksjdfgjahwreavglsjhvg.
 

Page last modified by Archy 16.10.2001