So I Got a Butterfly – Now What?
Total Page:16
File Type:pdf, Size:1020Kb
So I Got a Butterfly – Now What? Chuck Baird avrfreaks.net, user name zbaird © 2009 by Chuck Baird Table of contents: Introduction..............................................................................................1 Features....................................................................................................2 The bootloader.........................................................................................3 The demo application program................................................................3 Getting your Butterfly to do something...................................................4 But wait, there’s more..............................................................................4 Adding some goodies to your Butterfly...................................................5 Programming (uploading to) the Butterfly ..............................................6 An AVR Studio – Butterfly bootloader session ......................................7 Other programming methods...................................................................9 The AVR core........................................................................................10 Assembly, and C, and other languages, Oh My! ...................................10 Something to play with..........................................................................11 Butterfly (and other) resources ..............................................................12 Summary................................................................................................14 Introduction This guide was written to help the novice user understand and appreciate the potential of the AVR Butterfly, Atmel’s “demonstration and evaluation kit” for their line of LCD capable 8 bit processors. The term “AVR” allegedly means nothing more than a marketing term for the product line, although it coincidentally happens to be the initials of the inventors. At the time of this writing the Butterfly retails for about $20 in the United States. We will take a look at the Butterfly board and its complement of peripherals, the microcontroller that provides its “smarts,” how to upload new programs into it, and some of the documentation and support resources that are available for it. We will only be able to touch the high points to get you started; there is much more that we will omit but which you can discover with a little searching and digging. Always remember, Google is your friend. If this is your first foray into the world of embedded computing, enjoy the trip. These small, general purpose computers are ubiquitous, assisting you hundreds of times a day in ways you never dream of. The Butterfly will give you a glimpse into their array of capabilities. Butterfly Tutorial C. Baird Page 1 of 14 Features Photo 1 shows the front view of an older model Butterfly when they were shipped with a light sensor (an LDR, or photo resistor, shown in the upper left hand corner). Because of RoHS restrictions against cadmium the sensor is no longer mounted, although you are certainly free to add one yourself since all the support circuitry is still included. The small holes around the left and bottom edges of the board are for connecting various external circuits to the Butterfly. They use 0.1” spacing, so standard headers may be soldered in, or wires can be soldered directly to the board. However, nothing external has to be connected to the board for it to function right out of the box. Photo 1: The mighty Butterfly (early model with LDR) The Butterfly’s 6 character, 100 segment LCD is driven directly by the microcontroller – there is no separate LCD controller on the board. Other features of the Butterfly include a 5 position joystick (shown on the right hand side of Photo 1), a piezo element for making noise (or music, depending on your tastes), a 4 megabit nonvolatile dataflash memory, a temperature sensor, and RS-232 level converters for serial communications. It can read voltages (0 – 5v) and has a 32 KHz crystal for real time clock applications. And, as mentioned, it can read a variable resistance where the light sensor was designed to be mounted. The Butterfly’s processor is an Atmel ATMega169 8 bit RISC MCU (reduced instruction set microcontroller unit) powered by a 3 volt coin battery (CR2450). The MCU includes 16K bytes of flash memory (for program storage), 1K bytes of static RAM (for variable storage), and 512 bytes of EEPROM (for nonvolatile variable storage). It has 3 timer/counters, an 8 channel 10 bit A/D converter, an analog comparator, watchdog timer, sleep modes, and more. The ATMega169 datasheet (see the Resources section) details all the features. As shipped, the Butterfly is preloaded with a bootloader and an application demo program that shows off some of its tricks. The battery is included, but has a cardboard Butterfly Tutorial C. Baird Page 2 of 14 spacer which prevents battery drain prior to it reaching the end user. Removing the cardboard spacer should bring your Butterfly to life, although it may not appear to do anything until you press the joystick up and wake it from its nap. See the section below, Getting your Butterfly to do something . The bootloader A bootloader is a small program that can accept input, usually a ready-to-run program, from some source and write it into the memory of the computer the bootloader is running on. Typically a bootloader will either automatically or in response to some command jump to (execute) the new program after it writes it to memory. The name comes from the phrase “to lift yourself up by the bootstraps,” an idiom of unknown origin describing an impossibility but meaning to improve your situation by your own efforts. It was borrowed by the early computer geeks to mean getting a tiny program started which progressively brings in a larger program, and is the origin of the phrase “to boot a computer.” The Butterfly comes with a preloaded serial bootloader program. It also comes with a preloaded demo application, a separate program. The two programs reside in different parts of the ATMega169’s flash memory. The Butterfly’s bootloader is called a “serial” bootloader because it receives its commands and code to write to memory from the serial, or RS-232, communications line on the Butterfly. This is a standard method of computer communications initially used by almost all personal computers, although USB has pretty much replaced it in recent years. If your PC does not have a serial port (a place to plug in an RS-232 connection), you will need to buy a USB to serial converter to use the Butterfly’s serial bootloader. Or find a PC that does have a serial port. There are other ways to program a Butterfly, and we will briefly mention them later. For now we will just concentrate on the preloaded serial bootloader. The demo application program On the Butterfly’s box is a flowchart of the demo program. It is also reproduced in the Butterfly document (see Resources ). Each white rectangle represents an activity or choice presented as a menu item on the LCD. Until you get used to poking the joystick correctly you may find yourself jumping around to places in the flowchart other than where you expect. As you see from the diagram, you can set or read the time-of-day clock, play some music, enter text (via the joystick or the serial line) for display on the LCD, read the temperature, light sensor (if present), or an external voltage (0 – 5v), and set various options. When the joystick is inactive for a certain length of time, the demo program puts the MCU to sleep to conserve the battery. The demo program is a large and sophisticated piece of software, and its C source code has been published by Atmel. The program was converted (“ported”) to WinAVR C Butterfly Tutorial C. Baird Page 3 of 14 (more on that later) and that version is also available on the internet. Reading through the code, if you are interested in such things, will teach you a lot about how to program the Butterfly and AVRs in general. Atmel has also made the Intel hex file of the demo program available on its website. You may reload it into your Butterfly to restore it to its pristine state. Getting your Butterfly to do something When you apply power to your Butterfly it resets the MCU. This is a design feature of the AVR line and most other embedded processors. It is also possible to reset an AVR without removing and restoring power, and later we will see how to add a reset button to your Butterfly. Thus pulling the battery out and reinserting it causes a reset, as does that initial removing of the cardboard spacer isolating the battery on a new Butterfly. On a reset, the ATMega169 starts executing code in one of two places: at the start of the application section of memory, or at the start of the bootloader section of memory. The application section of memory starts at a fixed address (0x0000), and the bootloader section can start at one of four different addresses depending on how we have set things up. How do we tell it which we want? There are some indicators (“fuses”) in nonvolatile memory which hold our choices. If a certain fuse called BOOTRST is programmed one way, a reset jumps to the application section. If it is programmed the other way, a reset jumps to the bootloader section. It is our responsibility to have previous loaded code at the proper address for the way we have set the fuse(s). Perhaps needless to say, the Butterfly’s serial bootloader has been preloaded into the bootloader section of memory, and the demo application has been preloaded into the application section. The BOOTRST fuse has been programmed to make a reset jump to and execute the bootloader, or at least it should be . The bootloader needs to know whether you are wanting to upload a new application or run the existing application. On the Butterfly the one source of easy user input is the joystick, so the bootloader waits for you to press the joystick up (jump to the application) or in (start uploading a new application using the serial communications line). If it doesn’t see either joystick press within a certain amount of time, it goes to sleep. That is, it stops executing instructions and enters a low power mode.