Page 1 of 1

Setting up a new ATmena328P

Posted: Sun Sep 29, 2019 2:14 am
by Brissieboy
If you obtain a new ATmega328P it will not generally work in an Arduino or programme via a programmer such as the USBasp using the Arduino environment without some pre-configuration. Not all the default settings in a new device are what is used in a Uno.
This is an attempt to explain what needs to be done. The information is available on the web but can be a little hard to find. This description applies specifically for a Windows PC, but the principles are the same in other environments.

WARNING: you need to be careful when playing with these fuse settings as an incorrect setting could render the device useless.

To set up a new ATmega 328P that will not programme using the Arduino environment and/or a USBasp, do this:
Install the new device in the target board and connect to the PC USB using an ISP (such as a USBasp) connected to the ICSP header on your board.
Open a command window (using CMD)
Go to the directory which contains avrdude.exe. On a PC this is probably:
C:\Program Files (x86)\Arduino\hardware\tools\avr\bin>
and enter the following:
avrdude.exe -C "C:\Program Files (x86)\Arduino\hardware/tools/avr/etc/avrdude.conf" -v -B 10 -c usbasp -p m328p -U lfuse:w:0xFF:m -U hfuse:w:0xDE:m -U efuse:w:0x05:m

What these avrdude options are for:
-C : location of the configuration file "avrdude.conf" (may need to change this if not a PC or custom installation)
-v : sets verbose level
-B : sets the bit clock period (10 required here)
-c : sets programmer ID
-p : use port to identify the device to which the programmer is attached
-U lfues : low fuse byte
-U hfuse : high fuse byte
-U efuse : extended fuse byte

A good description of fuse settings is here:
Note that each fuse is 0 to set, 1 to disable or not set

The -U fuse settings above are those used by the UNO so may not be exactly what you want.
If you are using stand-alone ATmega328 boards and programming via an ICSP you could change the fuse setting to remove the programme space reserved for the bootloader by setting BOOTRST to 1 (not set) by replacing "-U hfuse:w:0xDE:m" with "-U hfuse:w:0xD9:m". This maximises the programme memory space (up to an extra 4k bytes). This can also be done with an Arduino Uno if programming via the ICSP header, you will no longer be able to communicate with the computer via the USB port as it removes the bootloader but makes that space available for your sketch.

lfuse:w:0xFF:m (= B11111111)
bits 0-3 set CKSEL which is the clock source selection. This should be B1111 (0xF) for Uno (low power crystal oscillator)
bits 4 & 5 set SUT1/SUT0 the start up delay to allow time for the crystal oscillator to stabilise - set to B11
bit 6 set CKOUT clock pulses appear on PB0 (pin 14) if - set to 1 disabled
bit 7 set CKDIV8 sets clock speed at oscillator divided by 8 - set to 0 enabled (for 1mHz)

hfuse:w:0xDE:m (= B11011110)
bit 0 BOOTRST enables reserved programme space for bootloader with reset vector then set to bootloader if enabled - set to 0 enabled
bits 1 & 2 BOOTTSZ0 & BOOTTSZ1 sets the boot loader memory size (set to B00 512 bytes)
bit3 EESAVE preserve EEPROM memory during chip erase (set to 1 disabled)
bit 4 WDTON watchdog timer always on (set to 1 disabled)
bit 5 SPIEN enables serial programming and data downloading (set to 0 enabled)
bit 6 DWEN debug wire enable (disabled)
bit 7 RSTDISBL disables the reset pin (not set)
A full list of avrdude parameters and their descriptions is available here:

efuse:w:0x05:m (= B00000101)
bit 0-2 BODLEVEL set the brownout level (set to 2.7V)
bits 3-7 reserved/not used

A new ATmega 328P has these default fuse settings:
Low fuse = 0x62 (B01100010)
High fuse = 0xD9 (B11011001)
Extended fuse = 0xFF (B11111111)

A UNO should have these fuse settings:
Low fuse = 0xFF (B11111111)
High fuse = 0xDE (B11011110)
Extended fuse = 0x05 (B00000101)

A fuse calculator is available here. Just select ATmega328P as the AVR part name.