You are on page 1of 97

AVR Microcontroller

Embedded Computing Machines

An Introduction to AVR microcontroller

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Atmel AVR
? The Atmel AVR is a family of RISC microcontroller from Atmel. ? AVR stands for Advanced Virtual RISC and/or the initials of the two company founders Alf- Egil Bogen and Vegard Wollans RISC ? It uses single cycle instructions and a well-defined I/O structure that limits the need for external components. ? Internal oscillators, timers, UART, SPI, pull-up resistors, pulse width modulation, ADC, analog comparator and watch-dog timers are some of the features you will find in AVR devices. ? AVR instructions are tuned to decrease the size of the program whether the code is written in C or Assembly.

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

An Overview of AVR microcontroller features

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

AVR Overview
? Features- High Performance ? True RISC architecture ? True single cycle execution ? One MIPS per MHz ? 32 general purpose registers ? Harvard architecture ? AVR Flash microcontrollers operate with clock rates up to 20 MHz, achieving close to 20 MIPS. ? With 32 general purpose registers, the AVR delivers unmatched performance and flexibility, especially when you program in high-level languages, like C, Pascal or Basic.
Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

AVR Overview
? Features- Low Power Consumption ? 1.8 to 5.5V operation ? A variety of sleep modes ? Fast wake-up from sleep modes ? Software controlled frequency ? Single cycle and high code density ? A wide operating range ensures great flexibility when it comes to power supplies. ? The microcontroller providing different sleep modes assures that between real-time events, AVR does not consume power while ensuring the fastest possible wakeup time. ? High code density ensures that a minimum of instructions and clock cycles are needed to execute a task, thereby reducing the power consumption significantly.
Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

AVR Overview
? Features- Scalable ? Large device range ? Variety of pin counts ? Full code compatibility ? Pin/feature compatible families ? A large family of processors that share a single core architecture. This makes it easy to reuse code

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

AVR Overview
? Features- In-System Development: ? In-System Programming ? On-Chip Debugging ? In-system verification ? The device is its own emulator which ensures actual behavior and electrical characteristics. ? All the resources required for debugging is available on the tiniest 8-pin parts to the large 100-pin devices. ? Lock-bits protect your code from being inappropriately read or copied. ? Using any existing communication interface, the AVR devices also support self-programming, allowing them to upgrade their own firmware.
Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

AVR Overview
? Features- Development Tools: ? Assemblers/ Simulator- AVR Studio ? Starter Kits ? Evaluation Boards ? On-chip Debuggers ? Emulators ? There is only one tool chain to learn for the whole range of AVR Flash microcontrollers.

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

What makes the AVR one of the best microcontroller to use?

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Why select AVR Microcontrollers?


? Getting started with the AVR requires nothing more than the free assembler / C compiler, a simple programmer and a development board. ? The AVR architecture was designed from the ground up for efficiency with C code. ? There are a number of C compilers available commercially and for free. ? Unlike the 8051 and the PIC, the AVR is supported by the industry accepted GNU C Compiler (GCC) which is available for free download. ? Atmel Corp. provides an integrated development environment (AVR Studio) with an assembler and simulator for all AVR devices, which can be obtained free of cost from their website.
Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

AVR Classifications

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

AVR microcontrollers
? The AVR microcontrollers are divided into three groups: ? TinyAVR ? AVR (Classic AVR) ? megaAVR ? The difference between these devices mostly lies in the available features. ? The tinyAVR microcontrollers are usually devices with lower pin-count or reduced feature set compared to the megaAVR's . ? All AVR devices have the same instruction set and memory organization, so migrating from one device to another AVR is easy.

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

AVR microcontrollers
? In addition, the classic AVR is mostly EOL'd (End of life), so if you are looking to be able to buy replacements ever, go for the Mega or Tiny series. ? Some AVR's contain SRAM, EEPROM, External SRAM interface, Analog to Digital Converters, Hardware Multiplier, UART, USART and the list goes on. ? So which AVR microcontroller must one choose? ? For a tight power budget, you should only select an AVR that only includes the features you need. ? However, if size or power usage is not a problem, you should go for the biggest AVR possible.

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

AVR microcontrollers
The available tiny and mega AVR microcontrollers

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

AVR Packages

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

AVR Packages
? AVR microcontrollers come in the following packages: ? TQFP (Thin Quad Flat Pack): The Thin Quad Flat Pack (TQFP) package is a cost-effective packaging solution for moderate performance and lower lead count applications, where a lightweight, low profile package is required. ? PDIP (Plastic Dual In-Line): It comes in a wide range of lead counts: from 8 - 48 leads with a lead pitch of 100 mils. SPDIP package which is a shrink version of the PDIP with a 70 mil lead pitch and lead counts ranging from 30 - 64 leads. The SPDIP provides the option of decreasing the package size while maintaining the same lead count.
Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Packaging

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Introduction to Tiny AVR Microcontroller

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Tiny AVR
? The tinyAVR devices are general purpose flash-based microcontroller. ? They offer an unrivaled combination of price, performance and flexibility. ? The same Flash-based microcontroller can be used for design, prototype evaluation and volume production resulting in cost reduction by minimizing inventory. ? The TinyAVR are available from 1K bytes to 8K bytes Flash program memory in very small packages.

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Tiny AVR
? Features: ? internal RC Oscillator ? integrated EEPROM ? A/D Converter ? On-Chip 8-bit/16-bit timers and Watchdog ? High Frequency PWM with PLL ? On-Chip debug interface (debugWIRE)

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Comparison of Tiny AVR

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Introduction to Mega AVR Microcontroller

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

MegaAVR
? The megaAVR devices are developed for applications that need to store a large amount of program code. ? The megaAVR offers substantial program and data memories, and performance approaching 1 MIPS per MHz. ? It delivers the power of self-programmability for fast, secure , cost-effective remote upgrades.

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

MegaAVR
? Features: ? Single cycle 8-bit RISC architecture ? Complete Family with 4K to 256K Bytes Flash ? Self programming Flash memory with boot block ? 10-bit A/D converter ? 8 x 8 Hardware Multiplier ? integrated EEPROM ? internal RC Oscillator ? USART, SPI and TWI compliant serial interfaces ? On-Chip 8-bit/16-bit timers and Watchdog ? On-Chip debug through JTAG or (debugWIRE) ? Extended voltage range ? Package range from 28-pin to 100-pin in DIP, TQFP and MLF types
Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Comparison of MegaAVR

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Features of Atmega 128

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Atmega 128 Features


? High-performance, Low-power AVR 8-bit Microcontroller ? Advanced RISC Architecture ? 133 Powerful Instructions ? Most Single Clock Cycle Execution ? 32 x 8 General Purpose Working Registers + Peripheral Control Registers ? Fully Static Operation ? Up to 16 MIPS Throughput at 16 MHz ? On-chip 2-cycle Multiplier ? Nonvolatile Program and Data Memories ? 128K Bytes of In-System Reprogrammable Flash ? 4K Bytes EEPROM ? 4K Bytes Internal SRAM
Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Atmega 128 Features


? JTAG (IEEE std. 1149.1 Compliant) Interface ? Boundary-scan Capabilities According to the JTAG Standard ? Extensive On-chip Debug Support ? Peripheral Features ? Two 8-bit Timer/Counters with Separate Prescalers and Compare Modes ? Two Expanded 16-bit Timer/Counters with Separate Prescaler, Compare Mode and Capture Mode ? Two 8-bit PWM Channels ? 8-channel, 10-bit ADC ? Two-wire Serial Interface ? Dual Programmable Serial USARTs ? SPI Interface for In-System Programming
Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Atmega 128 Features


? Special Microcontroller Features ? Power-on Reset and Programmable Brown-out Detection ? Internal Calibrated RC Oscillator ? External and Internal Interrupt Sources ? Six Sleep Modes ? I/O and Packages ? 53 Programmable I/O Lines ? 64-lead TQFP and 64-pad MLF ? Operating Voltages ? 2.7 - 5.5V for ATmega128L ? 4.5 - 5.5V for ATmega128 ? Speed Grades ? 0 - 8 MHz for ATmega128L ? 0 - 16 MHz for ATmega128
Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

AVR with Peripheral Controllers

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

CAN Networking
? It is a two-wire, half duplex, high-speed network system and is well suited for high speed applications using short messages, can theoretically link up to 2032 devices on a single network ? Atmel offer a family of product dedicated to advanced CAN bus standard (2.0A & 2.0B) with 80C51 core and AVR core. ? These microcontrollers with Flash memory provide the ultimate solution for designing advanced CAN based systems.

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

FPSLIC (AVR with FPGA)


? Atmel's AT94K and AT94S family of Field Programmable System Level Integrated Circuits (FPSLIC devices) combine all the basic system building blocks (logic, memory and microcontroller) in an SRAM-based monolithic field programmable device. ? The FPSLIC Secure (AT94S) family of devices offers security and even higher integration.

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

USB Controllers
? Atmel offers an extensive portfolio of USB products that include USB hubs, microcontrollers with integrated USB interface, and embedded USB hosts. ? These devices are designed specifically to hide the complexities of USB protocol from your embedded design. ? The availability of the USB firmware library, sample code, and software design tools such as the USB Wizard (a GUI based source code generator) further reduces your learning curve and ensures the shortest design cycle and the fastest time to market.

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Reading an AVR microcontroller datasheet

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Reading Data Sheets


? A datasheet starts with a front page giving the features of the device. ? On the left hand side, there is a bookmarks side window. ? In the bookmarks side window, is given an index to all the sections in the datasheet. ? Clicking on a link will lead to the corresponding part of the datasheet. ? For example, to know about the settings of I/O ports, click on I/O Ports to go to the I/O Ports section.

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Reading AVR Datasheet


? The datasheet normally contains: ? Features of the AVR microcontrollers. ? Pin configuration. ? An overview of the device which includes the block diagram and general description of the pins ? AVR CPU core that gives architectural overview of CPU, general purpose registers, stack pointers, reset and interrupt handling. ? AVR memories ? Clock system and its distribution ? Power management and sleep modes
Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Reading AVR Datasheet


? Interrupt Structure ? I/O Port description ? Peripheral Interfaces ?Each AVR microcontrollers have different set of interfaces ? Electrical Characteristics ? AVR Typical characteristics ? Register Summary and Instruction Set Summary, that vary depending on the AVR microcontroller ? Lastly the ordering and packaging information are given
Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Getting Started with the Architectural Overview of AVR microcontroller

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

AVR CPU core

CPU block diagram


Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Arithmetic Logic Unit


? The ALU supports arithmetic and logic operations between registers or between a constant and a register. ? Its operations are divided into three main categories arithmetic, logical, and bit-functions. ? Some also provide a powerful multiplier supporting both signed/unsigned multiplication and fractional format.

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

General Purpose Registers


? All AVR processors have 32 general purpose registers. ? They are named from R0 to R31, broken into 2 parts with 16 registers each. ? Most of the instructions operating on the Register File have direct access to all registers and most of them are single cycle instructions. ? Each register is also assigned a data memory address ? R0 and R26 through R31 have additional features, R0 used in LPM (load program memory) instruction while R26 through R31 are used as 16 bits program counter.

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

General Purpose Registers


Register File:

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Introduction to AVR Clock Systems and their distribution

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Clock Systems
? The clock frequency determines the rate at which the programs will execute. ? The clock can be operated with variety of components. ? Some processor have an internal RC oscillator.

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Clock Systems

Clock Distribution
Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Clock Distribution
? CPU Clock clk CPU: Required for the operation of AVR core. ? I/O Clock clk I/O: The I/O clock is used by the majority of the I/O modules, like Timer/Counters, SPI, and USART. ? Flash Clock clk FLASH: The Flash clock controls operation of the Flash interface. ? Asynchronous Timer Clock clk ASY: It allows the Asynchronous Timer/Counter to be clocked directly from an external 32 kHz clock crystal. ? ADC Clock clk ADC: The ADC is provided with a dedicated clock domain.
Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Clock Sources
? ? ? ? ? External Crystal/Ceramic Resonator External Low-frequency Crystal External RC Oscillator Calibrated Internal RC Oscillator External Clock

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Basics of RESET controls

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

RESET
? During Reset, all I/O registers are set to their initial values, and the program starts execution from the Reset Vector. ? The ATmega has certain sources of reset: ? Power-on Reset. The MCU is reset when the supply voltage is below the Power-on Reset threshold (V POT ). ? External Reset. The MCU is reset when a low level is present on the RESET pin for longer than the minimum pulse length. ? Watchdog Reset. The MCU is reset when the Watchdog Timer period expires and the Watchdog is enabled. ? Brown-out Reset. The MCU is reset when the supply voltage VCC is below the Brown-out Reset threshold .

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

RESET
RESET CIRCUIT:

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Start writing an Assembly Program

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Writing Code!
? In order to write an assembly code, we make use of the instruction set. ? We first see what is assembly language programming, followed by the instruction set. ? Prior to which we go through the I/O Port of the AVR microcontroller. ? Once our assembly code is ready, we then go ahead and learn how to assemble the code. ? Then we move on to program our controller. ? Our first assignment is to blink LEDs that are connected to a port of a AVR microcontroller.

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Introduction to Input/ Output Port

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

What is I/O?
? I/O is Input or Output (Input/Output). ? It can be: ? A number of digital bits formed into a number of digital inputs or outputs called a port ? A serial line from or to the microprocessor (Tx /Rx) allowing serial data communication in the form of a bit stream on a two wire interface ? Other I/O devices such as ADC DAC, Timer modules, Interrupt controllers etc. ? A digital I/O port can be realized using a number of D type Flip-Flops

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

AVR I/O Ports: Details


? Three I/O memory address locations are allocated for each port ? Data Register PORTx ? Data Direction Register DDRx ? Port Input Pins PINx ? The Port Input Pins I/O location is read only, while the Data Register and the Data Direction Register are read/write

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

I/O Pin Equivalent Schematic

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Ports as General Digital I/O


? Configuring the Pin ? Each port pin consists of three register bits: DDxn, PORTxn, and PINxn ? DDxn ? The DDxn bit in the DDRx Register selects the direction of this pin ? If DDxn is written logic one, Pxn is configured as an output pin ? If DDxn is written logic zero, Pxn is configured as an input pin

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Ports as General Digital I/O


? PORTxn: ? If PORTxn is written a logic one when the pin is configured as an input pin, the Pull-up resistor is activated ? If PORTxn is written a logic zero when the pin is configured as an input pin, the pull-up resistor is deactivated ? If PORTxn is written a logic one when the pin is configured as an output pin, the port pin is driven high (one). ? If PORTxn is written a logic zero when the pin is configured as an output pin, the port pin is driven low (zero). ? PINxn: ? It is a purely "read-only" register
Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Port Pin Configurations

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

LEDs connections

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

LEDs
VCC_ARROW

PB0

? We make use of common anode type LEDs. ? This means that the LEDs will glow only when the PORT provides a logic 0

PB7

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Switches
? This is how a switch is connected to a pin of a port. ? The switches are passive type of devices, that do not have a voltage source of its own. ? When these switches are connected to the input port we have to provide a pull up resistor so as to source current.

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Assemble language programming

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Assemble language programming


? Each line of an assembly language program is split into four fields, as shown below LABEL OPCODE OPERAND COMMENTS ? LABEL: The label field is optional. A label is an identifier (or text string symbol). ? Opcode: This field contains a mnemonic. Opcode stands for operation code, i.e., a machine code instruction. A mnemonic is an abbreviation for an operation. For example: ADD than 05

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Assemble language programming


? Operand: This field consists of additional information or data that the opcode requires. In certain types of addressing modes, the operand is used to specify ? constants or labels ? immediate data ? data contained in another accumulator or register an address ? Comment: This field is optional, and is used by the programmer to explain how the coded program works. Comments are preceded by a semi-colon.

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Instruction Set

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Arithmetic and Logic Instruction


? ADD Rd, Rr
? Adds two registers without the C flag and places the result in the destination register Rd. ?Operation: Rd <- Rd + Rr Syntax: ADD Rd, Rr Operands: 0 = d = 31, 0 = r = 31 Program Counter PC <- PC + 1

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Arithmetic and Logic Instruction


? COM - One's Complement ? Description: This instruction performs a one's complement of register Rd. ?Operation: Rd <- $FF - Rd Syntax: COM Rd Operands: 0 = d = 31 Program Counter: PC <- PC + 1

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Branch Instructions
? BRNE - Branch if Not Equal ? Description: Conditional relative branch. Tests the Zero flag (Z) and branches relatively to PC if Z is cleared. If the instruction is executed immediately after any of the instructions CP, CPI, SUB, SUBI, the branch will occur if and only if the unsigned or signed binary number represented in Rd was not equal to the unsigned or signed binary number represented in Rr. 1,k). ? Operation: If Rd ? Rr (Z = 0) then PC ? PC + k + 1, else PC ? PC + 1 ? Syntax: Operands: Program Counter: ? BRNE k -64 = k = +63 PC <- PC + k + 1 PC <- PC + 1, if condition is false

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Branch Instructions
? RJMP - Relative Jump: ? Description: Relative jump to an address within PC - 2K +1 and PC + 2K (words). In the assembler, labels are used instead of relative operands. For AVR microcontrollers with program memory not exceeding 4K words (8K bytes) this instruction can address the entire memory from every address location. ? Operation: (i) PC ? PC + k + 1 Syntax: RJMP k Operands: -2K = k < 2 PC: PC ? PC + k + 1 Stack Unchanged

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Data Transfer Instructions


? MOV - Copy Register ? Description: This instruction makes a copy of one register into another. The source register Rr is left unchanged, while the destination register Rd is loaded with a copy of Rr. ? Operation: (i) Rd <- Rr Syntax: Operands: (i) MOV Rd, Rr 0 = d = 31, 0 = r = 31 Program Counter: PC <- PC + 1

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Data Transfer Instructions


? LDI - Load Immediate ? Description: Loads an 8 bit constant directly to register 16 to 31. Operation: (i) Rd <- K Syntax: (i) LDI Rd, K Operands: 16 = d = 31, 0 = K = 255 Program Counter: PC <- PC + 1

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Data Transfer Instructions


? IN - Load an I/O Location to Register ? Description: Loads data from the I/O Space (Ports, Timers, Configuration registers etc.) into register Rd in the register file. Operation: (i) Rd <- I/O(A) Syntax: IN Rd, A Operands: 0 = d = 31, 0 = A = 63 Program Counter: PC <- PC + 1

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Data Transfer Instructions


? OUT - Store Register to I/O Location ? Description: Stores data from register Rr in the register file to I/O Space (Ports, Timers, Configuration registers etc.). ? Operation: (i) I/O(A) ? Rr Syntax: Operands: OUT A, Rr 0 = r = 31, 0 = A = 63 Program Counter: PC ? PC + 1

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Bit and Bit- test Instructions


? ROL - Rotate Left trough Carry Description: Shifts all bits in Rd one place to the left. The C flag is shifted into bit 0 of Rd. Bit 7 is shifted into the C flag. ? Operation: Cb7 ------------------b0? C Syntax: ROL Rd Operands: 0 = d = 31 Program Counter PC ? PC + 1

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Algorithm for Blinking LEDs

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Algorithm for Blinking LEDs


? Configure PortB as output, set DDRB=1 ? Load a register with FFh, the LEDs are in ON position ? ldi r16, 0xFFH ? out DDRB ? Call for a DELAY subroutine ldi r17, 0x06H DELAY: dec r16 brne DELAY dec r18 brne DELAY dec r16 brne DELAY ? Now load another register with 0xFFH and out to PORTB to turn OFF the LEDs
Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Assembly Code
.include

m8def. Inc rjmp ABC: ABC: ldi r16, 0XFF out DDRB,r16 Loop ldi r19,0x00 ldi r17, 0X06H DELAY: dec r16 brne DELAY dec r18 brne DELAY: dec r17 brne DELAY comp r19 out PORTB rjmp loop

; load register r16 with all 1s ; configure port B as output port

; Delay subroutine

;turn OFF LEDs

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

How to Assemble my assembly code?

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

AVR Studio
? AVR Studio is an integrated development environment (IDE) for developing and debugging AVR applications. ? AVR Studio provides a project management tool, source file editor, simulator, in-circuit emulator interface and programming interface

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Using AVR Studio


? Go to directory where you have installed AVR Studio4. ? Double click on AVR Studio to run it. ? Then click on Create New Project.

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Using AVR Studio


? Give name to your project ? Select the location where you want to save this project. ? Click Next.

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Using AVR Studio


? Select Debug platform as AVR simulator. ? Select device as ATmega8 for AVRY. ? Click Finish.

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Using AVR Studio


? It will open a text editor. ? Write your Code. ? At the beginning of each assembly language program we include a file m8515def.inc so that all the instructions present in m8515def.incare included in the file that is to be complied ? Then save the code, control + s.
Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Using AVR Studio


? After you save the code build it , Project->Build. ? Make sure that there are no errors. ? Then run it, Project->build and run.

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Using AVR Studio


? To simulate the code go to Debug. ? Press F11 for step by step execution. ? See the changes in register values. ? To check the changes in user defined variable add variable name in watch window at right corner

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Downloading a program into the microcontroller

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Megaload
? MegaLoad is a Windows based boot loader for all Atmel ATmega microcontrollers that support the Bootloader function. ? MegaLoad is a solution for everybody who wants fast loading (~8k in 3 seconds) and easy software upgrade via MCU RS232 port.

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Introduction to boot loader

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Bootloader
? Boot loader: ? A program that is programmed into flash boot which allows the reprogramming of the AVR microcontroller ? This allows the self-programming feature of AVR ? Once the boot loader is programmed, it remains resident until the chip is erased. ? The flash memory is divided into two parts: ? Application Section: ?It is the section used for storing the application code ? Boot loader Section (BLS): ?The boot loader is stored in this section. ?The protection level is selected by the Boot Loader Lock bits.
Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Using MegaLoaod
? Run the MegaLoad program ? Click Open and locate the HEX file to program ? Select the COM port that the ATmega is connected to (only COM1-COM6 are supported) ? Select "Bootload on Reset" ? Connect the ATmega to the Windows PC ? Reset the ATmega ? MegaLoad should see the ATmega reset and will program the ATmega at the speed of light!

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

How to program a STK board

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

STK board
? The STK500 is controlled from AVR Studio, version 3.2 and higher. ? To program a hex file into the target AVR device, select STK500 from the Tools menu in AVR Studio. ? Select the AVR target device from the pull-down menu on the Program tab and locate the intel -hex file to download. ? The status LED will now turn yellow while the part is programmed, and when programming succeeds, the LED will turn green. If programming fails, the LED will turn red after programming.

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Using STK board

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

Assignments
1. 2. Go through the Atmega8 datasheet Down Count Set a register as a down count register. Every time decrement the value of the register Out the value of register to PORT. Up Count Set a register as an up count register. Every time increment the value of the register. Send the value of register to PORT. Roll LEDs Blink one LED after another in succession. Set one bit in a temporary register. Rotate register using suitable instruction. Out the value of register to PORT.
Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com
2005 All Rights Reserved.

3.

4.

Vijay Iyengar

Assignments
5. Use the switches to choose an arithmetic or logical
operation and display the result on the LEDs. Initially put a zero on the LEDs and assign one of the following tasks to each switch: Increment (UP) Decrement (DOWN) Set all (OK) Clear all (Cancel) Pressing a switch should perform the task assigned to it using the data that currently exists on the LEDs. .

Vijay Iyengar

Embedded Computing Machines Pvt. Ltd. support@embeddedmachines.com

2005 All Rights Reserved.

You might also like