Step 17How it works
It produces a musical note by exciting a piezo speaker with a square wave. One output is made high and the other is made low, for a certain time. After some time this state is reversed, the output that was low going high and the other going from high to low. The piezo element, connected between these two outputs, sees a square wave of twice the supply voltage across it and so produces a loud note, louder than that produced if a single output was used.
The musical notes are produced by varying the delay between toggling the pins. The table of delays is according to the data taken from Don Lancaster's website, www.tinaja.com, and reproduced here. He also provided the delay routine with a resolution down to a single instruction period. The frequency of the note is produced by a software delay, and the numbers to be fed to this counter form the table that forms the melody. A 'zero' denotes that the end of the music is reached, and that playing is to be resumed from the beginning. A 'one' denotes that a rest is needed, and a period of silence instead of a tone is produced.
The period for which each note is sounded is measured in terms of the timer tmr0. It is set to increment from the instruction clock with a prescaler of 256, the maximum possible. Five overflows of the timer register TMR0 make up one note length.
A copy of the most significant bit of the timer register is maintained in (flags,tmrh) and if the flag is high when the timer MSB is low a rollover is deemed to have taken place. This check is done within the loop framed within the label "forever" and the instruction "goto forever".
The next note to be fetched is kept in count1. The instruction "call table" returns with the note delay in W. It is ORed with zero to check for the end of the melody. Then it is checked for the value One to check for a rest. If neither, the value in W is passed to the delay routine.
The program flow in the note generation loop has been equalised to take the same number of cycles for all conditions, except for the time that tmr0 rolls over. This is audible as a sort of ticking in the background.
The provided Hex file has been tested with a 10F200 and a 10F202 and found to work. The source code has the necessary changes to be made in order to make it suitable for a 10F204 or 10F206. It has also been tested with a 10F206.
A 10F220 or 10F222 could be used, but will need additional instructions to turn off the peripherals that are not used, and the fuse settings will also need to be modified.
Have fun, and do write if you manage to get a music maker to work. The eight pin DIP versions of these micros are available, and they are easier to handle, and they will work as well in this circuit.
| « Previous Step | Download PDFView All Steps | Next Step » |
![]() |
Add Comment
|
























































