Juno AVR Library - Low Power button Watchdog sleep mode 600nA - Tiny POT PWM Driver (Attiny13)
Juno AVR Library is a huge set of know-hows on various parts of an AVR MCU written by advanced assembly and advanced C programming language. It covers absolutely everything but most of it is in fragmented note and code forms, so it's not a traditional library that can be copy-and-pasted. In fact, it's not really completed yet or more precisely will never be completed because attempting to finalize this project requires a huge amount of effort and time.
This is a demo on what an abridged Juno AVR Library is capable of. This is a rather simple example, but in every advanced level on every level, yet programming a MCU is really that deceptively simple. The hardware is an attiny13 connected to 1 LED and 2 buttons. Here 1 button turns ON/OFF the MCU. Once the MCU is OFF, it's in power-down mode when current consumption is about 500nA. Another button controls the LED blinking rate. This example complicates things to demonstrate the various mechanisms that an AVR MCU has to offer and it uses 460 bytes out of 1024 bytes from the attiny13.
Challenge 1: Buttons are simple, but coding buttons in sleep mode is not necessarily easy. Here the button is used to wake up the MCU, and also to put it into sleep mode. Watchdog is used as a debounce timer. Watchdog is deceptively simple here because it's coded by an expert. In reality, using the watchdog is the most tricky thing and should be avoided by beginners. During the 16ms debounce duration, the MCU is in power-down mode, this saves a tiny amount of energy, but still critical for a battery-powered device.
Challenge 2: Another not that obvious challenge for coding the buttons is how should the button be read or more precisely how the button is interpreted. Under different stages/modes/states the same button can be coded to do different things. The easiest and lower quality solution would be to declare all the required variables as global. In this demo, a lot of attention is put into declaring various types of variables in their optimized forms. The scope of each variable is perfectly coded using high quality C programming language.