Tuesday, November 22, 2011

How to run a bare ATMEGA328-PU with an internal oscilator (at 8Mhz)


Arduino Ecosystem rocks
Arduino is a great way to minimize the amount of variables when getting into microcontrollers. Flashing, cross-compilation, libraries, dongles are much easier(and cheaper!) when standardized.

I bought an expensive  arduino nano v3, I got comfortable enough with it to blink an led and do with pwm. Unfortunately arduinos(except lilypad) feature lame 5V voltage regulators which require 6.2V to do anything useful; though atmega chips will supposedly run at as low as 1.8V. End result was that the NanoV3 did not run properly when hooked up to my 5V power supply on the bike.

So in the in the interests of not building a new power supply and not using a board with a whole bunch of pointless components I bought some straight-up ATMEGA328-PUs and a USB flasher.

The rest of this post documents various gotchas.

Pinout on Sparkfun is mirrored!
Red stripe on the cable indicates top. However the pinout is mirrored. One is supposed to look at it with the stripe on top and holes facing away.

ATMEGA328-PU ~= ATMEGA328P-PU
I wondered why seemingly identical chips were selling for a different price. P in 328P stands for picopower and as far as I can tell it allows one to turn off brownout protection. In my case brownout protection is something I want. However, fucking avrdude does not recognize ATMEGA328(without P) and bitches that one should pass -F when specifying -p atmega328p(there is no atmega328). This prevents Arduino IDE from functioning. In order to get a functioning IDE I needed to:
  1. Modify signature for atmega328p in avrdude.conf:

    - signature       = 0x1e 0x95 0x0F;
    + signature       = 0x1e 0x95 0x14;

    (Obviously the proper thing to do here is to add the new chip to avrdude)
  2. Use breadboard.zip from http://arduino.cc/en/Tutorial/ArduinoToBreadboard as an inspiration for the following boards.txt entry:

    usbtiny328.name=[usbtinyisp]ATmega328 8mhz

    usbtiny328.upload.using=usbtinyisp
    usbtiny328.upload.maximum_size=32768

    usbtiny328.build.mcu=atmega328p
    usbtiny328.build.f_cpu=8000000L
    usbtiny328.build.core=arduino:arduino

    usbtiny328.bootloader.low_fuses=0xE2
    usbtiny328.bootloader.high_fuses=0xDA
    usbtiny328.bootloader.extended_fuses=0x05
    usbtiny328.bootloader.path=arduino:atmega
    usbtiny328.bootloader.file=ATmegaBOOT_168_atmega328_pro_8MHz.hex
    usbtiny328.bootloader.unlock_bits=0x3F
    usbtiny328.bootloader.lock_bits=0x0F

  3. Burn bootloader with tools->burn bootloader->UsbTinyIsp
  4. Assemble board as per http://arduino.cc/en/Tutorial/ArduinoToBreadboard. One does not need to hook up GND(pin 22) or AVCC(pin 20). Connect ATMEGA RESET(pin 1), VCC(pin 7), GND(pin 8), MISO, MOSI, SCK(pin 19) to the flasher.
  5. Check the pin mapping. Note that Arduino software pins do not match physical pins(this cost me a few hours).
  6. Proceed to use Arduino tools with a convenient $2.82 chip* + $15 programmer rather than an inconvenient/overpriced Arduino board.
     * I would've spent an extra $1 to get the ATMEGA328P had I known the many hours of agony it would've avoided.


      1 comment:

      1. I found it really useful to use Optiloader.
        https://github.com/WestfW/OptiLoader

        This is a sketch you can push to a regular Arduino, which turns it into an chip programmer, with auto-detection support which includes the ATMEGA328-PU (without Pico-Power). I've just flashed a load of cheap chips from Mouser this way with good results, as you can see at...
        https://vimeo.com/44535515

        ReplyDelete