Mech143 Lab Number 1 Due by 5:00pm on Friday, April 19, 2002 XXX

Laboratory Assignment Number 1 for Mech 143/ELEN123

Due by 5:00pm in lab box on Friday, April 19, 2002

Pre-Lab due by 5:00pm in lab box on Tuesday, April 16, 2002

Purpose: / This lab is intended to acquaint you with:
- 'C' programming for embedded systems
- How to use the MS Visual Studio editor, AVR-GCC compiler, and Atmel AVR Studio
- How to write, debug and run a simple C program on an embedded micro-computer
Minimum Parts
Required: / A breadboard and wire kit
Atmel STK500 (the embedded system development board which holds the AVR microcontroller)
50K Potentiometer
(2) 10-pin IDC socket connectors
10-pin ribbon cable
10-pin wirewrap connector (mates with the IDC connectors above)
References: / Your Course Notes: The Events and Services Framework.
The Atmega163 microcontroller datasheet (available on the course website or from www.atmel.com)
Handouts: / Lab1.c code

Pre-Lab:

Complete the following exercises AFTER you have read through the lab assignment and BEFORE trying to complete the lab.
0.1) After reading through the assignment, brainstorm what your state machine will look like for your Microwave oven. Draw a state diagram and show all Events and Services between your states. Describe what your events are and explain how you’re going to interface them to the microcontroller (atmega163). Remember that each service and state will have code associated with it. Write out what each service and state will do (your description does not need to be written in actual C code, but try to get each step of your ideas in there).
In the Pre-Lab: / Include your concepts from part 0.1

Part 1 Using The Development Environment

Reading: / Course Notes: The Ideal Users Manual (at the back)
Assignment: / This portion of the lab is designed to get you familiar with the software which will allow you to edit, compile, and download your code into the mega163 microcontroller which is on-board the Atmel STK500. Here are the software packages you will be using throughout the quarter to accomplish these tasks. You can find these and other useful software programs under Start Menu->Programs->Mech143.
1.  Microsoft Visual Studio editor
The MS Visual Studio editor is capable of editing many different kinds of code files with lots of useful features such as syntax highlighting (automatic color highlighting of your code), find-and-replace, auto-indent, etc. The Visual Studio editor not only edits files but can also help you maintain a “project”. Projects become useful when your code expands into multiple files and you wish to associate them to make editing easier. Finally, Visual Studio offers an interface to the AVR-GCC compiler. Unfortunately Visual Studio is not free, but there are plenty of free tools that you could use in place of VS in case you wish to work on code at home. Feel free to ask about these.
2.  The AVR-GCC ‘C’ compiler
In basic terms, a C compiler converts C code written by you into assembly code which a given CPU or microcontroller can directly understand. AVR-GCC is an extension of the popular GCC compiler originally made for unix. Although AVR-GCC runs on PCs and unix workstations, the machine language code it produces runs on Atmel AVR processors. Like most serious compilers, the version of GCC we use in lab runs only from a command prompt. Fortunately, clicking the “build” icon in Visual Studio automatically runs AVR-GCC is you have the proper project setup. (the provided Lab1 files already contain a pre-made project for you to use)
  1. Atmel AVR Studio
    AVR Studio is a multi-function program made to help develop and debug programs for the AVR series microcontrollers. We will be using AVR Studio primarily to configure the STK500 board and to program your compiled code into the microcontroller on the STK500.
4.  HyperTerminal
Yes, this is the same HyperTerminal that has been distributed with every version of Microsoft Windows since Win95. Because you can’t see what’s happening with your code while it’s running inside an AVR processor, we use a serial port connection to send debug messages from your AVR processor to the PC. HyperTerminal is simply a terminal interface to the serial ports on the PC. Lab#1 has examples of debug messages.
Now, to compile your first program, complete the following exercises:
1.1) Begin by getting the example Lab1 code from the Mech143 web page and opening it in the Visual Studio editor.
1) Create a new folder in your account to store your Lab1 code. We’ll use E:\mech143\lab1.
2) Get the Lab1 files from the course web page under the LAB link. Save the lab1.zip to E:\mech143\lab1. Double-click on the zip file in your account and unzip the files into your E:\mech143\lab1 directory.
3) You should now have a number of files including: AVRProject.dsw, AVRProject.dsp, and lab1.c. AVRProject.dsw is the master project workspace file, opening it will cause Visual Studio to open and load the relevant C files into the editor. Double-click on AVRProject.dsw now.
4) Visual Studio should now be open. Notice the “file-browser-like” project window on the left side of the screen. If necessary, click the FileView tab at the bottom of the project window and expand the Source, Header, and Resource Files folders. Double-click the lab1.c file under the Source Files folder. Lab1.c will open in the main editor window. Briefly get familiar with lab1.c. The code is heavily commented, so it should be fairly understandable. If you have questions about the syntax, please refer to your C book, ask a fellow Mech143er, or e-mail me. Peruse the other files too if you like.
5) Now edit the first rprintf() line in the main() section of lab1.c:
rprintf("Welcome to the Example Lab1.c events and services program!\n");
to print anything you like. When you finish editing, use the File menu to Save it (CTRL-S also works). Note that all files in the project automatically get saved when you run the Build step below.
6) To compile the project, select Build AVRProject.exe from the Build menu. You can also compile by pressing F7, or by clicking the build icon on the toolbar. You will be able to see the compiler output, including any errors, in the message window at the bottom of the Visual Studio screen. Note that although Visual Studio thinks the build will result in an *.exe file, the build actually creates lab1.hex. It is this lab1.hex file that contains your compiled code and may be programmed into an AVR processor.
1.2) Before attempting to program your Atmel STK500 board, you must make sure you have it properly connected:
- A serial cable must connect COM1 to the STK500’s RS232 CTRL port
- A serial cable must connect COM2 to the STK500’s RS232 SPARE port
- A 9-12VDC power brick must be plugged into the STK500 power jack
- Using the power switch at the corner of the board, turn on the STK500
- With power on, the red POWER LED should light up and the STATUS LED should cycle through colors until it settles on green. Your STK500 is now ready to be programmed.
1.3) To configure the STK500 and program your AVR processor, you will need to open AVR Studio. (You may use version 3.53 or 4.0. For programming, there’s no difference.) Click the little AVR chip icon in AVR Studio. AVR Studio will search for your STK500 board and present you with a programming window. Check the message area of the programming window to see if your STK500 was detected. You should see something like this:
Detecting.. STK500 found on COM1:
Getting revisions.. HW: 0x02, SW Major: 0x01, SW Minor: 0x07 .. OK
1.4) Now you’ll need to tell the programming software which AVR processor you are using, and which *.hex file contains your code. Select the Atmega163 from the Device drop-down list. In the Flash box, enter the path to the lab1.hex file you created above when you compiled your C code. There’s a browse button […] to help you find your *.hex file.
1.5) To program begin programming, press the [Program] button. You will see the various programming steps complete in the message area. Once programming completes, your code automatically begins executing.
1.6) Most embedded systems have no display or screen like regular computers. So where does the rprintf() output go? In the case of this lab, rprintf() has been configured to output your messages via the serial port. In order to see them you need to open a terminal program on the PC. Find the links to HyperTerminal under Start Menu->Programs->Mech143->HyperTerminal. Select the Direct COM2 – 38400 shortcut. When HyperTerminal opens, you should see the A/D Value: messages flying by. Touch the pins of Port A on the STK500 and the numbers should respond chaotically.
1.7) If you press the RESET button on the STK500, your program will restart from the beginning. You should see the debug message you entered earlier go flying by. Pressing the pushbuttons SW0 or SW1 should trigger the interrupt service routines. If you want, try experimenting with the lab1.c program to make the STK500 respond differently. After making changes, re-compile and re-program by clicking Build in Visual Studio and [Program] in AVR Studio. If there are errors in your code, the compiler will indicate which lines have problems in the message area of Visual Studio. Use CTRL-G in the editor to quickly locate an offending line. Repeat as required to remove all errors (Some compiler warnings may be present in the original lab1.c code. Don’t worry about removing these).
1.8) Congratulations! You’ve just edited and downloaded your first embedded program.
In the report: / No write-up required for Part 1.

Part 2 Events and Services in Action

Reading: / Lab handouts. Go through lab1.c before you do part 2.
Assignment: / Complete the Following Exercises:
/ 1.1) Now that you know how to load, run, and modify the program, it’s time to discuss how the hardware and software interact. The bottom of the STK500 has eight pushbuttons and eight LEDs which can be connected to the mega163’s ports and used as input and output devices respectively. Near the white socket area on the STK500 is a strip of 10-pin connectors labeled PORTA through PORTE. Each of the PORTA-D 10-pin connectors carries eight I/O lines plus power and ground. Next to the port connectors are the matching LED and SW (pushbutton) connectors, as well as the 2-pin RS232 SPARE serial port header. To make it easier to talk about port pins below and in the future, understand that PC0 refers to PORTC, pin 0, or PD5 refers to PORTD, pin 5. These shortened names are used on the STK500 too.
1.2) For this lab, notice that the LEDs have been connected for you to PORTC. The 10-wire ribbon cable provides an easy way to connect an entire port to all the LEDs. In this configuration you can see that PC0 connects to LED0, PC1 connects to LED1, etc. The LEDs on the STK500 are wired such that a LOW level (0V) on a port pin will cause then to turn on, a HIGH level (5V) turns them off. For an example of this, look at the lab1.c code.
1.3) The mega163’s serial port has also been connected for you. However, if you need it in the future, simply remember to connect PD0 and PD1 to RXD and TXD respectively.
1.4) Finally, two pushbuttons have been connected to PORTD. SW0 and SW1 connect to PD2 and PD3 respectively. If you look at the mega163 datasheet, you can see that we chose PD2/3 because they double as the INTERRUPT0/1 pins. Beware that the pushbuttons, like the LEDs, operate in negative or inverted logic. Pressing a pushbutton causes its corresponding pin to go from HIGH to LOW.
1.5) Since there are no potentiometers available on the STK500, you will have to connect one yourself in order to take control of the A/D converter input on PA0. The best way to do this is to wire up the potentiometer on your breadboard and then connect PORTA to your breadboard via a 10-wire ribbon cable. You should have all the materials in your lab kits to make up to three such cables. If you’re not quite sure how to do it, ask for help after lecture or drop in for office hours.
PLEASE: Turn OFF the power to the STK500 when adding or editing any connections to it!
(This includes rewiring your breadboard when the STK500 is connected)
Once you have a cable, plug one end into PORTA and the other end into your 10-pin wirewrap connector (digikey part no. CHW10G). The wirewrap connector has pins long enough to easily plug into your breadboard.
You must bend the wirewrap connector pins so that they straddle the divider on your breadboard, otherwise your breadboard will short out the power on the STK500 (and may cause damage to the STK500). See image on the left for the right way to do it.
Plug a 50K potentiometer (variable resistor) into your breadboard. The potentiometer, or pot for short, will have three pins in a triangle pattern. Use a few wires from your wire kit to connect the pot and your 10-pin wirewrap connector. The middle pin of the pot should go to PA0, while the outer pins should go to the power and ground also provided on PORTA. If you need help getting things connected, just ask.
When you’re done, things should look something like this:
STK500 ® PORTA connector ® 10-pin ribbon cable ® wirewrap connector ® breadboard ® jumper wires ® potentiometer
1.6) Now that you have everything connected. Run the Lab1 code again and try adjusting the potentiometer. You should see the reported A/D value go from 0 to 255 as you vary the pot across its full range. The pot is acting as an adjustable voltage divider which produces 0®5V across its range. This 0®5V is translated into a digital number by the A/D converter inside the mega163. The lab1code, requests an 8-bit result from the A/D converter so analog inputs of 0®5V map to a range of digital outputs of 0®255.
1.7) Now that you have a controllable input for the A/D converter, experiment with the interrupt handlers again.
If you press SW0 and a falling edge (from 5 to 0V) is detected on PD2 (INTERRUPT0), then the first interrupt service routine executes… PC0 goes LOW (~0V) for a while and then toggles LOW and HIGH five times at a fixed interval then goes HIGH (~+5V). LED0 blinks in response to PC0’s changing levels.
If a falling edge is detected on PD3 (INTERRUPT1), then… The program gets the value at the A/D port, and blinks LED1 either 3 or 5 times depending on the A/D value compared to 128. Adjust the potentiometer to test both cases.
Note: even though main() contains an infinite loop to print the A/D value, the interrupt handlers will suspend the CPUs normal execution of main() while they take care of responding to an interrupt. During this time, main is in a “locked-up” state. After the interrupt handler finishes, main() will continue to execute precisely where it left off.
1.8) Fairly painless, eh? That’s a small example of an events and services framework that does something simple: blink lights (and decide the number of blinks using the A/D) when an external event (a pushbutton press) triggers an interrupt.
In the report: / No write-up required for Part 2.

Part 3 Your Turn to Program and Interface with the ATmega163