EE 476: Laboratory 0

Learning the hardware and software.


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.


The hardware you will be using to support the AT90-series is the Flash MCU evaluation board, a small board providing:

A small power supply provides power to the development board. The demo codes given in this lab are written for the MEGA32, but many of the examples throughout the 476 web site are written for the 8515 or MEGA163 mcus. This year we are using the MEGA32 which has more memory. To run the 8515 or MEGA163example programs you will need to:


Software you will use consists of:

Special Note

Some Mega32s are being shipped with the internal clock activated and set to 1 MHz. The symptom will be a Failure to program flash error. To program these MCUs in Codevision you must:

  1. From the main Codevision window: Choose menu Tools...Chip Programmer.
  2. In the resulting dialog box:
    1. set cpu clock to 1 MHz
    2. uncheck all fuse bits
    3. choose menu Program...Fuse Bits
    4. set cpu clock back to 16 MHz
    5. close chip programmer dialog box
  3. The normal make/program dialog should now work.



  1. 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 PortC 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.
  2. There will two serial connections to the STK500. One for programming the Mega32 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.

  3. Put all files on your Z drive! Do not creat a project in /cvavr/bin.
    Be sure to put all your files there and to back up daily!. Only open or save files in your own documents folder.

  4. There should be a shortcut to Codevision C on the desktop. Do NOT open or save projects into the Codevision bin directory (this is the default)! They will be deleted automatically! Only open or save files in your own documents folder.

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

  6. After you define a new project, you can add a C source file and edit it.
    1. 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.
    2. In the Codevision file menu choose to make a new file. When the prompt comes up, choose Project. When asked if you wish to use CodeWizard, decline the offer. At this point, you have made a new project, now you need to configure it. The configure dialog should be open but if it is not, under the Project menu, choose Configure...
    3. In this dialog box, in the Files tab, add the source code to the project you just defined.
    4. In the Compiler tab, set the Chip type to Mega32.
      Set clock speed to 16 MHz.
      Set (s)printf features to int,width.
    5. In the After make tab, set the Program the Chip checkbox.
      Clear all CKSEL check boxes.These select the clock source.
      You may need to uncheck the Check Signature box if the programmer throws an error.
      Then close the configure dialog.
    6. In the Settings...terminal menu, set the progrmming port to COM6.
      We will not use the Codevision Terminal. Setting to COM6 just gets it out of the way.
    7. In the Settings...programmer menu, set the progrmming port to COM1.
    8. Click on the compile icon. A message window will open to tell you if there are errors in the code. If there are errors, The open the listing file to see where they are. If the compile is successful, you should be able to download the program to the STK500 board and see some blinking LEDs. You will need to change PORTD to PORTC in the code for the switches to work.

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


  1. Using the short demo code downloaded above, modify the code:
    1. To stop blinking LED 1 when button 1 is pushed.
    2. To use PORTC rather than PORTD for switches.
    3. Add an LED and 300 ohm resistor as shown above. Make your program blink this LED rather than the one on the STK500.
  2. Using the short demo code downloaded above, include the debugger we wrote.
    1. Follow the directions on the debugger page for modifiying the target program to run with the debugger. If you uncomment the receive ISR enable in debugger.c, you will be able to get a command line using <cntl-c>.
    2. Further modify the demo code's task 3 to enter the debugger (using a debug(3) statement) when you push button 7. Just before your code enters the debugger, insert assembler code to set data register 1 to 0x55, then clear data register 2. See the complete instruction set document for information on assembler instructions.
    3. Once in the debugger use interactive commands to verify that register one was set correctly.
    4. Examine and explain the bits in the status register (which should reflect the results of the clear operation).
    5. With your modifed program, and using only interactive debugger commands:
      • turn on all the LEDs by modifying the appropriate i/o register.
      • turn off all the LEDs by modifying the appropriate i/o register.
      • modify the led variable in the program to display 0xaa. Consult the *.map file generated by the compiler to find out where the variable is stored.
      • determine the amount of hardware stack space used by the program at the time it entered the debugger.
      • modify the target program's timer0 prescaler from 64 to 8, then reenter the program (using the interactive g command) to show that it blinks the LED faster.
    6. Modify the demo program to define a local variable in task2 and use the debugger to show it was stored in data register 16.
    7. Modify the demo program to force the led variable into RAM using the #pragma regalloc- pragma. What is its address?
    8. Make sure that #define use_reporting near the beginning of debugger.c is uncommented. Modify the demo program task 2 to include a reportVdec(tsk2c). Insert the statement into the code so that you can see every count of the state variable.
    9. Make sure that #define use_logging near the beginning of debugger.c is uncommented. Modify the demo program to log 6 values of the led variable.
    10. Leave all debugging statements in your program, but insert the line: #define nullify_debugger just before the debugger include statement. Examine the complier's assembler output file to verify that all instructions corresponding to the debugger have been eliminated from the program.

    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 Dec 2007