Lecture One PIC Introduction: Simplest Microcontroller Architecture: (CPU)
Lecture One PIC Introduction: Simplest Microcontroller Architecture: (CPU)
The term microcomputer is used to describe a system that includes at minimum a microprocessor, program memory, data memory, and an input-output (I/O) device. Some microcomputer systems include additional components such as timers, counters,and analog-to-digital converters. Thus, a microcomputer system can be anything from alarge computer having hard disks, floppy disks, and printers to a single-chip embedded controller.In this book we are going to consider only the type of microcomputers that consist ofa single silicon chip. Such microcomputer systems are also called microcontrollers,andthey are used in many household goods such as microwave ovens, TV remote controlunits, cookers, hi-fi equipment, CD players, personal computers, and refrigerators.Many different microcontrollers are available on the market. In this book we shall belooking at programming and system design for the PIC (programmable interfacecontroller) series of microcontrollers manufactured by Microchip Technology Inc. we will concentrate on just one device, the PIC 16F877,which has a good range of features and allows most of the essential techniques to be explained. It has a set of serial ports built in, which are used to transfer data to and from other devices, as well as analogue inputs, which allow measurement of inputs such as temperature. All standard types of microcontrollers work in a similar way, so analysis of one will make it possible to understand all the others.
of the microprocessor and sends signals to other parts of the microcontroller to carry outthe required instructions.
RAM
RAM, random access memory, is a general purpose memory that usually stores the user data in a program. RAM memory is volatile in the sense that it cannot retain data in the absence of power (i.e., data is lost after the power is turned off). Most microcontrollers have some amount of internal RAM, 256 bytes being a common amount, although some microcontrollers have more, some less. The PIC18F452 microcontroller, for example, has 1536 bytes of RAM. Memory can usually be extendedby adding external memory chips.
ROM
ROM, read only memory, usually holds program or fixed user data. ROM is nonvolatile. If power is removed from ROM and then reapplied, the original data will still be there. ROM memory is programmed during the manufacturing process, and the user cannot change its contents. ROM memory is only useful if you have developed a program and wish to create several thousand copies of it .
PROM
PROM, programmable read only memory, is a type of ROM that can be programmed in the field, often by the end user, using a device called a PROM programmer. Once a PROM has been programmed, its contents cannot be changed. PROMs are usually used in low production applications where only a few such memories are required.
EPROM
EPROM, erasable programmable read only memory, is similar to ROM, but EPROM can be programmed using a suitable programming device. An EPROM memory has a small clear-glass window on top of the chip where the data can be erased under strong ultraviolet light. Once the memory is programmed, the window can be covered with
dark tape to prevent accidental erasure of the data. An EPROM memory must be erasedbefore it can be reprogrammed. Many developmental versions of microcontrollers aremanufactured with EPROM memories where the user program can be stored. Thesememories are erased and reprogrammed until the user is satisfied with the program.Some versions of EPROMs, known as OTP (one time programmable), can beprogrammed using a suitable programmer device but cannot be erased. OTP memoriescost much less than EPROMs. OTP is useful after a project has been developedcompletely and many copies of the program memory must be made.
EEPROM
EEPROM, electrically erasable programmable read only memory, is a nonvolatile memory that can be erased and reprogrammed using a suitable programming device. EEPROMs are used to save configuration information, maximum and minimum values,identification data, etc. Some microcontrollers have built-in EEPROM memories. Forinstance, the PIC18F452 contains a 256-byte EEPROM memory where each byte can beprogrammed and erased directly by applications software. EEPROM memories areusually very slow. An EEPROM chip is much costlier than an EPROM chip.
Flash EEPROM
Flash EEPROM, a version of EEPROM memory, has become popular in microcontrollerapplications and is used to store the user program. Flash EEPROM is nonvolatile andusually very fast. The data can be erased and then reprogrammed using a suitableprogramming device. Some microcontrollers have only 1K flash EEPROM while othershave 32K or more. The PIC18F452 microcontroller has 32K bytes of flash memory.
Microcontroller Features
Supply voltage
Most microcontrollers operate with the standard logic voltage of _5V. Some microcontrollers can operate at as low as _2.7 V and some will tolerate _6 V without any problems. You should check the manufacturers data sheets about the allowed limits of the power supply voltage. A voltage regulator circuit is usually used to obtain the required power supply voltage when the device is to be operated from a mains adaptor or batteries. For example, a 5 V regulator is required if the microcontroller is to be operated from a 5 V supply using a 9 V battery.
The clock
All microcontrollers require a clock (or an oscillator) to operate. The clock is usually provided by connecting external timing devices to the microcontroller. Most microcontrollers will generate clock signals when a crystal and two small capacitors are connected. Some will operate with resonators or external resistorcapacitor pair. Some microcontrollers have built-in timing circuits and they do not require any external timing components. If your application is not time-sensitive you should use external or internal (if available) resistorcapacitor timing components for simplicity and low cost. An instruction is executed by fetching it from the memory and then decoding it. This usually takes
several clock cycles and is known as the instruction cycle. In PIC microcontrollers an instruction cycle takes four-clock periods. Thus, the microcontroller is actually operated at a clock rate which
is a quarter of the actual oscillator frequency.
Timers
Timers are important parts of any microcontroller. A timer is basically a counter which is driven either from an external clock pulse or from the internal oscillator of the microcontroller. A timer can be 8-bits or 16-bits wide. Data can be loaded into a timer under program control and the timer can be stopped or started by program control. Most timers can be configured to generate an interrupt when they reach a certain count (usually when they overflow). The interrupt can be used by the user program to carry out accurate-timing-related operations inside the microcontroller. Some microcontrollers offer capture and compare facilities where a timer value can be read when an external event occurs, or the timer value can be compared to a preset value and an interrupt can be generated when this value is reached. It is typical to have at least one timer in every microcontroller. Some microcontrollers may have two, three, or even more timers where some of the timers can be cascaded for longer counts.
Watchdog
Most microcontrollers have at least one watchdog facility. The watchdog is basically a timer which is refreshed by the user program and a reset occurs if the program fails to refresh the watchdog. The Microcontroller systems 7 watchdog timer is used to detect a system problem, such as the program being in an endless loop. A watchdog is a safety feature that prevents runaway software and stops the microcontroller from executing meaningless and unwanted code. Watchdog facilities are commonly used in real-time systems where it is required to regularly check the successful termination of one or more activities.
Reset input
A reset input is used to reset a microcontroller. Resetting puts the microcontroller into a known state such that the program execution starts from address 0 of the program memory. An external reset action is usually achieved by connecting a push-button switch to the reset input such that the microcontroller can be reset when the switch is pressed.
Interrupts
Interrupts are very important concepts in microcontrollers. An interrupt causes the microcontroller to respond to external and internal (e.g. a timer) events very quickly. When an interrupt occurs the microcontroller leaves its normal flow of program execution and jumps to a special part of the program, known as the Interrupt Service Routine (ISR). The program code inside the ISR is executed and upon return from the ISR the program resumes its normal flow of execution. The ISR starts from a fixed address of the program memory. This address is also known as the interrupt vector address. For example, in a PIC16F84 microcontroller the ISR starting address is 4 in the program memory. Some microcontrollers with multi-interrupt features have just one interrupt vector address, while some others have unique interrupt vector addresses, one for each interrupt source. Interrupts can be nested such that a new interrupt can suspend the execution of another interrupt. Another important feature of a microcontroller with multi-interrupt capability is that different interrupt sources can be given different levels of priority.
Brown-out detector
Brown-out detectors are also common in many microcontrollers and they reset a microcontroller if the supply voltage falls below a nominal value. Brown-out detectors are safety features and they can be employed to prevent unpredictable operation at low voltages, especially to protect the contents of EEPROM-type memories.
Analogue-to-digital converter
An analogue-to-digital converter (A/D) is used to convert an analogue signal such as voltage to a digital form so that it can be read by a microcontroller. Some microcontrollers have built-in A/D converters. It is also possible to connect an external A/D converter to any type of microcontroller. A/D converters are usually 8-bits, having 256 quantisation levels. Some microcontrollers have 10-bit A/D converters with 1024 quantisation levels. Most PIC microcontrollers with A/D features have multiplexed A/D converters where more than one analogue input channel is provided. 8 PIC BASIC projects The A/D conversion process must be started by the user program and it may take several hundreds of microseconds for a conversion to complete. A/D converters usually generate interrupts when a conversion is complete so that the user program can read the converted data quickly. A/D converters are very useful in control and monitoring applications since most sensors (e.g. temperature sensor, pressure sensor, force sensor, etc.) produce analogue output voltages.
Serial I/O
Serial communication (also called RS232 communication) enables a microcontroller to be connected to another microcontroller or to a PC using a serial cable. Some microcontrollers have built-in hardware called USART (Universal SynchronousAsynchronous ReceiverTransmitter) to implement a serial communication interface. The baud rate and the data format can usually be selected by the user program. If any serial I/O hardware is not provided, it is easy to develop software to implement serial data communication using any I/O pin of a microcontroller. We shall see in Chapter 4 how to use the PicBasic and PicBasic Pro statements to send and receive serial data from any pin of a PIC microcontroller. Some microcontrollers incorporate SPI (Serial Peripheral Interface) or I2C (Integrated Inter Connect) hardware bus interfaces. These enable a microcontroller to interface to other compatible devices easily.
Analog comparator
Analog comparators are used where it is required to compare two analogue voltages. Although these circuits are implemented in most high-end PIC microcontrollers they are not common in other microcontrollers.
Real-time clock
Real-time clock enables a microcontroller to have absolute date and time information continuously. Built-in real-time clocks are not common in most microcontrollers since they can easily be implemented by either using a dedicated real-time clock chip, or by writing a program.
Sleep mode
Some microcontrollers (e.g. PIC) offer built-in sleep modes where executing this instruction puts the microcontroller into a mode where the internal oscillator is stopped and the power consumption is reduced to an extremely low level. The main reason of using the sleep mode is to conserve the battery power when the microcontroller is not doing anything useful. The microcontroller usually wakes up from the sleep mode by external reset or by a watchdog time-out.
Power-on reset
Some microcontrollers (e.g. PIC) have built-in power-on reset circuits which keep the microcontroller in reset state until all the internal circuitry has been initialised. This feature is very useful as it starts the microcontroller from a known state on power-up. An external reset can also be provided where the microcontroller can be reset when an external button is pressed.