Sie sind auf Seite 1von 34


PIC16F877A Microcontroller
At Centre for Development of Advanced Computing Mohali
Submitted in the partial fulfillment of the requirements of Degree, Bachelor of Technology in Electronics & Communication Engineering. By




To Kurukshetra University, Kurukshetra, Haryana, India) Oct.2011


A formal statement of acknowledgement is hardly sufficient to express my gratitude towards the persons who have helped me to undertake and complete this training. Training in an organization Centre for Development of Advanced Computing, Mohali is in itself a true learning experience. I hereby convey my thanks to all who have rendered their valuable help, support and guidance. I am highly thankful to Mr. Vikas Malhotra for the valuable guidance, technical acumen, round the clock encouragement and support that he has provided to me. I would also like to thank all the staff of C.D.A.C. Mohali, who has directly or indirectly helped in making my training a success and making it a fulfilling and a memorable experience. I also take this opportunity to thank my H.O.D and faculty members at college who consistently supported me and guided me during the training period. Though not physically present with me but your constant guidance not only through phone but the valuable acumen you provided me with during the college was practically put to use during the training and was a key factor in its successful completion. VATSALA SHARMA Roll No.1508229 B. Tech (E.C.E) (IVth Year) NCCE, ISRANA PANIPAT

1. Microcontroller Architecture...11 2. Block diagram....14 3. Pin diagram....15 4. CPU Memory..19 5. Basic Connection24 6. Interfacing..25 7. Interfacing switch to PIC..26 8. Interfacing LED to PIC.27 9. Interfacing LCD to PIC.28 10. Seven segment display30 10.1 Common anode.30 10.2 Common cathode..30 11. Interfacing seven segment display to PIC31

LIST OF TABLES 1. 2. 3. 4. Pin description16 Address banks.21 Instruction Set.22 Pin configuration for LCD.29

Acknowledgements ................................................................................................................ 2 Certificate.....3 List Of Figures.4 List of tables.5 Contents6 Chapter 1: Introduction 1.1 Training Organizational Details......8 1.2 Training Introduction.......9 Chapter 2:Microcontrollers 2.1 Microcontroller over Microprocessor10 2.2 Microcontroller Architectures11 Chapter 3:PIC16F877A 3.1 Advantages12 3.2 Features.13 3.3 Block Diagram..14 3.4 Pin Discription..15 Chapter 4: Central Processing Unit.19 4.1 Memory.19 4.1.1 ROM...19 4.1.2 EEPROM...19 4.1.3 RAM...19 4.2 Registers....20 4.2.1 General Purpose Register.20 4.2.2 SFR Register..20 4.3 Stack..20 4.4 RAM Memory Banks...21 Chapter 5: Instruction Set.22 Chapter 6: Basic Connection.24 Chapter 7: Interfacings..25 7.1 Interfacing Switch Display to PIC.26 7.2 Interfacing LED Display to PIC.27 7.3 Interfacing LCD Display to PIC.....28 7.4 Interfacing 7 Segment Display to PIC....30

Limitations..32 Summary.....33 Bibliography...34


Centre for Development of Advanced Computing(C-DAC) is the premier R&D organization of the Department of information technology (DIT), Ministry of Communication & Information technology (MCIT) for carrying out R&D in IT, Electronics and associated areas. A different areas of C-DAC, had originated at different times, many of which came out as a result of identification of opportunities. Starting from its initial mission on building indigenous supercomputers, Centre for Development of advanced computing(C-DAC) has progressively grown to build an ecosystem and institutional framework for innovation, technology development, skills, delivery plans, collaboration, partnership and market orientation in a number of skills, delivery plans, collaboration, partnership and market orientation in a number of niche areas of national importance and market relevance in ICT and Electronics. C-DACs focus has been on emerging as a leader in chosen enabling technology areas and work towards integration of these in end-to-end solutions in various verticals/domains including infrastructure. The latter is undertaken oftentimes by C-DAC itself but equally or more often in

conjunction/collaboration with other public and private agencies through a consortium and partnership mode. Institutional innovation to support scaling up process of such efforts is also one of the priority objectives. The focal areas in terms of enabling technologies as outlined above would be:-High Performance Computing & Grid Computing, Language Computing, Software Technologies with special reference to free/open source software, Professional Electronics including VLSI and Embedded System, Cyber security, Health Informatics, Education &training with special reference to finishing School and areas of specialized skills


A microcontroller is a programmable logic and integrated circuit which can be programmed to do a number of tasks. It is possible to control just about anything with the program written by the user. Basically, a microcontroller (sometimes abbreviated C, uC or MCU) is a small computer on a single integrated circuit containing a processor core, memory, and programmable input/output peripherals. Some of the functions of a microcontroller are Ability to execute a stored set of instructions to carry out user defined task. Ability to access external memory chips to read & write data from/to memory. Ability to interface with the I/O devices. Basic components of a Microcontroller are A CPU to execute programmed code Memory, both RAM and ROM Internal timers and input/output system, in form of i/o pins Some Microcontrollers have some extra features likeUART for serial communication, Internal EEPROM and PWM modules for analogue output Microcontrollers are used in automatically controlled products and devices, such as automobile engine control systems, implantable medical devices, remote controls, office machines, appliances, power tools, toys and other embedded systems. By reducing the size and cost compared to a design that uses a separate microprocessor, memory, and input/output devices, microcontrollers make it economical to digitally control even more devices and processes. Mixed signal microcontrollers are common, integrating analog components needed to control non-digital electronic systems.


With development in computer technology, newer versions of processing units are coming into existence. Every new unit have some advantages over previous one which results in replacement of older units. Microprocessors were used quite significantly in large number of applications but it was soon replaced by the microcontroller because of the following reasons: A microprocessor (P) is a one-chip CPU such as the Intel 80x86, or Pentium series, the Motorola 68000 series, the PowerPC series and so on These single-chip systems were called microprocessors because they replaced an older generation which used multiple chips and even multiple pc boards to perform the same functions. A P needs additional chips such as RAM, ROM, serial and parallel ports, timers, interrupt controllers and so on to make a complete cpu (motherboard). A microcontroller (C) has the CPU and most, if not all of these peripheral functions built into a single chip. Cs tend to be much smaller than modern Ps, with memory in tens of bytes to kilobytes. Cs can also include other features such as oscillator-on-chip, A/D converters, reset circuitry, high current I/O drive capability, watchdog timers etc. Basically, the benefit of Cs in embedded systems is that complete systems can be built with a very low component count, frequently just one chip. Microcontrollers are designed for embedded applications, in contrast to the microprocessors used in personal computers or other general purpose applications.



There are two types of basic architectures: Von-Neumann Architecture- It has only one bus which is used for both data transfers and instruction fetches, and therefore data transfers and instruction fetches must be scheduled they cannot be performed at the same time. Harvard Architecture- It has physically separate signals and storage for code and data memory. It is possible to access program memory and data memory simultaneously.

Fig 1. Microcontroller Architectures When we look at a typical microcontroller one of the first things we notice is that the microcontroller contains its own memory, parallel ports, clock and very often a good number of peripheral functions not found on a microprocessor. In the case of PIC microcontrollers we will find both flash program memory where instructions are stored and a separate ram memory space where only data is stored. Computers that have separate memory spaces for data and program instructions are classified as the Harvard Architecture. One particular advantage of the Harvard Architecture is that data access operations can be taking place simultaneously with program instruction execution. Each memory area has its own data bus to the CPU so operations can go on during the same time frame. Many different manufacturers use the Harvard Architecture for designs of microcontrollers because this design is very efficient for designing specific purpose controllers that go into other systems. Microcontrollers usually get embedded into other products such as microwave ovens, automobile engine controllers, power tools, appliances, industrial instruments, hand held devices, cell phones and many other areas. The design and programming for these types of products and applications has become so specialized that it has taken on its own identity as embedded design.


The PIC16F887 is one of the latest products from Microchip. It features all the components which modern microcontrollers normally have. For its low price, wide range of application, high quality and easy availability, it is an ideal solution in applications such as: the control of different processes in industry, machine control devices, measurement of different values etc.


The PIC architectures have these advantages: Small instruction set to learn RISC architecture Built in oscillator with selectable speeds Easy entry level, in circuit programming plus in circuit debugging PIC Kit units available from for less than $50 Inexpensive microcontrollers Wide range of interfaces including I2C, SPI, USB, USART, A/D, programmable Comparators, PWM, LIN, CAN, PSP, and Ethernet



RISC architecture o Only 35 instructions to learn o All single-cycle instructions except branches Operating frequency 0-20 MHz Precision internal oscillator o Factory calibrated o Software selectable frequency range of 8MHz to 31KHz Power supply voltage 2.0-5.5V o Consumption: 220uA (2.0V, 4MHz), 11uA (2.0 V, 32 KHz) 50nA (stand-by mode) Power-Saving Sleep Mode Brown-out Reset (BOR) with software control option 35 input/output pins o High current source/sink for direct LED drive o software and individually programmable pull-up resistor o Interrupt-on-Change pin 8K ROM memory in FLASH technology o Chip can be reprogrammed up to 100.000 times In-Circuit Serial Programming Option o Chip can be programmed even embedded in the target device 256 bytes EEPROM memory Data can be written more than 1.000.000 times 368 bytes RAM memory A/D converter: o 14-channels o 10-bit resolution 3 independent timers/counters Watch-dog timer Analogue comparator module with o Two analogue comparators o Fixed voltage reference (0.6V) o Programmable on-chip voltage reference PWM output steering control Enhanced USART module o Supports RS-485, RS-232 and LIN2.0 o Auto-Baud Detect Master Synchronous Serial Port (MSSP) o supports SPI and I2C mode



Fig 2. Block Diagram



Fig 3. Pin Diagram As seen in Fig.2, most of the pins are multi-functional. For example, designator RA3/AN3/Vref+ for the fifth pin specifies the following functions:

RA3 Port A third digital input/output AN3 Third analog input Vref+ Positive voltage reference



NUMBER FUNCTION DESCRIPTION Master Clear (Reset) input. This pin is 1 MCLR active low VPP Programming voltage input. 2 RA0 Digital I/O. AN0 Analog input 0 3 RA1 Digital I/O. AN1 Analog input 1. 4 RA2 AN2 VREF CVREF RA3 AN3 VREF+ RA4 T0CKI C1OUT RA5 AN4 SS C2OUT RE0 RD AN5 RE1 WR AN6 RE2 CS AN7 Digital I/O. Analog input 2. A/D reference voltage (Low) input. Comparator VREF output. Digital I/O. Analog input 3. A/D reference voltage (High) input. Digital I/O Open-drain when configured as output. Timer0 external clock input. Comparator 1 output. Digital I/O. Analog input 4. SPI slave select input. Comparator 2 output. Digital I/O. Read control for Parallel Slave Port. Analog input 5. Digital I/O. Write control for Parallel Slave Port. Analog input 6. Digital I/O. Chip select control for Parallel Slave Port. Analog input 7. Positive supply Ground Oscillator crystal input External clock source input. Always associated with pin function OSC1 Oscillator crystal output. In RC mode, OSC2 pin outputs CLKO, which









11 12 13















19 20 21 22 23


24 25




27 28 29 30

has 1/4 the frequency of OSC1 Digital I/O. Timer1 oscillator output. Timer1 external clock input Digital I/O. Timer1 oscillator input. Capture2 input, Compare2 output, PWM2 output. Digital I/O. Capture1 input, Compare1 output, PWM1 output. Digital I/O. Synchronous serial clock input/output for SPI mode. Synchronous serial clock input/output for I2C mode. Digital I/O. Parallel Slave Port data. Digital I/O. Parallel Slave Port data. Digital I/O. Parallel Slave Port data. Digital I/O. Parallel Slave Port data. Digital I/O. SPI data in. I2C data I/O Digital I/O. SPI data out. Digital I/O. USART asynchronous transmit. USART1 synchronous clock Digital I/O. USART asynchronous receive. USART synchronous data. Digital I/O. Parallel Slave Port data. Digital I/O. Parallel Slave Port data. Digital I/O. Parallel Slave Port data. Digital I/O.

PSP7 VSS VDD RB0/INT RB1 RB2 RB3/PGM 31 32 33 34 35 36




37 38 39




Parallel Slave Port data. Ground Positive supply Digital I/O. External interrupt. Digital I/O. Digital I/O. Digital I/O. Low-voltage ICSP programming enable pin. Digital I/O. Digital I/O. Digital I/O. In-circuit debugger and ICSP programming clock. Digital I/O. In-circuit debugger and ICSP programming data.

Table 3. Pin discription



CPU is manufactured with in RISC technology which is an important factor when deciding which microprocessor to use. RISC Reduced Instruction Set Computer, gives the PIC16F887 two great advantages: The CPU can recognizes only 35 simple instructions (In order to program some other microcontrollers it is necessary to know more than 200 instructions by heart). The execution time is the same for all instructions except two and lasts 4 clock cycles (oscillator frequency is stabilized by a quartz crystal). The Jump and Branch instructions execution time is 2 clock cycles. It means that if the microcontrollers operating speed is 20MHz, execution time of each instruc tion will be 200nS, i.e. the program will be executed at the speed of 5 million instructions per second!

Fig. 4. CPU Memory 4.1 MEMORY This microcontroller has three types of memory- ROM, RAM and EEPROM. All of them will be separately discussed since each has specific functions, features and organization. 4.1.1 ROM Memory ROM memory is used to permanently save the program being executed. This is why it is often called program memory. The PIC16F887 has 8Kb of ROM (in total of 8192 locations). Since this ROM is made with FLASH technology, its contents can be changed by providing a special programming voltage (13V).Anyway, there is no need to explain it in detail because it is automatically performed by means of a special program on the PC and a simple electronic device called the Programmer. 4.1.2 EEPROM Memory Similar to program memory, the contents of EEPROM is permanently saved, even the power goes off. However, unlike ROM, the contents of the EEPROM can be changed during operation of the microcontroller. That is why this memory (256 locations) is a perfect one for permanently saving results created and used during the operation. 4.1.3 RAM Memory This is the third and the most complex part of microcontroller memory. In this case, it consists of two parts: general-purpose registers and special-function registers (SFR).


Even though both groups of registers are cleared when power goes off and even though they are manufactured in the same way and act in the similar way, their functions do not have many things in common. 4.2REGISTERS 4.2.1General-Purpose Registers General-Purpose registers are used for storing temporary data and results created during operation. For example, if the program performs a counting (for example, counting products on the assembly line), it is necessary to have a register which stands for what we in everyday life call sum. Since the microcontroller is not creative at all, it is necessary to specify the address of some general purpose register and assign it a new function. A simple program to increment the value of this register by 1, after each product passes through a sensor, should be created. Therefore, the microcontroller can execute that program because it now knows what and where the sum which must be incremented is. Similarly to this simple example, each program variable must be preassigned some of general-purpose register. 4.2.2 SFR Registers Special-Function registers are also RAM memory locations, but unlike general-purpose registers, their purpose is predetermined during manufacturing process and cannot be changed. Since their bits are physically connected to particular circuits on the chip (A/D converter, serial communication module, etc.), any change of their contents directly affects the operation of the microcontroller or some of its circuits. For example, by changing the TRISA register, the function of each port A pin can be changed in a way it acts as input or output. Another feature of these memory locations is that they have their names (registers and their bits), which considerably facilitates program writing. Since high-level programming language can use the list of all registers with their exact addresses, it is enough to specify the registers name in order to read or change its contents. 4.3 STACK A part of the RAM used for the stack consists of eight 13-bit registers. Before the microcontroller starts to execute a subroutine (CALL instruction) or when an interrupt occurs, the address of first next instruction being currently executed is pushed onto the stack, i.e. onto one of its registers. In that way, upon subroutine or interrupt execution, the microcontroller knows from where to continue regular program execution. This address is cleared upon return to the main program because there is no need to save it any longer, and one location of the stack is automatically available for further use. It is important to understand that data is always circularly pushed onto the stack. It means that after the stack has been pushed eight times, the ninth push overwrites the value that was stored with the first push. The tenth push overwrites the second push and so on .In addition, the programmer cannot access these registers for write or read and there is no Status bit to indicate stack overflow or stack underflow conditions. For that reason, one should take special care of it during program writing.


4.4 RAM Memory Banks The data memory is partitioned into four banks. Prior to accessing some register during program writing (in order to read or change its contents), it is necessary to select the bank which contains that register. In order to facilitate operation, the most commonly used SFRs have the same address in all banks which enables them to be easily accessed.

Table 1. Address Banks



Table 2. Instruction set The PIC microcontroller also has a very different type of instruction set. The particular PIC microcontroller located on the trainer board we will be using is the PIC16F877A. This is in 22

the midrange family but its capabilities are at the top end of the midrange family. This unit only uses 35 instructions! This is the same for all microcontrollers in the midrange family. The strategy that is used in the design of this type of microcontroller is to make a few very simple instructions that execute very fast. Using this strategy a complex task can be accomplished with the combination of many very fast simple instructions in a relatively short time. Computers that use just a small number of very simple instructions that execute very fast are classified as RISC or Reduced Instruction Set Computers. Since PIC microcontrollers use only 35 very simple instructions they fit neatly into this category


6. BASIC CONNECTIONS As seen in the figure below, in order to enable the microcontroller to operate properly it is necessary to provide: Power Supply; Reset Signal; and Clock Signal.

Fig 5. Basic Connections


The following figure shows the internal components of a PIC16F877A and various types of peripherals which can be connected to it.

Fig 6. Interfacing



Fig 7. Interfacing Switch to PIC Fig 7. shows switch interfacing to RA0. Or any pin to be used as an inout pin. A high pin should be written to the pin if this is not done, the pin will be read low. In the above figure when the switch is not pressed the 10k resistor provides the current needed for LOGIC 1 closure of switch provides LOGIC 0 to the controller PIN.



Figure 8. shows how to interface the LED to microcontroller. As you can see the Cathode is connected through a resistor to Ground & the Anode is connected to the Microcontroller pin. So when the Port Pin is HIGH the LED is ON & when the Port Pin is LOW the LED is turned OFF.

Fig 8. Interfacing LED to PIC



Liquid Crystal Display also called as LCD is very helpful in providing user interface as well as for debugging purpose. The most common type of LCD controller is HITACHI 44780 which provides a simple interface between the controller & an LCD. These LCD's are very simple to interface with the controller as well as are cost effective. The most commonly used ALPHANUMERIC displays are 1X16 (Single Line & 16 characters), 2X16 (Double Line & 16 character per line) & 4X20 (four lines & Twenty characters per line).

Fig 9. Interfacing LCD to PIC The LCD requires 3 control lines (RS, R/W & EN) & 8 (or 4) data lines. The number on data lines depends on the mode of operation. If operated in 8-bit mode then 8 data lines + 3 control lines i.e. total 11 lines are required. And if operated in 4-bit mode then 4 data lines + 3 control lines i.e. 7 lines are required. How do we decide which mode to use? Its simple if you have sufficient data lines you can go for 8 bit mode & if there is a time constrain i.e. display should be faster then we have to use 8-bit mode because basically 4-bit mode takes twice as more time as compared to 8-bit mode. When RS is low (0), the data is to be treated as a command. When RS is high (1), the data being sent is considered as text data which should be displayed on the screen. When R/W is low (0), the information on the data bus is being written to the LCD. When RW is high (1), the program is effectively reading from the LCD. Most of the times there is no need to read from the LCD so this line can directly be connected to GND thus saving one controller line. The ENABLE pin is used to latch the data present on the data pins. A HIGH - LOW signal is required to latch the data. The LCD interprets and executes our command at the instant the

EN line is brought low. If you never bring EN low, your instruction will never be executed. For Contrast setting a 10K pot should be used as shown in the figure.

Table 3. Pin Configuration for LCD Pin Symbol I/O 1 Vss 2 Vcc 3 VEE 4 5 6 7 8 9 10 11 12 13 14 15 16 RS R/W E DB0 DB1 DB2 DB3 DB4 DB5 DB6 DB7 LEDLED+ I I I/O I/O I/O I/O I/O I/O I/O I/O I/O I I

Description Ground +5V Power supply Power supply to control Contrast RS = 0 to select command register RS = 1 to select data register R/W = 0 for write, R/W = 1 for read Enable The 8-bit data bus The 8-bit data bus The 8-bit data bus The 8-bit data bus The 8-bit data bus The 8-bit data bus The 8-bit data bus The 8-bit data bus Ground for LED backlight +5v for LED backlight

Display Data Ram (DDRAM) stores the display data. So when we have to display a character on LCD we basically write it into DDRAM. For a 2x16 LCD the DDRAM address for first line is from 80h to 8fh & for second line is 0c0h to 0cfh. So if we want to display 'H' on the 7th position of the first line then we will write it at location 87h.



7 Segment displays are basically 7 LED's as shown in Figure 1.9. It will be much easier to understand if you first read Interfacing LED's to Microcontroller. Basically there are two types of 7-Seg displays: Common Anode where all Segments share the same Anode. [See fig 1.8(a))] Common Cathode where all the segments share the same Cathode. [See fig 1.8(b))] Here we will be only discussing the Common Anode type. In common Anode in order to turn ON a segment the corresponding pin must be set to 0. And to turn it OFF it is set to 1. Fig 10. Seven Segment Display

Fig 10.1 Common Anode

Fig10.2 Common Cathode


Fig 11. Interfacing 7-Seg Display to PIC

Figure 11. shows how to interface 7-seg display to a microcontroller. Now we create a lookup table containing the seven segment pattern to display the corresponding hex digits. e.g. consider we have to display '1' from the above figure we come to know that turning ON segment B & C will show '1' on the 7-seg display so RB1 & RB2 should be LOGIC 0 whereas rest of the pins should be LOGIC 1.


Though PIC16F877A is very efficient microcontroller for some particular operation but still its architecture have these limitations:

Presence of only one accumulator Register-bank switching is required to access the entire RAM of many devices Operations and registers are not orthogonal; some instructions can address RAM and/or immediate constants, while others can only use the accumulator It has no internal oscillator so you will need an external crystal of other clock source.


The 16F877A is one of the most popular PIC microcontrollers and it comes in a 40 pin DIP pin-out and the PIC16F877A is rich in internal and external peripherals so it can be used for many different purposes. One of the most useful features of a PIC microcontroller is that it can be re-programmed as they use flash memory.


Study Material provided by Teachers of CDAC