PIC Training Report
PIC Training Report
PIC Training Report
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
ACKNOWLEDGEMENT
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
LIST OF FIGURES
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
CONTENT
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
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
10
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.
11
3. PIC16F877A MICROCONTROLLER
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.
12
13
14
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
15
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
16
RA3/AN3/VREF+
RA4/T0CKI/C1OUT
RA5/AN4/SS/C2OUT
RE0/RD/AN5
RE1/WR/AN6
RE2/CS/AN7
10
11 12 13
OSC1 CLKI
OSC2/CLKO
14
OSC2 CLKO
RC0/T1OSO/T1CKI
15
RC1/T1OSI/CCP2
16
RC0 T1OSO T1CKI RC1 T1OSI CCP2 RC2 CCP1 RC3 SCK SCL RD0 PSP0 RD1 PSP1 RD2 PSP2 RD3 PSP3 RC4 SDI SDA RC5 SDO RC6 TX CK RC7 RX DT RD4 PSP4 RD5 PSP5 RD6 PSP6 RD7
RC2/CCP1
17
RC3/SCK/SCL
18
19 20 21 22 23
RC5/SDO RC6/TX/CK
24 25
RC7/RX/DT
26
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.
17
RB0 INT
RB3 PGM
37 38 39
RB7/PGD
40
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.
18
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).
19
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.
20
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.
21
5. INSTRUCTIONS SET
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
23
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.
24
7. INTERFACINGS
The following figure shows the internal components of a PIC16F877A and various types of peripherals which can be connected to it.
Fig 6. Interfacing
25
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.
26
27
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
28
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.
29
30
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.
31
LIMITATIONS
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.
32
SUMMARY
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.
33
BIBLIOGRAPHY
Study Material provided by Teachers of CDAC http://pdf1.alldatasheet.com/datasheetpdf/view/222581/RFSOLUTIONS/RX6000.htl http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=255 http://en.wikipedia.org/wiki/PIC_microcontroller
34