Docsity

chapter 3 is amazing, Thesis of Computer Science

Toggle sidebar
bg1
Chapter 3: PIC16F887 Microcontroller
The PIC16F887 is a well known product by Microchip. It features all the components which modern
microcontrollers normally have. For its low price, wide range of application, high qual-ity 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. Some of its main features are listed
below.
•
3.1 THE PIC16F887 BASIC FEATURES
•
3.2 CORE SFRS
•
3.3 INPUT/OUTPUT PORTS
•
3.4 TIMER TMR0
•
3.5 TIMER TMR1
•
3.6 TIMER TMR2
•
3.7 CCP MODULES
•
3.8 SERIAL COMMUNICATION MODULES
•
3.9 ANALOG MODULES
•
3.10 CLOCK OSCILLATOR
•
3.11 EEPROM MEMORY
•
3.12 RESET! BLACK-OUT, BROWN-OUT OR NOISES?
3.1 THE PIC16F887 BASIC FEATURES
•
RISC architecture
RISC architectureRISC architecture
RISC architecture
o
Only 35 instructions to learn
o
All single-cycle instructions except branches
•
Operating frequency 0
Operating frequency 0Operating frequency 0
Operating frequency 0-
--
-20 MHz
20 MHz20 MHz
20 MHz
•
Precision internal oscillator
Precision internal oscillatorPrecision internal oscillator
Precision internal oscillator
o
Factory calibrated
o
Software selectable frequency range of 8MHz to 31KHz
•
Power supply voltage 2.0
Power supply voltage 2.0Power supply voltage 2.0
Power supply voltage 2.0-
--
-5.5V
5.5V5.5V
5.5V
o
Consumption: 220uA (2.0V, 4MHz), 11uA (2.0 V, 32 KHz)
50nA (stand-by mode)
•
Power
PowerPower
Power-
--
-Saving Sleep Mode
Saving Sleep ModeSaving Sleep Mode
Saving Sleep Mode
•
Brown
BrownBrown
Brown-
--
-out Reset (BOR) with software control option
out Reset (BOR) with software control optionout Reset (BOR) with software control option
out Reset (BOR) with software control option
•
35 input/output pins
35 input/output pins35 input/output pins
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
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download chapter 3 is amazing and more Thesis Computer Science in PDF only on Docsity!

Chapter 3: PIC16F887 Microcontroller

The PIC16F887 is a well known product by Microchip. It features all the components which modern microcontrollers normally have. For its low price, wide range of application, high qual-ity 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. Some of its main features are listed below.

• 3.1 THE PIC16F887 BASIC FEATURES

• 3.2 CORE SFRS

• 3.3 INPUT/OUTPUT PORTS

• 3.4 TIMER TMR

• 3.5 TIMER TMR

• 3.6 TIMER TMR

• 3.7 CCP MODULES

• 3.8 SERIAL COMMUNICATION MODULES

• 3.9 ANALOG MODULES

• 3.10 CLOCK OSCILLATOR

• 3.11 EEPROM MEMORY

• 3.12 RESET! BLACK-OUT, BROWN-OUT OR NOISES?

3.1 THE PIC16F887 BASIC FEATURES

• RISC architectureRISC architectureRISC architectureRISC architecture

o Only 35 instructions to learn

o All single-cycle instructions except branches

• Operating frequency 0Operating frequency 0Operating frequency 0Operating frequency 0----20 MHz20 MHz20 MHz20 MHz

• Precision internal oscillatorPrecision internal oscillatorPrecision internal oscillatorPrecision internal oscillator

o Factory calibrated

o Software selectable frequency range of 8MHz to 31KHz

• Power supply voltage 2.0Power supply voltage 2.0Power supply voltage 2.0Power supply voltage 2.0----5.5V5.5V5.5V5.5V

o Consumption: 220uA (2.0V, 4MHz), 11uA (2.0 V, 32 KHz)

50nA (stand-by mode)

• PowerPowerPowerPower----Saving Sleep ModeSaving Sleep ModeSaving Sleep ModeSaving Sleep Mode

• BrownBrownBrownBrown----out Reset (BOR) with software control optionout Reset (BOR) with software control optionout Reset (BOR) with software control optionout Reset (BOR) with software control option

• 35 input/output pins35 input/output pins35 input/output pins35 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 technology8K ROM memory in FLASH technology8K ROM memory in FLASH technology8K ROM memory in FLASH technology

o Chip can be reprogrammed up to 100.000 times

• InInInIn----Circuit Serial ProgrammingCircuit Serial ProgrammingCircuit Serial ProgrammingCircuit Serial Programming OptionOptionOptionOption

o Chip can be programmed even embedded in the target

device

• 256 bytes EEPROM memory256 bytes EEPROM memory256 bytes EEPROM memory256 bytes EEPROM memory

o Data can be written more than 1.000.000 times

• 368 bytes RAM memory368 bytes RAM memory368 bytes RAM memory368 bytes RAM memory

• A/D converter:A/D converter:A/D converter:A/D converter:

o 14-channels

o 10-bit resolution

• 3 independent timers/counters3 independent timers/counters3 independent timers/counters3 independent timers/counters

• WatchWatchWatchWatch----dog timerdog timerdog timerdog timer

• Analogue comparator module withAnalogue comparator module withAnalogue comparator module withAnalogue comparator module with

o Two analogue comparators

o Fixed voltage reference (0.6V)

o Programmable on-chip voltage reference

• PWM output steering controlPWM output steering controlPWM output steering controlPWM output steering control

• Enhanced USART moduleEnhanced USART moduleEnhanced USART moduleEnhanced USART module

o Supports RS-485, RS-232 and LIN2.

o Auto-Baud Detect

• Master Synchronous Serial Port (MSSP)Master Synchronous Serial Port (MSSP)Master Synchronous Serial Port (MSSP)Master Synchronous Serial Port (MSSP)

o supports SPI and I2C mode

PINOUT DESCRIPTION

Most pins of the PIC16F887 microcontroller are multi-functional as seen in figure above. For example, designator RA3/AN3/Vref+/C1IN+ for the fifth pin of the microcontroller indicates that it has the following functions:

  • RA3 Port A third digital input/output
  • AN3 Third analog input
  • Vref+ Positive voltage reference
  • C1IN+ Comparator C1 positive input Such pin functionality is very useful as it makes the microcontroller package more compact without affecting its operation. These various pin functions cannot be used simultaneously, but can be changed at any point during operation. The following tables refer to the PDIP 40 microcontroller.

CENTRAL PROCESSOR UNIT (CPU)

We are not going to bore you with the operation of the CPU at this stage. However, we will just state that the CPU is manufactured with RISC technology as it is an important factor when deciding which microcontroller to use. RISC stands for Reduced Instruction Set Computer , which gives the PIC16F877 two great advantages:

  • The CPU only recognizes 35 simple instructions. Just to mention that in order to program other microcontrollers in assembly

language it is necessary to know more than 200 instructions by heart.

  • The execution time is the same for almost all instructions, and lasts for 4 clock cycles. The oscillator frequency is stabilized by a quartz crystal. The execution time of jump and branch instructions is 2 clock cycles. It means that if the microcontroller’s operating speed is 20MHz, the execution time of each instruction will be 200nS, i.e. the program will execute 5 million instructions per second!

MEMORY

The PIC16F887 has three types of memory ROM, RAM and EEPROM. All of them will be separately discussed since each has specific functions, features and organization. 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 the ROM memory is made with FLASH technology, its contents can be changed by providing a special programming voltage (13V).

GENERAL-PURPOSE REGISTERS

General-purpose registers are used for storing temporary data and results created during operation. For example, if the program performs 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 that function. A simple program to increment the value of this register by 1, after each product passes through a sensor, should be created. Now the microcontroller can execute the program as it knows what and where the sum to be incremented is. Similarly, each program variable must be preassigned some of the general- purpose registers.

SPECIAL FUNCTION REGISTERS (SFRS)

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 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, the ADCON0 register controls the operation of A/D converter. By changing its bits it is determined which port pin is to be configured as converter input, the moment conversion is to start as well as the speed of conversion. Another feature of these memory locations is that they have their names (both registers and their bits), which considerably simplifies the process of writing a program. Since high-level programming languages can use the list of all registers with their exact addresses, it is enough to specify the name of a register in order to read or change its contents. RAM MEMORY BANKS The RAM memory is partitioned into four banks. Prior to accessing any register during program writing (in order to read or change its contents), it is necessary to select the bank which contains that register. Two bits of the STATUS register are used for bank selection to be discussed later. In order to simplify the operation, the most commonly used SFRs have the same address in all banks, which enables them to be easily accessed.

recoverable. 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 this reason, it is necessary to take special care of it during program writing. Let's do it in mikroC... /* When entering or exiting an assembly instruction in the program, the compiler doesn’t save data on the currently active RAM bank. It means that in this program section, bank selection depends on the SFR registers in use. When switching back to the program section written in C, the control bits RP0 and RP1 must return the state they had before assembly language code execution. In this example, the problem is solved by using the saveBank auxiliary variable which saves the state of these two bits. */ saveBank = STATUS & 0b01100000; // Save the state of bits RP0 and RP // (bits 5 and 6 of the STATUS register) asm { // Start of assembly sequence ... ... // Assembly code ... } // End of assembly sequence STATUS &= 0b10011111; // Bits RP0 and RP1 return their original state STATUS |= saveBank; ... ...

INTERRUPT SYSTEM

The first thing the microcontroller does when an interrupt request arrives is to execute the current instruction and then stops the regular program execution. As a result, the current program memory address is automatically pushed onto the stack and the default address (predefined by the manufacturer) is written to the program counter. The location from where the program proceeds with execution is called an interrupt vector. For the PIC16F887 microcontroller, this address is 0004h. As seen in figure below, the location containing the interrupt vector is passed over during regular program execution. A part of the program to be executed when an interrupt request arrives is called an interrupt routine. Its first instruction is located at the interrupt vector. How long will it take to execute this subroutine and what it will be like depends on the skills of the programmer as well as on the interrupt source itself. Some of the microcontrollers have more interrupt vectors (every interrupt request has its vector), but in this case

there is only one. Consequently, the first part of the interrupt routine consists in interrupt source detection. Finally, when the interrupt source is recognized and the interrupt routine is executed, the microcontroller reaches the RETFIE instruction, pops the address from the stack and proceeds with program execution from where it left off. mikroC recognizes an interrupt routine to be executed as the void interrupt() function. The body of that function, i.e. interrupt routine, should be written by the user. void interrupt() { // Interrupt routine cnt++ ; // Interrupt causes variable cnt to be incremented by 1 }

In Short: How to Use SFRs

You have bought the microcontroller and have a good idea how to use it... There is a long list of SFRs and their bits. Each of them controls some process. All in all, it looks like a big control table with a lot of instruments and switches. Now you are concerned about whether you will manage to learn how to use them all? You will probably not, but don’t worry, you don’t have to! Such powerful microcontrollers are similar to supermarkets: they offer so many things at low prices and it is up to you to choose those you need. Therefore, select the field you are interested in and study only what you need to know. When you completely understand hardware operation, study SFRs which are in control of it (there are usually a few of them).

o 111 1 - After power-on or after executing the CLRWDT instruction which resets the watchdog timer. o 000 0 - After executing the SLEEP instruction which sets the microcontroller into low-consumption mode.

  • ZZZZ ---- Zero bitZero bitZero bitZero bit o 111 1 - The result of an arithmetic or logic operation is zero. o 000 0 - The result of an arithmetic or logic operation is different from zero.
  • DCDCDCDC ---- Digit carry/borrow bitDigit carry/borrow bitDigit carry/borrow bitDigit carry/borrow bit is changed during addition and subtraction if an ‘overflow’ or a ‘borrow’ of the result occurs. o 111 1 - A carry-out from the 4th low-order bit of the result has occurred. o 000 0 - No carry-out from the 4th low-order bit of the result has occurred.
  • CCCC ---- Carry/Borrow bitCarry/Borrow bitCarry/Borrow bitCarry/Borrow bit is changed during addition and subtraction if an ‘overflow’ or a ‘borrow’ of the result occurs, i.e. if the result is greater than 255 or less than 0. o 111 1 - A carry-out from the most significant bit (MSB) of the result has occurred. o 000 0 - No carry-out from the most significant bit (MSB) of the result has occurred.

OPTION_REG Register

Legend: R/W - Readable/Writable Bit, (1) After reset, bit is set The OPTION_REG register contains various control bits to configure Timer0/WDT prescaler, timer TMR0, external interrupt and pull-ups on PORTB.

  • RBPURBPURBPURBPU --- Port B Pull up Ena-Port B Pull up EnaPort B Pull up EnaPort B Pull up Enable bit.ble bit.ble bit.ble bit. o 111 1 - PortB pull-ups are disabled. o 000 0 - PortB pull-ups are enabled.
  • INTEDGINTEDGINTEDGINTEDG ---- Interrupt Edge Select bit.Interrupt Edge Select bit.Interrupt Edge Select bit.Interrupt Edge Select bit. o 111 1 - Interrupt on rising edge of RB0/INT pin. o 000 0 - Interrupt on falling edge of RB0/INT pin.
  • T0CST0CST0CST0CS ---- TMR0 Clock Source Select bit.TMR0 Clock Source Select bit.TMR0 Clock Source Select bit.TMR0 Clock Source Select bit. o 111 1 - Transition on TOCKI pin. o 000 0 - Internal instruction cycle clock (Fosc/4).