Vous êtes sur la page 1sur 10

HLAB4

Breadboarding and programming the


ATmega8 microcontroller

ENGN3213
Digital Systems and Microprocessors

Semester 1, 2013

Copyright 2012-13, The Australian National University

ANU College of Engineering and Computer Science


E N G N 3 2 1 3 D i g i t a l S y s t e m s a n d M i c r o p r o c es s o r s

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

ANU College of Engineering and Computer Science P age 2/10


E N G N 3 2 1 3 D i g i t a l S y s t e m s a n d M i c r o p r o c es s o r s

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.

2. AIMS OF THE LAB

To gain hands-on skills about breadboarding a microcontroller


To learn how to use AVRstudio to program a microcontroller starting from C and Assembly
code, using an AVRISPmkII USB programmer

3. THE ATMEL ATMEGA8 MICROCONTROLLER


You have learned about the architecture of this microcontroller in lectures. In the following, the
hardware aspects which are of interest for the purpose of this lab are explained.

3.1. Appearance of the Atmega8


The ATmega8 used in this lab comes in a 28-pin, plastic dual in-line package (PDIP) as shown in
Figure 1. PDIPs have the largest pin spacings of all packages which makes it easier for manual wiring
up of circuits. Other packages with much smaller footprints do exist.
The ATmega8 has many internal peripherals (note the half-moon shape at the top of the chip to
determine orientation). There are up to three 8-bit signal ports on pins PBX, PCX, PDX, a UART (pins
2 and 3), six analogue to digital converters (ADCs) on pins 23-28, two interrupts (4 and 5) and many
others.
Six of the ports are used for programming the MCU via the Serial Peripheral Interface (SPI) protocol.
SPI has data and clock lines: MOSI (pin 17), MISO (pin 18) and the clock SCK (pin 19). The power
supply AVCC (pin 19) and ground (pin 22) are also sent to the SPI capable programming device.
Finally, a reset pin is also present (Pin 1). Note that many of the pins on the ATmega8 PDIP package
are multiplexed, including the programming pins. While this generally means that multiplexed pins
cannot be used in the same application, multiplexing does allow the programming pins to be used for
other purposes after the programming phase. For example, programming port MOSI SPI (pin 17) can
be used as PB3 during the execution of a program.

ANU College of Engineering and Computer Science P age 3/10


E N G N 3 2 1 3 D i g i t a l S y s t e m s a n d M i c r o p r o c es s o r s

Figure 1: Atmega8 pinout and image of the chip.

3.2. The programming interface


In order to write the machine code file onto the chips flash memory you will be using a programmer.
The device name is AVRISP mkII*. This is a serial programmer which can communicate with the PC
through a USB interface. The device is shown in Figure 2, with its multi-colour status LED highlighted
by the arrow. Table 1 lists the possible device statuses and corresponding LED colour.

Figure 2: AVRISP mkII programmer (status LED highlighted).

LED colour Description


Red Idle target not powered
Green Idle target is powered
Orange Busy Programming
Orange blinking Reversed target cable connection
Red blinking Short-circuit on target
Red Orange blinking Upgrade mode

Table 1: AVRISP mkII programmer (status LED highlighted).

*
Any info on the AVRISP mkII you may need and beyond http://www.atmel.no/webdoc/avrispmkii/avrispmkii.html

ANU College of Engineering and Computer Science P age 4/10


E N G N 3 2 1 3 D i g i t a l S y s t e m s a n d M i c r o p r o c es s o r s

3.3. Wiring up the Atmega8


Next, you will prepare the microprocessor for connection to the programmer.
Obtain:
x1 power supply
x1 breadboard
x1 programmer
x1 red LED
x1 resistor (620 Ohm)
connector leads as required

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.

WARNING. VERY IMPORTANT! The chip is to be powered by a 5V power supply connected to


pins 7 and 20, with the ground terminal connected to ports 8 and 22. Vcc and GND are very
close to each other and there is a risk you may accidentally short-circuit them or swap them
around. If you do, your chip may burn. Double check your connections before turning the
power on and refer to the position of the half moon on the chip's face in order to make sure
you have identified the pins correctly.

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)

ANU College of Engineering and Computer Science P age 5/10


E N G N 3 2 1 3 D i g i t a l S y s t e m s a n d M i c r o p r o c es s o r s

Once the power supply I adjusted you can move on and


wire up your circuit.

You should know how to set up circuits on a breadboard


from your previous engineering courses. Remember that
there are horizontal and vertical conductive pathways in
different regions of the board. Ask your demonstrator if you
are unsure. Be careful also when placing the chip into the
breadboard not to bend/break any pins. A photo of a
working setup is shown in Figure 4
Figure 4: photo of the set-up

4. YOUR FIRST MICROCONTROLLER PROGRAM


After a brief introduction on how to use the development software, you will be able to compile your
first program and finally bring your chip to life! The program we use to work with the Atmega8
processor is Atmel Studio 6. This program combines a wide variety of features. For the purpose of
your lab work, you will only be using the editor, the compiler and the MCU programming tools.

4.1. Creating a new project


Start up Atmel Studio 6 by using the ladybeetle icon. In the main screen which opens up, click New
Project In the New Project window that appears (see Figure 5 below), select GCC C Executable
Project if you want to work with C language or AVR Assembler project if you want to work with
Assembly language. Enter a meaningful name and location for your files and press OK.

Figure 5: New Project dialog window

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.

ANU College of Engineering and Computer Science P age 6/10


E N G N 3 2 1 3 D i g i t a l S y s t e m s a n d M i c r o p r o c es s o r s

Figure 6: New Project dialog window

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.

ANU College of Engineering and Computer Science P age 7/10


E N G N 3 2 1 3 D i g i t a l S y s t e m s a n d M i c r o p r o c es s o r s

Figure 7: Compiler result after build command

4.4. Programming the microcontroller


The built project can now be written into the microcontroller's program memory. To do so, open the
Tools menu and select Device Programming. A window will open. Ensure that the selected tool in the
top left hand side menu is the AVRISP mkII, the device is ATmega8 and the Interface is ISP. Press
the Apply button and further options will appear.

Figure 8: Device programming window before device selection

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.

Figure 9: Memories tab in device programming window

ANU College of Engineering and Computer Science P age 8/10


E N G N 3 2 1 3 D i g i t a l S y s t e m s a n d M i c r o p r o c es s o r s

4.5. Hardware verification [7 marks]


If the programming operation has been completed successfully and your wiring is correct, the LED
should start to blink. Show your work to your demonstrator to get your marks (both the wiring up and
the software use count towards these marks).

5. YOUR FIRST MICROCONTROLLER PROGRAM (in ASSEMBLY)

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.

5.1. The program


Below you find the assembly code for the program. Start up a new project in assembly which uses
this as the source code. You should compile and program it and see your LED light blink.
---------------------------------------------------------------------------------------------
/**************
* Blinky2.asm
*************/
.INCLUDE "m8def.inc" ;don't worry about these lines
.org 0x0000 ;don't worry about these lines

ldi r16, low(RAMEND) ;stack definition in this block


out SPL, r16
ldi r16, high(RAMEND)
out SPH, r16 ;end of stack definition

ldi r16, 0xFF


out DDRB, r16

;NOTE words followed by a colon (such as loop: below) represent a line


;label which can be used as a target for a jump or call instruction

loop: sbi PortB, 1


call delay_05
cbi PortB, 1
call delay_05
rjmp loop

delay_05: ldi r16, 8


outer_loop: ldi r24, low(58535)
ldi r25, high(58535)
delay_loop: adiw r25:r24, 1
brne delay_loop
dec r16
brne outer_loop
ret
---------------------------------------------------------------------------------------------
Code 2: program code for Blinky2.asm

ANU College of Engineering and Computer Science P age 9/10


E N G N 3 2 1 3 D i g i t a l S y s t e m s a n d M i c r o p r o c es s o r s

5.2. Your exercise


5.2.1. Interpreting the code [3 marks]
As you may have noticed, in the code there are some, but not too many comments. The first task of
your exercise is to figure out what the code is doing. Go through the program and using the provided
references, comment the rest of the code. You should be able to explain the frequency of the blinking
(hint, the instruction reference document has a big reference table where you can see how many
clock cycles are needed to execute an instruction). If you want to verify the frequency of the blinking
with exact precision you can use the oscilloscope on your bench.

5.2.1. Making changes [3 marks]


Now comment out the following lines in the code by inserting semicolons as follows

; delay_loop: adiw r25:r24, 1


; brne delay_loop

Rebuild the code and download the hex file. What is the new period of the square wave? Can you
explain what has changed?

6. SOME INDEPENDENT WORK

The final part of this lab will see you work independently to create a new system, program it and run
it.

6.2. Your exercise [7 marks]


Together with your lab partner you should (indicative time about 1 hour):

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!)

ANU College of Engineering and Computer Science P age 10/10

Vous aimerez peut-être aussi