3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Poor man's AVR music box

Step 2Source code explained

The program flows as follows:

Start:
Clear registers
Set data direction on ports of interest

Loop0:
if no input, produce no output
if input, continue

Note0:
is input=note0?
if so, load values into timer registers that let the output functions produce the correct square wave frequency
if not, continue

(Produce a NoteX loop for every possible note over 8 octaves)

Hi:
raise output high (1)

timerhi:
defines how long the output stays high

lo:
Render output to low (0)

timerlo:
defines how long the output stays low (0)

input:
check to see if the input has changed
if so, use dirty tricks to get back to loop0
if not, go to hi

There are many "rjmp islands" that let the program return to the start... this microcontroller does not support the jmp opcode, and rjmp can only bring you to a function within a distance of a certain number of instructions. We solve this by "seeding" the program with rjmp statements that are skipped on the way "down" to program, but can be used to climb back up to the program start.

BUG WARNING: Somewhere 3/4 the way down the scale, there is a cluster of a few (3-4) notes that aren't quite right. Out of the 3 values used for r30, I suspect the highest one used is incorrect (too low), I probably miscalculated the number of clock cycles somewhere, or perhaps put a wrong value for r29 somewhere... I'm just going to work around it, fix it if you like (and tell me where the mistake was!)

INEFFICIENCY WARNING: I know, I could have just used PWM and some EEPROM table, but by the time I thought of it, it was already working this way.
« Previous StepDownload PDFView All StepsNext Step »

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
10
Followers
7
Author:legionlabs
I publish my failures and my successes, as my teachers have done before me. I am a member of Foulab, an independent, nonprofit research and engineering group in Montreal. Check out our webpage at...
more »