STK500 User Guide

16
STK500 USER G Peter Bain GUIDE August 2008

Transcript of STK500 User Guide

Page 1: STK500 User Guide

STK500 USER G

Peter Bain

GUIDE

August 2008

Page 2: STK500 User Guide

1

The purpose of this guide is to make you comfortable with the basic use of the Atmel STK500

development board using the CodeVisionAVR C compiler and AVR Studio simulator. The example is a

simple program using the ATmega8 microcontroller.

The Atmel AVR STK500 Starter Kit is a development board produced by Atmel for their Flash

microcontrollers. It enables you to quickly program and develop prototypes and designs. It also has

ISP for programming you microcontroller on your target PCB.

The link on Atmel’s website is:

http://www.atmel.com/dyn/products/tools_card.asp?tool_id=2735

CodeVisionAVR is a C compiler which interfaces with the STK500 development board. It is very easy

to setup and use, but at the same time very powerful in what it can do and the number of AVR chips

it supports. A fully featured evaluation version is available for free (with a 2K code limit).

Pavel’s website:

http://www.hpinfotech.ro/html/cvavr.htm

Licence version is available from Dontronics:

http://www.dontronics-shop.com/codevision-standard-edition.html

AVR Studio is an assembly language programmer and debugger from Atmel. It also includes an

assembler and simulator. The simulator integrates with CodeVisionAVR to step through you C

program. This is available for free from Atmel’s website:

http://www.atmel.com/dyn/products/tools_card.asp?tool_id=2725

The School of EIE has a number of the STK500 Development boards for use by students. The main

two microcontrollers which are kept in stock and which are recommended are the ATmega8 and the

ATmega32. In special cases where you may only require a small 8 pin device or something larger,

come and talk to the staff in SCT1-09 and they can organise that for you. There are licensed versions

of CodeVisionAVR, but in most cases the free evaluation version should cover most of your needs

(limited to 2k of code). The evaluation version can be installed on your home computers or laptops,

but the licensed versions must be installed on computers here at the University.

Page 3: STK500 User Guide

2

Contents

Page

Installing CodeVisionAVR and AVR Studio ............................................................................... 3

Setting up the STK500 board ................................................................................................... 3

Configuring the software ......................................................................................................... 4

Creating a new project using the CodeWizardAVR Automatic Program Generator ............... 6

Editing and Compiling the C code ............................................................................................ 8

Using the AVR Studio Simulator ............................................................................................... 8

Programming the target microcontroller on the STK500 Starter Kit ....................................... 11

Further editing ......................................................................................................................... 13

Page 4: STK500 User Guide

3

Installing the Software

Install the CodeVisionAVR C Compiler in the default directory: C:\cvavr.

The default directory for AVR Studio is C:\Program Files\Atmel\AVR Tools, but use the following

path:

C:\Program Files\Atmel\AVR Studio.

Setting up the STK500 Development Board

(More detail can be found in the STK500 user manual found in the AVR Studio Help section)

At this point no cables should be attached to any connectors on the STK500.

Jumpers

VTARGET – This controls the source of the supply voltage to the target Microcontroller. Mount the

jumper as we will be using the on-board software controlled voltage.

AREF – Analogue Voltage Reference. This is the reference voltage for the on-chip ADC. Mount this

jumper to connect the voltage to the AREF pin of the target microcontroller.

RESET – Mount this jumper to allow the master microcontroller to control the Reset signal on the

target AVR device.

XTAL1 – Mount this jumper to enable the STK500 internal system clock. This can be either a crystal in

the on-board crystal socket or the software-generated clock from the master microcontroller.

OSCSEL – This selects the internal clock system. Mount the jumper on pins 1 & 2 to select the on-

board software clock signal.

BSEL2 – This is only needed for High Voltage Programming of certain versions of the AVR family, so

leave this disconnected.

PJUMP – This is only needed for High Voltage Programming of certain versions of the AVR family, so

leave this disconnected.

Target Socket

Each AVR microcontroller needs to be placed in a specific socket on the STK500. These sockets are

colour coded and named. Further details can be found in the AVR Studio help files for all the AVR

devices supported.

Place the ATmega8 into the green socket labelled SCKT3200A2 (the right way around!).

Page 5: STK500 User Guide

4

Cables

Connect the In-System Programmable (ISP) cable from the 6 pin header labelled ISP6PIN to the

corresponding green header labelled SPROG2 ensuring that pin 1 is connected to pin 1 between the

2 headers. The SPROG headers match the colour of their target microcontroller sockets.

Connect one 10-pin ribbon cable between the PORTD and LEDs headers.

Connect the serial cable to the RS232 CTRL connector and the serial port on your computer (or USB

port if you are using Serial to USB convertor).

Finally double check everything and connect the power cable, but do not turn the STK500 on yet.

Configuring the software

CodeVisionAVR

Open up CodeVisionAVR and under ‘Settings’ in the menu bar you will find there are a number of

options which can be configured for your own particular user environment.

The first is ‘Editor’. This is basically the look and feel of the user environment, things like colours, tab

spacing etc. The defaults are fine for now so leave them as they are for now.

‘Debugger’ – The debugger used in CodeVisionAVR is AVR Studio, so the address in this option needs

to point to the file ‘C:\Program Files\ATMEL\AVR Studio\AvrStudio4\AVRStudio.exe’ (or wherever

you installed AVR Studio if you did not follow the recommended address earlier.)

‘Programmer’ – This specifies what programmer you are using. In this case, select the Atmel

STK500/AVRISP option as below. You also need to specify the COM port that you have it attached to.

In the case of a serial to serial connection, it will most likely be COM1, but if you are using a USB to

Serial convertor, you will need to go into your Device Manager options to find which COM port is

being used. Leave the ‘ATmega169 CKDIV8 Fuse Warning’ unchecked as it is not needed.

Page 6: STK500 User Guide

5

‘Terminal’ – This sets up the inbuilt terminal in CodeVisionAVR. This is connected to your computer

via the 2nd

serial connector on the STK500 Development board labelled ‘RS232 SPARE’. There are a

couple options to connect this terminal. You can share the cable used for programming by simply

unplugging it from the ‘RS232 CNTRL’ connector and plugging it into the ‘RS232 SPARE’ as needed,

but this can get a bit tedious after a while. It is easier if you have two cables. If you use the latter

option (recommended) you will need to make sure that you have the COM ports in the

‘Programmer’ and ‘Terminal’ set up correctly. For example: Programmer on COM1 and the Terminal

as COM2. The rest of the settings are shown below in the picture.

AVR Studio

Go to ‘Tools – Program AVR – Connect’ and select the ‘STK500 or AVRISP’ option and the appropriate

COM port (or leave it as Auto).

Page 7: STK500 User Guide

6

Creating a new project using the CodeWizardAVR Automatic Program Generator

1. First start the CodeWizardAVR. This can be done three ways.

a. By pressing Shift+F2

b. The little gear like icon as indicated below:

c. By selecting from the menu bar ‘Tools-CodeWizardAVR’ as shown below:

2. Under the ‘Chip’ tab set the following:

a. Select the ATmega8 from the drop down list.

b. Set the clock speed to 8MHz.

c. Set the Program Type to Application.

Page 8: STK500 User Guide

7

3. Now the LEDs are going to be connected to PORTD so set all of the PORTD pins to output.

The LEDs should be ‘Off’ so set the initial status of the pins to ‘1’. This is done by simply

clicking on the text to toggle it between states.

4. Under the ‘Project Information’ fill in some details similar to the following example.

5. Click on ‘File’ then ‘Generate, Save and Exit’

6. Name and save the C Source file (*.c), the Project files (*.prj) and the CodeWizardAVR

Project files (*.cwp) in a destination folder of your choice.

7. The CodeWizardAVR will generate all your initialisation code which will now appear. A brief

description of what is there:

a. The top comment block is details about the program, author, revision etc. and some

details about the microcontroller in use.

b. This is followed by the include files.

c. Global variables (variables which are used in more than one function).

d. The main function

i. Local variables (variables used only in this function).

ii. All the initialisation code to setup the microcontroller.

iii. The while loop where your code will go.

Page 9: STK500 User Guide

8

Editing and Compiling the C code

The purpose of this exercise is simply to the LEDs off and on.

Firstly you will need a delay function to insert a delay for the LEDs to remain on or they will flash on

so fast you won’t see them. To do this add the following include file as below:

#include <delay.h>

In the while loop underneath the prompt write the following code. This loop continues eternally.

while (1)

{

// Place your code here

PORTD = 0x00; // Set the whole of PORTD to 0 – Turns LEDs on

delay_us(1); // Wait 1us

PORTD = 0xFF; // Set the whole of PORTD to 1 – Turns LEDs off

delay_us(1); // Wait 1us

};

Now you need to build the project. To do this, click on the ‘Project’ in the menu bar and then select

‘Build All’. This will automatically save your files as well.

If there are no errors you can now simulate the program using AVR Studio.

Using the AVR Studio Simulator

Go to ‘Tools – Debugger’ in the menu bar. This will start AVR Studio if you pointed to the correct

location in your setting up of CodeVisionAVR options.

The following screen appears, click on the ‘Open’ box.

Find the file named *.cof in your projects home folder. You will then be prompted to save it as an

AVR Studio Project File (*_cof.aps), select save.

Page 10: STK500 User Guide

9

Select the AVR Simulator and the ATmega8 Device as shown below:

The simulator window is now displayed with your code.

In the menu bar select the ‘Debug’ drop down menu and right down the bottom select ‘AVR

Simulator Options’. Set the Device to ATmega8 and the Frequency to 8MHz. Ignore the rest as they

don’t impact on what we are doing.

On the right hand side open the PORTD drop down box. Below you will now be able to see a

graphical representation of the PORTs bits. A clear square is when the bit is logic 0 and a dark square

is when the bit is logic 1.

Click this drop down box

Page 11: STK500 User Guide

10

Slowly press the ‘Step Into’ button (or F11) and watch the pointer step through your code.

You will notice that once the yellow pointer has executed the following two instructions, the DDRD

and PORTD registers all are shaded in. This is because you wrote logic 1 to every bit in those

registers.

Keep stepping through the program. Till you get to the while loop. Once there note what happens to

the PORTD values as you step through the while loop.

When PORTD = 0x00 you can see all the PIND and PORTD boxes clear.

So as you continue around the while loop you will see that PORTD continues to toggle its state, from

on to off.

The program is working!

Close AVR Studio.

Page 12: STK500 User Guide

11

Programming the target microcontroller on the STK500 Starter Kit

Back in CodeVisionAVR, we need to update a bit of the code. Change the 2 delays from 1us to 500ms

as follows:

PORTD = 0x00; // Set the whole of PORTD to 0 – Turns LEDs on

delay_ms(500); // Wait 500ms

PORTD = 0xFF; // Set the whole of PORTD to 1 – Turns LEDs off

delay_ms(500); // Wait 500ms

Build the project again checking that there are no errors. Then click on the little IC symbol next to

the red bug in the icons under the menu bar. This opens the chip programmer.

Make sure the Chip is set to the ATmega8 and the clock is set to 460800Hz. On first use of a new

microcontroller this may not work, if it doesn’t drop the clock down, complete the next step of

programming the fuse bits and then reset the clock to the 460800Hz.

Turn the STK500 Development Board on.

Setting the fuse bits

The Fuse Bits set various chip options, which are described in the Atmel data sheets.

If a Fuse Bit check box is checked, then the corresponding fuse bit will be set to 0, the fuse being

considered as programmed. If a Fuse Bit check box is not checked, then the corresponding fuse bit

will be set to 1, the fuse being considered as not programmed.

You are going to set up the ATmega8 with the following options:

• Internal 8 MHz clock

• 6 clock cycles + 4 ms start up time

• Brown Out Detection enabled at 2.7V. This watches the supply voltage and shuts the

microcontroller down if the voltage drops too low to protect it.

There are 2 ways to program the fuse bits. One is using AVR Studio and the other in CodeVisionAVR.

AVR Studio is probably the most user friendly and self explanatory as it provides descriptions of the

settings. This can be done by following the procedure below:

• Open AVR Studio and cancel out of the welcoming window.

• Click on the little IC icon below the menu bar with ‘AVR’ written on it.

• In the resulting window, select the ‘Main’ tab and ensure that the Device is the ATmega8

and that the Programming Mode is ISP mode.

Page 13: STK500 User Guide

12

• Set the SUT_CKSEL option to ‘Int. RC Osc. 8 MHz; Start-up time: 6 CK + 4ms.

• Set the Brown Out Detection Level (BODLEVEL) to 2.7V and enable it by checking the BODEN

box.

• Then press the ‘Program’ button.

• Close AVR Studio.

You can then go back to the Chip Programmer in CodeVisionAVR and select ‘Read – Fuse Bit(s)’ and

copy them into CodeVisionAVR from there.

Or if you want to sit down and work out what the corresponding register bits are in the

CodeVisionAVR Chip Programmer you could do it directly from there.

The CodeVisionAVR Chip Programmer should end up looking like this:

Now press the program all button. When asked whether you want to load a file into the EEPROM

press cancel. Close the Chip Programmer.

Your chip is now programmed and the 8 LEDs on the STK500 Development board should all be

flashing on and off together in 500ms intervals.

Page 14: STK500 User Guide

13

Further editing

The next step takes the coding a little further and has a look at some of the included functions which

CodeVision has inbuilt. These make life very easy to set certain processes up.

You have already used one of these functions when calling the delay_ms() function.

There are 2 external interrupt pins on the ATmega8. These are INT0 (PORTD.2) and INT1 (PORTD.3).

The aim is to connect each of these to a switch on the STK500 Development board and toggle a LED.

Remove the 10 way cable from the PORTD and LEDS headers. Connect one end of a 2 way cable to

PORTD bits PD2 and PD3 and the other end to Switches SW0 and SW1. Connect another 2 way cable

from PORTE XT1 and XT2 to the LEDs LED6 and LED7.

Pushing a switch will pull the corresponding input pin low, while releasing it will connect the pin to

VCC via a pull up resistor to limit the current.

Open the CodeWizardAVR. In the Ports tab, change PORTD bits 2 and 3 to inputs and their state from

Tri-State (T) to Pull Up (P) to enable the internal pull up resistor in the ATmega8. Also change PORT B

bits 6 and 7 to outputs and set them to a high state initially.

Select ‘File – Program Preview’ and copy the generated setup code for PORTB, PORTD. Paste them

over top of your existing setup code. If you want to double check see the completed code at the end

of this tutorial.

NOTE: PB6 & PB7 on the PORTB header of the STK500 will not work

with some of the newer AVR chips that use the green socket

SCKT3200A2. This is because these pins have not been available as

GPIO pins in earlier (but otherwise pin-compatible) AVRs like the

AT90S2333/4433 where they were solely used as XTAL1/XTAL2.

Thus, the STK500 layout routes them to these pin headers (PORTE

XT1 & XT2). To use these pins though, you will have to set the fuses

to use the ICs internal oscillator. If you are using ISP on your own

PCB, this is not an issue. Some of the chips affected are ATmega8,

48, 88, 168.

So

Page 15: STK500 User Guide

14

Open the CodeWizardAVR again and in the ‘External IRQ’ tab check both INT0 and INT1 to enable

them and to trigger on a Low Level as in the figure above. Preview the program again and copy the

interrupt setup code and the function routines. Paste them into the correct places in your code.

Add the following code to the INT0 subroutine to toggle the LED6 every time SW0 is pressed.

Included is a 200ms delay to stop switch bounce and the interrupt occurring again while you still

have it pressed:

// External Interrupt 0 service routine

interrupt [EXT_INT0] void ext_int0_isr(void)

{

PORTB.6 = !PORTB.6; // Toggle Bit

/*Simply, the bit equals the opposite of

whatever state it was in previously.*/

delay_ms(200);

}

Add the following code to the INT1 subroutine to toggle the LED7 every time SW1 is pressed:

// External Interrupt 1 service routine

interrupt [EXT_INT1] void ext_int1_isr(void)

{

PORTB ^= (0x01 << 7); // Toggle Bit

/* This Exclusive ORs PORTB, but only the bit specified

in the shift left. In this case we shift left 7 bits,

so it is bit 7 that is toggled */

delay_ms(200);

}

Page 16: STK500 User Guide

15

Both of the toggling routines above will toggle a port bit, but they are there simply to show 2 of the

methods available.

Delete the previous code you had inside the while loop in the main program. Leave the while loop

intact though. Also, just before the while loop add the asm code to enable interrupts as below:

#asm("sei") /* enable interrupts */

while (1)

{

// Place your code here

};

Build the project files and check for any errors.

Program the microcontroller.

When you press SW0 LED6 should toggle state and the same for SW1 and LED7.

You should now have a basic understanding of how these three products interact with each other

and how to use them.

If anything more specific is needed please come and see the technical staff in SCT1-09 and they will

be more than happy to help!