Thursday 4 October 2018

Another day, another AVR

"Obsession is the wellspring of genius and madness."
Michel de Montaigne

The original clock project is about to receive a major overhaul. Firstly the code for that clock is a mess - it doesn't do interrupts at all well, so the user has to hilariously wait for a full cycle to complete before the clock wakes up when the slide switch for "set time" is toggled. This leads to a dreadful disquieting delay where you have to wonder if the little blighter is going to respond to a simple and necessary request.

The code overhaul also comes at a time (pun intended) when new chips have arrived from 中国 in deepest darkest Tasmania - the ATmega8. We believe it was the original chip used in the Arduino, followed by the ATmega168 and finally the ATmega328 which is what the clock project has used previously in a standalone version.

Why change chips? The ATmega328 is going up in price, currently around AU $19.01 for 10 pieces, whereas the ATmega8 in the same form factor is around AU $9.29 for the same quantity. The interesting (!) part of the revamp will be whether the new code will fit in a "smaller" chip - the ATmega328p has 32k RAM, 2048b SRAM and 1024b EEPROM, whereas the ATmega8 has "only" 8k RAM, 1024b SRAM and 512b EEPROM. Won't it be hilarious if the new code is too chunky to squeeze into that space? Although it is true that our recent dabbling in the dark arts of avr assembler might help shrink parts of the code.

The start of the project involved the usual ubiquitous "blinky" code (the AVR equivalent of "Hello World") - i.e. can we get any code to work on the Atmega8? Well within 5 minutes it seemed we had bricked the first chip whilst trying to burn a bootloader on the assumption that it didn't come from the factory with an installed bootloader. However, it did come with a bootloader and now wasn't talking at all.

Quite a few hours of reading and a fair bit of swearing over the next 3 days resulted in finding and following the definitive guide to ATmega8 bootloading (thanks ElectroNoobs), with just the one twist in the tail. We didn't want an external 16Mhz crystal oscillator driving the chip, but rather we would prefer the internal 8Mhz oscillator, since in the clock project the timing aspect is taken care of by an external DS3231 module. But we set up the oscillator anyway, just to establish lines of communication:


New bootloader installed and functioning
Following the resurrection after the requisite three days of uncertainty, the next step was to reload the bootloader via the avr isp programming shield and re-burn the chip with the appropriate settings (internal 8Mhz) as shown below:

So many options - but the 8Mhz is the key
Finally a little blinky magic and we are back in action.



But why always with the stupid blinky? Well, it's easy to establish if there is communication with such a simple program. It's visual if you don't have an oscilloscope, and timing is easy to check to see if the fuses have been set properly. Oh, and it's pretty.


So now onward to rewriting the clock code, but first a little relaxation and reflection with Oskar at Kingston Beach - noice!


Oskar seeks inspiration overlooking his temple



No comments:

Post a Comment