The small LCD screen is a good clue to the presence of an MCU Microcontroller Unit – it needs a programmed device to control it.. Although small, microcontrollers are complex, and we hav
Trang 1Labcenter Electronics
www.labcenter.co.uk Manufacturer and supplier of Proteus VSM electronic design system
Microchip Technology Inc.
www.microchip.com Manufacturer of the PIC microcontroller range and MPLAB IDE
Custom Computer Services, Inc.
www.ccsinfo.com Manufacturer and supplier of PIC CCS ‘C’ Compilers
Data References and Trademark Acknowledgements
Microchip Technology Inc., RS Components, Fairchild, Intel, Freescale (Motorola), National Semiconductor, Sensor Technics, Densitron, Honeywell, SGS Thomson, Maxim, ST Microelectronics, HBM, ARM, AVR Atmel, Texas, Vishay
I would also like to thank the dedicated teachers of engineering that I have worked with, especially Melvyn Ball at Hastings College and Chris Garrett at the University of Brighton, and, of course, Julia Bates
Martin Bates Hastings, UK
Links, References and Acknowledgements
Trang 2‘C’ Programming 47
Trang 3Conversion 106
Arithmetic 112
Calculator 121
Trang 4Current Loop 163 Comparators 165
SPI 205
Sensors 223
Trang 511 System Design 249
Contents
Trang 6Part 1 Microcontroller
Trang 8PIC Hardware
The microcontroller is simply a computer on a chip It is one of the most important developments in electronics since the invention of the microprocessor itself It is essential for the operation of devices such as mobile phones, DVD players, video cameras, and most self-contained electronic systems The small LCD screen is a good clue to the presence of an MCU (Microcontroller Unit) –
it needs a programmed device to control it Working sometimes with other chips, but often on its own, the MCU provides the key element in the vast range of small, programmed devices which are now commonplace
Although small, microcontrollers are complex, and we have to look carefully
at the way the hardware and software (control program) work together to understand the processes at work This book will show how to connect the pop-ular PIC range of microcontrollers to the outside world, and put them to work
To keep things simple, 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 The PIC 16F877 is also a good choice for learning about micro-controllers, because the programming language is relatively simple, as compared with a microprocessor such as the Intel Pentium™, which is used in the PC This has
a powerful, but complex, instruction set to support advanced multimedia applications The supporting documentation for the PIC MCU is well designed,
Trang 9and a development system, for writing and testing programs, can be down-loaded free from the Microchip website (www.microchip.com)
Processor System
The microcontroller contains the same main elements as any computer system:
• Processor
• Memory
• Input/Output
In a PC, these are provided as separate chips, linked together via bus connec-tions on a printed circuit board, but under the control of the microprocessor (CPU) A bus is a set of lines which carry data in parallel form which are shared by the peripheral devices The system can be designed to suit a partic-ular application, with the type of CPU, size of memory and selection of input/output (I/O) devices tailored to the system requirements
In the microcontroller, all these elements are on one chip This means that the MCU for a particular application must be chosen from the available range
to suit the requirements In any given circuit, the microcontroller also tends to have a single dedicated function (in contrast to the PC); this type of system is described as an embedded application (Figure 1.1)
Processor
In a microprocessor system or a microcontroller, a single processor block is in charge of all input, output, calculations and control This cannot operate without a program, which is a list of instructions that is held in memory The
Interfacing PIC Microcontrollers
CPU
Memory
Output Input
Figure 1.1 Block diagram of a basic microprocessor system
Trang 10program consists of a sequence of binary codes that are fetched from memory
by the CPU in sequence, and executed (Figure 1.2)
The instructions are stored in numbered memory locations, and copied to
an instruction register in the CPU via the data bus Here, the instruction controls the selection of the required operation within the control unit of the processor The program codes are located in memory by outputting the address of the instruction on an address bus The address is generated in the program counter, a register that starts at zero and is incremented or modified during each instruction cycle The busses are parallel connections which transfer the address or data word in one operation A set of control lines from the CPU are also needed to assist with this process; these control lines are set up according to the requirements of the current in-struction
Decoding the instruction is a hardware process, using a block of logic gates
to set up the control lines of the processor unit, and fetching the instruction operands The operands are data to be operated on (or information about where
to find it) which follows most instructions Typically, a calculation or logical operation is carried out on the operands, and a result stored back in memory,
or an I/O action set up Each complete instruction may be 1, 2 or more bytes long, which includes the operation (instruction) code (op-code) itself and the operand/s (1 byte ⫽ 8 bits)
Address Instruction
0000 10010011
0001 01010001
0002 10000100
0003 00011001
0004 01011100
0005 xxxxxxxx
0006 xxxxxxxx etc etc
Instruction Register
Decoder Logic
Execution Logic
Control lines to system
Data bus Address bus ProgramCounter
Figure 1.2 Processor program execution