Carlsbad Cubes Wolf Paulus

embedded

Arduino, an open-source platform, Pt.3:
ATmega 8 / 168 internal clock and fuse settings
By Wolf Paulus <wolf@wolfpaulus.com>

Posted Thursday, December 28th, 2006

Considering the already low price of the Arduino board, building an Arduino-like board yourself probably doesn't save you any money but is certainly a fun and education project to do.
I tried to make the simple board even simpler, by using a Serial instead of an USB Port, the ATmega's internal clock instead of an external crystal, and requiring 5 VDC instead of providing any kind of power regulation. However, my board does expose the 6 header ICSP, equivalent to the one available on the original Arduino NG Board.
RadioShack's Multipurpose 417 Holes $1.79 PC Board (Model: 276-150 Catalog: 276-150) built a great starting point for this electronics project, which was obviously inspired by Tom Igoe's 'Arduino Breadboard'.
my simple and cheap arduino board
After soldering the components onto the board and checking all joints, it was time to put the Arduino bootloader into the Flash Ram, which raised a couple system / software related questions I hadn't thought much about before: Let's address the last question first and take a look at the fuse bytes. The content of those fuse bytes persists reprogramming of the Flash memory and almost every bit in those fuse bytes represent some kind of status setting that configures the microcontroller, for instance to work in concert with other components on the board. Oddly enough, for all fuses '1' means unprogrammed while '0' means programmed.

ATmega's con- fus(e) -ing settings

ATmega8 Fuse High Byte
Bit Name Description Default (0xD9) Custom Arduino (0xCA)
7 RSTDISBL Select if PC6 is I/O pin or RESET pin 1 (unprogrammed PC6 is RESET-pin) 1
6 WDTON WDT always on 1 (unprogrammed, WDT enabled by WDTCR) 1
5 SPIEN Enable Serial Program and Data Downloading 0 (programmed, SPI programming enabled) 0
4 CKOPT Oscillator options 1 (unprogrammed) 0
3 EESAVE EEPROM memory is preserved through the Chip Erase 1 (unprogrammed, EEPROM not preserved) 1
2 BOOTSZ1 Select Boot Size 0 (programmed) 0
1 BOOTSZ0 Select Boot Size 0 (programmed) 1
0 BOOTRST Select Reset Vector 1 (unprogrammed) 0
ATmega8 Fuse Low Byte
Bit Name Description Default (0xE1) Custom Arduino (0xDF)
7 BODLEVEL Brown out detector trigger level 1 (unprogrammed) 1
6 BODEN Brown out detector enable 1 (unprogrammed, no BOD) 1
5 SUT1 Select start-up time 1 (unprogrammed) 0
4 SUT0 Select start-up time 0 (programmed) 1
3 CKSEL3 Select Clock source 0 (programmed) 1
2 CKSEL2 Select Clock source 0 (programmed) 1
1 CKSEL1 Select Clock source 0 (programmed) 1
0 CKSEL0 Select Clock source 1 (unprogrammed) 1
AVR ATmega 8 Boottime
AVR ATmega 8 Startup Time
AVR ATmega 8 Internal Clocking
AVR ATmega 8 External Clocking

Looking at my slightly reconfigured Arduino board, I chose the following fuse settings:
High Fuse: 0xDA and Low Fuse: 0xD1, which leaves the ATmega's Oscillator options at their defaults while allowing the use of the EEPROM and also sets the boot size and boot vector like needed for the Arduino bootloader.

This brings me back to the other question:

How to address the speed difference, to keep software changes to a minimum?

The frequency the ATmega chip is running at, is hardcoded into the bootloader (ATmegaBOOT.c or ATmegaBOOT_168.c). While it is a configuration parameter in the Makefile, putting a bootloader on an Arduino-like board that does not run at 16.0MHz requires adjusting the Makefile and rebuilding the bootloader.
The best place to get Makefile and bootloader source for the ATMega8 is http://svn.berlios.de/viewcvs/arduino/trunk/bootloader/ the same for the ATmega168 can be found here: http://webzone.k3.mah.se/projects/arduino-workshop/upload/default.asp?folder=40

The ATmega8's Makefile defines DEFS= -DF_CPU=16000000 -DBAUD_RATE=19200 which needs to be modified to DEFS= -DF_CPU=1000000 -DBAUD_RATE=19200
For the ATmega168, make needs to be called with the proper command-line parameters: make PRODUCT=CRUMB168 AVR_FREQ=F1000000 all

The last thing that needs to be done is adjusting the Arduino IDE preferences file: ~/Library/Arduino/preferences.txt on the Mac and C:\Documents and Settings\<USERNAME>\Application Data\Arduino\preferences.txt on Windows:
The build.f_cpu preference needs to be set to 1000000L

For completeness, here are the ATmega 168 fuse bytes, (a couple bits more a.k.a. 'Extended Fuse Byte', compared to the ATmega 8).
ATmega168 Extended Fuse Byte
Bit Name Description Default Custom Arduino
7..3 not used N/A 1 1
2 BOOTSZ1 Select Boot Size 0 (default value of BOOTSZ1..0 results in max. Boot Size 0
1 BOOTSZ0 Select Boot Size 0 (default value of BOOTSZ1..0 results in max. Boot Size 0
0 BOOTRST Select Reset Vector 1 (unprogrammed) 0
ATmega168 Fuse High Byte
Bit Name Description Default Custom Arduino
7 RSTDISBL External Reset Disable 1 (unprogrammed PC6 is RESET-pin) 1
6 DWEN debugWIRE Enable 1 (unprogrammed) 1
5 SPIEN Enable Serial Program and Data Downloading 0 (programmed, SPI programming enabled) 0
4 WDTON Watchdog Timer Always On 1 (unprogrammed) 1
3 EESAVE EEPROM memory is preserved through the Chip Erase 1 (unprogrammed, EEPROM not preserved) 0
2 BODLEVEL2 Brown out detector trigger level 1 (unprogrammed) 1
1 BODLEVEL1 Brown out detector trigger level 1 (unprogrammed) 1
0 BODLEVEL0 Brown out detector trigger level 1 (unprogrammed) 1
ATmega168 Fuse Low Byte
Bit Name Description Default Custom Arduino
7 CKDIV8 Divide clock by 8 0 (programmed) 1
6 CKOUT Clock output on PORTB0 1 (unprogrammed) 1
5 SUT1 Select start-up time 1 (unprogrammed) 0
4 SUT0 Select start-up time 0 (programmed) 0
3 CKSEL3 Select Clock source, default for CKSEL3..0 results in intern. 8MHz Oscillator 0 (programmed) 0
2 CKSEL2 Select Clock source 0 (programmed) 1
1 CKSEL1 Select Clock source 1 (unprogrammed) 1
0 CKSEL0 Select Clock source 0 (programmed) 1
AVR ATmega 168 Boottime
AVR ATmega 168 Clocking


Resources




QR code, to put this blog on your mobileDisclosure:
This blog is written and edited by me, it contains my words and my opinions only, and does not contain any content which might present a conflict of interest.
I am not compensated to provide opinion on products, services, websites and various other topics. This blog does not accept any form of cash advertising, sponsorship, or paid topic insertions. However, I will and do accept and keep free products, services, and other forms of compensation from companies and organizations. All advertising is in the form of advertisements generated by a third party ad network and identified as such.
I will only endorse products or services that I believe, based on my expertise, are worthy of such endorsement. Any product claim, statistic, quote or other representation about a product or service should be verified with the manufacturer or provider.

Published on: Thursday, December 28th, 2006  •  Category: [embedded]

Article URL : http://wolfpaulus.com/journal/embedded/arduino3.html