content top

Arduino Pt.3

Arduino Pt.3

ATmega 8 / 168 internal clock and fuse settings

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‘.

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:

  • To be able to use either the ATmega8, which is used on the original Arduino, or the pin compatible ATmega168 microcontroller, which offers twice the amount of internal RAM, I had put a 28-Pin DIP Socket on the board. While the original Arduino board uses an 16.0MHz external crystal, my board relies on the internal clock, which for the ATmega8 means a 1.0MHz Internal RC Oscillator (slowly rising power) and for the ATmega168 an 8.0MHz internal RC oscillator, which by default however gets divided by 8, resulting in 1.0MHz system clock. How could I address the speed difference, to keep software changes to a minimum?
  • Since this obviously involved messing with the ATmega’s confusing fuse settings, what fuse bits do I have to adjust?

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

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 andC:\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

Resources

Read More

Arduino Pt.2

Arduino Pt.2

I have been playing with Arduino for one week now and really enjoyed the simplicity with which Wiring allows access to I/O ports. The comprehensible but mandatory structure of a program works really well as long as things are kept simple, while somewhat more advanced programs could certainly benefit from access to interrupts.
However, before making things more complicated on the Software side, I wanted to replace Arduino’s microcontroller chip (ATmega8) with the Atmega168-20PU ($4.11 at DigiKey), which most of all, doubles the amount of Flash Memory, the memory space, where binary versions of compiled programs end up.

Burn Baby Burn

The Arduino Mini board is already based on the ATmega168 and the development environment supports the ATmega168 as well. But how to burn the boot loader on to the bigger chip was still a bit of a mystery, at least to me.

Obviously, the microcontroller chips isn’t any bigger and comes in the same 28-lead PDIP packaging, and since it is not soldered on to the board but sits in a socket, replacing the chip is very straight forward and took only a couple of seconds.
Burning the boot loader on the other hand took a little longer. The Arduino board has the AVR typical 6 headers ICSP, an in system programming port, which allows burning a boot loader without removing the microcontroller chip from the board. Most ISPs (In-System-Programmers) still require an serial or parallel port but don’t work too well through USB-to-Serial adapters, not even with devices like the Keyspan HS19, and Mac users have to rely on AVR’s AVRISP mkII (ATAVRIPS2-ND), available for $34 at DigiKey.
The AVRIPS can program newer 8-bit RISC microcontrollers (with ICSP Interface) through the USB port. The target board still needs to be powered and its regular ports should be disconnected from other devices. Only after following Dr. T.C.P.‘s advice, disconnecting the HD44780 LCD, I was able to have the Mac communicate with the Arduino board through the ICSP.

Arduino with the original ATmega8 Mirco-Controller removed

Arduino Board with the ATmega168 chip in the socket

Arduino’s Development Environment uses the uisp tool for uploading binaries into the microcontroller’s flash memory. However, uisp doesn’t work directly through the USB port and therefore avrdude is our tool of choice. Unfortunately, the avrdude version that is included in the Arduino distribution (currently Arduino-0006) is somewhat outdated and doesn’t support the ATmega168 chip yet.
I’m not a big fan of Fink for compiling myself every tool ever needed and made my life a little easier, by downloading per-build avrdude 5.2 from theOSX-AVR project.

Finding the Boot Loader

The boot loader itself is not too widely available; I found it only here:

At this point, the microcontroller chip on the Arduino board had been replaced with the 16 KByte ATmega168 and everything but the ICSP port had been disconnected. The Arduino board itself was powered by a 7.5V 500mA power supply and the AVRISP-mkII connected the board with the Mac’s USB port.
On the software side, avrdude 5.2 was installed into /usr/local/bin and the ATmega168 boot-loader was made available here /AT168.hex

Arduino Board with AVRISP-mkII during the burn process

Burning and Fuses

Burning a new boot loader is a three-step process: unlocking the boot loader segment, uploading the new boot loader, and looking boot loader segment again. For an AVR microcontroller, the closest thing to a config file is its set of fuses. Fuses are used to configure important system parameters and a few important but obscure options can only be set through the ICSP. For instance, the ATmega168 defaults to the use of a 1 MHz internal clock and would ignoring the 16 MHz crystal on the Arduino board, if not told otherwise, through a fuse setting.

  1. Unlocking the boot loader segment, erase the chip, and set fuses (set boot loader size to 1024 words [=2KByte]: 0xE00-0xFFF, set clock speed to external 16 MHz)
    /usr/local/bin/avrdude -p m168 -b 115200 -P usb -c avrispmkII -V -e -U lock:w:0x3F:m -U hfuse:w:0xDF:m -U lfuse:w:0xFF:m -U efuse:w:0xF8:m
  2. Uploading the boot loader
    /usr/local/bin/avrdude -p m168 -b 115200 -P usb -c avrispmkII -V -D -U flash:w:/AT168.hex:i
  3. Re-locking the boot loader segment
    /usr/local/bin/avrdude -p m168 -b 115200 -P usb -c avrispmkII -V -U lock:w:0xCF:m

Arduino Board with the ATmega168

After removing the AVRISP mkII In-System Programmer and changing the power-selector on the Arduino board, it was time to fire-up the Arduino IDE. The IDE already supports the ATmega168 and it’s set by selecting atmega168 from the Tools/Microcontroller(MCU) menu. The blinky program was quickly uploaded and running – the IDE reported 14.336 bytes program memory available, which makes sense, considering that we set the efuse to preserve 2K = 1024 words for the boot loader.

Arduino IDE after uploading a program

What’s next

Like mentioned above, Wiring has a comprehensible but mandatory structure and allows uncomplicated access to all I/O ports. However, somewhat more advanced programs could certainly benefit from access to interrupts.
Let’s see if we can get the ATmega168 empowered Arduino to do C programmming, preferrably using an IDE like Eclipse.

Read More

Arduino Pt.1

Arduino Pt.1

Taking advantage of strong disagreements inside Germany, the Italian feudatories rebelled and, in 1002, elected Arduino, Marquess of Ivrea, king of Italy. He distinguished himself particularly because of his fights against the Bishop of Ivrea.

Today, Arduino distinguishes itself in price, capability, openness, and ease of use – but first things first.
Arduino is an open-source computing platform based on a simple board, and a development environment for writing software. The Arduino board hosts an Atmel MicroController chip, the AVR-ATMega8, which has 8-KByte self-programming Flash Program Memory, 1-KByte SRAM, 512 Byte EEPROM, 23 I/O pins, 6 or 8 Channel 10-bit A/D-converter, and 16 MIPS throughput at 16 MHz. The other IC on the board is an FTDI FT232RL, a single chip USB/Asynchronous serial data transfer solution, with 256 Byte receive and 128 Byte transmit buffer.

Considering that these two chips alone would cost you about $10, the $32 that Spark Fun charges for the fully assembled, RoHS compliant, and tested Arduino board looks like a real bargin. Moreover, like Tod points out, the Arduino looks even more attractive, when compared to the $119 Basic Stamp, which is another embedded computing platform, mainly for hobbyists and education. However, Arduino is actually two things, … Arduino is actually two things, a AVR-ATMega8 processor based hardware board and a Processing / Wiring based software programming environment, containing AVR GCC, AVR LIBC, and AVRlib.

Processing is an open source programming language and environment to program images, animation, and sound (initiated by Ben Fry and Casey Reas (UCLA Design | Media Arts).
Wiring builds on Processing, is a programming environment for exploring the electronic arts, teaching and learning computer programming and prototyping with electronics (initiated by Hernando Barragan, University of Los Andes Bogota, (Colombia)| Architecture and Design School).
Still, with Arduino you can also use real GCC C code to implement your algorithms and access all the hardware of the AVR chip, as long as you can make them fit into the 8 KByte onboard Flash Memory, which is actually more like 7,168 byte, considering the boot loader.

Small hack increases available memory by 6%

Arduino’s standard library still supports some legacy serial code, which can be removed by editing arduino/lib/targets/arduino/wiring.c (Arduino Version 0006):
Start commenting out code at line 241, insert /* just before void beginSerial(long baud)
This will reduce the size of a deployable program and increase the available memory for custom code by 450 Bytes, which doesn’t sound like much but is equivalent to 6.3%.

Adding an HD44780-compatible LCD to an Arduino board is as easy as can be. The Arduino-Playground has detailed information and there is even a 4-bit mode library, which would save 4 arduino port-pins for other uses.

What’s next

Like mentioned above, the Arduino board hosts an Atmel AVR-ATMega8 MicroController, which has 8-KByte self-programming Flash Program Memory, 1-KByte SRAM, and 512 Byte EEPROM.
If 8-KByte programming memory is not enough, there is fortunately a PIN compatible Atmel MicroController, the ATmega168, which has twice the Program Memory. However, even the ATmega168 board has only 1-KByte SRAM, which is the memory used for local variables, the call stack, etc.
Let’s see if we can get the ATmega168 to work on the Arduino board .

Resources

Arduino Workshop Tutorials, [Video Lectures]

Overview
Serial Ports
Soldering
Programming to the Chip
Additional Information
Read More

Tweak, Hack, and Bend Technology @ UCI

Tweak, Hack, and Bend Technology @ UCI

Later this week, I’ll be at the OCEJUGOrange County Embedded Java Users’ Group, to give a presentation titled: Innovate – Tweak, Hack, and Bend Technology, showing how a small device, about the size of an iPod Shuffle, but capable of executing Java-Byte-Code, is turned into a Web server. After taking a very close look inside the workings of a digital toy camera, we will connect it to the embedded device, creating a full featured web-cam.
Along the way we may add some more hardware and learn a few things about how CMOS sensors capture image data and how a Bayer-Pattern can be used to decode the sensor data.

The free OCEJUG event takes place on October 19th at the University of California, Irvine (Engineering Tower, Room 331 / ET331), starting at 6:30 PM.


Read More

Subscription TV

TIVO and ReplayTV introduced us to a new way of watching television, conveniently time-shifted watching. A disposable recording of previously selected content allowed us to consume the TV broadcast whenever we wanted; maybe just 15 minutes delayed, to skip over commercials, the next day, or whenever we felt was a good time to watch it. Anyway, time-shifted watching has changed the way we watch TV today and with the exception of sporting events, long gone seems the time of appointment TV, were millions would tune in to the same channel at the same time to consume the same broadcast.

An even greater change is just beginning to unfold, what TIVO did to the time of TV consumption, devices like the announced Apple iTV will do the the content that we are going to watch. While TIVO and ReplayTV were put between the cable-box and the TV-set, iTV connects your TV-set to the Internet and thereby enabling your TV to display content not governed by TV Networks or cable providers.
Watching TV may soon involve subscribing to a show (not available on any broadcast or cable channel, like a video Podcast for instance), which is then downloaded regularly to a computer at your home and available for you to watch whenever you like.

A couple of days ago, I had the chance to pick up one of the first Intel-based Apple MacMini, which come with an Intel Core Solo processor, 512 MByte of RAM and a 60 GByte Hard-drive; the perfect machine to be converted into an TV-Internet Connector and to try out, how Subscription-TV would work for me.

Connecting the MacMini to a TV

The first task was connecting the MacMini to the TV, which required an DVI-to-Video adapter. The DVI to Video Adapter connects the DVI port on the MacMini to any S-Video or Composite input on the TV-set. While S-Video is preferable over composite, my TV-set only has one S-video input, which was already occupied by the DVD-Player. Now, the MacMini would also serve perfectly fine as a DVD-Player but would enforce region codes, prevent playback of certain discs in certain geographical regions. A feature we successfully removed from the DVD-Player.

Connecting the MacMini to the Internet

Since the MacMini came with built-in wireless network support, (i.e. AirPort Extreme, an industry-standard, 802.11g-compatible wireless network adapter), connecting the MacMini to the Internet was easy and quickly done. Most time was probably spent, finding a good spot for the tiny computer, both close enough to the TV and also providing the best possible signal strength from the WiFI router.

Subscribing to Content

After connectivity to TV and Internet was achieved, it was time to subscribe and download some content. Since I already had subscribed to some audio and video Podcasts on my Laptop, getting content loaded on to the MacMini was as easy as exporting the subscription on one computer and importing it on the other. iTunes for instance imports and exports podcast-subscriptions in the commonly used opml file format.
With 60 GBytes, the MacMini’s harddrive-capacity seems to be sufficient, but still warrants some caution. Downloading only the most recent – and keeping only the last three episode from every subscription however is a reasonable configuration, which even a couple weeks down the road, should keep us out of trouble.
After importing the opml file and configuring iTunes’ Podcast settings, the MacMini started downloading content. Navigating the directory and selecting a Podcast for watching is fun and intuitive with the beautiful 10-foot user interface (the MacMini came with FrontRow built-in and an incredible easy to use six button remote control).

Maintaining the Headless MacMini

I think the MacMini fits aesthetically in almost every environment but I certainly did not want to have keyboard and mouse connected to the computer. A couple final configuration steps were necessary, to keep the MacMini in an almost maintenance free headless mode.

iTunes Podcast Configuration:

Enable Remote Login in the System Preferences’ Sharing settings, to be able to use a VNC client like Chicken of the VNC or RealVNC later.

Login Items (Application that run when a user logs in)
Since I have the MacMini configured to with automatic login enabled, these apps start whenever the MacMini gets (re-)started.

The Energy Saver is configured to put the computer to sleep after 15 minutes of inactivity. While iTunes is configured to check for new content once every hour, it will only do so, if the computer is running and not in sleep mode. Waking the computer once every night to have iTunes check for content seemed to be a good idea.

The revolution has begun

Watching TV on your own schedule makes it much more enjoyable. Being able to watch the content that you want and not that what the major networks want you watch, will fundamentally change not only the consumption experience but also all related industries, including production studios, major networks, and distributors.

[All Photos: (c) 2006 wolfpaulus.com]

Read More
content top