Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Smart Systems

Similar presentations


Presentation on theme: "Introduction to Smart Systems"— Presentation transcript:

1 Introduction to Smart Systems
(COMP 1659) Introduction to AVR Instruction Set and the ATmega1281 microcontroller This tutorial illustrates the use of a small subset of AVR instructions and provides some example code to illustrate their use with the ATmega1281 microcontroller on the STK300 development board. There is also some guidance on the use of AVR Studio. Introduction to Smart Systems Richard Anthony, Smart Systems Technologies, The University of Greenwich

2 Compiler Directives .include Tells the compiler to substitute this line in the program with the content of the file specified. For example the file m1281def.inc contains useful definitions of registers and ports and memory address details such as the highest usable memory address, for the ATmega1281 Microcontroller. The include file "m1281def.inc“ is automatically included by Atmel Studio (it knows which microcontroller you are using because you select it when you set up the project). .def Allows the programmer to define a meaningful name for a register etc. .def CountRegister=r16 ;usage example .cseg Tells the compiler this is the code segment .cseg ;usage example .org Allows the programmer to specify where in the memory the code should be located (its ‘origin’). .org 0x0D0 ;usage example Introduction to Smart Systems Richard Anthony, Smart Systems Technologies, The University of Greenwich

3 AVR Instruction subset (1)
This is a very basic subset, sufficient for the first laboratory exercise Ldi Load an ‘immediate’ value (a constant) into a specified register Example: ldi r16, 0x05 In Read an I/O port data value into a register Example: in r17, PINA Out Set a port output value, from the contents of a specified register Example: out PORTA, r18 Rjmp Jump to a specific location in the code defined by a label e.g. Main: Example: rjmp Main Rcall Call a subroutine Example: rcall DisplayPatternOnLEDs Ret Return from a subroutine Example: ret Introduction to Smart Systems Richard Anthony, Smart Systems Technologies, The University of Greenwich

4 AVR Instruction subset (2)
Inc Increment the value in the named register Example: inc r17 Dec Decrement the value in the named register Example: dec r18 Com One’s Complement Example: com r19 Cp Compare a pair of registers. Sets all flags as appropriate Example: cp r17,r18 Sub Subtract without carry (Subc subtract with carry) Example: sub r19,r20 Brne Branch if not equal (i.e. if the Z flag is not set) Example: dec r19 ;This will set the Z flag if the value in r19 was 0x01 and is now 0x00 brne RepeatLoop ;branch to RepeatLoop only if Z flag is NOT set, otherwise continue Other branch instructions follow the same pattern, all make their branch / not branch decision based on either one or two of the flags, see the AVR instruction set documentation for details. 4 Introduction to Smart Systems Richard Anthony, Smart Systems Technologies, The University of Greenwich

5 Parallel Ports ATmega1281 has six 8-bit parallel I/O ports (A, B, C, D, E, F) and one 6-bit port (G). Each port can be configured for input or output and each bit can be controlled separately. There are three I/O registers for each port: Data Direction Register (DDRn) – sets direction of the port (0 = input, 1 = output) ldi r16, 0xFF out DDRA, r16 ;Usage example, set all port A pins as outputs PORTn – the port output register. When configured for output, the data value on the PORT appears on the port pins. When configured for input, a ‘1’ (‘0’) turns on (off) the corresponding pull-up resistor out PORTA, r17 ;Usage example, place value of register r17 onto port A PINn – the port input register in r18,PINA ;Usage example, read port A data value into register r18 Introduction to Smart Systems Richard Anthony, Smart Systems Technologies, The University of Greenwich

6 Standard definitions ATMEL provide a header/include file m1281def.inc which contains standard register and bit-level definitions for the ATmega1281 microprocessor. For example the three registers for Port A are accessed at I/O addresses 0x00 – 0x02. The header file provides the following definitions: .equ PORTA = 0x02 .equ DDRA = 0x01 .equ PINA = 0x00 So by using the header file, the programmer does not have to remember the address mapping of all of the I/O registers. Another very important definition is that of the highest available RAM location: .equ RAMEND = 0x21ff There is 8KB of general-purpose RAM plus 512 bytes of registers The registers start from address 0x0000 to 0x01ff (see port A example above) The general-purpose RAM starts from address 0x0200 to 0x21ff Introduction to Smart Systems Richard Anthony, Smart Systems Technologies, The University of Greenwich

7 Useful Code Snippets (1)
Configuring a port for output (port A in this example) ldi temp, 0xFF ;All ‘1’s sets the direction of all bits to output out DDRA, temp ;Output the value to the port’s Data Direction Register Writing data to an output port ldi temp, 0x35 ;The value to be written to the port out PORTA, temp ;Output the value to the port pins Configuring a port for input (port A in this example) ldi temp, 0x00 ;All ‘0’s sets the direction of all bits to input Reading data in from a port in temp, PINA ;Read the value on port A into the temp register Introduction to Smart Systems Richard Anthony, Smart Systems Technologies, The University of Greenwich

8 Useful Code Snippets (2)
A short delay (busy wait) Delay_Subroutine : Enter ;A label so we can refer to it (e.g. rjmp) ldi r31,0xFF ;Place the value 0xFF in register r31 delay_loop: dec r31 ;Decrement the value brne delay_loop ;If not yet 0, go round the loop again ret Exit The delay subroutine above has only a single loop, and as the processor runs at between 1Mhz and 16MHz, it only takes a very small fraction of a second to execute. The code can be used ‘in-line’ (instead of as a subroutine), by not including the parts shown in green. 8 Introduction to Smart Systems Richard Anthony, Smart Systems Technologies, The University of Greenwich

9 Useful Code Snippets (3)
A longer delay can be achieved by nesting loops inside one another (a short delay is repeated a lot of times, making a larger delay) Longer_Delay_Subroutine: ldi r30,0xFF ; place the value 0xFF in register r30 outer_loop: ldi r31,0xFF ; place the value 0xFF in register r31 inner_loop: dec r31 ; decrement the value in register r31 brne inner_loop ; if not yet 0, go round inner loop again dec r30 ; decrement the value in register r30 brne outer_loop ; if not yet 0, go round outer loop again ret The delay subroutine above has two loops, the inside loop is the same as in the previous example. However, here it is repeated 255 times (0xFF). So this loop takes 255 times as long as the simpler delay example. More layers of loop nesting can be added to make even longer delays. 9 Introduction to Smart Systems Richard Anthony, Smart Systems Technologies, The University of Greenwich

10 Useful Code Snippets (4) A basic configuration skeleton (asm)
.def TEMP=r16 // Map variable names onto registers .cseg // CODE segment .org 0x0000 // Interrupt Vectors base address rjmp INIT // Reset (and power-on reset) handler .org 0x0080 // Program Code base address INIT: ldi TEMP,0xFF // Set Stack Pointer to top of SRAM out SPL,TEMP ldi TEMP,0x21 out SPH,TEMP sei // Set Global Interrupt Enable ldi TEMP,0xFF // Set port B direction as output (connected to LEDs) out DDRB,TEMP out PORTB,TEMP // Ensure LEDs are initially off ldi TEMP,0x00 // Set portD direction as input (connected to switches) out DDRD,TEMP // Place additional one-off initialisation code here MAIN: // Place your main-loop code here rjmp MAIN // Repeat continuously (the 'main-loop' of the program) Introduction to Smart Systems Richard Anthony, Smart Systems Technologies, The University of Greenwich 10

11 Working with Atmel Studio 6 (getting started)
The AVR Studio development environment is available in the hardware lab (KW116) Login to the CMS Domain Start → Atmel → Atmel Studio 6 Once you have started Atmel Studio, you can either OPEN an existing project or create a NEW PROJECT To create a new assembly code project: 1 select NEW PROJECT 2 select project type = C or Assembler 3 fill in the project name and location details (click OK when done) 4 select ATmega1281 from the scroll list 5 click OK This will create a project for you and will automatically set the device type – this is important because based on this the assembler / compiler automatically includes the various definition files / headers for the specific microcontroller. 11 Introduction to Smart Systems Richard Anthony, Smart Systems Technologies, The University of Greenwich

12 Working with Atmel Studio
(Build->Build) This example shows the ‘Switches And Lights’ Project loaded in Atmel Studio 6 The code has been built, and the results are shown in the Output window. Note that it reports ‘Build Succeeded’ – you must get to this stage before the code can be loaded onto the microcontroller. Introduction to Smart Systems Richard Anthony, Smart Systems Technologies, The University of Greenwich

13 Debugging with Atmel Studio 6 using the Dragon
When you click on the Debug tab a selection box will appear to ask if you want to use the Dragon, or the simulator – select Dragon. Debugging is far superior to simulation: - When you are debugging you are actually running the code on the microcontroller. - You can step through the code one command at a time, or run it until it hits a pre-defined breakpoint. - You can see the values in registers, as well as actually see output on the real LEDs and see the input value from the actual switches. 13 Introduction to Smart Systems Richard Anthony, Smart Systems Technologies, The University of Greenwich

14 This is an alternative to using the Dragon.
AVR-ISP (In-System Programmer) Setup This is an alternative to using the Dragon. Generally we shall use the Dragon, but the ISP is documented here for completeness. The ISP is a separate tool, not part of Atmel Studio 6, it is available from Start → Electronics 14 Introduction to Smart Systems Richard Anthony, Smart Systems Technologies, The University of Greenwich

15 AVR-ISP Auto-program options 15
Introduction to Smart Systems Richard Anthony, Smart Systems Technologies, The University of Greenwich

16 AVR-ISP After programming and successful verify – all bytes show green
16 Introduction to Smart Systems Richard Anthony, Smart Systems Technologies, The University of Greenwich

17 Simple Example Program
‘Switches and Lights’ Assembly version

18 Memory Image ‘Switches and Lights’ Assembly version
Introduction to Smart Systems Richard Anthony, Smart Systems Technologies, The University of Greenwich

19 Simple Example Program ‘Switches and Lights’ Embedded ‘C’ version
Introduction to Smart Systems Richard Anthony, Smart Systems Technologies, The University of Greenwich

20 Memory Image ‘Switches and Lights’ Embedded ‘C’ version
Introduction to Smart Systems Richard Anthony, Smart Systems Technologies, The University of Greenwich


Download ppt "Introduction to Smart Systems"

Similar presentations


Ads by Google