Файл: Programming Microcontrollers in C, 2-nd edit (Ted Van Sickle, 2001).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 15.06.2025
Просмотров: 4084
Скачиваний: 1
Summary 121
Summary
In this chapter, you have seen how pointers are used in C, learned about structures, and viewed a summary of the contents of the stan dard C library. You have also been shown the essentials of creating header files that can attach peripheral devices to your programs. These areas will be explored in detail in later chapters.
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
Microcontroller Memory 127
communications is a common example of asynchronous data transfer. Here, an input line can have two states: mark and space. A line is held at the mark state whenever no data are being transferred. When data are to be transferred, the data line is transitioned to the space state and held there for a specified time. This period is called the start bit. From that time onward, the data bits are placed on the line a bit at a time so that at the specified time intervals the receiving device can examine the data line and determine the bit sequence.
Asynchronous operation means that there is no computer clockrelated specification as to the time that events will occur. Another example of asynchronous transfer occurs within the computer. Generally, events and data transfers that are initiated by interrupts are considered to be asynchronous. Most of the peripheral devices that are found on Motorola microcontrollers will allow either synchronous or asynchronous notification of the program that the peripheral business is completed.
The following sections contain brief discussions of microcontroller memory and several of the standard peripherals found on these devices. These discussions are intended to be qualitative and provide a broad overview of these parts of the microcontroller. Detailed descriptions of how to access and use these several peripherals will be found in later chapters.
Microcontroller Memory
In a microcontroller, the program instructions are usually stored in a memory type called read-only memory (ROM). ROM is usually programmed by a special mask during the manufacture of the microcontroller and is called masked ROM. ROM is the least expensive means of storing a program in a microcontroller, especially for highvolume manufacturing.
There are at least two means for the end user of the microcontroller to place the program memory into the chip. The first is called erasable programmable read-only memory (EPROM). EPROM is a memory technology that can be erased by exposing it to high-energy ultraviolet light. The EPROM requires the application of a high voltage to be programmed. The memory can be programmed with either a development system or a special programming board designed specifically to program the microcontroller.
128 Chapter 3 What Are Microcontrollers?
Packages that contain EPROM have a quartz glass window through which the ultraviolet light can pass with minimum attenuation. These packages are quite expensive, and therefore, microcontrollers with EPROM are usually too expensive to use. EPROM was used for development purposes in the past, but it is just too expensive in light of more recent developments to be used for that purpose today.
For limited production purposes, a less expensive version of the EPROM chip is available. This is the one time programmable (OTP) chip. An OTP chip has the exact same silicon component as an EPROM, but it is packaged in a standard plastic package. This package is much less expensive than the windowed package discussed previously. However, once the chip has been programmed, the program contents cannot be changed.
There is yet another means of storing programs or, in some instances, data in a microcontroller. This technique is called electrically erasable programmable read-only memory (EEPROM). EEPROM is programmable from instructions within the microcontroller. EEPROM also requires a high programming voltage. If there are large blocks of EEPROM on the chip, the programming voltage is usually applied during the programming cycle through a pin connected to an external voltage source. In cases where the amount of EEPROM to be programmed is relatively small, a charge pump on the microcontroller chip will allow the EEPROM to be programmed with no externally applied voltage. The amount of EEPROM that can be programmed with an on-board charge pump is usually so small that it is not useful for storing program instructions. But onboard EEPROM can be quite useful in the storage of data generated by the program that must be saved through a power-down cycle. Sometimes in the execution of the program, some data are generated that must be saved for later use. These data are called volatile data or variables, and are usually stored in random access memory (RAM). Careful design of a program will usually result in the need for much less RAM than ROM. In most microcontrollers, the amount of RAM is usually 60 to at most a few hundred bytes. The amount of ROM, EPROM or EEPROM usually runs from 1000 bytes upwards to a few tens of thousands of bytes.
EEPROM is quite expensive, and has been replaced by a newer technology called FLASH memory. FLASH programs in a manner