Microcontrollers

Table of Contents

What is a microcontroller?

A microcontroller is a compact integrated circuit designed to govern a specific operation in an embedded system. It does this by interpreting data it receives from its I/O (Input Output) peripherals using its central processor. The temporary information that the microcontroller receives is stored in its data memory, where the processor accesses it and uses instructions stored in its program memory to decipher and apply the incoming data. It then uses its I/O peripherals to communicate and enact the appropriate action. Microcontrollers are used in multiple industries and applications, including in the home and enterprise, building automation, manufacturing, robotics, automotive, lighting, smart energy, industrial automation, communications and internet of things (IoT) deployments.


Elements of a microcontroller

1: The Processor: A processor can be thought of as the brain of the device. It processes and responds to various instructions that direct the microcontroller's function. This involves performing basic arithmetic, logic and I/O operations. It also performs data transfer operations, which communicate commands to other components in the larger embedded system.

2: Memory : A microcontroller's memory is used to store the data that the processor receives and uses to respond to instructions that it's been programmed to carry out. A microcontroller has two main memory types:

    1. Program memory, which stores long-term information about the instructions that the CPU carries out. Program memory is non-volatile memory, meaning it holds information over time without needing a power source.
    2. Data memory, which is required for temporary data storage while the instructions are being executed. Data memory is volatile, meaning the data it holds is temporary and is only maintained if the device is connected to a power source.

3: I/O peripherals : The input and output devices are the interface for the processor to the outside world. The input ports receive information and send it to the processor in the form of binary data. The processor receives that data and sends the necessary instructions to output devices that execute tasks external to the microcontroller.

4: Analog to Digital Converter (ADC) : An ADC is a circuit that converts analog signals to digital signals. It allows the processor at the center of the microcontroller to interface with external analog devices, such as sensors.

5: Digital to Analog Converter (DAC) : A DAC performs the inverse function of an ADC and allows the processor at the center of the microcontroller to communicate its outgoing signals to external analog components.

6: System bus : The system bus is the connective wire that links all components of the microcontroller together.

7: Serial port : The serial port is one example of an I/O port that allows the microcontroller to connect to external components. It has a similar function to a USB or a parallel port but differs in the way it exchanges bits.


Block Diagram- Microcontroller

Block diagram of Microcontroller with support devices



Peripherals and Interfaces

USART (Universal Synchronous/Asynchronous Receiver/Transmitter)

USART is a peripheral that communicates through a serial interface [16]. This means it communicates by sending and receiving each data bit sequentially, or one bit after another. As its name implies, USART can operate in synchronous or asynchronous modes. This Sparkfun article explains concepts needed when trying to understand the differences between the two modes. 

When operating asynchronously, the microcontroller’s UART generates a clock and syncs the data stream to that internal clock. Therefore, in the asynchronous mode the receiver only uses the data lines to communicate so the receiver must know what the baud rate is beforehand to interpret the data correctly.

When operating synchronously, the microcontroller’s USART generates a clock, syncs the data stream to that clock, and externally transmits that clock. Therefore, in synchronous mode, an additional clock signal line is needed between the devices in addition to the data lines. This is so the receiver can still interpret the data stream correctly without needing  to know the baud rate beforehand by using the clock line. The use of the external clock allows for higher data rates and increases the number of complex protocols the USART peripheral can support. However, UART or USART in asynchronous mode are still more widely used, especially when learning, due to its simplicity and ease of use.

Applications

When developing firmware for embedded systems in IDEs such as MS/AS7, there are no output consoles because the code is run on the microcontroller and not the computer itself. Therefore, a common way for a computer to send commands to and receive feedback from the microcontroller is to use a USB-to-TTL serial converter, bridge, or cable to communicate through a UART serial terminal. In MS/AS7, this Serial Port terminal can be accessed in Data Visualizer. Another popular open-source terminal that can be used is PuTTY.

Adafruit USB to TTL Serial Cable [17]

Image of Adafruit USB to TTL Serial Cable

TWI (Two-Wire Interface) /I2C (Inter-Integrated Circuit)

The TWI peripheral provides a bidirectional two-wire interface consisting of one Serial Data Line (SDA) and one Serial Clock Line (SCL) . TWI is almost identical to I2C, also known as I2C, as the TWI name was introduced by some manufacturers, like Atmel, to avoid trademark conflicts with Phillips/NXP’s I2C .

Applications

The I2C protocol operates in a master-slave architecture where the master generates the clock signal and the slave is synchronized with the master’s clock. These configurations can include multiple masters and/or multiple slaves. As most MCUs contain TWI peripherals that can operate as a master or a slave, they are often also compatible with most I2C compatible devices such as Real-Time Clocks/Calendars (RTC/C), EEPROMs, sensors, modules, expansion boards, etc.

I2C, and thus TWI, are used for relatively low-speed communications over short to medium distances. As a result, they are only intended for intra-board (between devices on the same PCB) communications, and short inter-board (between devices on different PCBs) communications [21]. This means they can not be used for long PC-MCU connections like with UART. 

TWI/I2C are also better suited to connect multiple devices at the same time, up to 128 devices for 7-bit addressing, compared to UART. TWI/I2C are better suited because they have specific features that help with bus contention that the simpler UART does not. These features include clock line synchronization, master/slave addressing, and transmission validation.

Multi-Device I2C Connection Example Diagram [25]

Image of multiple I2C slaves connected to a master controller

SPI (Serial Peripheral Interface)

The SPI peripheral communicates through a synchronous serial protocol on a four-wire interface [26]. It consists of a SPI Clock line(SCLK), Master Output/Slave Input line (MOSI), a Master Input/Slave Output line (MISO), and Chip Select line (CS) or sometimes called Slave Select (SS). 

SPI operates in a master-slave architecture as a full duplex interface. Similarly to I2C, SPI master devices are the ones that generate the clock signal and SPI slave devices are synchronized to their signal. 

Applications

Unlike I2C, SPI is able to operate in full duplex meaning that both a master and slave can transmit data at the same time. This is because the SPI interface has an increased number of wires over UART and I2C. SPI devices also support higher clock frequencies compared to I2C, but only can support one master with multiple slaves.

With higher clock speeds and full duplex communications, SPI generally can handle applications that require faster speeds than I2C. However, SPI is also more susceptible to noise than I2C, so it is better to be used over short distances like in intra-board communications. Therefore, many on-board types of the devices that I2C supports, such as sensors, ADCs, and EEPROM, may also have a version that communicates over SPI instead. Of course, the obvious downside of SPI’s added complexity of two additional wires or pins over I2C may discourage the use of the interface in some designs.

SPI is most often used with devices that require large amounts of data to be transferred quickly like TFT displays, flash memory, and MMC/SD cards.

Multi-Device SPI Connection with Independent Slaves (Left) and Daisy-Chained Slaves (Right) Example Diagram [28]

Image of two different connection methods for multi device SPI connection

UPDI (Unified Program and Debug Interface)

UPDI is a UART-based half-duplex one-wire interface for data reception and transmission . Microchip introduced this new interface standard for the new tinyAVR 0/1-series and megaAVR 0-series generations .

Applications

After Microchip acquired Atmel, they developed UPDI as a proprietary interface for external programming and on-chip debugging developed to replace older programming interfaces.

The aforementioned Microchip’s Atmel-ICE is a commercially currently available, as of 2021-03-18, programmer and debugger that supports UPDI as well as JTAG, PDI, SPI, TPI, SWD, aWire, and debugWire. There are also many online DIY tutorials for making a UPDI programmer using an Arduino or another AVR MCU.









Interrupts

When an interrupt fires, the main program stops to execute the code inside its corresponding Interrupt Service Routine (ISR). After the ISR is completed, the program continues from where it was stopped when the interrupt was fired . Therefore, it is important to keep the ISR as short as possible to avoid conflicts with the rest of the program. This can be done by avoiding additional function calls inside the ISR as the ISR would have to wait until that function call is finished before the main program can continue.

An ISR can be implemented like how any other function is called with “ISR” being the function name and the interrupt vector name as the only parameter.

"_vect" must be added to the end of the name of the desired peripheral interrupt source to create the correct name macro for the ISR parameter. In the previous Timer/Counter section, with the Periodic Interrupt example, to create an ISR that is executed whenever Timer/Counter A (TCA0)'s base counter is equal to the value in its Period register, the interrupt vector  was created by appending "_vect" to "TCA0_OVF" to create “TCA0_OVF_vect” that was used as the parameter for the ISR. 

For some idea of the peripherals that can trigger interrupts, the table below contains all of the interrupt vector mappings for the ATtiny3216/3217.

ATtiny3216/3217 Interrupt Vector Mapping Table 7-2 [18]

Image of table of ATtiny3217's 30 different interrupt vectors, their address, name, and definition

References

[1]    A. Daga, “AVR Microcontroller : All You Need To Know- (Part 1/46),” Engineers Garage, 01-Jan-2011. [Online]. Available: https://www.engineersgarage.com/article_page/avr-microcontroller-all-you-need-to-know-part-1-46/#:~:text=AVR%20was%20developed%20in%20the,known%20as%20Advanced%20Virtual%20RISC. [Accessed: 04-Feb-2021].

[2]    “ATmega328P,” ATmega328P - 8-bit Microcontrollers. [Online]. Available: https://www.microchip.com/wwwproducts/en/ATmega328p. [Accessed: 05-Feb-2021].

[3]    “Arduino Uno Rev3,” Arduino Uno Rev3 | Arduino Official Store. [Online]. Available: https://store.arduino.cc/usa/arduino-uno-rev3. [Accessed: 17-Mar-2021]

[4]    J. Beningo, “USART vs UART: Know the difference,” EDN, 21-Sep-2015. [Online]. Available: https://www.edn.com/usart-vs-uart-know-the-difference/. [Accessed: 19-Mar-2021].

[5]    A. Industries, “USB to TTL Serial Cable - Debug / Console Cable for Raspberry Pi,” Adafruit . [Online]. Available: https://www.adafruit.com/product/954. [Accessed: 19-Mar-2021].

[6]    M. Technology, “ATtiny3216/3217 Datasheet,” tinyAVR 1-series, May-2020. [Online]. Available: https://ww1.microchip.com/downloads/en/DeviceDoc/ATtiny3216-17-DataSheet-DS40002205A.pdf. [Accessed: 19-Mar-2021].

[7]    “TWI Bus,” I2C Bus, 14-Mar-2016. [Online]. Available: https://www.i2c-bus.org/twi-bus/. [Accessed: 19-Mar-2021].

[8]    M. Technology, “ What is TWI? How to Configure the TWI for I2C Communication,” TB3181, 2018. [Online]. Available: http://ww1.microchip.com/downloads/en/DeviceDoc/90003181A.pdf. [Accessed: 19-Mar-2021].

[9]     “SparkFun Real Time Clock Module,” BOB-12708 - SparkFun Electronics. [Online]. Available: https://www.sparkfun.com/products/12708. [Accessed: 19-Mar-2021].

[10]    “I2C EEPROM - 256k Bit (24LC256),” Sparkfun. [Online]. Available: https://www.sparkfun.com/products/525. [Accessed: 19-Mar-2021].

[11]    Yida, “UART vs I2C vs SPI – Communication Protocols and Uses,” Latest Open Tech From Seeed Studio, 16-Nov-2020. [Online]. Available: https://www.seeedstudio.com/blog/2019/09/25/uart-vs-i2c-vs-spi-communication-protocols-and-uses/. [Accessed: 19-Mar-2021].

[12]    A. Bhatt, “Understanding the I2C Protocol,” Engineers Garage, 27-May-2020. [Online]. Available: https://www.engineersgarage.com/tutorials/understanding-the-i2c-protocol/. [Accessed: 19-Mar-2021].

[13]    P. Dhaker, “Introduction to SPI Interface,” Introduction to SPI Interface | Analog Devices. [Online]. Available: https://www.analog.com/en/analog-dialogue/articles/introduction-to-spi-interface.html. [Accessed: 19-Mar-2021].

[14]    A. ON, “Difference between I2C and SPI ( I2C vs SPI ), you should know.,” Aticleworld, 11-Jun-2020. [Online]. Available: https://aticleworld.com/difference-between-i2c-and-spi/#:~:text=I2C%20is%20a%20half%2Dduplex,not%20the%20feature%20of%20SPI. [Accessed: 19-Mar-2021].

[15]    “UPDI Physical Interface,” Microchip Atmel-ICE. [Online]. Available: https://onlinedocs.microchip.com/pr/GUID-DDB0017E-84E3-4E77-AAE9-7AC4290E5E8B-en-US-4/index.html?GUID-9B349315-2842-4189-B88C-49F4E1055D7F%C2%A0. [Accessed: 19-Mar-2021].

[16]    M. Technology, “AVR Interrupts,” Microchip Developer Help, 19-Mar-2021. [Online]. Available: https://microchipdeveloper.com/8avr:int. [Accessed: 19-Mar-2021].

[17]    “Interrupts in AVR MicroController,” javaTpoint. [Online]. Available: https://www.javatpoint.com/interrupts-avr-microcontroller#:~:text=External%20Interrupts%20in%20AVR%20Microcontroller,%22Interrupt%20Sub%2DRoutine%22. [Accessed: 19-Mar-2021].

[18]    "We bring you the 10 most popular prototyping and development boards in 2021,” YoungWonks" 19-Mar-2021. [Online]. Available: https://www.youngwonks.com/blog/Top-10-IoT-boards-for-2019. [Accessed: 06-Aug-2021].