“Chiptunes and simple musical games on a 36 cent chip. Some assembly required.
You know those epoxy blob chips that play little chiptunes on musical greetings cards and dollar store childrens toys? Lets make those! Arguably, no one likes those things because they are annoying, but lets put that aside and ask another question what if we could use modern microcontrollers and sleep modes to make them work really efficiently so they can play for impressive periods of time? OK, maybe that just makes them annoying for longer but what if we could do it with a 36-cent microcontroller (attiny10) and very few external components, so we can reduce not just the energy cost of using the device, but also the energy that was invested in creating it, so you can pump out hundreds of them? Ok, maybe thats the last thing you would actually want in your life, but bear with me — what if we implemented it in assembly language so that you can really fit quite a lot of chiptune in 1kb of memory? Still not sold? Well, here we go anyway.
Build details are in the project log. I’d recommend starting at the oldest and reading towards the most recent.
Assembly code (including comments) is attached as a file. I’ve added a lot of code comments — most people asking online about assembly seem to be students struggling with an undergrad class. I’ve commented with that in mind.
If that describes you, then note that the general techniques I’ve laid out will probably work on whatever AVR your professor has chosen, but the specifics (such as register addresses) will need to change. The information you need to do this is in the datasheet for your chip — it’s highly unlikely your professor has chosen the attiny10 to teach a class!
Board design was very simple, so I just sketched it up in Inkscape. I’ve provided the file, already reversed for toner transfer.
While in deep sleep (waiting for the user to press the pushbutton), the circuit consumes about 100 nanoamperes (I measured this directly with a multimeter). This is negligible compared to the self-discharge of the CR2032 battery — mine are rated for 10 years, but ignoring that, the circuit would run for about 240 years in this standby mode.
During song playback, the chip will consume 350 microamps @3V while the MCU is in full operation at 1Mhz — however because of our code, it is only in that mode for a tiny fraction of the time. It almost always sits in idle sleep mode with watchdog timer enabled — which should consume on the order of 50 microamperes (~35uA for idle mode plus around 5uA for the watchdog timer and finally 10uA for timer/counter0). The waveform generation is done in hardware peripherals, letting us turn off the main MCU clock. I did not directly measure this for this project, it’s calculated from the datasheet (although I did measure this sleep mode in previous projects with the same chip and it matched the datasheet).
We also turn off all ADC hardware and internal pullup resistors, to save additional power.
The power consumption that remains is absolutely dominated by the piezo element. The one I used (not shown) was really awful in how quiet it was. I’m trying to dig up one of the piezo buzzers from a motherboard POST sequence to replace it.
Anyway, common piezos consume at most 10mA, which the pins of the Attiny10 are capable of directly sourcing (so we save a MOSFET).
As such we can round the power consumption to be very approximately whatever the consumption of the piezo element is. Expect at least 21 hours of playback time from a CR2032, and arbitrarily long standby time.”