Quick viewing(Text Mode)

AVR Microcontrollers- Introduction

AVR Microcontrollers- Introduction

Microprocessors, Lecture 3:

AVR - Introduction AVR Microcontrollers y Widely-used y Different families y Based on the application and capacity y Classic AVR y e.g. AT90S2313, AT90S4433 y Mega y e.g. ATmega8, ATmega32, ATmega128 y Tiny y e.g. ATtiny13, ATtiny25 y Special Purpose AVR y e.g. AT90PWM216,AT90USB1287

2 AVR internal architecture

RAM EEPROM Timers

PROGRAM ROM Program Bus CPU Bus

Interrupt Other OSC Ports Unit Peripherals

I/O PINS

3 3 AVR’s CPU y CPU: y RISC architecture y 131 instructions y Most instructions are executed in a single cycle y 32 general-purpose registers y 64 IO registers y Many useful peripherals y Very low-power y less than 10mW power consumption

4 AVR’s CPU y AVR’s CPU R0 ALU R1 y ALU R2 … y 32 General Purpose registers (R0 to R31) SREG: I T H SVN Z R15 y PC register R16 y Instruction decoder CPU R17 … y Almost the same for all PC families Instruction decoder R30 R31 Instruction Register registers

5 5 SREG flags

y Not all instructions affect flags y Example: load instruction has nothing to do with flags

6 Decision making by flags

7 Memory address space y A unified address space y 32 general-purpose registers y Directly connected to the ALU y 64 IO registers y To keep the data sent to/ received from peripherals y Stack y Starting from the end of SRAM and grow up to lower addresses y Indexed by SPL-SPH registers

8 Internal busses

9 Some simple instructions 1. Loading values into the general purpose registers LDI (Load Immediate) y LDI Rd, k y Its equivalent in high level languages: Rd = k y Example: LDI R16,53 y R16 = 53 y LDS (Load from SRAM) y LDS R0,0x300 y Load R0 by data from address 300

10 10 Some simple instructions 2. Arithmetic calculation

y There are some instructions for doing arithmetic and logic operations; such as: ADD, SUB, MUL, AND, etc. y ADD Rd,Rs y Rd = Rd + Rs y Example: y ADD R25, R9 y R25 = R25 + R9 y ADD R17,R30 y R17 = R17 + R30

11 11 Some simple instructions 3. store

y No immediate value store! Just stores can be done through registers

12 12 Some simple instructions 3. IN and OUT

y Each IO register can be addressed in two ways: y Memory address y IO address (relative to the beginning of the IO registers) y “IN” and “OUT” use the IO address y example: IN r1,0x16 ;// copy IO register no. 10 (memory address 0x36 to r1)

13 13 Some other instructions

14 ALU instructions

15 Single operand instructions

16 Directives y Like directives in high level languages y For core readability y Not instructions that generate machine code after compile y Example: EQU y To set a constant value y Equivalent to CONSTANT in C++

17 Other directives

18 AVR data size y AVR just has 8-bit data y or has to break larger data into 8-bit units

19 A simple program y Write a program that calculates 19 + 95

LDI R16, 19 ;R16 = 19 LDI R20, 95 ;R20 = 95 ADD R16, R20 ;R16 = R16 + R20

R0 ALU R1 R2 …

SREG: I T H SVN Z C R15 R16 CPU R17 PC …

Instruction decoder R30 R31 Instruction Register registers

20 20 Code memory y The program after compiling is stored in a ROM (flash memory) y To keep the code even when the system is powered off y Data is stored in another memory (SRAM) y In AVR each code memory location is 2- y In Atmega32, 32K flash memory is organized as 16Kx16 words y Needs 14-bit PC

21 Instruction size y Almost all instructions are 2 bytes y Example:

y 1110: machine code for LDI y Kkkk…= imidiate value y dddd= destination register

22 Another example

23 32-bit instructions

24 ATmega32 y 16 MHz clock frequency y 44 pins y 32 KB instruction memory (Flash) y 1KB data memory (EEPROM) y 2048 B data memory (SRAM) y SPI, USART, and I2C serial ports y 3 timers y 8 10-bit ADC channels y Analog comparator y 4 PWM ports y …….

25 ATMega32 pins

y 4 8-bit ports y PA, PB, PC, PD y Multiplexed with other in-outs

26 AVR programming y Assembly: AVR Studio y C: Code Vision

27