INTRODUCTION TO AVR

AVR Tutorials aims to provide information and tutorials for the Atmel AVR 8-bits series of microcontrollers such as the ATMega8515, ATMega16, ATMega32, ATTiny2313, ATTiny13, etc. The information on AVR Tutorials is geared towards assisting students in understanding concepts related to the exciting technology around embedded systems through the development of simple programs and building simple electronic circuits utilizing Atmel AVR microcontrollers.


New tutorials are posted as they become available so please visit frequently. The topics AVR Tutorials aims to cover include:
  1. Assembly Language for AVR Microcontrollers
  2. Embedded C Language for AVR Microcontrollers
  3. AVR Microcontroller Analog Comparator
  4. AVR Microcontroller AD Converter
  5. AVR Microcontroller Interrupt Sub-system
  6. AVR Microcontroller Timer/Counter
  7. AVR Microcontroller Serial USART
  8. AVR Microcontroller Interfacing
  9. AVR Microcontroller Projects
AVR Assembly Code looks like this:
; This is an AVR assembly code
.include "m8515def.inc"
.org $00
 
ADD R16, R17 ; Add value in R16 to value in R17
DEC R17 ; Minus 1 from the value contained in R17
MOV R18, R16 ; Copy the value in R16 to R18
END: JMP END ; Jump to the label END
AVR C Code looks like this:
/*This is an AVR C code*/
int main()
{
int i = 0;
for (i = 0; i < n; i++)
{
USARTWriteChar(string[i]);
}
}
Some AVR Chips:
AVR Tutorials microcontroller images
AVR Tutorials hopes these tutorials are beneficial to you and looks forward to your continued visits for all your microcontroller tutorial needs.

General & Miscellaneous Topics


Welcome to the general and miscellaneous topic page of AVR Tutorials. This page presents various topic related to AVR microcontrollers which are not covered in the categories listed above. The above topics give information on the topics covered. Although these topics are general to AVR microcontrollers they can be related to other microcontrollers .


ProjectBandya.com hopes that these AVR tutorials are beneficial to you and looks forward to your continued visit for all your microcontroller tutorial needs.




Microcontrollers Basics

Tutorial Objectives


After completing this AVR microcontroller tutorial readers should be able to:
  • Give a technical definition for the term microcontroller.
  • List the components that constitute a microcontroller.
  • Explain the functions of the microcontroller's components.
  • List at least three of the major developer/manufacturer of popular microcontrollers.

Microcontroller Overview & Basic Layout


Definition: A microcontroller is a single chip containing at least a CPU, non-volatile memory, volatile memory, a timer and an I/O control unit.


Basically a microcontroller can be described as a computer on a chip. The difference between a microcontroller and a regular PC is that the PC is a general purpose computer while a microcontroller is a computer dedicated to one or just a few tasks.


A microcontroller apart from the above mentioned components usually also include, but not limited to, serial communication capabilities, interrupt controls and analog I/O capabilities. The figure below gives the basic layout of a general AVR microcontroller.
Major manufacturers of microcontrollers include:
  • Atmel Corp. - Manufacturers of the popular AVR series of microcontrollers.
  • Microchip Technology Inc. - Manufacturers of the PIC series of microcontrollers.
  • Texas Instruments Inc. - One of the world's largest semiconductor companies..
  • Renesas Technology Corp. - A joint merger of Hitachi and Mitsubishi product lines.

Microcontroller Components/Peripherials


Processor - The processor refers to the Central Processing Unit (CPU) of the microcontroller. It contains the Arithmetic Logic Unit (ALU), Control Unit, Instruction Decoder and some Special Registers (Stack Pointer, Status Register, Program Counter, etc.).


Volatile Memory - This is memory used by ht microcontroller for temporary data storage, system setup and peripherals configurations. Memory in this category includes SRAM and DRAM. AVR microcontrollers utilize SRAM.


Non-Volatile Memory - This is memory used by the microcontroller to store programs. Data can also be stored in this memory but the access time is much slower than that of RAM. Memory in this category includes ROM, PROM, EPROM, EEPROM and FLASH. The AVR microcontrollers utilize Flash for program storage, some AVR controllers contains a bit of EEPROM as well.


Timer Module - Most microcontrollers have at least one timer/counter peripheral. Timer/Counter modules are used to perform timing or counting operations in the controller. These include time stamping, measuring intervals, counting events, etc.


Interrupt Module - Interrupts enable the microcontroller to monitor certain events in the background while executing and application program and react to the event if necessary pausing the original program. This is all coordinated by the interrupt module.


Digital I/O Module - This module allows digital/logic communication with the microcontroller and the external world. Communication signals are that of TTL or CMOS logic.


Analog I/O Modules - These modules are use to input/output analog information from/to the external world. Analog modules include Analog Comparators and Analog-to-Digital Converters.


Serial Modules - These modules are used for serial communication with the external world. An example is the USART peripherial which utilizes the RS232 standard.




Microcontroller vs Microprocessor

Tutorial Objectives


After completing this AVR microcontroller tutorial readers should be able to:
  • Give a technical definition for the term microcontroller.
  • Explain the major differences between a microcontroller and a microprocessor.
  • Discuss the advantages of a similar microcontroller vs microprocessor product.

Microcontroller vs Microprocessor



What is a microcontroller?


Microcontroller is basically a computer on a chip. It differs form normal desktop or laptop computers in that a microcontroller is an application specific computer that usually runs a single program performing dedicated task(s) while the the later two are general purpose computers that can run numerous programs depending on a users needs. A microcontroller contains on chip CPU, input/output interface, memory, clock, timer, and an assortment of of other peripherals.


Microprocessor on the other hand is just a CPU one has to add externally memory, clock, input/output interfaces, timer and all other needed peripheral. This is the reason a microprocessor has so many pins.

Advantages of a similar microcontroller vs microprocessor product

  1. Product smaller - A microcontroller product has many components on one chip and so is more compact. With a microprocessor product one have to add several other chips and so its bulky.
  2. Cheaper - All the components on a microcontroller is manufacturer on a single chip and so is mush cheaper than having to manufacture several chips as is the case for a microprocessor system.

    Introduction to the Program Counter


    What is the program counter?

    The Program Counter (PC) is a register that is apart of all central processing unit (CPU) or microprocessor. All microcontrollers contains a microprocessor and thus has a program counter. The purpose of the program counter is to hold/store the address of the next instruction to be executed by the microcontroller's microprocessor.
    The size (width) of the program counter of a microcontroller is measured in bits and is directly related to the size of the microcontroller's program memory.

    Determining the size of the AVR Program Counter


    As mentioned above the purpose of the program counter (PC) is to hold the address of the next instruction to be executed by the CPU. Instructions are stores in the program memory of a microcontroller and thus the PC width is directly related to the size of the micorcontroller program memory. As an example lets determine the width of the program counter for the ATMega16 and ATMega32 AVR microcontrollers.


    The width(size) of the program counter (PC) is basically the smallest possibly number of bits necessary to address the microcontroller program memory based on its organisation. Lets say n is the width of the PC then n would be related to the size of the program memory by the following equation:

    2n = size of program memory
    n = log(size of program memory)/(log2)


    For the ATMega8515 microcontroller the program memory is 8k-Bytes organised as 4k-Words. As such the width of the ATMega8515 microcontroller Program Counter n is given by:
    2n = 4k = 4x1024
    n = log(4096)/log(2) = 12bits


    For the ATMega16 microcontroller the program memory is 16k-Bytes organised as 8k-Words. As such the width of the ATMega16 microcontroller Program Counter n is given by:
    2n = 8k = 8x1024
    n = log(8192)/log(2) = 13bits

Tutorial Objectives


After completing this AVR microcontroller tutorial readers should be able to:
  • Give a technical definition for the term memory map.
  • Draw the memory map of an AVR microcontroller.
  • Extract the necessary information for a microcontroller's datasheet to create its memory map.
  • Give a technical definition for the term stack.
  • Give a technical definition for the term stack pointer.
  • Explain the operation of the stack and stack pointer.
  • Write assembly code to initialize an AVR stack pointer.

AVR Microcontrollers Memory Map


What is a memory map?
The memory map of a microcontroller is a diagram which gives the size, type and layout of the memories that are available in the microcontroller. The information use to construct the memory map is extracted from the datasheet of the microcontroller.


The diagram below gives the memory map for the ATMega8515 AVR 8-bit Microcontroller from Atmel. The ATMega8515 microcontroller contains three(3) blocks of memory: Program Memory, EEPROM Memory and Data Memory.
Data Memory Contains:
  • 32 8-bits General Purpose
  • 64 8-bits Input/Output Registers
  • 512 8-bits SRAM space
Program Memory Contains:
  • 8K byte Flash Memory
  • Organized as 4K-16bits space
EEPROM Memory Contains:
  • 512 8-bits EEPROM space
The information above was extracted from the ATMega81515 Data Sheet Summary. The full data sheet can be obtain on Atmel's website.
Notes:
  1. Although this map includes the EEPROM as part of the memory map its is seen by some as a peripheral and not included as part of the map.
  2. This memory map is basic. We could expand the General Purpose and Input/Output giving more details.
  3. The external memory option could also be included in the memory map.
  4. Each section can also be seen as a memory map in and of itself. Namely the Data Memory Map, theProgram Memory Map and the EEPROM Memory Map.

AVR Stack & Stack Pointer



What is a stack?

stack is a consecutive block of data memory allocated by the programmer. This block of memory can be use both by the microcontroller internal control as well as the programmer to store data temporarily. The stack operates with a Last In First Out (LIFO) mechanism, i.e the last thing store on the stack is the first thing to be retrieved from the stack.

What is the Stack Pointer?

The stack pointer is basically a register or registers that holds either "the memory address of the last location on that stack where data was stored" or "the memory address of the next available location on the stack to store data." The definition of the stack pointer depends on the design of the microcontroller.
In AVR microcontrollers, such as the ATMega8515, ATMega16, ATTiny13, etc., the stack pointer holds the address on the next available location on the stack available to store data.

The AVR Stack Pointer

The AVR 8-bits microcontroller stack pointer can either consist of a single I/O register SPL (Stack Pointer Low) or two (2) I/O registers SPL and SPH (Stack Pointer High). The size of the stack pointer depends on the amount of data memory a microntroller contains. If the entire data memory can be addressed using 8-bits then the stack pointer is 8-bits wide i.e. SPL only, otherwise the stack pointer is consist of SPL and SPH.

Setting-up the AVR Stack


A programmer set up the stack on a microcontroller by loading the start address of the stack into the microcontroller stack pointer. The code below shows how to set up the stack in an AVR ATMega8515 microcontroller. Here the address being loaded is that of the last memory location in SRAM. This code could also be used for the ATMega16 , ATMega32 or any one of the AVR microcontrooler with a 16-bits stack pointer, you just of the replace the"m8515def.inc" include file with "m16def.inc" or "m32def.inc" respectively.


Notes:
  1. RAMEND is a label that represents the address of the last memory location in SRAM. To use this label youMUST ensure that you include the definition header file for the specific microcontroller.
  2. The functions low() and high() are use by the assembler to return the low byte and high byte respectively of a 16-bit word. Remember we are dealing with a 8-bit microcontroller which can only handle only 8-bits at a time. RAMEND is a 16-bit word and so we use the functions to split it.
.include "m8515def.inc"
 
LDI R16, low(RAMEND)
OUT SPL, R16
LDI R16, high(RAMEND)
OUT SPH, R16

AVR Microcontroller's Stack Operation


As mentioned earlier the stack can be use by both the microcontroller internal control and the programmer. The programmer accesses the stack using the PUSH and POP instructions. The PUSH instruction is use to store data on the stack while the POP instruction is used to retrieve data from the stack. The operation of the AVR stack will be discussed using the code that follows.


Notes:
Notice here the microcontroller being used in the code below is the Atmel ATTiny2313 AVR microcontroller. This AVR microcontroller's data memory can be addressed using 8-bits and therefore its stack pointer is defined only by the SPL.
/*
* Compatibly in both AVR Studio 5 & AVR Studio 6
*
* Created: 10/15/2011 2:44:24 PM
* Author: AVR Tutorials
*/
 
.include "tn2313def.inc"
 
;Set up AVR ATTiny2313 stack
LDI R16, RAMEND
OUT SPL, R16
 
LDI R16, 0x33
LDI R17, 0x25
LDI R18, 0x0A
 
PUSH R16
PUSH R17
POP R17
PUSH R18
 
 
end: RJMP end

Atmel AVR Microcontroller Stack Operation
Figure 1 - Initial Stack
Atmel AVR Microcontroller Stack Operation
Figure 2 - Stack After First PUSH
Atmel AVR Microcontroller Stack Operation
Figure 3 - Stack After Second PUSH
Atmel AVR Microcontroller Stack Operation
Figure 4 - Stack After POP
Atmel AVR Microcontroller Stack Operation
Figure 5 - Stack After Third PUSH

projectbandya

1 comment:

  1. I have found that this site is very informative, interesting and very well written. keep up the nice high quality writing. Suva beauty promo code

    ReplyDelete