This chapter studies the structure and resources found in typical microcontrollers. It starts by introducing the concept of a microcontroller and exploring the differences between microcontrollers and microprocessors. It continues with the description of the resources that are available in microcontrollers, focusing again on how they differ from the resources available in microprocessors. The chapter then describes the von Neumann and Harvard architectures as well as how the reduced instruction set computer (RISC) and complex instruction set computer (CISC) architectures differ in their instruction sets. It finishes by describing the most common microcontrollers and listing their manufacturers.
1.1 Microprocessors and Microcontrollers: Characterization
Figure 1.1 shows the block diagram of a generic microcomputer. It consists of three fundamental blocks: central processing unit (CPU), memory, and input/output (I/O) system. These blocks are interconnected by groups of electrical lines called buses. The buses that transport memory or I/O addresses are called address buses; the buses that transport data or instructions are called data buses; and the buses that transport control signals are called control buses.
The CPU is the brain of the microcomputer, being under control of the program stored in memory. The tasks of the CPU are to fetch the instructions stored in memory, interpret those instructions, and execute them. The CPU also includes the circuitry necessary to perform arithmetic and logic operations with binary data. This special circuitry is called the arithmetic and logic unit (ALU).
In a microcomputer, the CPU is its microprocessor, which is the integrated circuit that carries out the operations described above. A microcontroller can be considered as a microcomputer built on a single integrated circuit or chip. Historically, microcontrollers appeared after microprocessors and followed independent paths. Microprocessors are mainly found in personal computers and workstations, as these require strong computational power, and the ability to manage large sets of data and instructions at a high speed. A very important parameter for microprocessors is the size of their internal registers (8, 16, 32, or 64 bits), as this determines the number of bits that can be processed simultaneously.
On the other hand, microcontrollers are used in a large variety of applications. They can be found in the automotive industry, communication systems, electronic instrumentation, hospital equipment, industrial equipment and applications, household appliances, toys, and so forth. Microcontrollers have been designed to be used in applications in which they have to carry out a small number of tasks at the lowest possible economic cost. They do this by executing a program permanently stored in their memory, whereas the input/output ports of the microcontroller are used to interact with the outside world. Therefore, the microcontroller becomes part of the application; it is a controller embedded in the system. Complex applications can use several microcontrollers, each one of them focusing on a small group of tasks.
The following generic requirements are important for microcontrollers and designs using microcontrollers:
Input/output resources. As opposed to microprocessors in which the emphasis is on computational power, microcontrollers put their emphasis on their input/output resources, such as the ability to handle interrupts, analog signals, number of different input and output lines, and so forth.
Optimization of space. It is important to use the smallest possible footprint at a reasonable cost. Given that the number of pins in a chip depends on its packaging, the footprint can be optimized by having one pin able to perform several different functions.
Using the most appropriate microcontroller for a given application. Microcontroller manufacturers have developed families of devices with the same instruction set but different hardware aspects, such as memory size, input/output devices, and so forth. This allows the designer to select the most appropriate device from a given family.
Protection against failure. It is critical for safety to guarantee that the microcontroller is executing the correct program. If for any reason the program goes astray, the situation has to be immediately corrected. Microcontrollers have a watchdog timer (WDT) to ensure that the program is being executed correctly. Watchdog timers do not exist in personal computers.
Low power consumption. Because batteries power many applications using microcontrollers, it is important to ensure the low power consumption of microcontrollers. Furthermore, the energy used when the microcontroller is not doing anything, for example, when it is waiting for an action from the user like a keyboard input, needs to be kept to a minimum. To do this, the microcontroller is set in sleeping state until it resumes the execution of the program.
Protection of programs against copies. The program stored in memory needs to be protected against unauthorized reading. To do this, the microcontrollers incorporate protection mechanisms against copying.
1.2 Components of a Microcontroller
Microcontrollers combine the fundamental resources available in a microcomputer such as the CPU, memory, and I/O resources in a single chip. Figure 1.2 shows the block diagram for a generic microcontroller.
Microcontrollers have an oscillator to generate the signal necessary to synchronize all internal operations. Although this can be a basic RC (resistance capacitor) oscillator, a quartz crystal (XTAL) is normally used due to its high frequency stability. The frequency of the oscillator has a direct influence on the speed at which program instructions are executed.
Similar to microcomputers, the CPU is the brain of the microcontroller. The CPU fetches the program instructions from their locations in memory one by one, interprets or decodes them, and executes them. The CPU also includes the ALU circuits for binary arithmetic and logic operations.
The microcontroller’s CPU has different registers. Some of these registers are intended for general use, whereas others have a specific purpose. Specific purpose registers include: instruction register, accumulator, status register, program counter, data address register, and stack pointer.
The instruction register (IR) stores the instruction that the CPU is executing. The programmer does not normally have access to the IR.
The accumulator (ACC) is a register associated with the arithmetic and logic operations that the ALU is carrying out. When executing any operation, one of the data needs to be in the ACC. The resulting value is also stored in the ACC. PIC microcontrollers do not have the ACC register. Instead, they have a working (W) register that is very similar to the ACC.
The status register (STATUS) contains the bits that show different characteristics related to the operations carried out by the ALU. These can be the sign of the resulting value (positive vs. negative), a flag to notify if the resulting value is zero, carry over, parity bits, and so forth.
The program counter (PC) is the CPU register where addresses of instructions are stored. Every time that the CPU looks for an instruction in the memory, the PC is increased, pointing to the following instruction. In an instant of time, the PC contains the address of the instruction that will be executed next. The control transfer instructions modify the value stored in the PC.
The data address register (DAR) stores data addresses from memory. This register plays a major role in indirect data addressing. Different types of microcontrollers use different specific names for the DAR. For example, PIC microcontrollers call this register the file select register (FSR).
The stack pointer (SP) stores data addresses in the stack. The stack and the SP register are studied in further detail in Chapter 4. PIC microcontrollers do not have an SP register.
The microcontroller memory stores both program instructions and data. Any microcontroller has two types of memory: random-access memory (RAM) and read-only memory (ROM). RAM can be read and written. RAM is volatile memory, meaning that its data is lost when it is not powered. On the other hand, although ROM can only be read, it is non-volatile. The different types of technologies used for ROM such as EPROM (erasable programmable read-only memory), EEPROM (electrical erasable programmable read-only memory), OTP (one-time programmable), and FLASH are described in detail in Chapter 3. Both RAM and ROM are “random access” memories, meaning that the time to access specific data does not depend on its stored location. This is opposed to sequential access memories in which the time needed to access a specific memory cell depends on the location of the last accessed cell.
ROM is used to permanently store the program for the microcontroller, whereas RAM is used to temporarily store the data that will be manipulated by the program. An increasing number of microcontrollers use non-volatile memory such as EEPROM to store some of the data that is changed only sporadically. The size of ROM is larger than the size of RAM for two main reasons: First, most applications require programs that manipulate a relatively small number of data. Second, RAM has a larger footprint compared to ROM, and therefore it is more expensive than ROM.
Being the vehicle to communicate with the outside world, the I/O resources are very important in microcontrollers. I/O resources consist of the serial and parallel ports, timers, and interruption managers. Some microcontrollers also incorporate analog input and output lines associated with analog-to-digital (A/D) and digital-to-analog (D/A) converters. The resources needed to ensure the regular operation of the microcontrollers such as the watchdog are also considered part of the I/O resources.
Parallel ports are normally structured in groups of up to eight lines of digital inputs and outputs. It is normally possible to manipulate each one of these lines individually. Serial ports can be of different technologies such as RS-232C (Recommended Standard 232, Revision C), I2C (interintegrated circuit), USB (universal serial bus), and Ethernet. In general, a microcontroller will have the largest possible number of I/O resources for the number of available pins in its integrated circuit package. To increase the performance, one physical pin can be connected to several internal blocks, and therefore that pin may carry out different functions depending on how the microcontroller has been configured.
1.2.1 The Watchdog
The watchdog timer (WDT) is a resource that can be found in most microcontrollers. As shown in Figure 1.3, the WDT consists of an oscillator and a binary counter of N bits. Although the oscillator can be the same oscillator used by the microcontroller, it is preferable to use an independent oscillator. The output of the counter is connected to the reset input for the microcontroller. The counting process can never be stopped, although the program being executed can periodically reset the counter to its initial value.
Every pulse at the output of the oscillator becomes an input to the counter....