Computer Systems:Microcontroller Applications

Microcontroller Applications

We have now looked at some of the main ideas to be used in explaining microcontroller operation: hardware, software, how they interact and how the function of complex systems can be represented using block diagrams and flowcharts.

The microcontroller provides, in a simplified form, all the main elements of the microprocessor system on a single chip. As a result, less complex applications can be designed and built quickly and cheaply. A working system can consist of a microcontroller chip and just a few external components for feeding data and signals in and out. They tend to be used for control operations requiring limited amounts of memory but operating at high speed, with external hardware attached only as required by a specific application.

As an example of a typical microcontroller system, a digital camera, is shown in Figure 1.8(a), with the microcontroller clearly visible as the large black chip on the main board. A block diagram is a useful way of identifying the main components and the connections between them Figure 1.8(b). This is called a mechatronic application, because it has a lot of mechanical components as well as electronics.

 

Computer Systems:Execution Cycle

Execution Cycle

Program execution is illustrated in Figure 1.7. Assuming that the application program code is in RAM, the program execution cycle proceeds as follows:

1. The CPU outputs (1) the address of the memory location containing the required instruction (this address is kept in the program counter). The sample address is shown in hexadecimal form (3A24) in Figure 1.7, but it is output in binary form on the address lines from the processor (for an explanation of hex numbering see Appendix A). The address decoder logic uses the address to select the RAM chip that has been allocated to this address. The address bus also connects directly to the RAM chip to select the individual location, giving a two-stage memory location select process.

2. The instruction code is returned to the CPU from the RAM chip via the data bus (2). The CPU reads the instruction from the data bus into an instruction register. The CPU then decodes and executes the instruction (3). The operands (code to be processed) are fetched

(4) from the following locations in RAM via the data bus, in the same way as the instruction.

3. The instruction execution continues by feeding the operand(s) to the data processing logic (5). Additional data can be fetched from memory (6). The result of the operation is stored in a data register (7), and then, if necessary, in memory (8) for later use. In the meantime, the program counter has been incremented (increased) to the address of the next instruction code. The address of the next instruction is then output and the sequence repeats from step 2.

The operating system, the application program and the user data are stored in different parts of RAM during program execution, and the application program calls up operating system

PIC Microcontrollers-1119

routines as required to read in, process and store the data. PC processors have multi-byte instructions, which are stored in multiple 8-bit locations, and use complex memory management techniques to speed up program execution.

 

Computer Systems: System Operation and Program Execution

System Operation

The CPU controls the system data transfers via the data and address buses and additional control lines. A clock circuit, usually containing a crystal oscillator (as found in digital watches), is required; this produces a precise fixed frequency signal that drives the microprocessor along. The CPU operations are triggered on the rising and falling edges of the clock signal, allowing their exact timing to be defined. This allows events in the CPU to be completed in the correct sequence, with sufficient time allowed for each step. The CPU generates all the main control signals based on the clock. A given CPU can be used in different system designs, depending on the type of application, the amount of memory needed, the I/O requirements and so on.

The address decoder controls access to memory and I/O registers for a particular design.

Typically, a programmable logic device (PLD) is used to allocate each memory chip to a specific range of addresses. An input address code in a particular range generates a chip select output, which enables that device. The I/O port registers, which are set up to handle the data transfer in and out of the system, are also allocated particular addresses by the same mechanism, and accessed by the CPU in the same way as memory locations. The allocation of addresses to particular peripheral devices is called a memory map (Figure 1.6b).

PIC Microcontrollers-1118

Program Execution

The ROM and RAM will contain program code and data in numbered locations, that are selected by a binary code at its address inputs. If the program is in ROM, it can start immediately (as in the PC BIOS), but RAM must be loaded from a non-volatile program store, such as a hard disk.

A register is a temporary store for data within the CPU or port. In the port chip, it holds working data or a control code which sets up how the port will operate. For example, the bits in the data direction register control whether each port pin operates as an input or an output. The data being sent in or out is then stored temporarily in the port data register.

The program consists of a list of instructions in binary code stored in memory, with each instruction and any associated data (operands) being stored in sequential locations. The program instruction codes are fetched into the CPU and decoded. The CPU sets up the internal and external control lines as necessary and carries out the operation specified in the program, such as read a character code from the serial port into the CPU. The instructions are executed in order of their addresses, unless the instruction itself causes a jump to another point in the program, or an ‘interrupt’ (signal) is received from an internal or external source. The program counter keeps track of the current step.

 

Computer Systems:Data Output

Data Output

Going back to the word processor, the characters must be displayed on the screen as they are typed in, so the character codes stored in memory are also sent to the screen via the graphics interface. The display is made up of single colored dots (pixels) organized in lines across the screen, which are accessed in sequence, forming a scanned display. The shape of the character on screen must be generated from its code in memory, and sent out on the

correct set of lines at the right time. The display must therefore be created as a two- dimensional image made up from a serial data stream which sets the color of each pixel on the screen in turn, line by line, where each line of text occupies a set of adjacent lines.

The exact arrangement depends on the font type and size.

If a file is transferred on a network, it must also be sent in serial form. The characters (letters) in a text file are normally sent as ASCII code, along with formatting information and network control codes. ASCII code represents one character as one byte (8 bits) of binary code, and is therefore a very compact form of the data. The code for the letter ‘A’ (upper case), for example, is 01000001.

The printer works in a similar way to the screen, except that the output is generated as lines of dots of ink on a page. In an inkjet printer, you can see the scanning operation

PIC Microcontrollers-1117

taking place. These days, printer data is usually transferred in serial form on a USB, wireless or network link. The printer itself is capable of formatting the final output; only the character code and any formatting codes are needed. A portable document format (PDF) file is a standard output format for the display and printing of documents containing text and graphics.

The operation of the word processor can be illustrated using a flowchart, which is a graphical method of describing a sequential process. Figure 1.5 shows the basic process of text input and word wrapping at the end of each line. Flowcharts will be used later to represent microcontroller program operation.

 

Computer Systems:Microprocessor Systems

Microprocessor Systems

All microprocessor systems perform the same essential functions, that is, data or signal input, storage, processing and output. However, the PC is a relatively complex microprocessor system, with a hierarchical bus structure, which has developed to improve system performance by alleviating the bus bottleneck of earlier designs. The Intel PC processor itself also has many additional performance-enhancing features such as cache memory, multiple processing pipelines and multiple cores. To understand the microcontroller, we need to go back to a simpler system.

The basic microprocessor system needs a certain set of chips, with suitable interconnections, as follows:

• CPU

• RAM

• ROM

• I/O ports.

These devices must be interconnected by:

• Address bus

• Data bus

• Various control lines.

These buses and control lines originate from the CPU, which is in charge of the system. RAM and ROM chips are usually general purpose hardware, which can be used in any system. The I/O chips are sometimes designed to work with a particular processor, but all provide specific interfacing functions. In a basic system, this would be simple digital input and output, with perhaps a serial port providing an RS232 (see Chapter 12) type data link.

Additional support chips are needed to make a CPU system work. In a minimal system, an address decoder is needed to select the memory chip or I/O device required for a data transfer to or from the CPU. This system is illustrated in Figure 1.6a, and there is further information about microprocessor system operation in Chapter 14 and Appendix C.

 

Computer Systems:Data Storage and Data Processing

Data Storage

The character data is received by the CPU from the keyboard, or other interface, in parallel form, via the internal data bus. It is stored in a CPU register and then copied back to RAM. RAM locations are numbered and accessed via the system address bus, a set of lines that select a location as a binary number. This is why the CPU has so many pins: for speed of transfer, all data and address pins, and control lines, are separately connected to the northbridge controller via the frontside bus, and hence to the RAM. The data is stored in RAM as charge on the gate of an electronic switch, a field effect transistor (FET; see Appendix B). When charged, the FET is switched on, and this state can be read back at a later time. The addressing system accesses an array of these switches in rows and columns to store and retrieve bits of data. Each byte has a unique address.

Data Processing

The data processing in the CPU required by a simple text editor is minimal; the input characters are simply stored as binary code and displayed, with a separate graphics processor converting the character code to a corresponding symbol on the screen. Nevertheless, the word-processor program has to handle different fonts, word wrapping at the end of lines and so on. It also has to handle text, page and document formatting, menu systems and the user interface. Editing embedded graphics is a bit more complex, since each pixel needs handling separately. The most demanding applications are those where the real world is simulated in a computer model in order to make predictions about the behavior of complex systems. Weather forecasting is an extreme example; the fact that we can still only forecast accurately a few days ahead illustrates the limitations of such system modeling, even on the most powerful computers.

The circuit simulation software used in this book, Proteus VSM, combining traditional circuit analysis with an interactive interface, is a good example of system modeling in a PC. It takes a circuit created as a schematic and applies network analysis (lots of simultaneous equations) to predict its operation when constructed. For digital elements, logic modeling is needed, and then the analogue and digital domains are co-simulated. Component

characteristics and input variables are typically represented by 32-bit binary numbers, which correspond to decimal numbers in exponential form (as on a scientific calculator).

The processor needs to be able to manipulate these circuit variables simultaneously to represent the circuit conditions at a series of points in time. The output is calculated and displayed via animated circuit components or virtual instruments, or graphically. Numerous examples are to follow!

 

Computer Systems:Starting the Application and Data Input

Starting the Application

Windows displays an initial screen with icons and menus, which allow the application to be selected by clicking on a shortcut. Windows converts this action into an operating system command which runs the executable file (WINWORD.EXE, etc.) stored on disk. The application program is transferred from disk to RAM, or as much of it as will fit in the available memory. The word-processor screen is displayed and a new document file can be created or an existing one loaded by the user from disk.

Data Input

The primary data input is from the keyboard, which consists of a grid of switches that are scanned by a dedicated microcontroller within the keyboard unit. This chip detects when a key has been pressed, and sends a corresponding code to the CPU via a serial data line in the keyboard cable, or wirelessly. The serial data is a sequence of voltage pulses on a single wire, which represent a binary code, each key generating a different code. The keyboard interface converts this serial code to parallel form for transfer to the CPU via the system data bus. It also signals separately to the CPU that a keycode is ready to be read into the CPU, by generating an ‘interrupt’ signal. This serial-to-parallel (or parallel-to-serial) data conversion process is required in all the interfaces that use serial data transfer, such as the keyboard, screen and network (see the appendices for more information on binary coding, and serial and parallel data).

The mouse is a convenient pointer controller for selecting options on screen and drawing graphics. The original mouse used two rollers set at right angles, with perforated disks attached. The holes were detected using an opto-sensor, sending pulses representing movement in two directions to the CPU. This mechanism has been replaced with direct optical sensing of variations in the surface under the mouse, using complex software to extract the direction and speed information. This also eliminates unreliable mechanical components.

Data input from a network or USB source is also in serial form, while the internal disk interface is traditionally in parallel, direct onto the peripheral bus. The parallel connection is inherently faster, since data bits are transferred simultaneously on all bus lines.

 

Computer Systems:Word-Processor Operation and Starting the Computer

Word-Processor Operation

In order to understand the operation of the PC microprocessor system, we will look at how the word-processor application uses the hardware and software resources. This will help us to understand the same basic processes that occur in microcontrollers.

Starting the Computer

When the PC is switched on, the RAM is empty. The operating system, application software and user files are all stored on the hard disk, so the elements needed to run the word processor must be transferred to RAM for quick access when using the application. The BIOS gets the system started. It checks that the hardware is working properly, loads (copies) the main operating system software (e.g. Windows) from hard disk into RAM, which then takes over. As you will probably have noticed, this all takes some time; this is because of the amount of data transfer required and the relatively slow access to the hard drive.

 

Computer Systems:PC Memory

PC Memory

There are two principal types of memory in the PC system. The main memory block is RAM, where input data is stored before and after processing in the CPU. The operating system and application program are also copied to RAM from disk for execution, because access to data in RAM is faster. Unfortunately, RAM storage is ‘volatile’, which means that the data and application software disappear when the PC is switched off, and these have to be reloaded each time the computer is switched back on.

This means that some read-only memory (ROM), which is non-volatile, is needed to get the system started at switch on. The basic input/output system (BIOS) ROM chip contains enough code to check the system hardware and load the main operating system software from disk. It also contains some basic hardware control routines so that the keyboard and screen can be used before the main system has been loaded.

The hard disk is a non-volatile, read and write storage device, consisting of a set of metal disks with a magnetic recording surface, read/write heads, motors and control hardware. It provides a large volume of data storage for the operating system, application and user files. The applications are stored on disk and then selected as required for loading into memory; because the disk is a read and write device, user files can be stored, applications added and software updates easily installed.

Standard hard disk drives can now hold over 1 TB (1 terabyte ¼ 1012 bytes) of data.

The PC system quickly becomes ever more elaborate, and this description may well already be out of date in some respects. However, the basic principles of microprocessor system operation are the same as established in the earliest digital computers, and these also apply to microcontrollers, as we will see.

 

Computer Systems:PC Motherboard

PC Motherboard

The main features of typical motherboards are shown in Figure 1.3. The heart of the system is the microprocessor, a single chip, or central processing unit (CPU). The CPU controls all the other system components, and must have access to a suitable program in memory before it can do anything useful. The blocks of program required at any one time are provided by both the operating system and the application software, which are downloaded to random access memory (RAM) from the hard disk as required. The programs consist of lists of machine code instructions (binary code) that are executed in sequence by the CPU.

The Intel CPU has undergone rapid and continuous development since the introduction of the PC in the early 1980s. Intel processors are classified as complex instruction set computer (CISC) chips, which means they have a relatively large number of instructions that can be used in a number of different ways. This makes them powerful, but relatively slow compared with processors that have fewer instructions; these are classified as reduced instruction set computer (RISC) chips, of which the PIC microcontroller is an example.

The CPU needs memory and input/output devices for getting data in, storing it and sending it out again. The main memory block is made up of RAM chips, which are generally mounted in Dual In-line Memory Modules (DIMMs). As far as possible, input/output (I/O) interfacing hardware is fitted on the motherboard (keyboard, mouse, USB, etc., preferably wireless), but additional peripheral interfacing boards may be fitted in the expansion card slots to connect the main board to extra disk drives and other specialist peripherals, traditionally using the PCI bus, a parallel data highway 32 bits wide.

PIC Microcontrollers-1115

All these parts are connected together via a pair of bus controller chips, which handle parallel data transfers between the CPU and the system. The ‘northbridge’ provides fast access to RAM and the graphics (screen) interface, while its partner, the ‘southbridge’, handles slower peripherals such as the disk drives, network and PCI bus. The motherboard itself can be represented as a block diagram (Figure 1.4) to show how the components are interconnected.

The block diagram shows that the CPU is connected to the peripheral interfaces by a set of bus lines. These are groups of connections on the motherboard, which work together to transfer the

PIC Microcontrollers-1116

data from the inputs, such as the keyboard, to the processor, and from the processor to memory. When the data has been processed and stored, it can be sent to an output peripheral, such as the screen.

Buses connect all the main chips in the system together, but, because they mainly operate as shared connections, can only pass data to or from one peripheral interface or memory location at a time. This arrangement is used because separate connections to all the main chips would need an impossible number of tracks on the motherboard. The disadvantage of bus connection is that it slows down the program execution speed, because all data transfers use the same set of lines, and only one data word can be present on the bus at any one time. To help compensate for this, the bus connections are as wide as possible. For example, a 64-bit bus, operating at 100 MHz (108 Hz), can transfer 6.4 gigabits (6.4 x 109 bits) per second. The current generation of Intel® CPUs also use multiple (typically 4) 64-bit cores in one chip to improve performance.