Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Programming Microcontrollers in C, 2-nd edit (Ted Van Sickle, 2001).pdf
Скачиваний:
296
Добавлен:
12.08.2013
Размер:
7.2 Mб
Скачать

Chapter 3

What Are Microcontrollers?

A microcontroller differs from a microprocessor in several im­ portant ways. The early name for a microcontroller was microcomputer. The big difference between a microprocessor and a microcomputer/microcontroller is the completeness of the machine each represents. A microprocessor was simply the “heart” of a com­ puter. To put a microprocessor into use, the designer required memory, peripheral chips, and serial and parallel ports to make a completely functional computer. By contrast, the microcomputer was designed to be a complete computer on a single chip. Necessary memory and peripheral components were integrated onto the chip so that a com­ plete computer-based system could be built with a minimum of external components. A basic microcontroller is shown in block dia­ gram form in Figure 3-1.

Program

Memory

Input/Output

Arithmetic

Logic Unit

Data Memory

Figure 3-1: A Typical Microcontroller Block Diagram

123

124 Chapter 3 What Are Microcontrollers?

The central control unit of the microcontroller is the arithmetic logic unit (ALU). Figure 3-1 shows that the ALU is connected to three different blocks. The first is the input/output block (I/O), the second is the program memory, and the third is the data memory. Most Motorola microcontrollers combine the last three blocks into one block. The architecture shown in the figure is known as a Harvard architecture, as opposed to the more common Von Neumann architecture. The Harvard architecture is a computer configuration in which the memory area that contains the program instructions for the computer is separated from the memory area in which data are stored. By contrast, the Von Neumann architecture has just one memory space where both program and data are stored.

The main functional difference between Harvard and Von Neumann architectures is in their ultimate operating speeds. Both architectures require that the ALU access memory once each instruction to get the next instruction to execute. Often the instruction being executed will also require an access to memory. Reading data into a register, storing data in a memory address, and accessing a location in memory that is in fact an input/output register are examples of operations that require memory accesses in addition to the normal memory fetches. As seen in Figure 3-1, the Harvard architecture has two or more internal data busses over which these different accesses can take place. There are usually two such internal busses: one for instruction access, and one for other data access. The processor can easily tell which data bus to use. If the access is to fetch an instruction, it is relative to the program counter. These accesses will go to the program memory area. All other memory accesses will go to the data memory area. It is entirely possible to have two or more memory accesses simultaneously with a Harvard architecture.

The Von Neumann architecture is somewhat simpler than the Harvard architecture. A Von Neumann processor has only one memory bus. All memory accesses must go through this single path on the system. With such a system, the processor can never process more than one memory access at a time and all memory accesses—instruction, data, or input/output—must pass through a single data bus. This is the origin of the term “Von Neumann bottleneck.” The multiple accesses to memory for each instruction ultimately limit the maximum speed of a Von Neumann architecture processor. However, the speed of such processors can be many millions of instructions per second, so there

What are Microcontrollers? 125

are numerous excellent, fast microcontrollers constructed with the Von Neumann architecture. The Von Neumann architecture has been the mainstay of microcontrollers and will be the only microcontroller configuration available for the foreseeable future.

A microcontroller has its program stored internally, and the ALU reads an instruction from memory. This instruction is decoded by the ALU and executed. At the completion of the execution of the instruction, the next instruction is fetched from memory and it is executed. This procedure is repeated until the end of the program is found, or the program gets into a loop where it is instructed to branch back to a beginning point. In this case, the machine will stay in the loop forever or until something happens to release it from the never-ending loop.

There are three ways for a machine locked in a loop to be removed from the loop so it can execute code outside of the loop. These operations are called exceptions. The first is to reset the part with a reset signal. A reset signal usually requires connecting the reset pin of the part to a logic low signal. A logic low is usually ground. When this condition is detected, several internal registers are set to predetermined values, and the microcontroller fetches the address of the reset routine from a specific memory location. This address is placed in the program counter, and the program starts to execute. There is a table in memory that contains the addresses of several routines accessed when exceptions occur. These are the addresses of the interrupt service routines, reset routines, etc. This table is called the vector table, and the addresses are called vectors.

A second means of forcing the part out of the loop is for the part to detect an external interrupt. An external interrupt occurs when the interrupt request (IRQ) pin on the part is set low. This pin is tested at the beginning of the execution of each instruction. Therefore, if an instruction is being executed when an IRQ is asserted, the instruction will complete before the IRQ signal is processed. Processing for the IRQ consists of first determining if IRQs are enabled. If they are, the status of the machine is saved. All interrupts are disabled by setting the interrupt mask bit in the status register of the microcontroller. Then the address stored in the IRQ vector location is fetched. This address, the address of the interrupt service routine (ISR), is placed in the program counter. The ISR then executes.

126 Chapter 3 What Are Microcontrollers?

The process of saving the status of the machine is to push the contents of all machine registers onto the machine stack. Therefore, the ISR can safely use any of the central machine resources without disrupting the operation of the main line of code when control is returned. When exiting an ISR, it is necessary to use a special instruction called a return from interrupt or a return from exception. This instruction restores the status of the machine from the stack and picks up execution of the code from the instruction following the one where the interrupt occurred.

The third means for exiting the main loop of the program is from internal interrupts. The microcontroller peripherals can often cause interrupts to occur. An internal interrupt causes exactly the same sequence of operations to occur as an external interrupt. Different interrupt vectors are used for each of the several internal peripheral parts so the cause of the interrupt is generally known and control is directed to the specific ISR for each of the several possible internal interrupts.

Data are transferred, information is passed, or events are handled either synchronously or asynchronously. The difference between these two methods of data transfer has mainly to do with how the clocking of the data is handled. The most common form of synchronous data transfer is with a three-wire serial link. One of the wires is a clock, and the other two are input data and output data, respectively. For a synchronous transfer, the value of the input is usually sampled at one edge of the clock signal (such as the fall of the clock) and the value of the bit to be sent out is guaranteed to be correct at the fall of the clock signal. Any synchronous system must set its output at such a time that it will be stable while the clock is high, and hold it in that condition until the clock signal falls. To receive a bit, the condition of the input line must be latched into the system as the clock signal falls from high to low.

Within the computer, there is another distinction for synchronous. Often an input is allowed to set a bit when it occurs. If this happens, the program will not expeditiously observe the fact that the bit is set. In fact, the program will test the state of the bit according to the program timing requirements. This type of operation is also called synchronous because the test is synchronized with the program.

Asynchronous operation, on the other hand, usually depends on a prearranged series of events to cause the data transfer. Serial data