25 3月 2016

Arduino; burning the bootloader

I bought some ATmega328, not 328P, for a new project. I think the trouble started here. There are lots of pages in Internet explain how to burn the boorloader onto a microcontroller using Arduino board. I followed those instructions and successfully burned the bootloader but I was confused by those instructions at the same time. So here is yet another instructions.
  • ATmega328 (not 328P) without bootloader
  • Arduino Uno
  • Arduino Leonardo (as ISP)

Wiring

Use jumper wires to connect two boards.


Leonardo - Uno
10 - Reset
11 - 11
12 - 12
13 - 13








Use Arduino as ISP

I used Arduino IDE 1.6.8 on a Linux box. It is the latest version (Mar/2016).
  1. Open a sketch
    File menu -> Examples -> 11.ArduinoISP -> ArduinoISP
  2. Select a board
    Tools menu -> Board -> Arduino Leonardo
  3. Select a port
    Tools menu -> Port -> /dev/ttyACM0 (Arduino Leonardo)

Because I did not use ISP connector (2x3 pins), I uncommented the following line (line 81) in the source code.

#define USE_OLD_STYLE_WIRING

Then click Upload button to upload the sketch to Leonardo.

Burn the bootloader

I had to edit boards.txt (arduino-1.6.8/hardware/arduino/avr/boards.txt) to  burn the bootloader and to build/upload a sketch later. I modified "Arduino/Genuino Uno" entry to support both 328P and 328.
  1. Comment out the line.
    # uno.build.mcu=atmega328p
  2. Add the following lines.
    uno.menu.cpu.atmega328p=ATmega328P
    uno.menu.cpu.atmega328p.build.mcu=atmega328p
    uno.menu.cpu.atmega328=ATmega328
    uno.menu.cpu.atmega328.build.mcu=atmega328
Then you will see new menu item if you select "Arduino/Genuino Uno". (If IDE is running, quit and run again to take effect.)
  1. Select a board
    Tools -> Board -> Arduino/Genuino Uno
  2. Select a processor
    Tools -> Processor -> ATmega328
  3. Select a port
    Tools -> /dev/ttyACM0 (Arduino Leonardo)
  4. Select a programmer
    Tools -> Programmer -> "Arduino as ISP"
  5. Burn
    Tools -> Burn Bootloader

Upload a sketch

    Somehow if you select Processor: "ATmega328" and upload a sketch, it failed.
    avrdude: Device signature = 0x1e950f
    avrdude: Expected signature for ATmega328 is 1E 95 14
             Double check chip, or use -F to override this check.


    It said device signature was wrong. 0x1e950f is 328P's device signature and as message said 0x1e9514 is 328's device signature. I was confused but I just tried to build/upload with "ATmega328p", and it succeeded. A sample sketch worked okay too.

    This is one thing I still cannot understand. Optiboot overwrites device signature? But anyway I can now upload a sketch to ATmega328 using Uno board and it is ready for a new project.

    Appendix) Failed attempt

    At first, I used a breadboard instead of Uno. And selected "Arduino Duemilanova or Diecimila" board when burning a bootloader. But it failed because of device signature mismatch.
    avrdude: Device signature = 0x1e9514
    avrdude: Expected signature for ATmega328P is 1E 95 0F
             Double check chip, or use -F to override this check.


    In boards.txt, build.mcu is defined as below.
    diecimila.menu.cpu.atmega328.build.mcu=atmega328p

    I guessed it was the cause and removed 'p'.
    diecimila.menu.cpu.atmega328.build.mcu=atmega328

    Then it successfully burned and 13pin/led started blinking. I removed the ATmega328 from a breadboard, placed it on Uno board and uploaded a sketch. But I got the following error.
    avrdude: stk500_recv(): programmer is not responding
    avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x00


    I checked boards.txt again and I thought it was because Uno's build.mcu was ATmega328P. And so removed 'p' again.
    #uno.build.mcu=atmega328p
    uno.build.mcu.atmega328

    But I still got the same error.

    I was about to give up and I almost ordered new ATmega328P, but I found the way to burn ATmega328 using Uno board. I have to lean some more basics of a microcontroller, a bootloader and a board...

    0 件のコメント:

    コメントを投稿