An Open-Source platform to create digital devices and interactive objects that sense and contol physical devices.
An AVR-based Arduino is a physical platform for an AVR Microcontroller. To understand the low-level details of an Arduino requires an understanding of the AVR device that powers it. The Arduino hardware is nothing more than a PCB for an AVR chip to sit on.
Birth of the Microcontroller
In 1971, around the same time that Intel invented the world's first microprocessor, Gary Boone and Michael Cochran invented the world's first microcontroller for Texas Instruments. Before passing at the age of 68 in 2013, Gary Boone conducted an oral history with the Institute of Electrical & Electronics Engineers which you can view here. According to him:
- A microprocessor is "a processor on a single chip. A processor has an instruction set and it operates on data, according to a program. The data, the program, and the input and output are arranged externally to that single chip."
- A microcontroller "attempts to provide a self-contained system on a single chip. In addition to a processor, it also includes memory for data, memory for program, an ability to deal with inputs like keyboard inputs, and an ability to provide outputs like numeric or alpha-numeric display outputs."
The tradeoff of having a microcontroller contain all those things is that the amount of processing power would be typically less than a microprocessor of equal price. A microprocessor, unlike a microcontroller, is not a self-contained computer on a chip. It cannot operate without the help of other ICs like RAMs, ROMs, and I/O chips to interact with other components.
Unlike the microprocessor, The worlds first microcontroller, the TMS 1000, did not go to general market immediately but was introduced in a TI calculator in 1972. It was made available to the general market in 1974. The TMS 1000, was cheap enough to be used in everything from Texas Instrument's own calculators, microwave ovens, washers, jukeboxes, video games, toys, and many other consumer electronics.
Today, Texas Instruments has a wide variety of microcontrollers available, some with specific hardware on board to specialize them for more specific tasks. Often a user must set data registers manually, which can involve reading 600+ page data sheets to find the proper line of code or variable. While these board may offer more than Arduino, they are require more time and recourse.
Here are some common terms you are likely to come across when evaluating microcontrollers:
- MCU - microcontroller
- MIPs - million instructions per second: a measure of the speed and power of the processor
- CPU - basic instruction processing unit in the microcontroller
- DSP - digital signaling processor: chip designed for data intensive mathematical calculations
- RISC - reduced instruction set computing
- CISC - complex instruction set computing
- SOC - system on a chip
- ADC - analog-to-digital converter
- DAC - digital-to-analog converter
- RAM - random access memory
- FRAM - ferroelectric RAM
- UART - universal asynchronous receiver transmitter: translates between parallel and serial data
- SPI - serial peripheral interface: a synchronous serial data bus
- PWM - pulse width modulation: modulation technique used mostly for controlling motors
The low-cost of the microcontroller and all-in-one approach made it economical to make what is now known as "embedded" computer application. One of the first applications of the microcontroller were in gas pumps where it was used for metering and starting of the pump. Also, it was used in microwave ovens where it was used to monitor the keypad, clock, and oven settings. Now, microcontrollers are the top selling chip of any kind. Your home probably has at least 20 microcontrollers and your automobile has over a dozen. They can be found in tools, toys, toothbrushes, and much more.
Memory Architectures: Harvard vs Von Neumann
In the early days of computing, around the time of the World War II, the US government asked Harvard and Princeton Universities to design computer architecture for computation of naval artillery shell distances for varying elevations and environmental conditions.
The ENIAC (Electronic Numerical Integrator and Computer) was among the earliest electronic general-purpose computers made. Among the first programs included the feasibility of the thermonuclear weapon (think Wargames).
It uses the same memory and data paths for both program and data storage.
Von Neumann Architecture:
Harvard's answer was the Mark 1 which read instructions from a 24-channel punched paper tape. It executed instruction and then read in teh next one. A separate tape contained numbers for input but the tape formats were not interchangeable. Instructions could not be executed from the storage registers. The separation of data and instructions is known as the Harvard Architecture.
The Princeton architecture was better suited for the time because using one memory was preferable because of the unreliability of current electronics (before the widespread use of transistors). A single memory interface would have fewer things go wrong.
The Harvard architecture was largely ingured until the late 1970s when microcontroller manufacturers realized the advantages for the devices they were designing.
The Von Neumann architecture's biggest advantage is that it simplifies microcontroller design because only one memory is accessed. Meanwhile, the Harvard architecture executes instructions in fewer instruction cycles than the Von Neumann architecture. Each architecture has its advantage. The Harvard model has the edge on performance while the the Von Neumann is more flexible.
The AVR Microcontroller
In the early 90s, the AVR line of microcontrollers was born from a student project at the Norwegian Institute of Technology. Alf-Egil Bogen and Vegard Wollan devised an 8-bit device with a RISC-type internal architecture. They would continue working and refining the design once the AVR design was sold to Atmel. The AVR is a modified Harvard Architecture RISC microcontroller. They are highly configureable and versatile. It has several features that set it apart from 8 bit microcontrollers of the time such as the 8051 by Intel and the 68HC05 by Motorola.
The AVR line was of devices was one of the first to incorporate on-board flash memory for program storage, as opposed to relying on one-time programmable ROM (read-only memory), EPROM (erasable programmable read-only memory), or EEPROM (electrically erasable programmable read-only memory) as was used with other microcontrollers of the time. This made reprogramming the AVR microcontroller a simple matter of loading program code into the devices internal flash memory.
Most AVR parts use a small amount of EEPROM for storing things like operating parameters that must persist between changes in the flash memory.
Although Atmel says that AVR does not stand for anything and its not an acronym, it's generally accepted that AVR stands for Alf and Vegard's RISC processor.
AVR is a modified Harvard Architecture whereby program and data are stored in separate physical memory systems that appear in different address spaces but have the ability to read data items from program memory using instructions.
There are three categories of AVR microcontrollers:
- ATtiny (Tiny AVR) - 6-32 pins, .5-8 KB of Flash Memory, small in size, ideal for simpler applications
- ATmega (Mega AVR) - 6-32 pins, 4- 256 KB of memory, more inbuilt peripherals, ideal for modest to difficult applications
- ATXmega (Xmega AVR) - 44-100 pins, 16-384 KB, used commercially for applications requiring large program memory and high speed; DMA, Event System Included
- 131 instructions
- 32 8-bit general purpose registers
- Up to 20 Mhz clock rate (20 MIPS operation)
Based on the Harvard architecture, every IC has two buses, an instruction bus and a data bus.
The CPU core consists of the ALU, General Purpus Registers, Program Counter, Instruction Register, Status Register, and Stack Pointer.
The 8-bit CPU controls peripheral functions via an internal high-speed data bus.
AVR devices contain three types of memory:
- Flash - used to store programs and initialize any data. You can execute program code from flash but can not modify data in the Flash Memory from your executing code. To modify data, it needs to be copied into SRAM (up to 256 K)
- SRAM (static random-access memory) - (upused to hold transient data such as the program variables and the stack (up to 32K)
- EEPROM - holds data that needs to persist between software changes and power cycles. (up to 4K)
Flash and EEPROM can be loaded externally and will retain its content when the AVR is powered off
The SRAM is voltatile and its contents will be lost when the AVR loses power.
All peripheral functions share port pins with discrete digital I/O capabilities.
The 8 bit CPU has built in peripheral functions integrated into the CPU logic.
These functions vary from one type of AVR device to another but include the following:
- bidirectional I/O pins for discrete digital signals
In addition to the 32 8-bit general-purpose registers in the CPU, a number of control registers determine how the I/O ports, timers, interfaces, and other features behave.
The settings on the control registers allow most of the pins on an AVR microcontroller to be configured to perform specific functions. The pins are dynamically configurable making it possible for the pins to perform one type function and then perform a different function once the control register value has been modified.
Digital I/O Ports
The digital I/O ports allow digital communication or logic communication with the external world. Communication signals are TTL/CMOS logic.
AVR microcontrollers use bidirectional I/O ports.
A port is an 8-bit register wherein some or all the bits connect to physical pins on the AVR package. The pins of the port are controlled by internal logic that manages signal direction, state of the internal pull-up resistor, timing, etc. The logic is sophisticated enough to all ports to perform many different functions, some simultaneously.
It is still possible to read data from a port that is configured as an output, and an output can be used to trigger an interrupt. Ports are labeled as A, B, C, etc.
8-bit / 16 bit Timer Counters
An AVR microcontroller has 8- and 16-bit timers. When a timer reaches its maximum value (255 in 8-bit and 65535 in 16-bit counters), the next cycle will generate an overflow of the value using a prescaler.
Two forms of 8-bit timers are available in AVR microcontrollers:
- synchronous mode - clock input is derived from the primary system clock
- asynchronous mode - the ability to operate using an external clock source (TOS1 and TOS2 clock input pins)
The 8-bit timer is a general purpose timer that includes the following modes of operation:
- Normal Mode - the count always increments and is not cleared when the counter reaches its maximum 8-bit value. If this happens then the counter overflows and returns to zero and the (TOV0) overflow flag is set. It is set, not cleared by the timer overflow. The timer overflow interrupt automatically clears the overflow flag bit, and the interrupt can be used to increment a second software-based counter in memory.
- Clear Timer on Compare (CTC) - this allows greater control of the compare match output frequency and helps to simplify external event counting
- Fast PWM mode - supports high-frequency PWM waveform generation
- Phase Correct PWM mode - a hi-res phase correct PWM waveform generation option
16-bit timer/counters are similar to the 8-bit version with extended count range. True 16-bit logic can be used for hires external event capture, frequency generation, and signal timing measurement. It can generate for different interrupts (TOV1, OCF1A, OCF1B, and ICF1)
In electronics, comparators are used to compare two voltages or currents and outputs a digital signal to indicate which is larger. It has two analog input terminals and one binary digital output.
The analog comparator of an AVR microcontrollor is used to compare input voltages in the A1N0 and AIN1 pins. AIN0 is set as the positive input and AIN1 is the negative (refers to the relationship and not the polarity). This circuit can compare the voltages between these two inputs as well as be configured to do tother things such as compare AIN1 input to the internal bandgap reference voltage or compare AIN0 with the output of the ADC multiplexer.
The world around us is analog so to communicate with an analog world in a digital form requires the use of an analog-digital-converter. It converts an analog voltage on a pin to a digital number. AVR microcontrollers include 8-bit, 10-bit, or 12-bit analog to digital converters. An ATtiny 6 has 8-bit converters while an Arduino Uno has 10-bit. A 10-bit ADC means it has the ability to detect 1024 (2^10) discrete analog levels. An ADC that is included in an AVR design has anywhere from 4 to 28 inputs. ATmega168 devices have between 6 to 8 ADC input channels depending on the package type.
The ATMega168 provides three primary forms of serial communication:
- UART/USART - synchronous/asynchronous serial
- All arduino boards have a serial port (aka UART or ASART) that communicate on ditital pins 0 (RX) and 1 (TX) as well as with the computer via USB
- Pins TX/RX use TTL logic levels (5 V or 3.3 volts depending on the board
- SPI master/slave synchronous - I/O pins may be configured to act as
- TWI (two wire interface) - compatible with Philips I2C protocol. Supports master and slave modes and a 7-bit device address.
Interrupts are events that require immediate attention by the microcontroller. This happens when the microcontroller pauses from its current task and attends to the interrupt by executing an Interrupt Service Routine (SSR). Afterwards, it returns to its task its paused and continues normal operation. In the AVR an interrupt response may be enabled or disabled via bits in the control register.