Presentation is loading. Please wait.

Presentation is loading. Please wait.

USING THE AVR MICROPROCESSOR M. Neil - Microprocessor Course 1 Introduction to Assembly language.

Similar presentations


Presentation on theme: "USING THE AVR MICROPROCESSOR M. Neil - Microprocessor Course 1 Introduction to Assembly language."— Presentation transcript:

1 USING THE AVR MICROPROCESSOR M. Neil - Microprocessor Course 1 Introduction to Assembly language

2 Outline M. Neil - Microprocessor Course 2 Introduction to Assembly Code The AVR Microprocessor Binary/Hex Numbers Breaking down an example microprocessor program AVR instructions overview Compiling and downloading assembly code Running and debugging assembly code

3 A really simple program Some variables (i,j) Set variables to initial values A loop  Check a condition to see if we are finished  Do some arithmetic  Output some information  Increment the loop counter int main(void) { char i; char j; i=0; j=0; while (i<10) { j = j + i; PORTB = j; PORTB = i; i++; } return 0; } M. Neil - Microprocessor Course 3

4 Running a program on a microprocessor When you want to turn your program into something which runs on a microprocessor you typically compile the program This creates a program in the “machine language” of the microprocessor  A limited set of low level instructions which can be executed directly by the microprocessor We can also program in this language using an assembler We can have a look at the assembly language the c compiler generates for our simple program to understand how this works M. Neil - Microprocessor Course 4

5 Translating our program into assembly language 000000be : be:80 e0 ldir24, 0x00; 0 c0:90 e0 ldir25, 0x00; 0 c2:98 0f addr25, r24 c4:92 bb out0x18, r25; 18 c6:82 bb out0x18, r24; 18 c8:8f 5f subir24, 0xFF; 255 ca:8a 30 cpir24, 0x0A; 10 cc:d1 f7 brne.-12 ; 0xc2 ce:80 e0 ldir24, 0x00; 0 d0:90 e0 ldir25, 0x00; 0 d2:08 95 ret 000000be : be:80 e0 ldir24, 0x00; 0 c0:90 e0 ldir25, 0x00; 0 c2:98 0f addr25, r24 c4:92 bb out0x18, r25; 18 c6:82 bb out0x18, r24; 18 c8:8f 5f subir24, 0xFF; 255 ca:8a 30 cpir24, 0x0A; 10 cc:d1 f7 brne.-12 ; 0xc2 ce:80 e0 ldir24, 0x00; 0 d0:90 e0 ldir25, 0x00; 0 d2:08 95 ret int main(void) { char i; char j; i=0; j=0; while (i<10) { j = j + i; PORTB = j; PORTB = i; i++; } return 0; } int main(void) { char i; char j; i=0; j=0; while (i<10) { j = j + i; PORTB = j; PORTB = i; i++; } return 0; } Location in program memory (Address) Opcodes – the program code The Assembly language equivalent of the opcodes M. Neil - Microprocessor Course 5

6 AVR Microprocessor architecture Program Memory Your code goes here Program Memory Your code goes here Registers Storage for numbers the processer will perform arithmetic on Registers Storage for numbers the processer will perform arithmetic on Arithmetic Logic Unit (ALU) The heart of the processor which handles logic/math Arithmetic Logic Unit (ALU) The heart of the processor which handles logic/math Input/Output Interface to the outside world Input/Output Interface to the outside world M. Neil - Microprocessor Course 6

7 Numbers on a microprocessor We’ve seen that our program is converted into a series of numbers for execution on the microprocessor Numbers are stored in a microprocessor in memory  They can be moved in an out of registers and memory  Calculations can be done  Numbers can be sent to Output devices and read from Input devices The numbers are stored internally in binary representations.  We will study precisely how this is done shortly, and even build some simple memory devices. M. Neil - Microprocessor Course 7

8 Binary/Hexadecimal Numbers A “Bit” can be a 0 or 1  The value is set using transistors in the hardware Bits are organized into groups to represent numbers  4 Bits is a “Nybble”  Can store numbers 0-15  8 Bits is a “Byte”  Can store numbers 0-255  16 Bits is a word  Can store numbers 0-65535 Hexadecimal is a very handy representation for binary numbers  Each 4 bits maps onto a HEX number  Can quickly convert from HEX to binary BinaryHexDecimal 000000 000111 001022 001133 010044 010155 011066 011177 100088 100199 1010A10 1011B11 1100C12 1101D13 1110E14 1111F15 M. Neil - Microprocessor Course 8

9 Binary Representation This representation is based on powers of 2. Any number can be expressed as a string of 0s and 1s M. Neil - Microprocessor Course 9 Example: 9 = 1001 2 = 1* 2 3 + 0* 2 2 + 0*2 1 + 1*2 0 Example: 5 = 101 2 = 1* 2 2 + 0*2 1 + 1*2 0 Exercise: Convert the numbers 19, 38, 58 from decimal to binary. (use an envelope, a calculator or C program) Exercise: Convert the numbers 19, 38, 58 from decimal to binary. (use an envelope, a calculator or C program)

10 Hexadecimal Representation This representation is based on powers of 16. Any number can be expressed in terms of: 0,1,2,…,9,A,B,C,D,E,F (0,1,2,…,9,10,11,12,13,14,15) M. Neil - Microprocessor Course 10 Example: 1002 = 3EA 16 = 3* 16 2 + 14*16 1 + 10*16 0 Example: 256 = 100 16 = 1* 16 2 + 0*16 1 + 0*16 0 Exercise: Convert the numbers 1492, 3481, 558 from decimal to hex. (use calculator or C program) Exercise: Convert the numbers 1492, 3481, 558 from decimal to hex. (use calculator or C program)

11 HEX/Binary Conversion Converting HEX/Binary to Decimal is a bit painful, but converting HEX/Binary is trivial We often use the notations 0x or $ to represent a HEX number  Example 0x15AB for the HEX number 15AB  In assember language we see the notation $A9 for the HEX number A9 Exercise: Convert the numbers 0xDEAD 0xBEEF from Hex to binary. Now convert them to Decimal Exercise: Convert the numbers 0xDEAD 0xBEEF from Hex to binary. Now convert them to Decimal M. Neil - Microprocessor Course 11

12 8 Bit Microprocessors The AVR microprocessor we will be using is an “8 bit” processor The operations the processor performs work on 8 bit numbers  Data is copied from memory into the processors internal “registers” 8 bits at a time  Operations (addition/subtraction/etc..) can be performed on the 8 bit registers  We can of course do calculation with bigger numbers, but we will have to do this as a sequence of operations on 8 bit numbers  We will see how to do this later – using a “carry” bit M. Neil - Microprocessor Course 12

13 Operations M. Neil - Microprocessor Course 13 Exercise: (1) Find NOT(AAA) (2) Find OR(AAA; 555) (3) Find AND (AEB123; FFF000) Exercise: (1) Find NOT(AAA) (2) Find OR(AAA; 555) (3) Find AND (AEB123; FFF000) Why is shift important ? Try SHIFT R(011) SHIFT L(011) Why is shift important ? Try SHIFT R(011) SHIFT L(011) OperationRegister ARegister BResult Add A,B000011110000000100010000 Not A0101010110101010 OR A,B000101010010101000111111 ShiftL A0000111100011110 ShiftR A0000111100000111 AND A,B010101011010101000000000 The processor can perform several operations  Including “Boolean” algebra

14 What About Subtraction: Negative Numbers With 4 bits, you can represent  0 to +15  -8 to + 7  There are three ways to represent these negative numbers M. Neil - Microprocessor Course 14 IntegerSign Magnitude 1’s complement 2’s complement +70111 +60110 +50101 +40100 +30011 +20010 +10001 00000 100111101111 -2101011011110 -3101111001101 -4110010111100 -5110110101011 -6111010011010 -7111110001001 -81000 (-0)111111000 Sign/Magnitude: Set the top bit to 1 1’s complement : Take the complement of the number 2’s complement : Take the complement of the number and then add 1

15 What About Subtraction: Negative Numbers M. Neil - Microprocessor Course 15 Exercise: Fill out the same table using sign magnitude numbers. Do you understand now why 2’s complement is a useful representation! There is a good reason to use a 2’s complement representation  Binary addition of a two’s complement numbers “just works” whether the numbers are positive or negative 3 + 43 + -4-3 + 4-3 + -4 0011 1101 + 0100+ 1100+ 0100+ 1100 = 0111= 1111= 0001= 1001 (7)(-1)(+1)(-7)

16 8 Bit representations 8 bits can be used for The numbers 0-255 The numbers -128 to + 127 Part of a longer number A “Character”  Hence “char” in c  This is a bit out of date  Unicode uses 16 bits M. Neil - Microprocessor Course 16

17 Back to our program 000000be : be:80 e0 ldir24, 0x00; 0 c0:90 e0 ldir25, 0x00; 0 c2:98 0f addr25, r24 c4:92 bb out0x18, r25; 18 c6:82 bb out0x18, r24; 18 c8:8f 5f subir24, 0xFF; 255 ca:8a 30 cpir24, 0x0A; 10 cc:d1 f7 brne.-12 ; 0xc2 ce:80 e0 ldir24, 0x00; 0 d0:90 e0 ldir25, 0x00; 0 d2:08 95 ret 000000be : be:80 e0 ldir24, 0x00; 0 c0:90 e0 ldir25, 0x00; 0 c2:98 0f addr25, r24 c4:92 bb out0x18, r25; 18 c6:82 bb out0x18, r24; 18 c8:8f 5f subir24, 0xFF; 255 ca:8a 30 cpir24, 0x0A; 10 cc:d1 f7 brne.-12 ; 0xc2 ce:80 e0 ldir24, 0x00; 0 d0:90 e0 ldir25, 0x00; 0 d2:08 95 ret AddressValue 00BE80 00BFE0 00C090 00C1E0 00C298 00C30F 00C492 The program is stored in program memory There are 64Kilobytes of program memory (2^16 bytes) Each location stores an 8 bit number The location is specified with a 16 bit Address (0x0000-0xFFFF) The program is stored in program memory There are 64Kilobytes of program memory (2^16 bytes) Each location stores an 8 bit number The location is specified with a 16 bit Address (0x0000-0xFFFF) M. Neil - Microprocessor Course 17

18 Registers on the AVR There are 32 General purpose registers on the AVR microprocessor (R0-R31) Each of these can hold an 8 bit number R26:R27 is also a 16 bit register called X R28:R29 is Y R30:R31 is Z You can perform calculations on these registers very quickly AVR Registers – where the numerical work is done in a program M. Neil - Microprocessor Course 18

19 Back to our program: Loading a register 000000be : be:80 e0 ldir24, 0x00; 0 c0:90 e0 ldir25, 0x00; 0 c2:98 0f addr25, r24 c4:92 bb out0x18, r25; 18 c6:82 bb out0x18, r24; 18 c8:8f 5f subir24, 0xFF; 255 ca:8a 30 cpir24, 0x0A; 10 cc:d1 f7 brne.-12 ; 0xc2 ce:80 e0 ldir24, 0x00; 0 d0:90 e0 ldir25, 0x00; 0 d2:08 95 ret 000000be : be:80 e0 ldir24, 0x00; 0 c0:90 e0 ldir25, 0x00; 0 c2:98 0f addr25, r24 c4:92 bb out0x18, r25; 18 c6:82 bb out0x18, r24; 18 c8:8f 5f subir24, 0xFF; 255 ca:8a 30 cpir24, 0x0A; 10 cc:d1 f7 brne.-12 ; 0xc2 ce:80 e0 ldir24, 0x00; 0 d0:90 e0 ldir25, 0x00; 0 d2:08 95 ret AddressValue 00BE80 00BFE0 00C090 00C1E0 00C298 00C30F 00C492 The first instruction is loading a value of 0x00 into register r24 This instruction is encoded in the 16 bit opcode stored at address 00BE The Assember code is ldi r24,0x00 LoaD Immediate r24 with 0x00 The first instruction is loading a value of 0x00 into register r24 This instruction is encoded in the 16 bit opcode stored at address 00BE The Assember code is ldi r24,0x00 LoaD Immediate r24 with 0x00 M. Neil - Microprocessor Course 19

20 Decoding an Opcode be:80 e0 ldir24, 0x00 Words are stored “little endian” Read into the processor as E080 E080=1110 0000 1000 0000 dddd: 1000=8 -> 16+8 = r24 KKKKKKKK= 0x00 Words are stored “little endian” Read into the processor as E080 E080=1110 0000 1000 0000 dddd: 1000=8 -> 16+8 = r24 KKKKKKKK= 0x00 Exercise: what is the opcode for ldi r19, 0x3F Exercise: what is the opcode for ldi r19, 0x3F M. Neil - Microprocessor Course 20

21 Back to our program: Loading a register 000000be : be:80 e0 ldir24, 0x00; 0 c0:90 e0 ldir25, 0x00; 0 c2:98 0f addr25, r24 c4:92 bb out0x18, r25; 18 c6:82 bb out0x18, r24; 18 c8:8f 5f subir24, 0xFF; 255 ca:8a 30 cpir24, 0x0A; 10 cc:d1 f7 brne.-12 ; 0xc2 ce:80 e0 ldir24, 0x00; 0 d0:90 e0 ldir25, 0x00; 0 d2:08 95 ret 000000be : be:80 e0 ldir24, 0x00; 0 c0:90 e0 ldir25, 0x00; 0 c2:98 0f addr25, r24 c4:92 bb out0x18, r25; 18 c6:82 bb out0x18, r24; 18 c8:8f 5f subir24, 0xFF; 255 ca:8a 30 cpir24, 0x0A; 10 cc:d1 f7 brne.-12 ; 0xc2 ce:80 e0 ldir24, 0x00; 0 d0:90 e0 ldir25, 0x00; 0 d2:08 95 ret The second instruction is loading a value of 0x00 into register r25 This adds r24 to r25 and stores the result into register r24 The next instructions output r24 and r25 (we’ll learn where later) This is subtracting a value of 0xFF from register r24 This is the compiler cleverly adding 1 This is subtracting a value of 0xFF from register r24 This is the compiler cleverly adding 1 This instruction is comparing the value 0x0A to register r24 If they are not equal, the next instruction will branch back to location 0xC2 that is loop back if they are equal it continues on (and returns from main) M. Neil - Microprocessor Course 21

22 Compare assembly language to c 000000be : be:80 e0 ldir24, 0x00; 0 c0:90 e0 ldir25, 0x00; 0 c2:98 0f addr25, r24 c4:92 bb out0x18, r25; 18 c6:82 bb out0x18, r24; 18 c8:8f 5f subir24, 0xFF; 255 ca:8a 30 cpir24, 0x0A; 10 cc:d1 f7 brne.-12 ; 0xc2 ce:80 e0 ldir24, 0x00; 0 d0:90 e0 ldir25, 0x00; 0 d2:08 95 ret 000000be : be:80 e0 ldir24, 0x00; 0 c0:90 e0 ldir25, 0x00; 0 c2:98 0f addr25, r24 c4:92 bb out0x18, r25; 18 c6:82 bb out0x18, r24; 18 c8:8f 5f subir24, 0xFF; 255 ca:8a 30 cpir24, 0x0A; 10 cc:d1 f7 brne.-12 ; 0xc2 ce:80 e0 ldir24, 0x00; 0 d0:90 e0 ldir25, 0x00; 0 d2:08 95 ret int main(void) { char i; char j; i=0; j=0; while (i<10) { j = j + i; PORTB = j; PORTB = i; i++; } return 0; } int main(void) { char i; char j; i=0; j=0; while (i<10) { j = j + i; PORTB = j; PORTB = i; i++; } return 0; } Here the variables are stored in registers r24, r25 Pretty easy to see how this program is translated More complex programs quickly become very complicated to understand in assembly language Here the variables are stored in registers r24, r25 Pretty easy to see how this program is translated More complex programs quickly become very complicated to understand in assembly language M. Neil - Microprocessor Course 22

23 Programming in this course We will be programming exclusively in assembler  Allows us to understand precisely what the microprocessor is going to do and how long it will take to do so  important for time critical applications  Full access to all functions of the microprocessor  With care can make very efficient use of resources  Sometimes very important for small microprocessors Programming in assembler requires some discipline  Code can be very difficult to understand  The code is very low level  Line by line comments very important M. Neil - Microprocessor Course 23

24 The AVR instruction set We’ve seen a few sample instructions which cover most of the basic type of operations Arithmetic and Logic instructions  (ADD, SUB, AND, OR, EOR, COM, INC, DEC, …) Branch Instructions  Jump to a different location depending on a test Data transfer instructions  Move data to/from Registers and memory Bit setting and testing operations  Manipulate and test bits in registers M. Neil - Microprocessor Course 24

25 Arithmetic and Logic Instructions Addition add r20,r21 R20  r20+r21 Increment inc r20 R20  r20+1 Subtraction subi r20,$22 R20  r20-$22 sub r20,r21 R20  r20-r21 Logic and r20,r24 R20  AND(r20,r24) Many instructions work either with two registers, or with “immediate” data values (stored in the opcode) M. Neil - Microprocessor Course 25

26 Arithmetic and Logic Instructions – The full set M. Neil - Microprocessor Course 26

27 The Status Register Every time the processor performs an operation it sets bits in the Status Register (SREG)  Example cpi r01,$77  This register is in the I/O region SREG can be examined/set with the in and out instructions  in r17,SREG  out SREG,r22 The status bits are also tested by branch instructions to decide whether or not to jump to a different location M. Neil - Microprocessor Course 27

28 Branch Instructions Branch breq label1 Branch if equal to location label1 brlo label2 Branch if lower to location label2 If the Branch test fails – the next line of code is executed If the Branch test is successful, the program jumps to the location specified Call, Return rcall mysub ret Call subroutine mysub. The program saves information about where it currently is executing and then jumps to the code at mysub. When the subroutine is finished it calls ret, which then returns to where the rcall was made. Jump jmp label3 Jump to label3 – no information about where we jumped from is saved. This is a one way trip M. Neil - Microprocessor Course 28

29 All Branch Instructions M. Neil - Microprocessor Course 29

30 Data Transfer Instructions Load ldi r20,$73 R20  $73 ld r20,X R20  (X) Copy Register mov r20,r21 R20  r21 Input in r20,PIND R20  PIND Output out PORTD,r24 PORTD  r24 There are a few quirks about loading and storing to memory. We will cover this in detail soon. M. Neil - Microprocessor Course 30

31 Data transfer reference M. Neil - Microprocessor Course 31

32 The Atmega128 The microprocessor you will be using has several input and output ports Some of these are already connected to switches or LEDs on the boards we are using Others will be available to you to connect to various devices. It is time to make some lights blink! M. Neil - Microprocessor Course 32

33 M. Neil - Microprocessor Course 33 Setting up the Input/Output ports: ; ******* Port B Setup Code **** ldi r16, $FF; all bits out out DDRB, r16; Port B Direction Register ldi r16, $FF ; Init value out PORTB, r16; Port B value ; ******* Port D Setup Code **** ldi r16, $00; all bits in out DDRD, r16; Port D Direction Register ldi r16, $FF; Init value out PORTD, r16; Port D value ; ******* Port B Setup Code **** ldi r16, $FF; all bits out out DDRB, r16; Port B Direction Register ldi r16, $FF ; Init value out PORTB, r16; Port B value ; ******* Port D Setup Code **** ldi r16, $00; all bits in out DDRD, r16; Port D Direction Register ldi r16, $FF; Init value out PORTD, r16; Port D value For the ports we are using we set the Data Direction Register (DDR) which has a bit for each bit of I/O (1 for input, 0 for output) We can then set the initial value of the data bits at the I/O port PortB is connected to LEDs on our board PortD is connected to the blue switches For the ports we are using we set the Data Direction Register (DDR) which has a bit for each bit of I/O (1 for input, 0 for output) We can then set the initial value of the data bits at the I/O port PortB is connected to LEDs on our board PortD is connected to the blue switches

34 M. Neil - Microprocessor Course 34 The ATmega128 Microprocessor In this course you will be using the ATmega128 processor mounted on an ATMEL programming board (STK300)

35 M. Neil - Microprocessor Course 35 The ATMEL BOARD Connecting the board with your PC

36 Where the I/O ports are connected PORTD Switches PORTB LEDs M. Neil - Microprocessor Course 36

37 Setting up a code directory M. Neil - Microprocessor Course 37 Create a directory where you will store your code Download the file Simple.ASM into your code directory  from the course web page (Lecture 2b): DO NOT DOWNLOAD m128def.inc

38 M. Neil - Microprocessor Course 38 Getting Started with STUDIO 4: Go to Start  Programs  ATMEL AVR Tools  AVR Studio 4 Select New Project Select New Project

39 M. Neil - Microprocessor Course 39 Getting Started with STUDIO 4: You should now see the window: Pick Atmel AVR Assembler Click this and navigate to your code directory Pick a name for your project Do not create new file Create new folder

40 M. Neil - Microprocessor Course 40 Getting Started with STUDIO 4: You should now see the window: Select AVR Simulator Select ATmega 128 At the end

41 M. Neil - Microprocessor Course 41 Entering files in STUDIO 4 (I): (1) Right click here and select to ‘ Add Files to Project ’ (2) Navigate and find SIMPLE.ASM Chose Open to load the file

42 M. Neil - Microprocessor Course 42 Entering files in STUDIO 4 (II): Here is list of files attached to project This window is for editing one of the files Change the ‘ editing window ’ by double clicking on a file icon

43 M. Neil - Microprocessor Course 43 Select the output file format : Click on Project/ Assembler Options Change the format of the output file to Intel Hex format

44 M. Neil - Microprocessor Course 44 Running your Program in Studio 4 : Click on Build/ Build and run Click on Build/ Build and run

45 M. Neil - Microprocessor Course 45 Running your Program in Studio 4: Green means ok; Otherwise click on red and debug Green means ok; Otherwise click on red and debug Program is halted at this instruction The Build process generates some new files

46 M. Neil - Microprocessor Course 46 Open monitoring Windows: View/ Toolbars/ I/O

47 M. Neil - Microprocessor Course 47 Open monitoring Windows: Input Output Input Output Expand views as required Adjust screen display using Window/…

48 M. Neil - Microprocessor Course 48 Watch your Program Running: Start here: Everything zeroed Start here: Everything zeroed Click this once Step through the whole program and make sure you understand what is happening and again Use this to run continuously with active display Use this to stop Use this to run continuously without display Use this to reset

49 Exercising the ATmega128 commands : M. Neil - Microprocessor Course 49 Download the program simple.asm, assemble it and run it in the simulator Step through the program and make sure you understand what is happening after each instruction Try changing the number of times the loop is executed and make sure you understand the outputs on PORTB Now we will download the program to the development board

50 M. Neil - Microprocessor Course 50 Downloading with AVRISP Select from Start : AVRISP Make sure Device Atmega128 is selected Select from Start : AVRISP Make sure Device Atmega128 is selected

51 Erase the device M. Neil - Microprocessor Course 51

52 M. Neil - Microprocessor Course 52 Select the file to load Select Load/Flash…

53 Chose your.hex file to download M. Neil - Microprocessor Course 53 Tell AVRISP which.hex file to download to the Atmega128

54 M. Neil - Microprocessor Course 54 Now download the program into the Flash Select Program/Flash -- This writes the machine code to be downloaded into the ATmega128 chip

55 M. Neil - Microprocessor Course 55 Running your program Select Run – If all goes well your program should now be executing

56 Programs to write I: M. Neil - Microprocessor Course 56 Download the program simple.asm, assemble it download it to the board and run it.  What do you see on the LEDs?  Do you know why (note that a dark LED  1)?  change the code so that output on the LEDs has a lit LED for a 1 (think about using the neg instruction) Modify the program so that it changes the number of times the loop is run depending on which switch button is pushed  Use the instruction in Rxx,PIND to get the state of the buttons  Make sure that the LED output makes sense when you push the different buttons

57 Programs to write : M. Neil - Microprocessor Course 57 Make a counter from 0 – FF, output the values to PORTB and look with your scope probe at the LSB (PB0). How long does it take to make an addition? Why does the B0 bit toggle with a frequency that is twice that of B1? (check this using two scope probes; one on B0 and another on B1) In the documentation you will find how many clock counts are required to perform an instruction in your program. The ATmega128 has an 8 MHz clock. Predict the time it takes to do an addition and compare with your measurement using the scope.


Download ppt "USING THE AVR MICROPROCESSOR M. Neil - Microprocessor Course 1 Introduction to Assembly language."

Similar presentations


Ads by Google