Professional Documents
Culture Documents
ENGN3213
Digital Systems and Microprocessors
Semester 1, 2013
TABLE OF CONTENTS
1. INTRODUCTION........................................................................................................................... 3
2. AIMS OF THE LAB........................................................................................................................ 3
3. THE ATMEL ATMEGA8 MICROCONTROLLER ............................................................................ 3
3.1. Appearance of the Atmega8.................................................................................................... 3
3.2. The programming interface ..................................................................................................... 4
3.3. Wiring up the Atmega8............................................................................................................ 5
4. YOUR FIRST MICROCONTROLLER PROGRAM ......................................................................... 6
4.1. Creating a new project ............................................................................................................ 6
4.2. Coding .................................................................................................................................... 7
4.3. Compiling................................................................................................................................ 7
4.4. Programming the microcontroller............................................................................................. 8
4.5. Hardware verification [7 marks] ............................................................................................... 9
5. YOUR FIRST MICROCONTROLLER PROGRAM (in ASSEMBLY)................................................ 9
5.1. The program ........................................................................................................................... 9
5.2. Your exercise........................................................................................................................ 10
5.2.1. Interpreting the code [3 marks]........................................................................................ 10
5.2.1. Making changes [3 marks] .............................................................................................. 10
6. SOME INDEPENDENT WORK.................................................................................................... 10
6.2. Your exercise [7 marks]......................................................................................................... 10
1. INTRODUCTION
During this lab you will have the opportunity to work with a real microprocessor, the AVR Atmega8 by
Atmel. This chip (this is also true of its siblings with similar architecture) is fairly inexpensive (at less
than $10) and widely used in industrial applications (automotive and robotics mainly, but hey, you
could come up with some new ones).
During the first part of the Lab, you will breadboard the chip and the chip programmer ready for use,
while in the second part of the lab we will be running some pre-written simple programs on it, one in
assembly language and one in C. Finally, you will have the time to run something of your own.
NOTE: we will re-use the breadboarded controller over the next lab. Therefore, it is in your interest
to take as much care as possible to create a well-wired circuit that you can reliably use over
and over. Your assembled breadboard will be stored in a lab cabinet marked with a number. Make
sure you record that number so that you can re-obtain your board the following week.
*
Any info on the AVRISP mkII you may need and beyond http://www.atmel.no/webdoc/avrispmkii/avrispmkii.html
And set up the circuit as shown in the diagram below (Figure 3). It should not take you more than 30
minutes (plus 15 minutes or so to do the first programming task).
AVRISP mkII
RST programmer
connector 5V
1 MISO Vcc 2
ATMEGA8
5V
3 SCK MOSI 4
VCC GND
5V
GND
VCC
SCK 5 RST GND 6
MISO
MOSI
5V
PB1
620 LED
Figure 3: wire-up schematic for AVRISP mkII programming and the LED blink application.
The power supply should also be used in current-limited mode to ensure that you do not push
too much current through your circuit if you incorrectly wire up. To set up the power supply
correctly:
1. Use the black/red connectors from the SLAVE output (just left of the centre of the front
panel) and NOT the 5V/3A output located on the right hand side of the box.
2. Take the SLAVE current knob just off the minimum setting
3. Using a multimeter, check the output voltage and adjust the SLAVE voltage knob until
you obtain 5V
4. Adjust the SLAVE current knob down and then back up just a bit higher than the
minimum allowed (you will see the CC and CV LEDs switch when you get to that point)
In the Device Selection window that appears (Figure 6 below), select the ATmega8. The list is very
long and you can use the search field to speed things up. Press ok when done.
MCU stands for microcontroller unit. For our purposes, it is just fancy way of calling your chip.
Atmel Studio will then bring up your editor window where you can enter your program.
4.2. Coding
The next thing to do is to write the program. The example below takes you through the editing for
program Blinky1.c, designed to light up a LED intermittently. The code is provided below. Copy the
code in your program editor window.
---------------------------------------------------------------------------------------------
/*
* Blinky1.c
*/
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
DDRB=0x2; //set port PB1 to be an output (equivalent to 0b00000010)
while(1)
{
PORTB=~PORTB; //change sign of port output
_delay_ms(250); //this function is in library delay.h and can force a
//maximum delay of 263ms
}
}
---------------------------------------------------------------------------------------------
Code 1: program code for Blinky1.c
4.3. Compiling
Once the code is written, you are ready to compile. Go to the Build menu in the menu bar and select
Build Solution (Shortcut key F7). The system will run the compiler and if no errors are detected, will
generate the executable file and a "Build succeeded" message will appear in the bottom left corner of
the AtmelStudio window. The screenshot below shows the output from the build command.
Click on Memories in the left hand side pane of the window (Figure 9 below). The main frame will
then allow you to do several things: erase chip, write to flash and write to EEPROM. Remember that
the Flash memory is where the program is stored. To program the device use the control in the Flash
subsection to navigate to your project folder and locate the file with extension .hex (e.g., Blinky1.hex).
The two options "Erase device before programming" and "Verify Flash after programming" are helpful
for error control so keep them checked. When you are ready, click Program.
In this section you will work on a program similar to Blinky but you will do so using the Assembly
language. Although we have not worked with AVR Assembly, you should be able to interpret this
code in light of the AVR architecture knowledge which we have discussed in the lectures (particularly
the use of registers), your previous experience with simple MU0 Assembly, and, if all else fails, using
the AVR Instruction Set reference document (on Wattle). This exercise is about interpreting a short
and simple low-level program written in AVR Assembly. This section should take you 45 minutes at
the most.
Rebuild the code and download the hex file. What is the new period of the square wave? Can you
explain what has changed?
The final part of this lab will see you work independently to create a new system, program it and run
it.
Add another two LEDs to the breadboard, connected to another two of the port B pins (I
suggest PB0 and PB2, but ultimately it is up to you). You can add more than two if you would
like to have more fun, but in any case, two is a minimum.
Hint: LEDs have very low resistance, so if you do not want to burn your microcontroller by
drawing too much current, you should make sure that you are using your series resistor(s)
adequately.
Modify your C program from part 4 so that the LEDs can flash in a desired sequence. If you
have only three LEDs all up, they should light one at a time in sequence, and stay lit for at
least 200ms (a bit like the Blinky program). If you have more than that (>3), you can choose
your own pattern as long as:
o No LED stays on for more than 500ms
o The LEDs are never all on at the same time
Hint: You can work across ports or even use the pins taken by the programmer as outputs.
(after the programming is done, the ports are free to be used, the only problem with sharing
pins is that if you have a low resistance feeding into one port, it might weaken the signal from
your programmer and cause programming to fail, so be smart in your pin allocation!)