AGH University of Science and Technology Cracow Department of Electronics

AGH University of Science and Technology Cracow Department of Electronics Microprocessors laboratory Tutorial 1 Laboratory introduction Author: Paw...
Author: Gillian Booth
41 downloads 0 Views 781KB Size
AGH University of Science and Technology Cracow Department of Electronics

Microprocessors laboratory

Tutorial 1 Laboratory introduction

Author: Paweł Russek http://www.fpga.agh.edu.pl/upt ver. 04/03/15

1/20

1. Introduction 1.1. Objective The main goal of this tutorial is to introduce all the elements of the working environment for the Microprocessors Laboratory. In principle the working environment is build around ATMega32 from Atmel AVR family of microprocessors. The laboratory stand consist of: •

Microsoft Windows based desktop PC ,



ZL3AVR development kit,



Atmel Studio 6.0 software tool.

Some important information about AVR microprocessors will be recalled at the beginning of this manual. Then AVR Studio and ZL3AVR will be introduced. Some basic information about assembly language coding and simple AVR instructions will be introduced. The practical part of the exercises will regard I/O port operations. More details about I/O ports and appropriate instructions will be given to let the student run a simple program at the end of this tutorial. The manual will conclude with a simple student's exercise.

1.2. Prerequisites •

Digital Electronics principle



Hexadecimal and binary coding system. Decimal/hexadecimal/binary data conversions.

2. AVR architecture basics This section introduce the basic information only and does not cover the topic of AVR architecture completely. It is assumed that student is already familiar with the topic. For in depth knowledge please refer to: •

Data sheet: ATMega16/ATMega32/ATMega64 (available also at the lab. web page)



Book: AVR Introductory course by John Morton



www: http://www.avrbeginners.net/



and many other sources as AVR microcontrollers are profoundly popular.

Atmel AVR is an 8-bit RISC single chip microcontroller. AVRs are microcontrollers as they integrate CPU, memory and peripherals in a single chip. AVR need a minimal number of external components to operate. Below is an example configuration with internal crystal and reset circuit. Note that AVR can run with external crystal as well.

2/20

The AVR needs only power supply and a reset button to run. As \Reset is active low. To keep \Reset high during normal operation a pull-up resistor is added. Other pins are devoted to constitute I/O parallel ports: •

Port A: pins 40 down to 33



Port B: pins 1 to 8



Port C: pins 22 to 29



Port D: pins 14 to 21

In basic mode, I/O ports can be read and written as microcontroller registers. The state of those 8-bit registers reflects the logic states of ports pins. You can use these ports e.g. to turn on a LED or to read a state of the push button. Also, some pins of the selected ports may have additional functions (such like UART, SPI or ADC input signals for example). Most of the AVRs come with ADC, timers, UART, I2C and SPI. Some of them have USB, CAN bus, etc.

2.1. Harvard architecture Internal AVR architecture is so called harvard architecture. That means that program memory and data memory are separated. That makes the CPU to run faster because data and code can be read simultaneously.

Program memory is a non-volitaile flash memory. Flash memory can be erased in seconds. It is called code ROM. The program ROM size can vary from 1k to 256k bytes. ATMega32 can store 32kB of program binaries. As AVR instructions are 16-bit words, flash is 2-byte wide so it is organized as 16k⨯16b. AVR data memory is a RAM memory. RAM memory map is presented in the following figure .

3/20

Data memory is an 8-bit memory. Where AVR maximum is 64kB of data RAM space. ATMega32 has 4kB of memory space. In all AVRs, the first 32 address locations of RAM are occupied by general purpose CPU registers. The general purpose registers are named Rn (R0-R31). The rest of RAM is occupied by I/O registers and SRAM. The size of I/O and SRAM (see the above figure) varies from chip to chip. SRAM memory can be use by user applications to store data but I/O registers have special functions in AVR architecture. I/O registers are used to manipulate various peripherals. In AVR we have also small amount of data EEPROM to store critical data. Block diagram of AVR architecture is given below:

Exercise 1.1 Open ATMega16/ ATMega32/ ATMega64 datasheet. 1. Name at least three features of the AVR 2. Give the SRAM size of ATMega16/ ATMega32/ ATMega64 3. Find locations of XTAL1 and XTAL2 pins. What is the purpose of those pins 4/20

3. Atmel studio Atmel Studio 6 is the integrated development platform for developing and debugging Atmel AVR microcontroller (MCU) based applications.

Exercise 1.2a Verify that Atmel Studio 6.0 is installed on your PC. Try to locate Atmel Studio 6.0 entry and lady bug icon in Windows' start menu. If the software is already installed you can skip this exercise. Download Atmel Studio installation package from the address given by the instructor. You can also find the installation package at Atmel's web site www.atmel.com. Run the program and follow installer instruction. •

Choose the full installation option.



Install at default destination path.



Install all components proposed by installer.

Exercise 1.2b Run Atmel Studio 6.0 and crate new AVR assembler project. 1. Click “Atmel Studio 6.0” in Windows' start menu 2. Choose “New project” in “Start Page – Atmel Studio” window 3. Select “Assembler” and “AVR assembler project” 4. Rename your project in “Name” field. The best if project name include your name/nickname and laboratory name in the project name. For example 'shrek_upt_lab`. You can also change project “Location” field.

5/20

5. Click 'OK' 6. Select device: ATMega32

7. Click 'OK' 8. Your workspace is ready now.

6/20

4. AVR assembler quick start The Assembler translates assembly source code into object code. The generated object code can be used as input to a simulator or an emulator such as the Atmel AVR In-Circuit Emulator. The Assembler also generates EEPROM file which can be programmed directly into the program memory and EEPROM memory of an AVR microcontroller.

4.1. Assembly code The Assembler works on source files containing instruction mnemonics, labels and directives. The instruction mnemonics and the directives often take operands. Code lines should be limited to 120 characters. Every input line can be preceded by a label, which is an alphanumeric string terminated by a colon. Labels are used as targets for jump and branch instructions and as variable names in Program memory and RAM. An input line may take one of the four following forms: 1. [label:] directive [operands] [Comment] 2. [label:] instruction [operands] [Comment] 3. Comment 4. Empty line A comment has the following form: ; [Text] Items placed in braces are optional. The text between the comment-delimiter (;) and the end of line (EOL) is ignored by the Assembler. Examples: label: .EQU count=100 ; Set COUNT to 100 (Directive) test:

ldi r21, count ; Load immediate value 'count' to register r21 (Instruction) jmp test ; Infinite loop (Instruction)

; Pure comment line ; Another comment line Instructions tell the CPU what to do, directives gives directions to the assembler tool.

4.2. EQU directive EQU directive sets a symbol equal to an expression. This is used to define constant value or fixed address.

4.3. Load immediate value (ldi) instruction Simply stated, ldi instruction copies 8-bit data into the general purpose registers. ldi Rd,K

;16 ≤ d ≤ 31, 0 ≤ K ≤ 255

K is 8-bit value that can be 0-255 in decimal, and rd is r16 to r31 (any of the upper general 7/20

purpose registers). The i in ldi stands for “immediate”. If we see the word “immediate” in any instruction, we are dealing with value that must be provided right there with the instruction.

4.4. Relative jump (rjmp) instructions Instruction: rjmp address makes CPU to execute next instruction at address position. Usually in an assembly code address is replaced by appropriate label: stop: rjmp stop

4.5. Data format representation There are four ways to represent a byte of data in the AVR assembler. The numbers can be represented in hex, binary, decimal or ASCII formats. Hex numbers There are two ways to show hex numbers: 1. Put 0x in front of the number like this: 0x99 2. Put $ in front of the number like this: $99 Binary numbers Put 0b in front of binary string like this: 0b10011001 ASCII numbers To represent ASCII data in AVR assembler we use single quotes as follows: 'A' Decimal numbers To indicate decimal numbers we simply use decimal. We can also use signed values like: 2 and -2.

Exercise 1.3 In Atmel Studio editor window, write assembly code that: 1. Define values in decimal, hex, binary and ascii formats. Name constants as 'decimal', 'hex', 'binary', and 'ascii' respectively. You may choose the target values for the constants. 2. Load decimal constant to r16, hex constant to r17, binary constant to r18, and ascii constant to r19. 3. Loop infinitely your code. Label as 'stop' the line where rjmp instruction jump. 4. Assembly your code.

4.6. Assembling your code Select 'Build->Build all' from Atmel Studio menu. If everything went fine expect message similar to this: ========== Build: 1 succeeded or up-to-date, 0 failed, 0 skipped ==========

8/20

in output window.

5. AVR Simulator 5.1. Debugging your code Before you will run your program in a real microcontroller you can check and debug your program execution in the AVR Simulator. Run simulator: Select 'Debug->Start debug and break' Window will appear

Select 'AVR Simulator' and 'Click OK' Now You are in “Debugger View” of Atmel Studio. New windows Watch and Memory appeared. Also, there is a highlighted line in an editor window. That indicates a place where Simulator has stopped. You can step through the code using simulator step instruction. a)

Important simulator commands (Debug menu)

Start Debugging and break (Alt-F5)

Starts simulator and stops at first instruction

Stop debugging (Cntr-Shift-F5)

Stops simulation

Continue (F5)

Continues simulation till next breakpoint

Step into(F11)

Goes to next program instruction

b)

Breakpoints

To set-up breakpoint at a selected instruction you can click left editor window margin next to instruction. Dot indicates breakpoint at a given line.

9/20

When you run simulation (F5), execution stops when simulator reaches instruction marked by breakpoint.

Exercise 1.4 1. Add 'hex', 'decimal', 'binary', 'ascii', 'r16', 'r17', 'r18', 'r19', and 'stop' to the Watch list. Comment Values displayed next to each variable. 2. Change memory region to 'data REGISTERS' in Memory window. 3. Set-up breakpoint at selected instruction. 4. Run simulation (F5). Compare register states in Memory window with the values defined in a program code. 5. Stop simulation.

6. I/O ports operations The 40-pin AVR has four ports. They are port A, port B, port C, and port D. To use any port as an input or output, it must be programmed. Each port has I/O registers associated with it. Every port bit corresponds to a register bit (e.g. register bit 0 controls the first pin of the port). There is an output value register portx and a direction register ddrx for each port. Table below shows registers for port A. Register name Address

Usage

porta

$3b

output

ddra

$3a

direction

6.1. DDRx register role in outputting and inputting data Each pin in a port can be used as an input or output. Te ddrx I/O register is used for making a given port pin an input or output . To output data on the whole port A we must put 0b11111111 into ddra. To make a whole port an input port, we must first put 0s into ddrx. Upon reset, all ports have the value 0x00 in their ddr registers. Tip As an aid for remembering that te port is input when ddrx bits are 0s, imagine a person who has 0 dollars. The person can only get money (input), not give it (output).

6.2. Store to SRAM (sts) instruction Sts instruction tells the CPU to store (copy) the contents of general purpose register (r0-31) to an address location in the data space. It can be one of I/O registers, a location in SRAM, or a general purpose register. sts

K,

Rr

; store register into location k ; k is an address between $0000 to $FFFF

Example: ldi r16,0x55 ;r10=55 (in hex) 10/20

sts 0x3b, r10 copy r10 to port A

6.3. Output (out) instruction Out instruction tells CPU to store the general purpose register to the I/O register. out

P,

Rr

;store register to I/O location (r=0..31, P=0..63)

Important Note that in the out instruction I/O registers are referred to by their I/O address. For example, the “out 0x16, r20” instruction will copy the content of register r20 into location $16 of I/O memory, whose data memory address is $36 !!!

6.4. Out vs. sts As mentioned earlier, we can use sts to copy the content of the general purpose register into any memory location. This means we can also copy into I/O register space. So, what is the advantage of using out to do the same. The out has the following advantages: 1. CPU executes out faster than sts. Sts takes 2 machine cycles. Out takes 1 machine cycle. 2. Out is 2 byte instruction. Sts is 4 byte instruction. 3. When we use out we can use the names of I/O registers instead of their addresses. 4. Out is available on all of the AVRs, whereas sts is not implemented in some of the AVRs. Table below shows registers, addresses in memory and IO space for port A. Register name

Memory space address (STS instruction)

IO address (out instruction)

Usage

porta

$3b

$1b

output

ddra

$3a

$1a

direction

7. ZL2AVR development kit 7.1. Short introduction ZL3AVR schematic is presented in figure.

11/20

12/20

The ZL3AVR uses internal 1MHz oscillator but it is possible to use external clock source but to use the oscillator the JP25 jumper should be positioned just like in the figure below.

Jumper settings for internal oscillator

7.2. Programming There are three programming ports on the PCB: Atmel ISP, KANDA ISP, JTAG. Each of them is used for a different kind of the programmer. Here JTAG will be used in this tutorial.

7.3. LED controls LEDs can be used to indicate the state of microcontroller port. High port bit state is signalled by the LED on. LED anodes are available on JP22.

8. Preparing the ZL3AVR kit to the laboratory exercises 8.1. Connecting AVR Dragon AVR Dragon can use JTAG or ISP ports of AVR. In this case, we will use JTAG to progrm ATMega. 1. Connect AVR Dragon's JTAG port to ZL3AVR's JTAG (JP21) port (see photo below). Watch pin 1 location.

13/20

Pin 1

Pin 1

Note Use 20 cm short cable to connect AVR Dragon to ZL3AVR. Due to violation of timing constraints, long cables do not work properly . 2. Connect AVR Dragon to PC USB port. AVR Dragon driver installation can start if AVR Dragon is used for the first time. You will need system administrator privileges to complete installation ( ask your instructor if problems occur). 3. Check Windows device manager if AVR Dragon is properly installed.

8.2. Connecting and wiring the ZL3AVR The board view of ZL3AVR is presented on figure below. 1. Connect eight pin of port A (JP17) to the LED pins (JP22). 2. Connect 12V DC power supply to the ZL3AVR power socket. Note that unfortunately there is no power LED on the ZL3AVR. 3. Connect the AVTPROG2 to the KANDA ISP on the ZL3AVR.

14/20

12V DC power supply

KANDA ISP Port A LEDs port

9. Controlling ZL3AVR LEDs Exercise 1.5 1. Save the assembler program from the previous exercise as 'first_AVR_asm.asm' Tip Always save your previous working code before you start a new exercise. That might be useful for the future reference and reuse. For the convenience of Atmal Studio you should always write a current code in a file that is named after your project name (shrek_upt_lab.asm for example). For that reason you will have to modify/clear previous program to write a new one. 2. Write a program that turns on the even LEDs at ZL3AVR kit. Hints •

On the schematic determine the logic state of the port which turns the LED on.



Write to DDRx register value that sets the port as output.



Write appropriate value to PORTx register.

15/20

3. Step through the code and debug using AVR Debugger. To verify the state of the LEDs, you can watch the state of Port A corresponding registers in “IO View” window (See below).

4. Save your working program as 'LEDs_even.asm'. (File->Save shrek_upt_lab.asm As)

9.1. Programming your code to the microprocessor Now you will program the AVR's flash (code) memory with the compiled binary code of the assembly program from exercise 1.5. Run microcontroller programmer tool: Select 'Tools->Device programming' 'Device Programming' window should appear.

16/20

Select progrmming tool, microcontroller type, and programing interface. 'Tool' → 'AVR Dragon' 'Device' → 'ATMega32' 'Interface' → JTAG Click 'Apply' button and then 'Read' button in device signature field. Text: 'Reading device ID … OK' shouild appear at the bottom of the window.

17/20

Select 'Memories' from side bar menu.

Select name of the hex file from your project folder (for example shrek_upt_lab.hex) in 'Flash' box. Click '… ' button to open 'Open file for programming' window.

18/20

To download your program binaries to microcontroller flash memory click 'Program' button

9.2. Veryfing program on the board ATMega is programmed now and it should work according to the algorithm from exercise 1.5 . Verify that leds are turned on/off correctly. Even LEDs should be lit at ZL3AVR kit.

10. Controlling single register bit instructions AVR instruction set allows programmer to control the state of the single bit in a 8-bit register. This instructions are particularly handy when only single bir must be set or reset and the states of the other register bits are not known.

10.1. Set Bit in I/O Register (sbi) instruction Sets a specified bit in an I/O Register. This instruction operates on the I/O registers that are indexed 0-31. sbi

A,

b;

Set bit b in A register

Example Example: sbi $12,7 ; Set bit 7 in Port D

19/20

0 ≤ A ≤ 31, 0 ≤ b ≤ 7

10.2. Clear Bit in I/O Register (cbi) instruction Clears a specified bit in an I/O Register. This instruction operates on I/O registers that are indexed 0-31.. cbi

A,

b; Clear bit b in A register

0 ≤ A ≤ 31, 0 ≤ b ≤ 7

Example: cbi $12,7 ; Clear bit 7 in Port D

Exercise 1.6 1. Write a program that turns on the even LEDs at ZL3AVR kit. Use sbi ans cbi instructions. 2. Optionally, step through and debug the code using AVR Debugger 3. Program the ZL3AVR and verify that program works correctly. 4. Save a file as 'LEDs_even_set.asm'. Note Hexadecimal output file that is used to program AVR is named after your project name

20/20

Suggest Documents