بسم هللا الرمحن الرحيم Microcontrollers and Embedded Systems ( Lecture 2 : MC) Microcontroller And Embedded System by Dr. Ayman Mohamed Microcontrollers and Embedded Systems ( Lecture 2 : MC) AVR Microcontroller ▪ AVR Microcontrollers is a Family of RISC Microcontrollers from Atmel designed in 1996. ▪ AVR stand for? o Advanced Virtual RISC, or it refers to the founders are Alf Egil Bogen and Vegard Wollan RISC ▪ AVR architecture was conceived by two students at Norwegian Institute of Technology (NTH) and further refined and developed at Atmel Norway. ▪ The AVR is a Harvard architecture CPU. o The AVR is a Harvard architecture CPU, Program Memory is separated from data Memory ▪ Program memory is accessed with a single level pipelining (Fetch & execute). Microcontrollers and Embedded Systems ( Lecture 2 : MC) AVR Microcontroller The AVR is a Harvard architecture CPU. Harvard Architecture ▪ Computer architectures that used physically separate storage and signal pathways for their instructions and data. ▪ CPU can read both an instruction and data from memory at the same time that makes it faster. von Neumann architecture ▪ CPU can Read an instruction or data from/to the memory. ▪ Read, Write can`t occur at the same time due to same memory and signal pathway for data and instructions. Microcontrollers and Embedded Systems ( Lecture 2 : MC) What’s special about AVR? ▪ ▪ ▪ ▪ AVR is an 8-bit microcontroller belonging to the family of Reduced Instruction Set Computer (RISC). ❑ In RISC architecture the instruction set of the computer is not only fewer in number but also simpler and faster operation. This means that the microcontroller is capable of transmitting and receiving 8-bit data. ❑ The input/output registers available are of 8-bits. ❑ The AVR family controllers have a register-based architecture which means that both the operands for an operation are stored in a register and the result of the operation is also stored in a register. All AVR microcontrollers share the same instruction set and a basic CPU (Harvard) architecture. It has 32 8-Bit general purpose registers. Mostly instruction Execute in Single clock cycle. Which makes it faster among 8- bit microcontrollers. AVR was designed for efficient execution of compiled C code. Microcontrollers and Embedded Systems ( Lecture 2 : MC) AVR microcontrollers categories • TinyAVR: Less memory, small size, suitable only applications for simpler • MegaAVR : These are the most popular ones having good amount of memory (up to 256 KB), higher number of inbuilt peripherals and suitable for moderate to complex applications. • XmegaAVR:Used commercially for complex require large program memory and high speed. applications, which Series Name Pins Flash memory Special feature TinyAVR 6-32 0.5-8 KB Small in size MegaAVR 28-100 4-256 KB Extended Perpherals XmegaAVR 44-100 16-384 KB DMA, Event system included Microcontrollers and Embedded Systems ( Lecture 2 : MC) Various microcontroller of Mega AVR series ATmega8, and Atmega32 are other members of MegaAVR series controllers. They are quite similar to ATmega16 in architecture. Low power version MegaAVR controllers are also available in markets. The following table shows the comparison between different members of MegaAVR family: Microcontrollers and Embedded Systems ( Lecture 2 : MC) Comparison between Microcontrollers 8051 PIC AVR Speed Slow Moderate Fast (4 times faster than PIC) Memory Small Large Large Architecture CISC RISC RISC ADC Not Present Built-in Built-in Timers Built-in Built-in Built-in PWM Channels Not Present Built-in Built-in Microcontrollers and Embedded Systems ( Lecture 2 : MC) AVR Microcontroller • The AT refers to Atmel the manufacturer, • Mega means that the microcontroller belong to MegaAVR category, • 16 signifies the memory of the controller, which is 16KB. There are two flavors for Atmega16 microcontroller: 1.Atmega16:- Operating frequency range is 0 – 16 MHz. 2.Atmega16L:- Operating frequency range is 0 – 8 MHz. If we are using a crystal of 8 MHz = 8 x 106 Hertz = 8 Million cycles, then AVR can execute 8 million instructions. Microcontrollers and Embedded Systems ( Lecture 2 : MC) AVR Microcontroller AVR microcontroller executes most of the instructions in single execution cycle, so that they are fast microcontroller. • It means that AVR can execute 1 million instructions per second if cycle frequency is 1MHz. • The higher is the operating frequency of the controller, the higher will be its processing speed. So that we need to optimize the power consumption with processing speed and hence need to select the operating frequency accordingly. AVRs are about 4 times faster than PICs, they consume less power and can be operated in different power saving modes. The comparison between the three most commonly used families of microcontrollers. Microcontrollers and Embedded Systems ( Lecture 2 : MC) Architecture of Atmega16 AVR The AVR microcontrollers are based on the advanced RISC architecture and consist of 32 x 8-bit general purpose working registers. Within one single clock cycle, AVR can take inputs from two general purpose registers and put them to ALU for carrying out the requested operation, and transfer back the result to an arbitrary register. The ALU can perform arithmetic as well as logical operations over the inputs from the register or between the register and a constant. Single register operations like taking a complement can also be executed in ALU. We can see that AVR does not have any register like accumulator as in 8051 family of microcontrollers; the operations can be performed between any of the registers and can be stored in either of them. Microcontrollers and Embedded Systems ( Lecture 2 : MC) Simplified View of an AVR Microcontroller Microcontrollers and Embedded Systems ( Lecture 2 : MC) Architecture Diagram: Atmega16 Microcontrollers and Embedded Systems ( Lecture 2 : MC) Atmega16 AVR In our journey with the AVR we will be working on Atmega16 microcontroller, which is a 40-pin IC and belongs to the megaAVR category of AVRfamily. Some of the features of Atmega16 are: ⚫ 16KB of Flash EEPROM memory ⚫ 1KB of SRAM (data Memory) ⚫ 512 Bytes of EEPROM ⚫ Available in 40-Pin DIP ⚫ 8-Channel 10-bit ADC ⚫ Two 8-bit Timers/Counters ⚫ One 16-bit Timer/Counter ⚫ 4 PWM Channels ⚫ In System Programmer (ISP) ⚫ Serial USART ⚫ SPI Interface ⚫ Digital to Analog Comparator. Microcontrollers and Embedded Systems ( Lecture 2 : MC) Atmega16 AVR Pin Diagram ❑ This microcontroller has 40 pins and each pin has its importance. In these 40 pins, I/O pins are 32. And these are categorized into 4 ports. Each port has 8 I/O pins. • PORT-A: 8 pins (PIN 33 to 40). This port A acts as an analog input to the A/D converter. Port A can be used as an 8-bit bidirectional I/O port. It has an internal pull-up resistor. • PORT–B: It has pins from 1 to 8. This port B is used for I/O bidirectional pins. • PORT–C: It has pins from 22 to 29. This port has eight I/O bidirectional pins. • PORT–D: It has pins from 14 to 21. it can be used as an input or output pin. The extra peripherals like PWM channels, timer/counter, and USART are connected to this port. • RESET – Pin 9 is the for Reset pin. • Pin 10 – This pin is used for power supply purposes. By this pin, a power supply of 5V can be connected to the microcontroller. • Pin 12 & Pin 13 – High clock pulses can be generated by a crystal oscillator. And this crystal oscillator is connected to these pins. This microcontroller works at the 1MHz frequency. Microcontrollers and Embedded Systems ( Lecture 2 : MC) Architecture Diagram: Atmega16 Internal Calibrated Oscillator: Atmega16 is equipped with an internal oscillator for driving its clock. o By default Atmega16 its clock is set to operate at an internal calibrated oscillator of 1 MHz. o The maximum frequency of the internal oscillator is 8Mhz. o Alternatively, ATmega16 can be operated using an external crystal oscillator with a maximum frequency of 16MHz. In this case you need to modify the fuse bits. Microcontrollers and Embedded Systems ( Lecture 2 : MC) Architecture Diagram: Atmega16 Analog to Digital Converter (ADC) Interface: Atmega16 is equipped with an 8 channel ADC with a resolution of 10- bits. ADC reads the analog input for e.g., a sensor input and converts it into digital information which is understandable by the microcontroller. Digital to Analog Converter (DAC) Interface: AVR is also equipped with a interface which can be used for reverse action performed by ADC. DAC can be used when there is a need of converting a digital signal to analog signal. DAC is not present in Atmega16. Microcontrollers and Embedded Systems ( Lecture 2 : MC) Architecture Diagram: Atmega16 Timers/Counters: Atmega16 consists of two 8-bit and one 16-bit timer/counter. Timers are useful for generating precision actions for e.g., creating time delays between two operations. Watchdog Timer: Watchdog timer is present with internal oscillator. Watchdog timer continuously monitors and resets the controller if the code gets stuck at any execution action for more than a defined time interval. Interrupts: Atmega16 consists of 21 interrupt sources out of which four are external. The remaining are internal interrupts which support the peripherals like USART, ADC, Timers etc. Universal Synchronous and Asynchronous Receiver & Transmitter (USART) interface is available for interfacing with external device capable of communicating serially (data transmission bit by bit). It is used to transmit and receive serial data Microcontrollers and Embedded Systems ( Lecture 2 : MC) Architecture Diagram: Atmega16 In System Programmable (ISP) Flash: AVR family of controllers have Memory which can be programmed without removing the IC from the circuit, ISP allows to reprogram the controller while it is in the application circuit. Serial Peripheral Interface (SPI): SPI port is used for serial communication between two devices on a common clock source. The data transmission rate of SPI is more than that of USART. Two Wire Interface (TWI) can be used to set up a network of devices, many devices can be connected over TWI interface forming a network, the devices can simultaneously transmit and receive and have their own unique address. Microcontrollers and Embedded Systems ( Lecture 2 : MC) Architecture Diagram: Atmega16 General Purpose I/O Ports: o Ports are simply the gates through which the CPU interacts with the outside world Each port has 3 control registers associated with it. o Atmega16 has four (PORTA, PORTB, PORTC and PORTD) 8-bit input-output ports. o The Data Direction Register (DDR) bit tells a leg to act as an input (0), or output (1). o The PORT (Pin Output / write) The PIN (Port INput) register is read only. Microcontrollers and Embedded Systems ( Lecture 2 : MC) Architecture Diagram: Atmega16 ❑ Memory: Three separate memories o Program Memory (Flask Memory) o Data Memory o EEPROM SRAM (128x8) Microcontrollers and Embedded Systems ( Lecture 2 : MC) Architecture Diagram: Atmega16 Memory: Atmega16 consists of three different memory sections: ⚫ Flash Memory: 16 Kbytes of In-System Self-programmable Flash program memory. ▪ program memory holds interrupt function addresses, 16 bit and doubleword (32 bit) opcode, and static data tables. ▪ It is used to store the program dumped or burnt by the user onto the microcontroller. ▪ It can be easily erased electrically as a single unit. ▪ Flash memory is non-volatile i.e., it retains the program even if the power is cut-off. ▪ Atmega16 is available with 16KB of in-system programmable Flash EEPROM. ⚫ Byte Addressable EEPROM: This is also a nonvolatile memory used to store data like values of certain variables. Atmega16 has 512 bytes of EEPROM, this memory can be useful for storing the lock code if we are designing an application like an electronic door lock. ❑ Microcontrollers and Embedded Systems ( Lecture 2 : MC) Architecture Diagram: Atmega16 ⚫ Data Memory o Used for data and is separate from the program memory. ▪ SRAM o Static Random Access Memory, this is the volatile memory of microcontroller i.e. data is lost as soon as power is turned off. o Atmega16 is equipped with 1KB of internal SRAM. o A small portion of SRAM is set aside for general purpose registers used by CPU and some for the peripheral subsystems of the microcontroller. o Register reassigned the 32 Data Space addresses I/O memory space contains 64 addresses for CPU peripheral functions such as control registers, Timer/Counters, A/D converters I/O functions. I/O memory can be accessed directly or as the Data Space locations those of the Register File. ▪ Stack is effectively allocated in the general data SRAM, and consequently the stack size is only limited by the total SRAM size and the usage of the SRAM. Microcontrollers and Embedded Systems ( Lecture 2 : MC) Architecture Diagram: Atmega16 ❑ Registers: Two types of registers ➢ General Purpose Registers: • Atmega16 is equipped with 32 general purpose registers having storage capacity of 8-Bits Named as R0,R1,R2 to R31. • They are coupled directly with the Arithmetic Logical Unit (ALU) of CPU. • Can store both Data & Addresses. Microcontrollers and Embedded Systems ( Lecture 2 : MC) Architecture Diagram: Atmega16 ❑ Registers: Two types of registers ➢ SPECIAL purpose: Three registers • Stack Pointer • Pointer Register (Program counter) • Status Register ❑ Stack Pointer (SP): o 16-bit stack pointer (SP) holds address in data space of area to save function call information. ❑ Pointer Register o Three 16-bit address registers pairs of registers 26 to 31 have extra meaning in AVR assembly. X (r27:r26), y (r29:r28), z (r31:r30). Microcontrollers and Embedded Systems ( Lecture 2 : MC) Architecture Diagram: Atmega16 ❑ status register (SREG) : o It is 8-bit long each bit has a different meaning. I T H S V N Z C I: Global Interrupt Enable/Disable Flag, SREG7 T: Transfer bit used by BLD and BST instructions, SREG6 H: Half Carry Flag, SREG5 S: For signed tests Instruction Set, SREG4 V: Two's complement overflow indicator, SREG3 N: Negative Flag, SREG2 Z: Zero Flag, SREG1 C: Carry Flag, SREG0 Microcontrollers and Embedded Systems ( Lecture 2 : MC) Questions? Microcontrollers and Embedded Systems ( Lecture 2 : MC)