The demand for 8-bit microcontrollers in the embedded systems market remains robust, driven by their cost-effectiveness, simplicity, and efficiency. According to industry reports, 8-bit MCUs are extensively used in smart sensor applications, such as smoke detectors and thermostats, due to their ultra-low power consumption and high-performance analog features. They are also critical in smart metering, enabling detailed consumption measurements and wireless data communication. The consumer electronics and home appliances sectors benefit from 8-bit MCUs’ ability to enhance energy efficiency and performance in devices like washing machines and refrigerators. The market for 8-bit MCUs is expected to continue growing as they meet the requirements of both current and future applications, supported by a robust supply chain and innovative features from manufacturers.
2.1 Factors to be considered in selecting a microcontroller
Choosing the right microcontroller (MCU) for a specific application is crucial for the success of an embedded system. Here are key factors to consider:
1. Performance Requirements
- CPU Speed: Determine the required processing speed (measured in MHz or GHz) based on the application’s computational demands.
- Instruction Set Architecture: Choose between 8-bit, 16-bit, or 32-bit architectures depending on the complexity of the tasks.
- Throughput: Evaluate the ability to handle data and execute instructions efficiently.
- Memory Requirements
- Flash Memory: Ensure sufficient storage for the program code. Embedded systems with larger or more complex software require more flash memory.
- RAM: Adequate volatile memory is needed for data storage during operation.
- EEPROM: Consider if non-volatile memory is needed for storing settings or calibration data that must be retained after power is turned off.
- Power Consumption
- Operating Voltage: Choose MCUs that match the power supply available in the design.
- Low-Power Modes: For battery-operated devices, MCUs with various low-power modes can extend battery life.
- Peripherals and I/O Capabilities
- Analog and Digital I/O: The number and type of I/O pins required, including analog-to-digital converters (ADCs) and digital-to-analog converters (DACs).
- Timers and Counters: Essential for time-sensitive tasks, such as generating precise delays or PWM signals.
- Communication Interfaces: Availability of communication protocols such as UART, SPI, I2C, CAN, USB, etc., for interfacing with other devices.
- Development Tools and Ecosystem
- Integrated Development Environment (IDE): Availability and quality of IDEs for writing and debugging code.
- Libraries and Middleware: Pre-existing libraries and middleware can significantly speed up development.
- Support and Community: Availability of documentation, community support, and manufacturer support.
- Cost
- Unit Price: The cost per microcontroller unit, which is crucial for large-scale production.
- Development Costs: Consideration of development tools’ costs and time investment.
- Packaging and Physical Constraints
- Form Factor: The physical size and package type (e.g., DIP, QFP, BGA) must fit the design constraints of the device.
- Environmental Tolerance: Temperature ranges and resistance to environmental factors, important for industrial or outdoor applications.
- Scalability and Upgradability
- Family of Microcontrollers: Using a scalable family of MCUs allows for easier upgrades or downgrades within the same architecture.
- Firmware Update Capability: Ensure the MCU supports in-system programming (ISP) or in-application programming (IAP) for easy firmware updates.
- Reliability and Security
- Error Handling: Features like watchdog timers and brown-out detection can improve system reliability.
- Security Features: Integrated security features, such as hardware encryption and secure boot, are critical for applications requiring high security.
2.2 ATmega328 Microcontroller and Arduino Platform
The ATmega328 microcontroller, especially when used with the Arduino platform, offers a powerful, flexible, and cost-effective solution for developing 8-bit embedded systems. Its ease of use, robust community support, and versatile application potential make it a preferred choice for both beginners and experienced developers. Whether for educational purposes, hobby projects, or commercial products, the combination of ATmega328 and Arduino provides a reliable and efficient development environment.
2.2.1 ATmega328 Microcontroller Architecture
The ATmega328 microcontroller is built on the AVR architecture, which is designed for high efficiency and performance. It combines a rich set of features and peripherals, making it suitable for a variety of applications. Below is a detailed explanation of its architecture, along with an architectural diagram.
- AVR Core Architecture
- RISC Architecture
- Reduced Instruction Set Computing (RISC): The AVR core uses a simplified instruction set, allowing most instructions to be executed in a single clock cycle. This results in high performance and efficiency.
- Single Cycle Execution: Instructions are designed to execute in a single clock cycle, which enhances the speed of the microcontroller.
- 32 General Purpose Registers
- Register File: The core contains 32 general-purpose working registers. These registers are directly connected to the Arithmetic Logic Unit (ALU), allowing for efficient data manipulation and arithmetic operations.
- Direct Access: All 32 registers can be accessed within a single clock cycle, providing fast data processing capabilities.
Figure 1‑1: Simplified Architecture of Atmega328 Microcontroller
- Arithmetic Logic Unit (ALU)
- High-Speed Operations: The ALU performs arithmetic and logic operations between registers or between a register and an immediate value.
- Bitwise Operations: Supports bitwise operations such as AND, OR, XOR, and NOT.
- Arithmetic Operations: Supports addition, subtraction, increment, decrement, and comparison operations.
- Program Memory (Flash)
- 32 KB Flash Memory: Non-volatile memory used to store the program code. Flash memory is electrically erasable and reprogrammable, allowing for updates to the firmware.
- Harvard Architecture: The AVR core uses a Harvard architecture, which separates the storage and signal pathways for instructions and data. This allows simultaneous access to program memory and data memory, increasing performance.
- Data Memory
- SRAM (Static RAM): 2 KB of SRAM for storing runtime data and variables. SRAM is volatile and used for temporary data storage during program execution.
- EEPROM (Electrically Erasable Programmable Read-Only Memory): 1 KB of EEPROM for non-volatile data storage, useful for storing configuration settings and other persistent data.
- Special Function Registers (SFRs)
Special Function Registers are used to control the operation of the microcontroller. These registers are mapped into the data memory space and can be accessed using standard data manipulation instructions.
- Status Register (SREG)
- Flag Bits: Contains important flags like the Carry (C), Zero (Z), Negative (N), Overflow (V), Sign (S), and Half Carry (H) flags. These flags are set or cleared based on the results of operations, allowing for conditional branching and decision making in the program.
- General I/O Registers
- PORTx, DDRx, PINx Registers: Used to configure and control the I/O pins. For instance,
PORTB
controls the data written to Port B,DDRB
sets the direction of the pins in Port B (input or output), andPINB
reads the data from the pins in Port B. - Interrupt System
- Interrupt Vectors
- Multiple Sources: The AVR core supports multiple interrupt sources, including external interrupts, timer interrupts, and peripheral interrupts. Each interrupt source has a corresponding vector that points to the interrupt service routine (ISR).
- Vector Table: The interrupt vectors are stored in a vector table located at the beginning of the program memory.
- Global Interrupt Enable (GIE)
- Control Bit: A global interrupt enable bit in the SREG (I-bit) controls whether interrupts are globally enabled or disabled. This allows for enabling and disabling interrupts in a controlled manner.
- External Interrupts
- INT0 and INT1: Two external interrupt pins (INT0 and INT1) can trigger interrupts based on external events. These are edge-triggered or level-triggered based on the configuration.
- Pin Change Interrupts
- PCINT0 to PCINT23: Pin change interrupts can be triggered on any change in state of the I/O pins, providing a flexible mechanism for detecting pin changes.
- Timer/Counters
- Timer/Counter0 and Timer/Counter2 (8-bit)
- Configuration Registers: TCCR0A, TCCR0B for Timer0 and TCCR2A, TCCR2B for Timer2. These registers configure the mode of operation (Normal, CTC, Fast PWM, Phase Correct PWM).
- Counter Register: TCNT0 for Timer0 and TCNT2 for Timer2. These registers hold the current count value.
- Timer/Counter1 (16-bit)
- Configuration Registers: TCCR1A, TCCR1B for configuring the mode of operation.
- Counter Register: TCNT1 holds the current count value.
- Output Compare Registers
- OCR0A, OCR0B, OCR1A, OCR1B, OCR2A, OCR2B: These registers are used in conjunction with the timer/counters to generate precise time delays or PWM signals.
- Interrupt Registers
- TIMSK0, TIMSK1, TIMSK2: Timer/Counter Interrupt Mask Registers for enabling/disabling timer interrupts.
- TIFR0, TIFR1, TIFR2: Timer/Counter Interrupt Flag Registers for checking the status of timer interrupts.
- Communication Interfaces
- USART (Universal Synchronous/Asynchronous Receiver/Transmitter)
- Control Registers: UCSR0A, UCSR0B, UCSR0C for configuring the mode, baud rate, and operation of the USART.
- Data Registers: UDR0 for reading and writing data to be transmitted or received.
- SPI (Serial Peripheral Interface)
- Control Registers: SPCR for configuring the SPI, SPSR for status checking.
- Data Register: SPDR for reading and writing data to be transmitted or received.
- I2C (Inter-Integrated Circuit) / TWI (Two-Wire Interface)
- Control Registers: TWBR for setting the bit rate, TWSR for status checking, TWAR for setting the slave address, TWCR for controlling the operation, TWDR for reading and writing data.
- Power Management
- Low Power Modes
- Idle Mode: CPU is stopped while peripherals continue to operate.
- ADC Noise Reduction Mode: Stops the CPU and all I/O modules except the ADC to reduce noise during ADC conversions.
- Power-down Mode: Stops the oscillator, with only external interrupts or the watchdog timer capable of waking the microcontroller.
- Power-save Mode: Allows the asynchronous timer to continue running while the rest of the device is in power-down mode.
- Standby Mode: Keeps the oscillator running while the rest of the device is in power-down mode.
- Extended Standby Mode: Similar to Standby mode but with an additional low power mode for the oscillator.
- Power Reduction Register (PRR)
- Peripheral Control: PRR is used to disable unused peripherals to save power. Bits in this register control the power to specific modules such as ADC, timers, and communication interfaces.
- Additional Features
- Watchdog Timer
- System Reliability: Ensures the microcontroller resets if the program becomes unresponsive, enhancing system reliability.
- Brown-Out Detection
- Voltage Monitoring: Resets the microcontroller if the supply voltage drops below a certain threshold, preventing erratic behavior.
- In-System Programming (ISP)
- Convenient Updates: Enables programming and reprogramming of the microcontroller while it is installed in the circuit, using the SPI interface.
2.2.2 Arduino Uno R3
The Arduino UNO is a standard board of Arduino. Here UNO means ‘one’ in Italian. It was named as UNO to label the first release of Arduino Software. It was also the first USB board released by Arduino. It is considered as the powerful board used in various projects. Arduino.cc developed the Arduino UNO board. Arduino UNO is based on an ATmega328P microcontroller. It is easy to use compared to other boards, such as the Arduino Mega board, etc. The board consists of digital and analog Input/Output pins (I/O), shields, and other circuits. The Arduino UNO includes 6 analog pin inputs, 14 digital pins, a USB connector, a power jack, and an ICSP (In-Circuit Serial Programming) header. It is programmed based on IDE, which stands for Integrated Development Environment. It can run on both online and offline platforms. Central to the Arduino interface board, shown in Fig. 1.2, is an onboard microcontroller.
Specifications of the Arduino UNO R3 are as follows:
- Microcontroller: ATmega328
- Operating Voltage: 5 V
- Input Voltage (recommended): 7–12 V
- Input Voltage (limits): 6–20 V
- Digital I/O Pins: 14 (of which 6 provide PWM outputs)
- Analog Input Pins: 6
- DC Current per I/O Pin: 40 mA
- DC Current for 3.3 V Pin: 50 mA
- Flash Memory: 32 KB of which 0.5 KB is used by the bootloader
- SRAM: 2 KB (ATmega328)
- EEPROM: 1 KB (ATmega328)
- Clock Speed: 16 MHz (Fig. 1.2)
Figure 1‑2: Arduino Uno Development Board
Pin description
- Tx and Rx are serial UART pins used for RS-232 and USB communications
- I2C is another serial communications method that uses a bidirectional data line (SDA) and a clock line (SCL)
- SPI is another serial communications method that uses one line for the master to transmit (MOSI—Master Out Slave In), another for the master to receive (MISO), and a third as a clock (SCK)
- A/D, the Analog to Digital input, converts an analog voltage into a digital representation
- PWM (Pulse Width Modulator) is used to create a square wave with a specific duty cycle (high time vs low time)
- ICSP is the In-Circuit Serial Programming—another way to program the processor
- Vcc is the voltage supplied to the processor (+5VDC regulated from a higher input voltage) 3.3VDC is a regulated voltage (from the higher input voltage) for peripherals requiring that voltage—50 mA maximum
- IOREF provides a voltage reference so shields can select the proper power source
- AREF is a reference INPUT voltage used by the A/Ds
- GND is the ground reference
- RESET resets the processor (and some peripherals)