EE 476: Laboratory 0
Learning the hardware and software.
Introduction.
This assignment introduces you to the software and hardware development tools
you will use this semester to work with the Atmel MEGA-series microcontroller
(MCU) chips. You will use the STK500 development board to run some C programs.
The STK500 pushbuttons, LEDs, and a serial connection to the PC will be used
to demonstrate basic compiler, i/o, and debugging techniques. You will run demo
programs, use a debugger, and make
minor modifications to the existing programs.
Hardware
The hardware you will be using to
support the MEGA-series is the Flash MCU evaluation board, a small board providing:
- A target microcontroller (mcu) with onboard flash program memory.
- A programmer for the target flash memory, including a programmer serial connection
to a PC.
- A separate serial connection for applications.
- Eight general pushbuttons, eight LEDs and a RESET button. For this lab, connect the LEDs and pushbuttons with jumpers as shown at the left below. You will need a 2-pin jumper for serial communication. You will also need to have a programming jumper as shown on the bottom-center. The crystal socket is shown with a crystal mounted. Make sure your board has a 16 MHz crystal. The top serial connector (left image) is the programming connector, the bottom one the application connector. In this lab, it is used for debugging. For more detail refer to the hardware manual. The rightmost image shows a closeup of programming, reset, power, analog reference and crystal select jumpers.
- For most labs, you will use 10-pin connectors connectors for bringing out the microcontroller signals to user-designed
peripheral equipment. Note that there is no protection on these lines. You
are connecting directly to the mcu. Prudent design suggests you might want
to use buffers when you are uncertain of voltage levels or other conditions.
The pin definitions for the connectors are shown below. The inner squares
represent pins. In the image above, pin 0 of each connector is at the top-left.
- There are cables connecting the 10-pin header plugs shown above to DIP plugs
to make it easy to connect to the usual white protoboards. The connection
pin-out is given below along with a picture of the cable attached to PORTB of the STK500 and to a white board..
In this lab you will use this cable to add an LED to the circuit as shown below. The positive lead of the LED is slightly longer.
Software
Software you will use is freely downloadable and consists of:
More information
Procedure:
- Make sure the evaluation board is connected to power and to the PC as specified
in the evaluation board description. Turn on the power supply with the switch
on the board. An LED in the middle of the board should cycle from red to yellow
to green. For this first lab, there should be jumpers on the PortB to the
LED header and on PortD to the switch header. Ask your instructor for help
if these are not installed. There should be a two wire connector from pins D.0 and D.1 to the RS232 jumper.
- Make sure the crystal on the STK500 is actually 16 MHz.
- There will two serial connections to the STK500. One for programming the Mega644 and one for serial communication between the running program and the PC, which will be running hyperterm. Set up hyperterm for 9600 baud, no parity, 1 stop-bit, no flow-control.
- Put all files on your
Z drive!
Do not creat a project on the local drive! Be sure to put all your files there and to back up daily!.
- There should be a shortcut to AVRstudio on the desktop or start menu.
- To invoke hyperterm on the PC, you will need to use
Start...Run...hypertrm
from the Windows Start
menu.
Connect hyperterm to whatever serial port the USB dongle configures. Use Control Panel...System...Hardware Tab...Device Manager Button...+Ports
to find out which serial port is connected to the USB dongle.
The STK500 serial port RS232 Spare
will be connnected to the dongle.
Set up hyperterm for 9600 baud, no parity, 1 stop-bit, no flow-control.
- After you define a new project, you can add a C source file and edit it.
- Save this code into your
z drive
. This program blinks LEDs and
responds to buttons. It is organized as three task subroutines. You will
modify this code in the assignment below.
- In the AVRstudio
Project
menu choose Project Wizard
. When the dialog box
appears, choose New Project
.
- In the new dialog box choose a GCC project, give the project a name, and indicate a source file to use. Choose a location on your Z drive to store the project.
- In the next dialog box choose to debug with
AVR simulator
and choose ATmega644,
then press Finish
- In the menu item
Project-> Configuration Options -> General -> Frequency
enter 16000000 because the crystal frequency is 16 MHz.
- Paste this code into the newly created source file.
- Choose the menu item
Build
from the Build
menu. A message window will open to tell you if
there are errors in the code.
- If the compile is successful, you should be
able to download the program to the STK500 board and see some blinking
LEDs.
To download, select the Tools>Program AVR>Autoconnect
menu item which will open a dialog box. In the dialog box:
- In the
Main tab
, set the ISP frequency (Settings
... dropdown) to 57 Khz, then press Write
then Close
.
Make sure the device is Mega644
.
- In the
Program Tab, Flash box
, select the hex
file generated by the compiler. The file will be in the
project folder>default folder
. Press the Program
button. If the program does not download to the chip, call a TA. Note that the hex file name is NOT unpated for you and will default to the last project compiled on the computer.
- If the Mega644 is new (unprogrammed):
- Go to the
Fuses
tab in the programmer window and set the SUT_CLKSEL
fuse to:
Ext crystal osc 8- MHz; startup time: 16k clk+65 mSec.
- Uncheck the
CLKDIV8
box.
- Uncheck the
JTAGEN
box and answer YES in the confirming dialog. (If you leave the box checked, PORTC does not work correctly).
- Click
Program
- Timing of all functions in this lab, and every exercise in this course will be handled by interrupt-driven counters, not by software wait-loops. This will be enforced because wait-loops are hard to debug and tend to limit multitasking. Specifically, you may not use the delay library routine
delay_ms
. Using delay_us
will be acceptable when you need to produce delays on the order of a few microseconds.
Assignment
- Using the short demo code downloaded above, modify the code:
- To stop blinking LED 1 when button 6 is pushed.
- To blink 8 times faster.
- Add an LED and 300 ohm resistor as shown above. Make your program blink this LED rather than the one on the STK500.
- Make a separate version of the code which blinks all eight LEDs in sequence at a rate of 4 LEDs/second.
- Show all these to your TA.
- Using the assert command .
- Download AssertGCC644.c, uart.c and uart.h and put them in the same folder. Build a new project with
AssertGCC644.c
and uart.c
as source files. The assert macro tells you what condition failed, the function, and the source file. This utility can very useful for debugging and is often better than just using printf because conditional logic is included and because all asserts in a program can be removed by inserting the macro #define NDEBUG
before including assert.h
.
- Modify the demo code's task 3 to assert when you push button D.6.
- Modify the demo code's task 2 to assert after ten seconds. The printed message might be something like:
Assertion failed: (time<10), function task2, file ../AssertGCC644.c, line 90.
- When you are eady to remove asserts (for a fully debugged code) insert the macro
#define NDEBUG
before including the assert.h.
- Show all these to your TA.
There is NO written lab report for this exercise, but your attendance is mandatory and your performance in lab will be included in the lab 1 grade.
Copyright Cornell University Jan 2009