Microcontrollers
Overview
Microcontrollers are small computers consisting of microprocessor (CPU) and a number of useful peripherals. They are designed to be operated standalone and may not even require a clock source; many microcontrollers have an internal RC oscillator and can operate with just a power supply and a bypass cap.
The distinction between microcontrollers and microprocessors is typically that microcontrollers have an internal memory bus with peripherals connected to it, although some microcontrollers such as AT90USB1286 also expose their bus externally.
Common peripherals
GPIO
General Purpose I/O pins can be used for data input and output, to directly switch small loads such as LEDs, and to switch larger loads through relays and transistors. They typically have two directions:
- Input or Hi-Z: high impedance, used for digital or analog input
- Output: low impedance, used for output (high or low)
Pins are organized in ports consisting of up to 16 pins or so. Each port has two registers associated with it:
- Data direction register, one bit per pin to specify input or output
- Output register, one bit per pin. For input, turns internal pullup on or off. For output, sets output low or high.
Most GPIO pins also have internal pullups when in input mode. The output register bit is typically set to high to enable the pullup. This means that the pin could be directly connected to an SPST switch.
A GPIO pin in output mode is either connected to ground or VCC. This can be used to communicate data, or to power things: you would set the pin high to turn on an LED (assuming the pin is connected through a current limiting resistor to the anode) or switch a load high (assuming an NPN transistor or N-channel MOSFET). GPIO pins have limits on how much current they source or sink, and often also overall limits per port and for the microcontroller as a whole. They can typically sink more current than they can source.
ADC
Some GPIO pins will have an analog-digital converter (ADC). A microcontroller will typically have a single ADC with a multiplexer so it can be connected to many different pins. Voltages are expressed as a fraction of VCC, but some microcontrollers have an internal band gap reference.
Timers and PWM
Timers allow you to execute code at precise intervals. They can be set to trigger a timer interrupt, and so invoke a function you write called an ISR, when the timer overflows or hits a certain threshold.
Some timers can drive PWM on certain output pins more precisely than you can do it manually.
Some microcontrollers also have timers that can be driven from external clock sources other than the main system clock.
Communication
UART
SPI, I2C and 1-wire
USB
Debugging
Power Management
Interrupts
Families of microcontrollers
Atmel AVR8
Atmel's AVR is a very popular and easy to use family of 8-bit microcontrollers. The "8-bit" part refers to the fact that the registers are 8 bits wide; the memory bus is actually 16 bits wide. The ATmega328P chip is used in Arduinos. The CPU is a very efficient Harvard architecture RISC.
Some very low end AVRs have no SRAM. Some high end AVRs have USB support (both host and device) and an externally exposed memory bus.
AVRs can be programmed by in-system serial programming (ISP) using 6 pins: ground, VCC, reset, and the 3 SPI pins. Most AVRs also support a bootloader that provides for programing over the UART. Some very low end AVRs are programming with a 2-wire interface.
Very good open source tools are available. On-chip debugging may require special software from Atmel.
Some common AVR models:
- ATtiny85: available as PDIP8
- ATmega328P: used in Arduino, PDIP28 available, has a reasonable full set of peripherals
- ATmega32U4: USB device support, built-in differential amplifier with programmable gain
- AT90USB1286: USB device support
- AT90USB1287: USB host and OTG support
Cortex-M0 based
Cortex-M0 is a scaled down version of the ARM CPUs that are probably inside your phone or tablet. They have maximum clock speeds in the 50 MHz range. Cortex-M0 microcontrollers are known for providing a lot of capability for a low price, with prices to or lower than similar AVR and PIC parts. For 32-bit math operations, they can be a lot faster than AVRs at similar clock speeds. They can also be slower than similarly clocked AVRs because of the von Neumann architecture, which means that data and instructions can't be fetched simultaneously. The upcoming (as of Fall 2012) Cortex-M0+ architecture should remedy this.
NXP's LPCXpresso is a cheap (sub-$30) board that includes a USB connected JTAG programmer/debugger and a prototyping board for several families of their LPC microcontroller. The LPC1114 is available as a 0.6" wide PDIP28 and may be the only 32-bit microcontroller available as a DIP. LPCXpresso is also an IDE based on Eclipse, limited to 128 KB flash.
The debugger on LPCXpresso is called LPC-Link and is proprietary, so it will only work with the LPCXpresso IDE. Yagarto is an open source toolchain for these parts. Bus Blaster is a JTAG programmer that can be programmed to support Serial Wire Debug, the programming/debugging used by LPC11xx.
Most LPC11xx parts run at 3.3V. Unlike ATmega328P and similar AVR parts, they can run at their full rated clock speed at 3.3V.
STMicroelectronics also has Cortex-M0 based parts in their STM32 line.
Links
Cortex-M3 based
Cortex-M3 is a more powerful microcontroller family with clock speeds around 100 MHz. Most of the parts have built-in USB support. Many STM32 Cortex-M3 parts have a special bootloader mode where the device emulates a disk drive so you can just copy the new binary to the device.
PIC
PIC is a popular family of microcontrollers with an older architecture than AVR. PICs have a good set of peripherals and are often cheaper than equivalent AVRs. They are difficult to program because programming requires a 12V power supply.
Propeller
Propeller is a microcontroller sold at Radio Shack with a novel multicore architecture. It is normally programmed using a special programming language, but C compilers and assemblers are also available.