Step 2Source code explained
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 Step | Download PDFView All Steps | Next Step » |
![]() |
Add Comment
|





































