You are on page 1of 51

LES MICROCONTROLEURS PIC 16F87X

SOMMAIRE
1) INTRODUCTION........................................................................................................................................... 4 2) CARACTRISTIQUES DES MICROCONTROLEURS................................................................................ 5 2) ORGANISATIONS INTERNES..................................................................................................................... 6 2.1) LES CS 16F873 ET 16F876. ................................................................................................................. 6 2.2) LES CS 16F874 ET 16F877. ................................................................................................................. 7 3) DESCRIPTION DES DIFFERENTES BROCHES ........................................................................................ 8 3.1) LES CS 16F873 ET 16F876. ................................................................................................................. 8 3.2) LES CS 16F874 ET 16F877. ................................................................................................................. 9 4) BROCHAGES PHYSIQUES DES DIFFERENTES VERSIONS DE CS. ................................................. 10 5) LES BROCHES DU C............................................................................................................................... 11 5.1) MCLR.................................................................................................................................................... 11 5.2) OSCILLATEUR : OSC1 ET OSC2 OU CLKIN ET CLOUT....................................................................... 12 5.3) ALIMENTATION : VDD ET VSS. ............................................................................................................. 13 5.4) LINTERRUPTION : RBO/INT. ................................................................................................................ 13 6) L'UNIT CENTRALE. ................................................................................................................................. 14 6.1) ORGANISATION MEMOIRE. ................................................................................................................... 14 6.1.1) Plan Mmoire pour les instructions (code programme). ............................................................................... 14 6.1.2) Plan Mmoire pour les donnes et registres internes (SFR : Special Function Register). ............................ 15 6.1.2) LUAL : Lunit Arithmtique et Logique. ..................................................................................................... 17 7) JEU D'INSTRUCTIONS. ............................................................................................................................. 18 8) LES MODES DADRESSAGES ................................................................................................................. 19 8.1) ADRESSAGE INHERENT OU IMPLICITE ................................................................................................. 19 8.2) ADRESSAGE IMMEDIAT......................................................................................................................... 19 8.3) ADRESSAGES DIRECT ET ETENDU. ...................................................................................................... 19 8.4) ADRESSAGE RELATIF. .......................................................................................................................... 20 8.5) ADRESSAGE INDIRECT OU ENCORE INDEXE........................................................................................ 20 8.6) MANIPULATION DE BITS........................................................................................................................ 21 8.6.1) Forage de bits............................................................................................................................................... 21 8.6.2) Test de bits...................................................................................................................................................... 21 9) LES PORTS D'ENTREES SORTIES.......................................................................................................... 22 9.1) GENERALITES. ...................................................................................................................................... 22 9.2 CONFIGURATION DES PORTX , LES REGISTRES PORTX ET TRISX..................................................... 23 9.3) LE PORT A............................................................................................................................................ 24 9.3) LE PORT B............................................................................................................................................ 25 9.4) LE PORT C. .......................................................................................................................................... 26 9.5) LES PORT D ET E................................................................................................................................. 27 10) LE CONVERTISSEUR ANALOGIQUE NUMERIQUE. ............................................................................ 28 10.1) ORGANISATION INTERNE.................................................................................................................... 28 10.2) FONCTIONNEMENT DU CONVERTISSEUR. ......................................................................................... 29 10.3) LE REGISTRE ADCON1. ..................................................................................................................... 30 10.4) LE REGISTRE ADCON0. ..................................................................................................................... 31 10.5) EXEMPLE DUTILISATION. ................................................................................................................... 32

Philippe LETENNEUR - GRANVILLE - 2003 -

page 1

LES MICROCONTROLEURS PIC 16F87X


11) LES TIMERS. ............................................................................................................................................ 33 11.1) LE TIMER 0. ......................................................................................................................................... 33 11.1.1) Prsentation : ............................................................................................................................................... 33 11.1.2) Fonctionnement :.......................................................................................................................................... 34 11.1.3) Configuration et registres associs :............................................................................................................ 34 11.2) LE TIMER 1. ......................................................................................................................................... 35 11.2.1) Prsentation : ............................................................................................................................................... 35 11.2.2) Fonctionnement :.......................................................................................................................................... 35 11.2.3) Configuration et registres associs :............................................................................................................ 36 11.3) LE TIMER 2. ......................................................................................................................................... 37 11.3.1) Prsentation : ............................................................................................................................................... 37 11.3.2) Fonctionnement :.......................................................................................................................................... 37 11.3.3) Configuration et registres associs :............................................................................................................ 38 11.4) LES MODULES CCP1 ET CCP2 (C.C.P. :CAPTURE COMPARE PWM)............................................... 39 11.4.1) Le mode CAPTURE : ................................................................................................................................... 39 12) LA LIAISON SERIE USART OU SCI (SERIAL COMMUNICATION INTERFACE). ............................... 40 12.1) PRESENTATION : ................................................................................................................................ 40 12.2) LA TRANSMISSION : ............................................................................................................................ 41 12.2.1) Prsentation : ............................................................................................................................................... 41 12.2.2) Fonctionnement :.......................................................................................................................................... 41 12.2.3) Configuration et registres associs :............................................................................................................ 42 12.3) LA RECEPTION : .................................................................................................................................. 43 12.3.1) Prsentation : ............................................................................................................................................... 43 12.3.2) Fonctionnement :.......................................................................................................................................... 43 12.3.3) Configuration et registres associs :............................................................................................................ 44 12.4) LE GENERATEUR DHORLOGE: ........................................................................................................... 45 12.4.1) Prsentation et fonctionnement:................................................................................................................... 45 12.4.2) Configuration et registres associs :............................................................................................................ 46 13) LES INTERRUPTIONS. ............................................................................................................................ 47 13.1) PRESENTATION. ................................................................................................................................. 47 13.2) FONCTIONNEMENT. ............................................................................................................................ 47 13.3) DEROULEMENT DUNE INTERRUPTION............................................................................................... 48 13.4) CONFIGURATION ET REGISTRES ASSOCIES : .................................................................................... 49

Philippe LETENNEUR - GRANVILLE - 2003 -

page 2

LES MICROCONTROLEURS PIC 16F87X

Philippe LETENNEUR - GRANVILLE - 2003 -

page 3

LES MICROCONTROLEURS PIC 16F87X

1) INTRODUCTION.
Ces microcontrleurs mritent bien une introduction. En effet, ils ont t conus sur une architecture dite HARVARD (RISC) et non sur un modle VON NEUMANN (COMPLEX). Larchitecture VON NEUMANN employe par la plupart des microcontrleurs actuels (INTEL80XX, motorola HC05, HC08 et HC11, ou ZILOG Z80) est base sur un bus de donnes unique. Celui-ci vhicule les instructions et les donnes.

Larchitecture HARVARD utilise par les microcontrleurs PICS est base sur deux bus de donnes. Un bus est utilis pour les donnes et un autre pour les instructions.

Avantages et inconvnients.
Architecture VON NEUMANN (MOTOROLA, INTEL, ZILOG, ..) - Jeu dinstructions riches. - Accs la mmoire facile. Avantages Architecture HARVARD (RISC) (MICROCHIP PICs) - Jeu dinstructions pauvre, mais facile mmoriser. - Le codage des instructions est facile, chaque instruction est code sur un mot et dure un cycle machine. - Le code est plus compact. - Le jeu dinstruction est trs pauvre, par exemple pour effectuer une comparaison il faut faire une soustraction. - Les accs aux registres internes et la mmoire sont trs dlicats.

Inconvnients -

Le temps pour excuter une instruction est variable. Le codage des instructions se fait sur plusieurs octets.

Remarque : La taille mmoire spcifie pour un PICs sexprime en Kilo Mots (14 bits pour la famille 16F87X) et non en kilo octets. Comme chaque instruction est code par un mot de 14 bits, compares aux microcontrleurs classiques (1,2 ou 3 octets par instruction), les PICs ont un code plus compact et ils utilisent moins de mmoire.

Philippe LETENNEUR - GRANVILLE - 2003 -

page 4

LES MICROCONTROLEURS PIC 16F87X

2) CARACTRISTIQUES DES MICROCONTROLEURS.


Ces microcontrleurs appartiennent la famille des PICs. Le 16 signifie quils font partie de la famille des 16F de MICROCHIP et le PIC16F876 est une version 28 broches alors que le 16F877 est une version 40 broches.
Key Features PICmicro Mid-Range Reference Manual (DS33023) Operating Frequency RESETS (and Delays) FLASH Program Memory (14-bit words) Data Memory (bytes) EEPROM Data Memory Interrupts I/O Ports Timers Capture/Compare/PWM Modules Serial Communications Parallel Communications 10-bit Analog-to-Digital Module Instruction Set

PIC16F873 DC - 20 MHz POR, BOR (PWRT, OST) 4K 192 128 13 Ports A,B,C 3 2 MSSP, USART 5 input channels 35 instructions

PIC16F874 DC - 20 MHz POR, BOR (PWRT, OST) 4K 192 128 14 Ports A,B,C,D,E 3 2 MSSP, USART PSP 8 input channels 35 instructions

PIC16F876 DC - 20 MHz POR, BOR (PWRT, OST) 8K 368 256 13 Ports A,B,C 3 2 MSSP, USART 5 input channels 35 instructions

PIC16F877 DC - 20 MHz POR, BOR (PWRT, OST) 8K 368 256 14 Ports A,B,C,D,E 3 2 MSSP, USART PSP 8 input channels 35 instructions

Caractristiques communes : Programmable sur site ICSP (In Circuit Serial Programming). Dbuggable sur site ICD. Frquence de fonctionnement leve, jusqu 20 Mhz. Une mmoire vive de 192 368 octets. Une mmoire EEPROM pour sauver des paramtres de 128 256 octets. Une mmoire morte de type FLASH de 4 Kmots 8 Kmots (1mot = 14 bits). Chien de garde WDT. Surveillance d'horloge OST. Surveillance de tension dalimentation BOR. De 21 32 Entres et sorties suivant le type de micro contrleur. Chaque sortie peut sortir un courant maximum de 25mA. 3 Temporisateurs : TIMER0 (8 bits avec pr diviseur), TIMER1 (16 bits avec pr diviseur avec possibilit dutiliser une horloge externe rseau RC ou QUARTZ) et TIMER2 (8 bits avec pr diviseur et post diviseur) 2 entres de captures et de comparaison avec PWM (Modulation de largeur dimpulsions). Convertisseur analogique numrique 10 bits avec de 5 8 entres multiplexes maximum. Une interface de communication srie asynchrone et synchrone. (USART/SCI). Une interface de communication srie synchrone. (SSP/SPI et I2C). Plusieurs modes de fonctionnements faible consommation. Une seule tension d'alimentation 2 ou 5V. Conservation des informations en mmoire vive jusqu' 1.5V. Faible consommation : <2mA 5V pour Fquartz=4Mhz. 20A 3V pour Fquartz 32Khz. <1A pour en mode sommeil.

Philippe LETENNEUR - GRANVILLE - 2003 -

page 5

LES MICROCONTROLEURS PIC 16F87X


2) ORGANISATIONS INTERNES 2.1) Les Cs 16F873 et 16F876.
Device PIC16F873 PIC16F876 Program FLASH 4K 8K Data Memory 192 Bytes 368 Bytes
13 Program Counter FLASH Program Memory

Data EEPROM 128 Bytes 256 Bytes


Data Bus 8 PORTA RA0/AN0 RA1/AN1 RA2/AN2/VREFRA3/AN3/VREF+ RA4/T0CKI RA5/AN4/SS PORTB RB0/INT RB1 RB2 RB3/PGM RB4 RB5 RB6/PGC RB7/PGD PORTC

8 Level Stack (13-bit)

RAM File Registers RAM Addr(1)

Program Bus

14 Instruction reg Direct Addr 7

Addr MUX 8 Indirect Addr

FSR reg STATUS reg 8 3

Power-up Timer Instruction Decode & Control Timing Generation OSC1/CLKIN OSC2/CLKOUT Oscillator Start-up Timer Power-on Reset Watchdog Timer Brown-out Reset In-Circuit Debugger Low Voltage Programming 8

MUX

ALU

RC0/T1OSO/T1CKI RC1/T1OSI/CCP2 RC2/CCP1 RC3/SCK/SCL RC4/SDI/SDA RC5/SDO RC6/TX/CK RC7/RX/DT

W reg

MCLR

VDD, VSS

Timer0

Timer1

Timer2

10-bit A/D

Data EEPROM

CCP1,2

Synchronous Serial Port

USART

Philippe LETENNEUR - GRANVILLE - 2003 -

page 6

LES MICROCONTROLEURS PIC 16F87X


2.2) Les Cs 16F874 et 16F877.
Device PIC16F874 PIC16F877 Progr am FLASH 4K 8K Data Memo ry 192 Bytes 368 Bytes
13 FLASH Program Memory 8 Level Stack (13-bit) Program Bus 14 Instruction reg Direct Addr 7 Program Counter

Data EEPROM 128 Bytes 256 Bytes


Data Bus 8 PORTA RA0/AN0 RA1/AN1 RA2/AN2/VREFRA3/AN3/VREF+ RA4/T0CKI RA5/AN4/SS PORTB RB0/INT RB1 RB2 RB3/PGM RB4 RB5 RB6/PGC RB7/PGD PORTC

RAM File Registers RAM Addr(1) 9

Addr MUX 8 Indirect Addr

FSR reg 8 3 STATUS reg

Power-up Timer Instruction Decode & Control Timing Generation OSC1/CLKIN OSC2/CLKOUT Oscillator Start-up Timer Power-on Reset Watchdog Timer Brown-out Reset In-Circuit Debugger Low-Voltage Programming 8

MUX

ALU

RC0/T1OSO/T1CKI RC1/T1OSI/CCP2 RC2/CCP1 RC3/SCK/SCL RC4/SDI/SDA RC5/SDO RC6/TX/CK RC7/RX/DT PORTD RD0/PSP0 RD1/PSP1 RD2/PSP2 RD3/PSP3 RD4/PSP4 RD5/PSP5 RD6/PSP6 RD7/PSP7 PORTE

W reg

Parallel Slave Port

MCLR

VDD, VSS

RE0/AN5/RD RE1/AN6/WR RE2/AN7/CS

Timer0

Timer1

Timer2

10-bit A/D

Data EEPROM

CCP1,2

Synchronous Serial Port

USART

Remarque : Les 16F873 et 16F874 prsentent peu dintrt par rapport aux 16F876 et 16F877, en effet ils possdent moins de mmoires programmes et ils sont peu prs au mme prix.

Philippe LETENNEUR - GRANVILLE - 2003 -

page 7

LES MICROCONTROLEURS PIC 16F87X

3) DESCRIPTION DES DIFFERENTES BROCHES 3.1) Les Cs 16F873 et 16F876.


Pin Name OSC1/CLKIN OSC2/CLKOUT DIP Pin# 9 10 SOIC Pin# 9 10 I/O/P Type I O Buff er Type Descri ption

ST/CMOS(3) Oscillator crystal input/external clock source input. Oscillator crystal output. Connects to crystal or resonator in crystal oscillator mode. In RC mode, the OSC2 pin outputs CLKOUT which has 1/4 the frequency of OSC1, and denotes the instruction cycle rate. Master Clear (Reset) input or programming voltage input. This pin is an active low RESET to the device. PORTA is a bi-directional I/O port. RA0 can also be analog input0. RA1 can also be analog input1. RA2 can also be analog input2 or negative analog reference voltage. RA3 can also be analog input3 or positive analog reference voltage. RA4 can also be the clock input to the Timer0 module. Output is open drain type. RA5 can also be analog input4 or the slave select for the synchronous serial port. PORTB is a bi-directional I/O port. PORTB can be software programmed for internal weak pull-up on all inputs.

MCLR/VPP

I/P

ST

RA0/AN0 RA1/AN1 RA2/AN2/VREFRA3/AN3/VREF+ RA4/T0CKI RA5/SS/AN4

2 3 4 5 6 7

2 3 4 5 6 7

I/O I/O I/O I/O I/O I/O

TTL TTL TTL TTL ST TTL

RB0/INT RB1 RB2 RB3/PGM RB4 RB5 RB6/PGC RB7/PGD

21 22 23 24 25 26 27 28

21 22 23 24 25 26 27 28

I/O I/O I/O I/O I/O I/O I/O I/O

TTL/ST(1) TTL TTL TTL TTL TTL TTL/ST(2) TTL/ST(2)

RB0 can also be the external interrupt pin.

RB3 can also be the low voltage programming input. Interrupt-on-change pin. Interrupt-on-change pin. Interrupt-on-change pin or In-Circuit Debugger pin. Serial programming clock. Interrupt-on-change pin or In-Circuit Debugger pin. Serial programming data. PORTC is a bi-directional I/O port. RC0 can also be the Timer1 oscillator output or Timer1 clock input. RC1 can also be the Timer1 oscillator input or Capture2 input/Compare2 output/PWM2 output. RC2 can also be the Capture1 input/Compare1 output/ PWM1 output. RC3 can also be the synchronous serial clock input/output for both SPI and I2C modes. RC4 can also be the SPI Data In (SPI mode) or data I/O (I2C mode). RC5 can also be the SPI Data Out (SPI mode). RC6 can also be the USART Asynchronous Transmit or Synchronous Clock. RC7 can also be the USART Asynchronous Receive or Synchronous Data. Ground reference for logic and I/O pins. Positive supply for logic and I/O pins. P = power ST = Schmitt Trigger input

RC0/T1OSO/T1CKI RC1/T1OSI/CCP2 RC2/CCP1 RC3/SCK/SCL RC4/SDI/SDA RC5/SDO RC6/TX/CK RC7/RX/DT VSS VDD Legend: I = input

11 12 13 14 15 16 17 18 8, 19 20

11 12 13 14 15 16 17 18 8, 19 20

I/O I/O I/O I/O I/O I/O I/O I/O P P

ST ST ST ST ST ST ST ST

O = output = Not used

I/O = input/output TTL = TTL input

Note 1: This buffer is a Schmitt Trigger input when configured as the external interrupt. 2: This buffer is a Schmitt Trigger input when used in Serial Programming mode. 3: This buffer is a Schmitt Trigger input when configured in RC oscillator mode and a CMOS input otherwise.

Philippe LETENNEUR - GRANVILLE - 2003 -

page 8

LES MICROCONTROLEURS PIC 16F87X


3.2) Les Cs 16F874 et 16F877.
Pin Name OSC1/CLKIN OSC2/CLKOUT DIP Pin# 13 14 PLCC Pin# 14 15 QFP Pin# 30 31 I/O/P Type I O Bu ffer Type ST/CMOS
(4)

Descri pti on Oscillator crystal input/external clock source input. Oscillator crystal output. Connects to crystal or resonator in crystal oscillator mode. In RC mode, OSC2 pin outputs CLKOUT which has 1/4 the frequency of OSC1, and denotes the instruction cycle rate. Master Clear (Reset) input or programming voltage input. This pin is an active low RESET to the device. PORTA is a bi-directional I/O port. RA0 can also be analog input0. RA1 can also be analog input1. RA2 can also be analog input2 or negative analog reference voltage. RA3 can also be analog input3 or positive analog reference voltage. RA4 can also be the clock input to the Timer0 timer/ counter. Output is open drain type. RA5 can also be analog input4 or the slave select for the synchronous serial port. PORTB is a bi-directional I/O port. PORTB can be software programmed for internal weak pull-up on all inputs.

MCLR/VPP

18

I/P

ST

RA0/AN0 RA1/AN1 RA2/AN2/VREFRA3/AN3/VREF+ RA4/T0CKI RA5/SS/AN4

2 3 4 5 6 7

3 4 5 6 7 8

19 20 21 22 23 24

I/O I/O I/O I/O I/O I/O

TTL TTL TTL TTL ST TTL

RB0/INT RB1 RB2 RB3/PGM RB4 RB5 RB6/PGC RB7/PGD

33 34 35 36 37 38 39 40

36 37 38 39 41 42 43 44

8 9 10 11 14 15 16 17

I/O I/O I/O I/O I/O I/O I/O I/O

TTL/ST(1) TTL TTL TTL TTL TTL TTL/ST(2) TTL/ST(2)

RB0 can also be the external interrupt pin.

RB3 can also be the low voltage programming input. Interrupt-on-change pin. Interrupt-on-change pin. Interrupt-on-change pin or In-Circuit Debugger pin. Serial programming clock. Interrupt-on-change pin or In-Circuit Debugger pin. Serial programming data. PORTC is a bi-directional I/O port. RC0 can also be the Timer1 oscillator output or a Timer1 clock input. RC1 can also be the Timer1 oscillator input or Capture2 input/Compare2 output/PWM2 output. RC2 can also be the Capture1 input/Compare1 output/PWM1 output. RC3 can also be the synchronous serial clock input/ output for both SPI and I2C modes. RC4 can also be the SPI Data In (SPI mode) or data I/O (I2C mode). RC5 can also be the SPI Data Out (SPI mode). RC6 can also be the USART Asynchronous Transmit or Synchronous Clock. RC7 can also be the USART Asynchronous Receive or Synchronous Data. PORTD is a bi-directional I/O port or parallel slave port when interfacing to a microprocessor bus.

RC0/T1OSO/T1CKI RC1/T1OSI/CCP2 RC2/CCP1 RC3/SCK/SCL RC4/SDI/SDA RC5/SDO RC6/TX/CK RC7/RX/DT

15 16 17 18 23 24 25 26

16 18 19 20 25 26 27 29

32 35 36 37 42 43 44 1

I/O I/O I/O I/O I/O I/O I/O I/O

ST ST ST ST ST ST ST ST

RD0/PSP0 RD1/PSP1 RD2/PSP2 RD3/PSP3 RD4/PSP4 RD5/PSP5 RD6/PSP6 RD7/PSP7 RE0/RD/AN5 RE1/WR/AN6 RE2/CS/AN7 VSS VDD NC

19 20 21 22 27 28 29 30 8 9 10 12,31 11,32

21 22 23 24 30 31 32 33 9 10 11 13,34 12,35 1,17,28, 40

38 39 40 41 2 3 4 5 25 26 27 6,29 7,28 12,13, 33,34

I/O I/O I/O I/O I/O I/O I/O I/O I/O I/O I/O P P

ST/TTL(3) ST/TTL(3) ST/TTL(3) ST/TTL(3) ST/TTL(3) ST/TTL(3) ST/TTL(3) ST/TTL(3) PORTE is a bi-directional I/O port. ST/TTL(3) ST/TTL(3) ST/TTL(3) RE0 can also be read control for the parallel slave port, or analog input5. RE1 can also be write control for the parallel slave port, or analog input6. RE2 can also be select control for the parallel slave port, or analog input7. Ground reference for logic and I/O pins. Positive supply for logic and I/O pins. These pins are not internally connected. These pins should be left unconnected.

Philippe LETENNEUR - GRANVILLE - 2003 -

page 9

LES MICROCONTROLEURS PIC 16F87X


4) BROCHAGES PHYSIQUES DES DIFFERENTES VERSIONS DE CS.
RA3/AN3/VREF+ RA2/AN2/VREFRA1/AN1 RA0/AN0 MCLR/VPP NC RB7/PGD RB6/PGC RB5 RB4 NC 6 5 4 3 2 1 44 43 42 41 40
39 38 37 36 35 34 33 32 31 30 29

PLCC

PDIP, SOIC
MCLR/VPP RA0/AN0 RA1/AN1 RA2/AN2/VREFRA3/AN3/VREF+ RA4/T0CKI RA5/AN4/SS VSS OSC1/CLKIN OSC2/CLKOUT RC0/T1OSO/T1CKI RC1/T1OSI/CCP2 RC2/CCP1 RC3/SCK/SCL 1 2 3 4 5 6 7 8 9 10 11 12 13 14 28 27 26 25 24 23 22 21 20 19 18 17 16 15 RB7/PGD RB6/PGC RB5 RB4 RB3/PGM RB2 RB1 RB0/INT VDD VSS RC7/RX/DT RC6/TX/CK RC5/SDO RC4/SDI/SDA

PIC16F876/873

QFP
MCLR/VPP RA0/AN0 RA1/AN1 RA2/AN2/VREFRA3/AN3/VREF+ RA4/T0CKI RA5/AN4/SS RE0/RD/AN5 RE1/WR/AN6 RE2/CS/AN7 VDD VSS OSC1/CLKIN OSC2/CLKOUT RC0/T1OSO/T1CKI RC1/T1OSI/CCP2 RC2/CCP1 RC3/SCK/SCL RD0/PSP0 RD1/PSP1 1 2 3 4 5 6 40 39 38 37 36 35 RB7/PGD RB6/PGC RB5 RB4 RB3/PGM RB2 RB1 RB0/INT VDD VSS RD7/PSP7 RD6/PSP6 RD5/PSP5 RD4/PSP4 RC7/RX/DT RC6/TX/CK RC5/SDO RC4/SDI/SDA RD3/PSP3 RD2/PSP2

44 43 42 41 40 39 38 37 36 35 34

RC6/TX/CK RC5/SDO RC4/SDI/SDA RD3/PSP3 RD2/PSP2 RD1/PSP1 RD0/PSP0 RC3/SCK/SCL RC2/CCP1 RC1/T1OSI/CCP2 NC

RC1/T1OSI/CCP2 RC2/CCP1 RC3/SCK/SCL RD0/PSP0 RD1/PSP1 RD2/PSP2 RD3/PSP3 RC4/SDI/SDA RC5/SDO RC6/TX/CK NC

18 19 20 21 22 23 24 25 26 27 28

RA4/T0CKI RA5/AN4/SS RE0/RD/AN5 RE1/WR/AN6 RE2/CS/AN7 VDD VSS OSC1/CLKIN OSC2/CLKOUT C0/T1OSO/T1CK1 NC

7 8 9 10 11 12 13 14 15 16 17

PIC16F877 PIC16F874

RB3/PGM RB2 RB1 RB0/INT VDD VSS RD7/PSP7 RD6/PSP6 RD5/PSP5 RD4/PSP4 RC7/RX/DT

PIC16F877/874

7 8 9 10 11 12 13 14 15 16 17 18 19 20

34 33 32 31 30 29 28 27 26 25 24 23 22 21

Philippe LETENNEUR - GRANVILLE - 2003 -

NC NC RB4 RB5 RB6/PGC RB7/PGD MCLR/VPP RA0/AN0 RA1/AN1 A2/AN2/VREFRA3/AN3/VREF+

12 13 14 15 16 17 18 19 20 21 22

RC7/RX/DT RD4/PSP4 RD5/PSP5 RD6/PSP6 RD7/PSP7 VSS VDD RB0/INT RB1 RB2 RB3/PGM

1 2 3 4 5 6 7 8 9 10 11

PIC16F877 PIC16F874

33 32 31 30 29 28 27 26 25 24 23

NC RC0/T1OSO/T1CKI OSC2/CLKOUT OSC1/CLKIN VSS VDD RE2/AN7/CS RE1/AN6/WR RE0/AN5/RD RA5/AN4/SS RA4/T0CKI

page 10

LES MICROCONTROLEURS PIC 16F87X

5) LES BROCHES DU C. 5.1) MCLR.


Cette broche sert initialiser le C . Le C dispose de plusieurs sources de RESET : POR. EXTERNAL RESET. WDT. BOR. POR: (POWER ON RESET) Mise sous tension. Un front montant sur MCLR dclenche l'initialisation du C. Le temps ncessaire est au minimum de 72mS et au maximum de 72mS+1024*Tosc. Le C dispose en interne dun circuit de dtection de niveau, quand la tension VDD est comprise entre 1.2V et 1.7V, il dmarre une procdure dinitialisation. Cette broche peut tre simplement relie VDD si on na pas besoin de RESET externe. Par contre si on souhaite implanter un bouton de remise zro, on pourra cbler un simple rseau RC sur la broche MCLR. Remarque importante : On peut se passer de circuit RC la seule condition que le temps de mont de VDD soit suffisamment rapide (au minimum 50mV/mS). Si le temps de monte est infrieur 50mV/ms, il faut rajouter un rseau RC. EXTERNAL RESET (Mise ltat bas de MCLR). Remise zro extrieure. Il faut appliquer un niveau bas sur l'entre RESET pendant au moins 2S pour que l'Initialisation soit prise en compte. WDT: Chien de garde. Si le WDT arrive la fin du temps de garde sans avoir t rafrachi il y aura alors une initialisation du C. BOR: Baisse de lalimentation. Si la tension VDD chute en dessous de 4V pendant 100S au moins, le microcontrleur peut gnrer un RESET.

Schma structurel du cblage de la broche MCLR.


External Reset MCLR WDT Module VDD Rise Detect VDD Brown-out Reset OST/PWRT OST 10-bit Ripple Counter OSC1 (1) On-chip RC OSC PWRT 10-bit Ripple Counter Chip_Reset R Q Power-on Reset S WDT SLEEP

Time-out Reset

BODEN

Enable PWRT

Enable OST

Philippe LETENNEUR - GRANVILLE - 2003 -

page 11

LES MICROCONTROLEURS PIC 16F87X

5.2) Oscillateur : OSC1 et OSC2 ou CLKIN et CLOUT.


Ces broches permettent de faire fonctionner loscillateur interne du PIC. On peut utiliser 3 types doscillateurs :
C1(1)

Un quartz ou rsonateur cramique.


OSC1 To Internal Logic SLEEP PIC16F87X

TABLE 12-1:

CERAMIC RESONATORS
Ranges Teste d:

XTAL OSC2 C2(1) Rs (2)

RF(3)

Mode XT

Freq . 455 kHz 2.0 MHz 4.0 MHz 8.0 MHz 16.0 MHz

OSC1 68 - 100 pF 15 - 68 pF 15 - 68 pF 10 - 68 pF 10 - 22 pF

OSC2 68 - 100 pF 15 - 68 pF 15 - 68 pF 10 - 68 pF 10 - 22 pF

HS
Note 1: See Table 12-1 and Table 12-2 for recommended values of C1 and C2. 2: A series resistor (Rs ) may be required for AT strip cut crystals. 3: RF varies with the crystal chosen.

TABLE 12-2:

CAPACITOR SELECTION FOR CRYSTAL OS CILLATOR


Cap. Range C1 33 pF 15 pF 47-68 pF 15 pF 15 pF 15 pF 15-33 pF 15-33 pF Cap. Range C2 33 pF 15 pF 47-68 pF 15 pF 15 pF 15 pF 15-33 pF 15-33 pF

Osc Type LP XT

Cry stal Freq . 32 kHz 200 kHz 200 kHz 1 MHz 4 MHz

HS

4 MHz 8 MHz 20 MHz

Un oscillateur externe.
VDD

Un rseau RC.
REXT

Clock from Ext. System


Open

OSC1 PIC16F87X OSC2

OSC1 CEXT VSS FOSC/4 Recommended values: OSC2/CLKOUT 3k REXT 100 k

Internal Clock PIC16F87X

CEXT > 20pF

Remarque : Les instructions standards durent 1 cycle machine (sauf les instructions de sauts 2 cycles). Le C utilise 4 coups dhorloge pour raliser un cycle machine. Si la frquence du QUARTZ est de 20MHz( T=50nS) , une instruction sera excute toutes les 200nS, Dans ce cas l, le C a une puissance de calcul de 5MIPS (5 Millions dinstructions par secondes ! ! !). La frquence MAX est de 20MHz pour les C dont les rfrences se terminent par -20. Par exemples : 16F876-20 (20MHz max) et 16F876-04 (4MHz max). La frquence MIN est le continu. Remarque : La consommation du circuit sera d'autant plus faible que la frquence sera petite, cela peut tre intressant pour des applications de faible consommation (alimentation autonome). Pour des applications faible consommation, on peut utiliser les sries LF (Low Frequency and Low Power).

Philippe LETENNEUR - GRANVILLE - 2003 -

page 12

LES MICROCONTROLEURS PIC 16F87X


5.3) Alimentation : VDD et VSS.
Ce sont les broches d'alimentation du circuit. Les tensions qui peuvent tre appliques vont : - De 4,5V 6V pour la gamme standard F. - De 2 6V pour la gamme tendue LF. L'intensit du courant consomm peut aller de 1A 10mA. La consommation du C sera fonction de : - La tension d'alimentation. - La frquence interne. - Le mode de fonctionnement. De plus ces bornes doivent tre dcouples par deux condensateurs : - 1F lectrolytique. - 10nF cramique.

5.4) LInterruption : RBO/INT.


Cette broche a une double fonction elle peut tre utilise comme une broche standard RBO ou comme une entre dinterruption INT. Si cette broche est utilise comme une entre d'interruption externe, elle doit tre maintenue un niveau haut par l'intermdiaire de rsistances de 10 k pour ne pas dclencher dinterruptions imprvues, cela permet aussi de relier plusieurs sources d'interruptions sur une mme ligne (OU CABLE).

Philippe LETENNEUR - GRANVILLE - 2003 -

page 13

LES MICROCONTROLEURS PIC 16F87X


6) L'UNIT CENTRALE. 6.1) Organisation mmoire.
Comme les PICs utilisent un bus pour les instructions et un bus pour les donnes, il faut considrer deux plans mmoire lun pour les instructions et lautre pour les donnes ainsi que les registres internes.

6.1.1) Plan Mmoire pour les instructions (code programme).


PC<12:0>

CALL, RETURN RETFIE, RETLW

13

Stack Level 1 Stack Level 2

Stack Level 8

RESET Vector

0000h

Interrupt Vector Page 0

0004h 0005h 07FFh 0800h

On-Chip Program Memory

Page 1
0FFFh 1000h

Page 2
17FFh

Page 3

1800h 1FFFh

Le plan mmoire est linaire les adresses vont de 0000h 1FFFh (8k mots de 14 bits), par page de 2K mots . On peut remarquer, le vecteur de reset est fig en 0000h. Les PICs nont quun seul vecteur dinterruption en 0004h. Lors dune interruption, le sous programme associ devra dterminer quel priphrique a demand une interruption. La pile utilise par les sous programmes nest pas implante en mmoire de donne comme avec les microcontrleurs classiques, mais dans la mmoire programme. Elles sont utilises lors dappels de sous programmes, on ne peut pas imbriquer plus de 8 sous programmes (Ce qui est dj beaucoup ! !).

Philippe LETENNEUR - GRANVILLE - 2003 -

page 14

LES MICROCONTROLEURS PIC 16F87X


6.1.2) Plan Mmoire pour les donnes et registres internes (SFR : Special Function Register).
Le plan mmoire des donnes et des registres internes est dcoup en 4 zones ou bank de 128 octets, pour accder une zone il faut positionner les bits RP0 (bit 5) et RP1 (bit 6) du registre STATUS.
RP1 : RP0 00 01 10 11
File Address Indirect addr.(*) TMR0 PCL STATUS FSR PORTA PORTB PORTC PORTD(1) PORTE(1) PCLATH INTCON PIR1 PIR2 TMR1L TMR1H T1CON TMR2 T2CON SSPBUF SSPCON CCPR1L CCPR1H CCP1CON RCSTA TXREG RCREG CCPR2L CCPR2H CCP2CON ADRESH ADCON0 00h 01h 02h 03h 04h 05h 06h 07h 08h 09h 0Ah 0Bh 0Ch 0Dh 0Eh 0Fh 10h 11h 12h 13h 14h 15h 16h 17h 18h 19h 1Ah 1Bh 1Ch 1Dh 1Eh 1Fh 20h Indirect addr.(*) OPTION_REG PCL STATUS FSR TRISA TRISB TRISC TRISD(1) TRISE(1) PCLATH INTCON PIE1 PIE2 PCON

Zone slectionne (BANK) De 00h 7Fh : BANK 0 De 80h FFh : BANK 1 De 100h 17Fh : BANK 2 De 180h 1FFh : BANK 3
File Address 80h 81h 82h 83h 84h 85h 86h 87h 88h 89h 8Ah 8Bh 8Ch 8Dh 8Eh 8Fh 90h 91h 92h 93h 94h 95h 96h 97h 98h 99h 9Ah 9Bh 9Ch 9Dh 9Eh 9Fh A0h Indirect addr.(*) TMR0 PCL STATUS FSR PORTB File Address 100h 101h 102h 103h 104h 105h 106h 107h 108h 109h 10Ah 10Bh 10Ch 10Dh 10Eh 10Fh 110h 111h 112h 113h 114h 115h 116h 117h 118h 119h 11Ah 11Bh 11Ch 11Dh 11Eh 11Fh 120h Indirect addr.(*) OPTION_REG PCL STATUS FSR TRISB File Address 180h 181h 182h 183h 184h 185h 186h 187h 188h 189h 18Ah 18Bh 18Ch 18Dh 18Eh 18Fh 190h 191h 192h 193h 194h 195h 196h 197h 198h 199h 19Ah 19Bh 19Ch 19Dh 19Eh 19Fh 1A0h General Purpose Register 80 Bytes accesses 70h - 7Fh Bank 3

PCLATH INTCON EEDATA EEADR EEDATH EEADRH

PCLATH INTCON EECON1 EECON2 Reserved(2) Reserved(2)

SSPCON2 PR2 SSPADD SSPSTAT

TXSTA SPBRG

General Purpose Register 16 Bytes

General Purpose Register 16 Bytes

ADRESL ADCON1

General Purpose Register 96 Bytes

General Purpose Register 80 Bytes accesses 70h-7Fh Bank 1

EFh F0h FFh

General Purpose Register 80 Bytes accesses 70h-7Fh Bank 2

16Fh 170h 17Fh

1EFh 1F0h 1FFh

Bank 0

7Fh

Unimplemented data memory locations, read as 0. * Not a physical register. Note 1: These registers are not implemented on the PIC16F876. 2: These registers are reserved, maintain these registers clear.

Philippe LETENNEUR - GRANVILLE - 2003 -

page 15

LES MICROCONTROLEURS PIC 16F87X

Les registres appels General Purpose Register ne sont ni plus ni moins que des cases mmoires pour stocker les donnes. Remarque : Les 4 banques mmoires ne facilitent pas la gestion de la mmoire. Par exemple, pour accder la case mmoire 1A0h (BANK3), il ne faut pas oublier de positionner correctement les bits RP0 et RP1 du registre STATUS. Code : Transfert du contenu de ladresse 1A0h dans le registre W. BSF STATUS,5 ; RP0 = 1 Slection de la BANK 3 BSF STATUS,6 ; RP1 = 1 Slection de la BANK 3 MOVF 0x20,0 ; Transfert du contenu de ladresse 1A0h dans W. Pourquoi les concepteurs des PICs ont t obligs de scinder en 4 banques mmoires ? Ladresse des donnes est code sur 7 bits, donc les adresses vont de 00h 7Fh. Les 2 bits RP0 et RP1 sont 2 bits dadresses supplmentaires et extrieurs, ce qui porte 29 adresses possibles ou 512 registres. Code binaire de MOVF. 001000 d fff ffff d : destination 0 si W et 1 si F fff ffff : adresse mmoire.

Pour lexemple ci-dessus : MOVF 0x20,0 d=0 et fff ffff = 010 0000 Donc le code de MOVF 0x20,0 est : 001000 0 010 0000 En conclusion, la gestion de la mmoire avec les PICs nest pas trs facile, la programmation demande de la rigueur. La programmation des PICs avec des langages de hauts niveaux tels que BASIC, PASCAL et langage C permet de saffranchir de la gestion des pages mmoires.

Philippe LETENNEUR - GRANVILLE - 2003 -

page 16

LES MICROCONTROLEURS PIC 16F87X


6.1.3) LUAL : Lunit Arithmtique et Logique.

Elle est compose : - Dun accumulateur 8 bits W : WORKING (travail), cest lui qui effectue toutes les oprations arithmtiques et logiques. - Un registre d'tat 8 bits STATUS.
R/W-0 IRP bit 7 bit 7 IRP: Register Bank Select bit (used for indirect addressing) 1 = Bank 2, 3 (100h - 1FFh) 0 = Bank 0, 1 (00h - FFh) RP1:RP0: Register Bank Select bits (used for direct addressing) 11 = Bank 3 (180h - 1FFh) 10 = Bank 2 (100h - 17Fh) 01 = Bank 1 (80h - FFh) 00 = Bank 0 (00h - 7Fh) Each bank is 128 bytes TO: Time-out bit 1 = After power-up, CLRWDT instruction, or SLEEP instruction 0 = A WDT time-out occurred PD: Power-down bit 1 = After power-up or by the CLRWDT instruction 0 = By execution of the SLEEP instruction Z: Zero bit 1 = The result of an arithmetic or logic operation is zero 0 = The result of an arithmetic or logic operation is not zero DWFADD , LW,SUBLW,SUBWF instructions) DC: Digit carry/borrow bit (AD (for borrow, the polarity is reversed) 1 = A carry-out from the 4th low order bit of the result occurred 0 = No carry-out from the 4th low order bit of the result , DLW,SUBLW,SUBWF instructions) C: Carry/borrow bit (ADDWFAD 1 = A carry-out from the Most Significant bit of the result occurred 0 = No carry-out from the Most Significant bit of the result occurred Note : For borrow, the polarity is reversed. A subtraction is executed by adding the twos complement of the second operand. For rotate (RRF, RLF ) instructions, this bit is loaded with either the high, or low order bit of the source register. R/W-0 RP1 R/W-0 RP0 R-1 TO R-1 PD R/W-x Z R/W-x DC R/W-x C bit 0

bit 6-5

bit 4

bit 3

bit 2

bit 1

bit 0

Legend: R = Readable bit - n = Value at POR W = Writable bit 1 = Bit is set U = Unimplemented bit, read as 0 0 = Bit is cleared x = Bit is unknown

Philippe LETENNEUR - GRANVILLE - 2003 -

page 17

LES MICROCONTROLEURS PIC 16F87X


7) JEU D'INSTRUCTIONS.
Mnemo nic, Operan ds Description Cycl es 14-Bit Opco de MSb LSb Statu s Affected Notes

BYTE-ORIENTED FILE REGISTER OPERATIONS ADDWF ANDWF CLRF CLRW COMF DECF DECFSZ INCF INCFSZ IORWF MOVF MOVWF NOP RLF RRF SUBWF SWAPF XORWF f, d f, d f f, d f, d f, d f, d f, d f, d f, d f f, d f, d f, d f, d f, d Add W and f AND W with f Clear f Clear W Complement f Decrement f Decrement f, Skip if 0 Increment f Increment f, Skip if 0 Inclusive OR W with f Move f Move W to f No Operation Rotate Left f through Carry Rotate Right f through Carry Subtract W from f Swap nibbles in f Exclusive OR W with f 1 1 1 1 1 1 1(2) 1 1(2) 1 1 1 1 1 1 1 1 1
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0111 0101 0001 0001 1001 0011 1011 1010 1111 0100 1000 0000 0000 1101 1100 0010 1110 0110 df ff df ff lf ff 0x xx df ff df ff df ff df ff df ff df ff df ff lf ff 0x x0 df ff df ff df ff df ff df ff ffff ffff ffff xxxx ffff ffff ffff ffff ffff ffff ffff ffff 0000 ffff ffff ffff ffff ffff

C,DC,Z Z Z Z Z Z Z Z Z

1,2 1,2 2 1,2 1,2 1,2,3 1,2 1,2,3 1,2 1,2

C C C,DC,Z Z

1,2 1,2 1,2 1,2 1,2

BIT-ORIENTED FILE REGISTER OPERATIONS BCF BSF BTFSC BTFSS ADDLW ANDLW CALL CLRWDT GOTO IORLW MOVLW RETFIE RETLW RETURN SLEEP SUBLW XORLW Note f, b f, b f, b f, b k k k k k k k k k Bit Clear f Bit Set f Bit Test f, Skip if Clear Bit Test f, Skip if Set Add literal and W AND literal with W Call subroutine Clear Watchdog Timer Go to address Inclusive OR literal with W Move literal to W Return from interrupt Return with literal in W Return from Subroutine Go into standby mode Subtract W from literal Exclusive OR literal with W 1 1 1 (2) 1 (2) 1 1 2 1 2 1 1 2 2 2 1 1 1
01 01 01 01 11 11 10 00 10 11 11 00 11 00 00 11 11 00bb 01bb 10bb 11bb 111x 1001 0kkk 0000 1kkk 1000 00xx 0000 01xx 0000 0000 110x 1010 bf bf bf bf ff ff ff ff ffff ffff ffff ffff kkkk kkkk kkkk 0100 kkkk kkkk kkkk 1001 kkkk 1000 0011 kkkk kkkk

1,2 1,2 3 3 C,DC,Z Z TO,PD Z

LITERAL AND CONTROL OPERATIONS


kk kk kk kk kk kk 0110 kk kk kk kk kk kk 0000 kk kk 0000 0110 kk kk kk kk

TO,PD C,DC,Z Z

1: When an I/O register is modified as a function of itself ( e.g., MOVF PORTB, 1 ), the value used will be that value present on the pins themselves. For example, if the data latch is 1 for a pin configured as input and is driven low by an external device, the data will be written back with a 0. 2: If this instruction is executed on the TMR0 register (and, where applicable, d = 1), the prescaler will be cleared if assigned to the Timer0 module. 3: If Program Counter (PC) is modified, or a conditional test is true, the instruction requires two cycles. The second cycle is . executed as a NOP

LEGENDE DES TABLEAUX DINSTRUCTIONS d f fff ffff k b bbb Dtermine la destination : d=0 Registre Travail d=1 Registre Mmoire SFR ou RAM. Adresse SFR ou RAM en hexa. Adresse SFR ou RAM en binaire. Valeur immdiate sur 8bits ou Adresse de destination sur 11 bits. Valeur dcimale, elle dtermine le bit a modifier ou tester. Valeur binaire, elle dtermine le bit a modifier ou tester.

Remarque: toutes les instructions ne durent quun seul cycle machine, sauf les instructions de sauts tels que GOTO, CALL.

Philippe LETENNEUR - GRANVILLE - 2003 -

page 18

LES MICROCONTROLEURS PIC 16F87X

8) LES MODES DADRESSAGES 8.1) Adressage inhrent ou implicite


8.1.1) Description. Le mnmonique de l'instruction mentionne la donne sur laquelle porte l'opration (contenu des registres), ou aucune donne nest ncessaire. 8.1.2) Syntaxe. MNEMONIQUE 8.1.3) Exemples. CLRW NOP SLEEP ; Mise zro de W ; aucune opration (temporisation) ; Mise en sommeil du C

8.2) Adressage immdiat.


8.2.1) Description. L'instruction porte sur une valeur constante indique immdiatement aprs le mnmonique. 8.2.2) Syntaxe. MNEMONIQUE 8.2.3) Exemples. MOVLW ADDLW 255 0x20 constante ; charge 0xFF dans W ; additionne 32 avec W et met le rsultat dans W

8.3) Adressages direct et tendu.


8.3.1) Description. Les PICs ne disposent pas vraiment de modes dadressages DIRECT et ETENDU, ladressage de la mmoire de donnes se fait dans la page slectionne par les BIT 5 (RP0) et BIT 6 (RP1) du registre STATUS. 8.3.2) Syntaxe. MNEMONIQUE f,d d=0 Registre W comme destination (WORKING) d=1 Registre f comme destination (un des registers SFR) 8.3.3) Exemple. PORTB VARIABLE EQU EQU 0x06 0x20

; page 0

; Sassurer que nous sommes bien en BANK 0 BCF STATUS,5 ; RP0 = 0 Slection de la BANK 0 BCF STATUS,6 ; RP1 = 0 Slection de la BANK 0 ; PORTB <- VARIABLE MOVF VARIABLE,0 MOVWF PORTB ; ; ; ; Transfert le contenu de VARIABLE dans W Transfert le contenu de W dans le registre PortB

Philippe LETENNEUR - GRANVILLE - 2003 -

page 19

LES MICROCONTROLEURS PIC 16F87X


8.4) Adressage relatif.
8.4.1) Description. Ce mode d'adressage nexiste pas vraiment, mais des instructions permettent de raliser des sauts de programme, ceux sont les instructions GOTO et CALL. 8.4.2) Syntaxe. MNEMONIQUE 8.4.3) Exemple N1. Deux cas sont considrer : 1) Les sauts dans la mme page mmoire : CALL TEMPO ; Appel du SP TEMPO GOTO FIN ; Branchement ltiquette FIN 2) Les sauts dans une page mmoire diffrente, dans ce cas il faut positionner correctement les bits 4 et 3 du registre PCLATCH, pour accder la bonne page mmoire. BSF PCLATCH,3 ; 1 et 1 Slection de la BANK 3 de 1800h 1FFFh BSF PCLATCH,4 CALL CONV En effet pourquoi PCLATCH et pas PCH ? PCH nest pas accessible directement il faut passer par PCLATCH pour que les bits 4 et 3 de celui-ci soient recopis dans PCH.
PCH 12 PC 2 PCLATH<4:3> 11 Opcode <10:0> PCLATH 11 10 8 7 PCL 0
GOTO,CALL

Adresse

Remarque : Pour le retour de sous programme, il nest pas ncessaire de ce proccuper de ces bits car la valeur du PC est mmorise sur 13 bits dans la pile.

8.5) Adressage indirect ou encore index.


8.5.1) Description. Les PICs disposent travers les registres INDF(ou f0) et FSR(ou f4) dun mode dadressage index, la structure est un peu particulire, FSR est le registre dindex et INDF permet daccder son contenu. 8.5.2) Syntaxe. MNEMONIQUE INDF,d 8.5.3) Exemple. PORTB <- TAB_VAL[4] Rcuprer le 4me lment dune table TAB_VAL ; ; PORTB <- TAB_VAL[4] MOVLW TAB_VAL ADDLW 4 MOWF FSR MOVF INDF,0 MOVWF PORTB ; ; ; ; ; W <- Adresse de TAB_VAL W <- W + 4 Adresse + 4 dans le registre dindex FSR Transfert du contenu de TABLE[4] dans W Transfert du contenu de W sur le PORTB

Philippe LETENNEUR - GRANVILLE - 2003 -

page 20

LES MICROCONTROLEURS PIC 16F87X


8.6) Manipulation de bits. 8.6.1) Forage de bits.
8.6.1.1) Description. Il sagit de 2 instructions permettant de mettre 0 ou 1 un bit d'un octet de l'espace mmoire SFR. Elles sont le plus souvent utilises pour positionner des bits des registres du C. 8.6.1.2) Syntaxe. BSF ou BCF 8.6.1.3) Exemples. BCF BSF PORTA,2 STATUS,0 ; Mise 0 du bit 2 du PORTA ; Mise 1 du bit 0 du registre STATUS ; C'est--dire la CARRY f,b f,b pour mettre 1 pour mettre 0

8.6.2) Test de bits.


8.6.2.1) Description. Il sagit de 2 instructions permettant de tester un bit d'un octet de l'espace mmoire SFR. Elles sont le plus souvent utilises pour dterminer ltat des bits des registres du C. En fonction du rsultat du test : - le programme se poursuit avec l'instruction suivante (rsultat du test faux) - le programme saute linstruction qui suit le test. 8.6.2.2) Syntaxe. BTFSS ou BTFSC 8.6.2.3) Exemple. MOVF BTFSS GOTO MOVLW MOVWF GOTO DECF .. . CMP,1 STATUS,Z SINON 0xFF CMP FSI CMP,1 ; ; ; ; ; ; Transfert du contenu CMP dans CMP Cela permet de tester si le contenu de CMP est nul en positionnant le bit Z Test du bit Z ? Z=0 alors excuter le code pour SINON Alors CMP <- 0xFF f,b f,b

SINON FSI

; allez en fin de si ; CMP <- CMP -1 Dcrmenter CMP

Philippe LETENNEUR - GRANVILLE - 2003 -

page 21

LES MICROCONTROLEURS PIC 16F87X


9) LES PORTS D'ENTREES SORTIES 9.1) Gnralits.
Le C dispose de 3 PORTS (A,B et C) pour le 16F876 et 5 PORTS (A,B,C,D et E) pour le 16F877. Tous les ports dentres sorties Input/ Output sont bidirectionnels. La plupart des lignes de PORTs ont une double fonction. Le PORT A (5 bits) I/O pure et/ou convertisseur analogique et/ou TIMER 0. La broche RA4 du PORT A (Entre du TIMER 0 T0CKI) est du type DRAIN OUVERT. Le PORT B (8 bits) I/O pure et/ou programmation in situ ICSP/ICD (Broche RB3/PGM, RB6/PGC et RB7/PGD) et lentre dinterruption externe RB0/INT. Remarque : Si le PIC est utilis en mode ICSP/ICD il faut laisser libre les broches RB3/PGM, RB6/PGC ainsi que RB7/PGD) et les configurer en entre. Le PORT C (8 bits) I/O pure et/ou TIMER 1 et/ou SPI / I2C et/ou USART. Le PORT D (8 bits) I/O pure et/ou port parallle 8 bits associ au PORT E. Le PORT E (3 bits) I/O pure et/ou pilotage du PORT E RE0/RD, RE1/WR et RE2/CS.

Toutes les lignes de PORTs peuvent fournir un courant de 25mA par ligne de PORT. Une limite de 40mA par PORT doit tre respecte pour des questions de dissipation.

PORTA RA0/AN0 RA1/AN1 RA2/AN2/VREFRA3/AN3/VREF+ RA4/T0CKI RA5/AN4/SS

PORTB RB0/INT RB1 RB2 RB3/PGM RB4 RB5 RB6/PGC RB7/PGD

PORTC RC0/T1OSO/T1CKI RC1/T1OSI/CCP2 RC2/CCP1 RC3/SCK/SCL RC4/SDI/SDA RC5/SDO RC6/TX/CK RC7/RX/DT

PORTD RD0/PSP0 RD1/PSP1 RD2/PSP2 RD3/PSP3 RD4/PSP4 RD5/PSP5 RD6/PSP6 RD7/PSP7

PORTE RE0/AN5/RD RE1/AN6/WR RE2/AN7/CS

Philippe LETENNEUR - GRANVILLE - 2003 -

page 22

LES MICROCONTROLEURS PIC 16F87X


9.2 Configuration des PORTx , les registres PORTx et TRISx.
Tous les ports sont pilots par deux registres : Le registre de PORTx, si le PORT x ou certaines lignes de PORT X sont configures en sortie, ce registre dtermine ltat logique des sorties. Le registre TRISx, cest le registre de direction. Il dtermine si le PORTx ou certaines lignes de port sont en entre ou en sortie. Lcriture dun 1 logique correspond une entre (1 comme Input) et lcriture dun 0 logique correspond une sortie (0 comme Output). Au RESET toutes les lignes de ports sont configures en entres. Remarque : Les registres TRISx appartiennent la BANQUE 1 des SFR. Lors de linitialisation du C il ne faut pas oublier de changer de page mmoire pour les configurer. Exemple : On souhaite obtenir la configuration suivante des PORTA et PORTB. SENS PORTA SENS PORTB NC NC E RB7 NC NC E RB6 S RA5 S RB5 E RA4 S RB4 S RA3 E RB3 S RA2 S RB2 S RA1 S RB1 E RA0 E RBO

Programme en assembleur. Remarque toutes les lignes de sorties des PORTs sont mises zro. ; Mise zro des registres de donnes des ports A et B clrf PORTA clrf PORTB ; Configuration des PORTA et B ; Accs aux registres TRISx bsf STATUS,RP0 ; RP0 = 1 bcf STATUS,RP1 ; RP1 = 0 ; Configuration des ; movlw B11010001 ; movwf TRISA ; movlw B11001001 ; movwf TRISB (Banque mmoire 1)

registres de direction Configuration du PORTA X X S E S S S E valeur binaire 1 1 0 1 0 0 0 1 Configuration du PORTB E E S S E S S E valeur binaire 1 1 0 0 1 0 0 1

; Retour en banque mmoire 0 bcf STATUS,RP0 ; RP0 = 0 bcf STATUS,RP1 ; RP1 = 0 . .

Philippe LETENNEUR - GRANVILLE - 2003 -

page 23

LES MICROCONTROLEURS PIC 16F87X


9.3) Le PORT A.
Le PORT A (5 bits) I/O pure et/ou convertisseur analogique et/ou TIMER 0. Attention PA4 (Entre du TIMER 0 T0CKI), elle est de type DRAIN OUVERT.
BLOCK DIA GRAM OF RA3:RA0 AND RA5 PINS
Data Bus WR Port Data Latch D Q VDD CK Q P I/O pin(1) WR TRIS Data Bus WR Port D CK Q Q

BLOCK DIA GRAM OF RA4/T0CKI PIN


Data Latch

N VSS Schmitt Trigger Input Buffer

I/O pin(1)

TRIS Latch D Q Q

TRIS Latch D WR TRIS Q N

CK

CK

VSS Analog Input Mode

RD TRIS

RD TRIS

Q TTL Input Buffer

D EN EN

RD Port TMR0 Clock Input Note 1: I/O pin has protection diodes to VSS only.

EN RD Port

To A/D Converter Note 1: I/O pins have protection diodes to VDD and VSS.

PORTA FUNCTIONS
Name RA0/AN0 RA1/AN1 RA2/AN2 RA3/AN3/VREF RA4/T0CKI RA5/SS/AN4 Bit# bit0 bit1 bit2 bit3 bit4 bit5 Buffe r TTL TTL TTL TTL ST TTL Func Input/output or analog input. Input/output or analog input. Input/output or analog input. Input/output or analog input or VREF. Input/output or external clock input for Timer0. Output is open drain type. Input/output or slave select input for synchronous serial port or analog input. tion

Legend: TTL = TTL input, ST = Schmitt Trigger input

SUMMARY OF REGISTERS ASSOCIATED WITH PORTA


Addre ss 05h 85h 9Fh Name PORTA TRISA Bit 7 Bit 6 Bit 5 RA5 Bit 4 RA4 Bit 3 RA3 Bit 2 RA2 Bit 1 RA1 Bit 0 RA0 Value on: POR, BOR
--0x 0000 --11 1111

Value on all ot her RESETS


--0u 0000 --11 1111 --0- 0000

PORTA Data Direction Register

ADCON1 ADFM

PCFG3 PCFG2 PCFG1 PCFG0 --0- 0000

Legend: x = unknown, u = unchanged, - = unimplemented locations read as '0'. Shaded cells are not used by PORTA.

Philippe LETENNEUR - GRANVILLE - 2003 -

page 24

LES MICROCONTROLEURS PIC 16F87X


9.3) Le PORT B.
Le PORT B dispose de (8 bits) I/O pure et/ou programmation in situ ICSP/ICD (Broche RB3/PGM, RB6/PGC et RB7/PGD) et une entre dinterruption externe RB0/INT. Il dispose de deux fonctions spciales : - La premire cest la possibilit de configurer toutes ses lignes avec une rsistance de PULL-UP en configurant le bit RBPU 0 du registre OPTION. - La deuxime, cest la possibilit de gnrer une interruption sur un changement dtat des broches RB4 RB7. Cest trs pratique pour la gestion des claviers matrics.
BLOCK DIAGRAM OF
RBPU(2) Data Latch D CK TRIS Latch D WR TRIS CK Q TTL Input Buffer Q I/O pin(1) Data Bus WR Port

RB3:RB0 PINS
VDD Weak P Pull-up

BLOCK DIAGRAM OF
RBPU(2) Data Latch D Q CK TRIS Latch D Q WR TRIS CK

RB7:RB4 PINS
VDD Weak P Pull-up

Data Bus WR Port

I/O pin(1)

TTL Input Buffer

ST Buffer

RD TRIS Q RD Port EN RB0/INT RB3/PGM Schmitt Trigger Buffer RD Port D

RD TRIS Q RD Port Set RBIF

Latch D EN Q1

Q From other RB7:RB4 pins RB7:RB6 In Serial Programming Mode

D RD Port EN Q3

Note 1: I/O pins have diode protection to VDD and VSS. 2: To enable weak pull-ups, set the appropriate TRIS bit(s) and clear the RBPU bit (OPTION_REG<7>).

Note 1: I/O pins have diode protection to VDD and VSS. 2: To enable weak pull-ups, set the appropriate TRIS bit(s) and clear the RBPU bit (OPTION_REG<7>).

PORTB FUNCTIONS
Name RB0/INT RB1 RB2 RB3/PGM(3) RB4 RB5 RB6/PGC RB7/PGD Bit# bit0 bit1 bit2 bit3 bit4 bit5 bit6 bit7 Bu ffer TTL/ST(1) TTL TTL TTL TTL TTL TTL/ST(2) TTL/ST(2) F uncti on

Input/output pin or external interrupt input. Internal software programmable weak pull-up. Input/output pin. Internal software programmable weak pull-up. Input/output pin. Internal software programmable weak pull-up. Input/output pin or programming pin in LVP mode. Internal software programmable weak pull-up. Input/output pin (with interrupt-on-change). Internal software programmable weak pull-up. Input/output pin (with interrupt-on-change). Internal software programmable weak pull-up. Input/output pin (with interrupt-on-change) or In-Circuit Debugger pin. Internal software programmable weak pull-up. Serial programming clock. Input/output pin (with interrupt-on-change) or In-Circuit Debugger pin. Internal software programmable weak pull-up. Serial programming data.

Legend: TTL = TTL input, ST = Schmitt Trigger input Note 1: This buffer is a Schmitt Trigger input when configured as the external interrupt. 2: This buffer is a Schmitt Trigger input when used in Serial Programming mode. 3: Low Voltage ICSP Programming (LVP) is enabled by default, which disables the RB3 I/O function. LVP must be disabled to enable RB3 as an I/O pin and allow maximum compatibility to the other 28-pin and 40-pin mid-range devices.

SUMMARY OF REGISTERS ASSOCIATED WITH PORTB


Addres s 06h, 106h 86h, 186h 81h, 181h Name PORTB TRISB OPTION_REG Bit 7 RB7 RBPU Bit 6 RB6 INTEDG Bit 5 RB5 T0CS Bit 4 RB4 T0SE Bit 3 RB3 PSA Bit 2 RB2 PS2 Bit 1 RB1 PS1 Bit 0 RB0 PS0 Value on: POR, BOR
xxxx xxxx 1111 1111 1111 1111

Value o n all o ther RESETS


uuuu uuuu 1111 1111 1111 1111

PORTB Data Direction Register

Legend: x = unknown, u = unchanged. Shaded cells are not used by PORTB.

Philippe LETENNEUR - GRANVILLE - 2003 -

page 25

LES MICROCONTROLEURS PIC 16F87X


9.4) Le PORT C.
Le PORT C (8 bits) I/O pure quil partage avec le TIMER 1, la liaison SPI / I2C et lUSART.
PORTC BLOCK DIAGRAM (PERIPHERAL OUTPUT OVERRIDE) RC<2:0>, RC<7:5>
Port/Peripheral Select(2) Peripheral Data Out Data Bus WR Port
D CK Q Q

PORTC BLOCK DIAGRAM (PERIPHERAL OUTPUT OVERRIDE) RC<4:3>


Port/Peripheral Select(2)

VDD P I/O pin(1)

Peripheral Data Out Data Bus WR Port D CK Q Q

VDD P I/O pin(1)

Data Latch WR TRIS


D CK Q Q

Data Latch WR TRIS D CK Q Q N Vss Schmitt Trigger Q D EN 0 Schmitt Trigger with SMBus levels

N VSS Schmitt Trigger Q D EN

TRIS Latch RD TRIS Peripheral OE(3) RD Port Peripheral Input Note 1: I/O pins have diode protection to VDD and VSS. 2: Port/Peripheral select signal selects between port data and peripheral output. 3: Peripheral OE (output enable) is only activated if peripheral select is active. RD TRIS Peripheral OE(3) RD Port SSPl Input

TRIS Latch

1 CKE SSPSTAT<6> Note 1 : I/O pins have diode protection to VDD and VSS. 2: Port/Peripheral select signal selects between port data and peripheral output. 3: Peripheral OE (output enable) is only activated if peripheral select is active.

PORTC FUNCTIONS
Name RC0/T1OSO/T1CKI RC1/T1OSI/CCP2 RC2/CCP1 RC3/SCK/SCL RC4/SDI/SDA RC5/SDO RC6/TX/CK RC7/RX/DT Bit# bit0 bit1 bit2 bit3 bit4 bit5 bit6 bit7 Buffe r Type ST ST ST ST ST ST ST ST Function Input/output port pin or Timer1 oscillator output/Timer1 clock input. Input/output port pin or Timer1 oscillator input or Capture2 input/ Compare2 output/PWM2 output. Input/output port pin or Capture1 input/Compare1 output/ PWM1 output. RC3 can also be the synchronous serial clock for both SPI and I2C modes. RC4 can also be the SPI Data In (SPI mode) or data I/O (I2C mode). Input/output port pin or Synchronous Serial Port data output. Input/output port pin or USART Asynchronous Transmit or Synchronous Clock. Input/output port pin or USART Asynchronous Receive or Synchronous Data.

Legend: ST = Schmitt Trigger input

SUMMARY OF REGISTERS ASSOCIATED WITH PORTC


Addres s 07h 87h Name PORTC TRISC Bit 7 RC7 Bit 6 RC6 Bit 5 RC5 Bit 4 RC4 Bit 3 RC3 Bit 2 RC2 Bit 1 RC1 Bit 0 RC0 Value on: POR, BOR
xxxx xxxx 1111 1111

Value on all ot her RESETS


uuuu uuuu 1111 1111

PORTC Data Direction Register

Legend: x = unknown, u = unchanged

Philippe LETENNEUR - GRANVILLE - 2003 -

page 26

LES MICROCONTROLEURS PIC 16F87X


9.5) Les PORT D et E.
Le PORT D (8 bits) I/O et PORT E (3 bits) utilisent la mme type structure interne.
PORTD BLOCK DIAGRAM (IN I/O PORT MODE)
Data Bus WR Port Data Latch D Q CK TRIS Latch D Q WR TRIS CK Schmitt Trigger Input Buffer I/O pin
(1)

PORTD FUNCTIONS
Name RD0/PSP0 RD1/PSP1 RD2/PSP2 RD3/PSP3 RD4/PSP4 RD5/PSP5 RD6/PSP6 RD7/PSP7 Bit# bit0 bit1 bit2 bit3 bit4 bit5 bit6 bit7 Buffe r Type ST/TTL(1) ST/TTL(1) ST/TTL(1) ST/TTL(1) ST/TTL(1) ST/TTL(1) ST/TTL(1) ST/TTL(1) Functi on Input/output port pin or parallel slave port bit0. Input/output port pin or parallel slave port bit1. Input/output port pin or parallel slave port bit2. Input/output port pin or parallel slave port bit3. Input/output port pin or parallel slave port bit4. Input/output port pin or parallel slave port bit5. Input/output port pin or parallel slave port bit6. Input/output port pin or parallel slave port bit7.

RD TRIS

Legend: ST = Schmitt Trigger input, TTL = TTL input Note 1: Input buffers are Schmitt Triggers when in I/O mode and TTL buffers when in Parallel Slave Port mode.
Q D EN EN

SUMMARY OF REGISTERS ASSOCIATED WITH PORTD


Addre ss Name PORTD TRISD TRISE Bit 7 RD7 IBF Bit 6 RD6 Bit 5 RD5 Bit 4 RD4 Bit 3 RD3 Bit 2 RD2 Bit 1 RD1 Bit 0 RD0 Value on: POR, BOR
xxxx xxxx 1111 1111 0000 -111

RD Port

Note 1: I/O pins have protection diodes to VDD and VSS.

Value on all o ther RESETS


uuuu uuuu 1111 1111 0000 -111

08h 88h 89h

PORTD Data Direction Register OBF IBOV PSPMODE PORTE Data Direction Bits

PORTE BLOCK DIAGRAM (IN I/O PORT MODE)


Data Bus WR Port Data Latch D Q CK TRIS Latch D WR TRIS CK Q Schmitt Trigger Input Buffer I/O pin
(1)

Legend: x = unknown, u = unchanged, - = unimplemented, read as '0'. Shaded cells are not used by PORTD.

PORTE FUNCTIONS
Name Bit# Buffe r Type Func tion I/O port pin or read control input in Parallel Slave Port mode or analog input: RD 1 = Idle 0 = Read operation. Contents of PORTD register are output to PORTD I/O pins (if chip selected) I/O port pin or write control input in Parallel Slave Port mode or analog input: WR 1 = Idle 0 = Write operation. Value of PORTD I/O pins is latched into PORTD register (if chip selected) I/O port pin or chip select control input in Parallel Slave Port mode or analog input: CS 1 = Device is not selected 0 = Device is selected

RE0/RD/AN5

bit0

ST/TTL(1)

RE1/WR/AN6

bit1

ST/TTL

(1)

RD TRIS

RE2/CS/AN7
Q D EN EN RD Port

bit2

ST/TTL(1)

Legend: ST = Schmitt Trigger input, TTL = TTL input Note 1: Input buffers are Schmitt Triggers when in I/O mode and TTL buffers when in Parallel Slave Port mode.

SUMMARY OF REGISTERS ASSOCIATED WITH PORTE


Note 1: I/O pins have protection diodes to VDD and VSS.

Addre ss 09h 89h 9Fh

Name PORTE TRISE ADCON1

Bi t 7 IBF ADFM

Bit 6 OBF

Bit 5 IBOV

Bit 4 PSPMODE

Bit 3 PCFG3

Bit 2 RE2 PCFG2

Bit 1 RE1 PCFG1

Bit 0 RE0 PCFG0

Value on: POR, BOR


---- -xxx 0000 -111 --0- 0000

Value on all ot her RESETS


---- -uuu 0000 -111 --0- 0000

PORTE Data Direction Bits

Legend: x = unknown, u = unchanged, - = unimplemented, read as 0. Shaded cells are not used by PORTE.

Philippe LETENNEUR - GRANVILLE - 2003 -

page 27

LES MICROCONTROLEURS PIC 16F87X


10) LE CONVERTISSEUR ANALOGIQUE NUMERIQUE.
Le convertisseur analogique numrique est approximations successives et il possde une rsolution de 10 bits. Il est compos de : - Un multiplexeur analogique 5 voies (PIC16F876) ou 8 voies PIC16F877). - Un chantillonneur bloqueur. - Un Convertisseur Analogique Numrique de 10 bits.

10.1) Organisation interne.


A/D BLOCK DIAGRAM
CHS2:CHS0

111 110 101 100

RE2/AN7(1) RE1/AN6(1) RE0/AN5(1) RA5/AN4 RA3/AN3/VREF+ RA2/AN2/VREFRA1/AN1 RA0/AN0

VAIN (Input Voltage) A/D Converter


011 010 001

VDD VREF+ (Reference Voltage) PCFG3:PCFG0

000

VREF(Reference Voltage) VSS PCFG3:PCFG0

Note 1: Not available on PIC16F873/876 devices.

Remarque : Les entres analogiques RE1/AN5, RE2/AN6 et RE3/AN2 sont disponibles avec les PICs 16F877 et 16F874. De plus la broche RA4 nest pas concerne pas le convertisseur.

Philippe LETENNEUR - GRANVILLE - 2003 -

page 28

LES MICROCONTROLEURS PIC 16F87X

10.2) Fonctionnement du convertisseur.


La conversion se passe en 2 temps : - 1er temps le signal convertir est appliqu sur lentre convertir, ce signal doit tre prsent au moins pendant le temps Tacq (temps dacquisition environ 20S pour 5V). - 2me temps la conversion, approximations successives. Le temps de conversion minimum est de 12 TAD (TAD cest le temps de conversion dpendant de lhorloge interne, typiquement 1,6S). Une conversion commence toujours par la mise 1 du bit GO/DONE du registre ADCON0. Lorsque la conversion est termine se bit repasse 0. Donc pour pouvoir lire le rsultat dans les registres ADRESL et ADRESH il suffit dattendre que le bit GO/DONE passe 0.

TCY to TAD TAD1

TAD2 b9

TAD3 b8

TAD4
b7

TAD5 b6

TAD6 b5

TAD7
b4

TAD8
b3

TAD9 TAD10 TAD11

b2

b1

b0

Conversion starts Holding capacitor is disconnected from analog input (typically 100 ns) Set GO bit

ADRES is loaded GO bit is cleared ADIF bit is set Holding capacitor is connected to analog input

La valeur rsultante N de la conversion ADRSH:ADRESL est gale :

N (valeur numrise) = ((VIN - VREF-) / (VREF+ - VREF-)) * 1023)


Si VREF+ = VDD = 5V et VREF- = VSS = 0V alors

N (valeur numrise) = 1023 * (VIN / 5 )


Mais avant de raliser une conversion il faut dfinir la configuration du convertisseur : - Le nombre dentres analogiques. - Le nombre dentres logiques. - Le type de tension de rfrence : - Interne VREF =VDD-VSS. - Externe, soit VREF= VREF+ - VSS ou VREF= VREF+ - VREF-. Cette configuration ce fait travers le registre ADCON1, voir page suivante.

Philippe LETENNEUR - GRANVILLE - 2003 -

page 29

LES MICROCONTROLEURS PIC 16F87X


10.3) Le registre ADCON1.
Il permet de choisir une configuration parmi les 16 proposes. Remarque : La configuration de ce registre ADCON1 ne dispense pas de configurer les registres de directions des PPORTA et PORTE respectivement TRISA et TRISE.
ADCON1 REGISTER (ADDRESS 9Fh)
U-0 ADFM bit 7 bit 7 ADFM: A/D Result Format Select bit 1 = Right justified. 6 Most Significant bits of ADRESH are read as 0. 0 = Left justified. 6 Least Significant bits of ADRESL are read as 0. Unimp lemente d: Read as '0' PCFG3:PCFG0: A/D Port Configuration Control bits:
PCFG3: PCFG0
0000 0001 0010 0011 0100 0101 011x 1000 1001 1010 1011 1100 1101 1110 1111

U-0

R/W-0

U-0

R/W-0 PCFG3

R/W-0 PCFG2

R/W-0 PCFG1

R/W-0 PCFG0 bit 0

bit 6-4 bit 3-0

AN 7(1) RE2 A A D D D D D A D D D D D D D

AN6 (1) RE1 A A D D D D D A D D D D D D D

AN5 (1) RE0 A A D D D D D A A A A D D D D

AN4 RA5 A A A A D D D A A A A A D D D

AN3 RA3 A VREF+ A VREF+ A VREF+ D VREF+ A VREF+ VREF+ VREF+ VREF+ D VREF+

AN 2 RA 2 A A A A D D D VREFA A VREFVREFVREFD VREF-

AN1 RA1 A A A A A A D A A A A A A D D

AN0 RA0 A A A A A A D A A A A A A A A

VREF+ VDD RA3 VDD RA3 VDD RA3 VDD RA3 VDD RA3 RA3 RA3 RA3 VDD RA3

VREFVSS VSS VSS VSS VSS VSS VSS RA2 VSS VSS RA2 RA2 RA2 VSS RA2

CHAN/ Refs (2) 8/0 7/1 5/0 4/1 3/0 2/1 0/0 6/2 6/0 5/1 4/2 3/2 2/2 1/0 1/2

A = Analog input

D = Digital I/O

Note 1: 2:

These channels are not available on PIC16F873/876 devices. This column indicates the number of analog channels available as A/D inputs and the number of analog channels used as voltage reference inputs.

Legend: R = Readable bit - n = Value at POR W = Writable bit 1 = Bit is set U = Unimplemented bit, read as 0 0 = Bit is cleared x = Bit is unknown

De plus le bit ADFM permet de choisir entre deux types de justification pour le rsultat. - Si ADFM=1 alors le rsultat sera justifi droite dans les registre ADRESH et ADRESL, c'est-dire ARESL contient les bits 7 0 du rsultat de la conversion et ADRESH contient 6 zros suivi des bit 9 et 8 du rsultat de la conversion. - Si ADFM=0 alors le rsultat sera justifi gauche, c'est--dire ARESH contient les bits 9 2 du rsultat de la conversion et ADRESH contient les bit 1 et 0 du rsultat de la conversion suivi de 6 zros.
A/D RESULT JUSTIFICATION
10-bit Result ADFM = 1 ADFM = 0

7 0000 00

2107

0765 0000 00

ADRESH

ADRESL 10-bit Result

ADRESH 10-bit Result

ADRESL

Right Justified

Left Justified

Philippe LETENNEUR - GRANVILLE - 2003 -

page 30

LES MICROCONTROLEURS PIC 16F87X

10.4) Le registre ADCON0.


Ce registre permet de dfinir lhorloge de conversion (bit ADCS1 et ADCS0), le canal convertir (CHS2, CHS1 et CHS0) et ADON bit de mise en fonctionnement. Remarque : Lors de la mise en fonctionnement du CAN par le bit ADON, Le bit GO/DONE ne doit pas tre modifi en mme temps, c'est--dire dans la mme instruction. .
ADCON0 REGISTE R (A D DRE S S : 1Fh) R/W-0 ADCS 1 bit 7 bit 7-6 ADCS1 :ADCS 0: A/D Conversion Clock Select bits 00 = FOSC/2 01 = FOSC/8 10 = FOSC/32 11 = FRC (clock derived from the internal A/D module RC oscillator) CHS2:CHS0 : Analog Channel Select bits 00 0 = channel 0, (RA0/AN0) 00 1 = channel 1, (RA1/AN1) 01 0 = channel 2, (RA2/AN2) 01 1 = channel 3, (RA3/AN3) 10 0 = channel 4, (RA5/AN4) 10 1 = channel 5, (RE0/AN5)(1) 11 0 = channel 6, (RE1/AN6)(1) 11 1 = channel 7, (RE2/AN7)(1) GO /DONE: A/D Conversion Status bit If ADON = 1: 1 = A/D conversion in progress (setting this bit starts the A/D conversion) 0 = A/D conversion no t in progress (this bit is automatically cleared by hardware when the A/D conversion is complete) Un impl emented: Read as '0' AD ON: A/D On bit 1 = A/D converter module is operating 0 = A/D converter module is shut-off and consumes no operating current Note 1: These channels are not available on PIC16F873/876 devices. Legend: R = R eadable bit - n = Value at POR W = Writable bit 1 = Bit is set U = Unimplemented bit, read as 0 0 = Bit is cleared x = Bit is unknown R/W-0 ADCS0 R/W-0 CHS2 R/W-0 CHS1 R/W-0 CHS0 R/W-0 GO/DONE U-0 R/W-0 ADO N bit 0

bit 5-3

bit 2

bit 1 bit 0

Le choix de lhorloge est dtermin par les bit ADCS1 et ADCS0, sachant que le temps TAD doit tre au minimum de 1,6S.
TAD vs. MAXIMUM DEVICE OPERATING FREQUENCIES (STANDARD DEVICES (C))
AD Clo ck Sourc e (TAD ) Operation 2TOSC 8TOSC 32TOSC RC(1, 2, 3) ADCS1:ADCS0 00 01 10 11 Maximum Device Frequenc y Max. 1.25 MHz 5 MHz 20 MHz (Note 1)

Note 1: The RC source has a typical TAD time of 4 s, but can vary between 2-6 s. 2: When the device frequencies are greater than 1 MHz, the RC A/D conversion clock source is only recommended for SLEEP operation. 3: For extended voltage devices (LC), please refer to the Electrical Characteristics (Sections 15.1 and 15.2).

Philippe LETENNEUR - GRANVILLE - 2003 -

page 31

LES MICROCONTROLEURS PIC 16F87X


10.5) Exemple dutilisation.
Exemple : On souhaite obtenir la configuration suivante avec un PIC16F877 : RE2 : Sortie logique RE1 : Sortie logique RE0 : Entre logique RA5 : Sortie logique RA4 : Entre Logique RA3 : Entre analogique RA2 : Entre logique RA1 : Entre analogique RA0 : Entre analogique Tension de rfrence VREF = VDD VSS = 5V et frquence du quartz gale 12MHz. Programme en assembleur. Remarque : Toutes les lignes de sorties des PORTs sont mises zro.
; Mise zro des registres de donnes des ports A et E clrf PORTA clrf PORTE ; Configuration des registres de directions des PORT A et E ; Accs aux registres TRISx (Banque mmoire 1) bsf STATUS,RP0 ; RP0 = 1 bcf STATUS,RP1 ; RP1 = 0 ; Configuration des registres de directions ; Configuration du PORTA X X S E E E E E movlw B11011111 ; valeur binaire movwf TRISA ; Configuration du PORTE 0 0 0 0 0 S S E movlw B00000001 ; valeur binaire movwf TRISE

1 1 0 1 1 1 1 1 0 0 0 0 0 0 0 1

; Configuration du registre ADCON1 Page 1 ; ADFM = 1 justification droite du rsultat ; PCFG 3:0 0100 => RE3:RE0 Type D comme Digitale ; => RA5 : D, RA3 : A comme Analogique ; => RA2 : D, RA1 : A et RA0 : A movlw B10000100 ; valeur binaire 1 0 0 0 0 1 0 0 movwf ADCON1 ; Retour en banque mmoire 0 bcf STATUS,RP0 ; RP0 = 0 bcf STATUS,RP1 ; RP1 = 0 ; Configuration du registre ADCON0 Page 0 ; ADCS1 et ADSC0 = 1 0 Frquence Max 20MHz ; ADON = 1 Mise en route du CAN ; 0 pour les autres bits movlw B10000001 ; valeur binaire movwf ADCON0

1 0 0 0 0 0 0 1

ATT

; Conversion du canal RA3 ; Slection du canal 3 avec les bits CHS2, CHS1 et CHS0 : ; GO/DONE = 1 Lancement dune conversion bcf ADCON0,CHS2 bsf ADCON0,CHS1 bsf ADCON0,CHS0 bsf ADCON0,GO ; Dclenchement de la conversion btfsc ADCON0, GO_DONE ; attendre la fin de conversion goto ATT ; fin de conversion, lecture du rsultat movf ADRESH,0 ; Partie haute movwf RES_HAUT,1 bsf STATUS,RP1 ; Passage en page 1 movf ADRESL,0 ; Partie basse bsf STATUS,RP0 ; Passage en page 0 movwf RES_BAS,1 ;

0 1 1

Philippe LETENNEUR - GRANVILLE - 2003 -

page 32

LES MICROCONTROLEURS PIC 16F87X


11) LES TIMERS.
Les PICs 16F87x disposent de 3 timers : - Le timer 0 (8 bits): Il peut tre incrment par des impulsions extrieures via la broche (TOCKI/ RA4) ou par lhorloge interne (Fosc/4). - Le timer 1 (16 bits): Il peut tre incrment soit par lhorloge interne, par des impulsions sur la broche T1CKI/RC0 ou par un oscillateur (RC ou quartz) connect sur les broches TOSO/RCO et T1OSI/RC1. - Le timer 2 (8 bits) : Il est incrment par lhorloge interne, celle peut tre pr divise. Tous ces timers peuvent dclencher une interruption interne, si ils ont t autoriss.

11.1) Le timer 0. 11.1.1) Prsentation :


Cest le plus ancien des timers implants dans les PICs, son ancienne appellation tait RTC, pour Real Time Clock (horloge temps relle). On peut se servir de celui-ci pour gnrer des vnements priodiques, comme le rafrachissement dafficheurs multiplexs ou lincrmentation de variables (secondes, minutes . ). Celui-ci est incrment soit par lhorloge interne (Fosc/4) ou par une horloge applique sur la broche TOCKI/ RA4 .
BLOCK DIAGRAM OF THE TIMER0/WDT PRESCALER
CLKOUT (= FOSC/4) Data Bus 8 M U X SYNC 2 Cycles

0 RA4/T0CKI pin 1 T0SE

M U X

1 0

TMR0 Reg

T0CS

PSA PRESCALER

Set Flag Bit T0IF on Overflow

0 1

Watchdog Timer

M U X

8-bit Prescaler 8 8 - to - 1MUX PS2:PS0

PSA WDT Enable bit 0 MUX 1 PSA

WDT Time-out Note: T0CS, T0SE, PSA, PS2:PS0 are (OPTION_REG<5:0>).

Comme on peut le constater sur ce schma le timer 0 partage avec le chien de garde Watchdog le pr diviseur. Celui-ci est affect lun ou lautre, suivant la valeur du bit PSA (0 : Timer0 et 1 : chien de garde).

Philippe LETENNEUR - GRANVILLE - 2003 -

page 33

LES MICROCONTROLEURS PIC 16F87X


On peut obtenir un schma simplifi du fonctionnement du timer0 sans le chien de garde.
Timer0 Block Diagram
Data bus

FOSC/4

0
1

PSout

T0CKI pin T0SE

Programmable Prescaler
3

Sync with Internal clocks


(2 cycle delay)

TMR0
PSout

PS2, PS1, PS0

PSA

T0CS

Set interrupt ag bit T0IF on overow

11.1.2) Fonctionnement :
Le bit T0CS permet de choisir lhorloge, interne (Fosc/4) ou externe T0CKI/RA4. Dans ce dernier cas lincrmentation du timer 0 peut se faire soit sur front montant ou descendant suivant la valeur du bit T0SE. Le bit PSA de choisir si horloge permet de pr diviser lhorloge dun rapport allant de 2 256. La valeur de pr division est fixe par les bits PS2, PS1 et PS0. Quand le contenu du timer 0 passe de FF 00 le bit T0IF passe 1 pour signaler un dbordement, si le bit T0IE est 1 alors une interruption timer 0 est dclenche. Le contenu du timer 0 peut tre modifi tout instant, une condition prs, la nouvelle valeur inscrite dans le registre TMR0 sera prise en compte aprs 3 cycles machines. Remarque importante : Lorsque le bit T0IF passe 1 lors du passage de la valeur FFh 00h du registre TMR0, il doit tre remis 0 de faon logicielle par une instruction du type : bcf INTCON,T0IF

11.1.3) Configuration et registres associs :


La configuration du timer 0 passe par les registres TMR0 (adresse 01h), OPTION_REG (adresse 81h page 1) et INTCON (adresse 0Bh : toutes les pages).
OPTION_REG REGISTER (@ 81h or 181h)
R/W-1 RBPU bit 7 bit 7 bit 6 bit 5 RBPU INTEDG T0CS: TMR0 Clock Source Select bit 1 = Transition on T0CKI pin 0 = Internal instruction cycle clock (CLKOUT) T0SE: TMR0 Source Edge Select bit 1 = Increment on high-to-low transition on T0CKI pin 0 = Increment on low-to-high transition on T0CKI pin PSA: Prescaler Assignment bit 1 = Prescaler is assigned to the WDT 0 = Prescaler is assigned to the Timer0 module PS2:PS0: Prescaler Rate Select bits Bit Value 00 0 00 1 01 0 01 1 10 0 10 1 11 0 11 1 Legend: R = Readable bit - n = Value at POR W = Writable bit 1 = Bit is set U = Unimplemented bit, read as 0 0 = Bit is cleared x = Bit is unknown TMR0 Rate 1 1 1 1 1 1 1 1 : : : : : : : : 2 4 8 16 32 64 128 256 WDT Rate 1 1 1 1 1 1 1 1 : : : : : : : : 1 2 4 8 16 32 64 128 R/W-1 INTEDG R/W-1 T0CS R/W-1 T0SE R/W-1 PSA R/W-1 PS2 R/W-1 PS1 R/W-1 PS0 bit 0

bit 4

bit 3

bit 2-0

REGISTERS ASSOCIATED WITH TIMER0


Addres s 01h,101h 0Bh,8Bh, 10Bh,18Bh 81h,181h Name TMR0 INTCON OPTION_REG Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 Value on: POR, BOR
xxxx xxxx 0000 000x 1111 1111

Value on all oth er RESETS


uu uu uuuu 00 00 000u 11 11 1111

Timer0 Modules Register GIE RBPU PEIE INTEDG T0IE T0CS INTE T0SE RBIE PSA T0IF PS2 INTF PS1 RBIF PS0

Legend: x = unknown, u = unchanged, - = unimplemented locations read as '0'. Shaded cells are not used by Timer0.

Philippe LETENNEUR - GRANVILLE - 2003 -

page 34

LES MICROCONTROLEURS PIC 16F87X


11.2) Le timer 1. 11.2.1) Prsentation :
Il fonctionne sur le mme principe que le timer 0, mais il est plus moderne dans sa conception. Cest un compteur 16 bits.
TIMER1 BLOCK DIAGRAM
Set Flag bit TMR1IF on Overflow TMR1H

TMR1 TMR1L

0 1

Synchronized Clock Input

T1OSC RC0/T1OSO/T1CKI

TMR1ON On/Off 1 T1OSCEN FOSC/4 Enable Internal Oscillator(1) Clock

T1SYNC Prescaler 1, 2, 4, 8 Synchronize det Q Clock

RC1/T1OSI/CCP2(2)

0 2 T1CKPS1:T1CKPS0 TMR1CS

Note 1: When the T1OSCEN bit is cleared, the inverter is turned off. This eliminates power drain.

11.2.2) Fonctionnement :
Le bit TMR1CS permet de choisir lhorloge soit interne (Fosc/4), externe T1CKI ou un oscillateur quartz connect sur les broches T1OSO et T1OSI. Les bits T1CKPS1 et T1CKPS0 permettent de choisir la valeur de la pr division appliquer lhorloge choisie, de 1 8. Le bit T1SYNC permet de choisir si lhorloge de sortie du pr diviseur doit tre synchrone avec lhorloge du microcontrleur. Dans le cas o lon choisit lhorloge interne Fosc/4, il nest pas ncessaire de la synchroniser. Le bit TMR1ON active ou dsactive le timer 1. Si ce bit est 1 alors le timer 1 est en fonctionnement et les registres TMR1H :TMR1L sont incrments chaque coup dhorloge. Quand le contenu du timer 1 passe de FFFF 0000 le bit TMR1IF passe 1 pour signaler un dbordement, de plus si le bit TMR1IE est 1 alors une interruption timer 1 est dclenche. Remarque importante : Lorsque le bit TMR1IF passe 1 lors du passage de la valeur FFFF 0000 des registres TMR1H :TMR1L, il doit tre remis 0 de faon logicielle par une instruction du type :
bcf PIR1,TMR1IF

Philippe LETENNEUR - GRANVILLE - 2003 -

page 35

LES MICROCONTROLEURS PIC 16F87X


11.2.3) Configuration et registres associs :
La configuration du timer 1 passe par les registres : PIR1 (adresse 0Ch), PIE1 (adresse 8Ch page 1) le registres TMR1L et TMR1H (adresses 0Eh et 0Fh), T1CON (adresse 10h page 0) et INTCON (adresse 0Bh : toutes les pages).
T1CON: TIMER1 CONTROL REGISTER (ADDRESS 10h)
U-0 bit 7 bit 7-6 bit 5-4 Unimp lemente d: Read as '0' T1CKPS1: T1CKPS0: Timer1 Input Clock Prescale Select bits 11 = 1:8 Prescale value 10 = 1:4 Prescale value 01 = 1:2 Prescale value 00 = 1:1 Prescale value T1OSCEN: Timer1 Oscillator Enable Control bit 1 = Oscillator is enabled 0 = Oscillator is shut-off (the oscillator inverter is turned off to eliminate power drain) T1SYNC: Timer1 External Clock Input Synchronization Control bit When TMR1CS = 1: 1 = Do not synchronize external clock input 0 = Synchronize external clock input When TMR1CS = 0: This bit is ignored. Timer1 uses the internal clock when TMR1CS = 0. TMR1CS: Timer1 Clock Source Select bit 1 = External clock from pin RC0/T1OSO/T1CKI (on the rising edge) 0 = Internal clock (FOSC/4) TMR1ON: Timer1 On bit 1 = Enables Timer1 0 = Stops Timer1 Legend: R = Readable bit - n = Value at POR W = Writable bit 1 = Bit is set U = Unimplemented bit, read as 0 0 = Bit is cleared x = Bit is unknown U-0 R/W-0 R/W-0 R/W-0 T1OSCEN R/W-0 R/W-0 R/W-0 bit 0 T1CKPS1 T1CKPS0 T1SYNC TMR1CS TMR1ON

bit 3

bit 2

bit 1

bit 0

REGISTERS ASSOCIATED WITH TIMER1 AS A TIMER/COUNTER


Ad dress 0Bh,8Bh, 10Bh, 18Bh 0Ch 8Ch 0Eh 0Fh 10h Name INTCON PIR1 PIE1 TMR1L TMR1H T1CON Bit 7 GIE PSPIF
(1) (1)

Bit 6 PEIE ADIF ADIE

Bit 5 T0IE RCIF RCIE

Bit 4 INTE TXIF TXIE

Bit 3 RBIE SSPIF SSPIE

Bit 2 T0IF CCP1IF CCP1IE

Bit 1 INTF TMR2IF TMR2IE

Bit 0 RBIF TMR1IF TMR1IE

Value on: POR, BOR


0000 000x 0000 0000 0000 0000 xxxx xxxx xxxx xxxx

Value on all o ther RESETS


0000 000u 0000 0000 0000 0000 uuuu uuuu uuuu uuuu -- uu uuuu

PSPIE

Holding Register for the Least Significant Byte of the 16-bit TMR1 Register Holding Register for the Most Significant Byte of the 16-bit TMR1 Register T1CKPS1 T1CKPS0 T1OSCEN T1SYNC TMR1CS TMR1ON

--00 0000

Legend: x = unknown, u = unchanged, - = unimplemented, read as '0'. Shaded cells are not used by the Timer1 module. Note 1: Bits PSPIE and PSPIF are reserved on the PIC16F873/876; always maintain these bits clear.

Philippe LETENNEUR - GRANVILLE - 2003 -

page 36

LES MICROCONTROLEURS PIC 16F87X


11.3) Le timer 2. 11.3.1) Prsentation :
Cest un timer 8 bits, son horloge ne peut tre que lhorloge interne divise par 4 (Fosc/4)
TIMER2 BLOCK DIAGRAM
Sets Flag bit TMR2IF TMR2 (1) Output RESET Postscaler 1:1 to 1:16 4 T2OUTPS3: T2OUTPS0 Note 1: TMR2 register output can be software selected by the SSP module as a baud clock. Prescaler 1:1, 1:4, 1:16 2 T2CKPS1: T2CKPS0

TMR2 Reg Comparator PR2 Reg

FOSC/4

EQ

11.3.2) Fonctionnement :
Il est incrment par lhorloge interne (Fosc/4) pr divise ou non. permettent de choisir la valeur de la pr division (1,4 ou 16). Les bits T2CKPS1 et T2KPS0

Le contenu du registre incrment TMR2 et il est compar au registre PR2, si ces deux registres sont gaux alors une impulsion dhorloge est gnre et le contenu de TMR2 est remis 00h. Celle-ci peut servir dhorloge pour piloter les liaisons I2C et SPI du module SSP ou encore tre divise par un post diviseur appel : POSTSCALER. Son rapport de division peut tre de 1 16. Les bits T2OUTPS0, T2OUTPS1, T2OUTPS2, et T2OUTPS3, permettent de choisir la valeur de la post division 1, 2, 3, 4, 5 . 16. Quand la sortie du post diviseur passe 1 le bit TMR2IF est positionn, celui-ci peut dclencher une interruption si celle-ci a t autorise (Bit TMR2IE 1 du registre PIE1). Remarque importante : Lorsque le bit TMR2IF passe 1 lorsquune impulsion est gnre en sortie du POSTSCALER, il doit tre remis 0 de faon logicielle par une instruction du type : bcf PIR1,TMR2IF

Philippe LETENNEUR - GRANVILLE - 2003 -

page 37

LES MICROCONTROLEURS PIC 16F87X


11.3.3) Configuration et registres associs :
La configuration du timer 2 passe par les registres : PIR1 (adresse 0Ch), PIE1 (adresse 8Ch page 1), TMR2 (adresse 11h page 0), PR2 (adresse 92h page 1), T2CON (adresse 12h page 0) et INTCON (adresse 0Bh : toutes les pages).

T2CON: TIMER2 CONTROL REGISTER (ADDRESS 12h)


U-0 bit 7 bit 7 bit 6-3 Unimp lemente d: Read as '0' TOUTPS3:TOUTPS0: Timer2 Output Postscale Select bits 0000 = 1:1 Postscale 0001 = 1:2 Postscale 0010 = 1:3 Postscale R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 bit 0 TOUTPS3 TOUTPS2 TOUTPS1 TOUTPS0 TMR2ON T2CKPS1 T2CKPS0

1111 = 1:16 Postscale bit 2 TMR2ON: Timer2 On bit 1 = Timer2 is on 0 = Timer2 is off T2CKPS1:T2CKPS0: Timer2 Clock Prescale Select bits 00 = Prescaler is 1 01 = Prescaler is 4 1x = Prescaler is 16 Legend: R = Readable bit - n = Value at POR W = Writable bit 1 = Bit is set U = Unimplemented bit, read as 0 0 = Bit is cleared x = Bit is unknown

bit 1-0

Philippe LETENNEUR - GRANVILLE - 2003 -

page 38

LES MICROCONTROLEURS PIC 16F87X

11.4) Les modules CCP1 et CCP2 (C.C.P. :Capture Compare Pwm).


Ces deux modules peuvent fonctionner dans lun des trois modes ci-dessous : - Mode capture (CAPTURE) : Ce mode permet en autre deffectuer des mesures de temps. - Mode comparaison (COMPARE) : Ce mode permet en autre de gnrer des vnements priodiques. - Mode PWM (PULSE WITH MODULATION): Ce mode permet de gnrer des signaux dont le rapport cyclique est variable. Ces modules sont associs aux broches RC2/CCP1 et RC1/T1OSI/CCP2. Suivant le mode choisit, les timers 1 ou 2 vont tre utiliss. Les modes Capture et Comparaison utilise le timer 1, tandis que le mode PWM utilise le timer 2.

11.4.1) Le mode CAPTURE :


Il mmorise la valeur du timer 1 dans les registres CPP1R1H : CPP1R1L ou CPP1R2H : CPP1R2L quand un vnement se produit sur une des broches CCP1 ou CCP2. Cette mmorisation peut avoir lieu : - Tous les fronts montants. - Tous les fronts descendants. - Tous les 4 fronts montants. - Tous les 16 fronts montants.
Capture Mode Operation Block Diagram
Set Flag bit CCPxIF Prescaler 1, 4, 16 CCPx Pin and edge detect CCPRxH Capture Enable TMR1H Q s CCPxCON<3:0> TMR1L CCPRxL

Philippe LETENNEUR - GRANVILLE - 2003 -

page 39

LES MICROCONTROLEURS PIC 16F87X


12) LA LIAISON SERIE USART OU SCI (SERIAL COMMUNICATION INTERFACE). 12.1) Prsentation :
Remarque : La liaison USART du PIC peut fonctionner soit en mode synchrone ou asynchrone, seul le mode asynchrone sera tudi. La liaison srie SCI est une interface srie asynchrone de type START / STOP. Elle permet d'effectuer des communications avec d'autres systmes ou objets techniques sur de longues distances (quelques mtres quelques kilomtres). Elle dispose des fonctionnalits suivantes: - Fonctionnement en Full Duplex, c'est dire mission et rception de donnes en mme temps. - Transmission et rception de donnes (compatibles avec la norme RS232 en utilisant une fonction dadaptation de niveaux). - Contrle des erreurs de transmission et de rception. - Transmission sur 8 ou 9 bits. - Mode rveil automatique lors de la rception de signaux valides. - 4 Sources d'interruptions possibles.

Etat de repos ligne libre (Idle line)

8 ou 9 bits de donnes
D0 D1 D2 D3 D4 D5 D6 D7 D8 D0

START

STOP START

Cette interface est compose de 3 fonctions : - La transmission. - La rception. - Le gnrateur dhorloge (choix de la vitesse de transmission et de rception).

Philippe LETENNEUR - GRANVILLE - 2003 -

page 40

LES MICROCONTROLEURS PIC 16F87X


12.2) La transmission : 12.2.1) Prsentation :
USART TRANSMIT BLO CK DIAGRAM
Data Bus TXIF TXIE MSb (8) Interrupt TXEN Baud Rate CLK TRMT SPBRG Baud Rate Generator TX9 TX9D SPEN TSR Register TXREG Register 8 LSb 0 Pin Buffer and Control RC6/TX/CK pin

12.2.2) Fonctionnement :
ASYNCHRONOUS MASTER TRANSMISSION
Write to TXREG BRG Output (Shift Clock) RC6/TX/CK (pin) TXIF bit (Transmit Buffer Reg. Empty Flag) Word 1

START Bit

Bit 0

Bit 1 Word 1

Bit 7/8

STOP Bit

TRMT bit (Transmit Shift Reg. Empty Flag)

Word 1 Transmit Shift Reg

Cette fonction utilise un registre dcalage pour transmettre les 8 ou 9 bits de linformation du registre TXREG. Pour que cette fonction soit oprationnelle, il faut que la broche RC6 du PORTC soit configure en sortie, positionner 1 le bit SPEN du registre RCSTA et le bit TXEN du registre TXSTA. Dans le cas o lon utilise une transmission sur 9 bits (Bit de parit par exemple), il faut autoriser la transmission sur 9 bits via le bit TX9 du registre TXSTA, la valeur du neuvime bit doit tre mise dans le bit TX9D du registre TXSTA. Avant de transmettre une information, il faut sassurer que le registre de transmission soit libre travers le bit TXIF (1 libre et 0 occupe). Le bit TMRT du registre TXSTA indique si la transmission est compltement termine (1 termine et 0 occupe).

Philippe LETENNEUR - GRANVILLE - 2003 -

page 41

LES MICROCONTROLEURS PIC 16F87X


12.2.3) Configuration et registres associs :
La configuration de la transmission de la SCI passe par les registres : PIR1 (adresse 0Ch), PIE1 (adresse 8Ch page 1), RCSTA (adresse 18h page 0), TXREG (adresse 19h page 0), TXSTA (adresse 98h page 1), SPBRG (adresse 99h page 1) et INTCON (adresse 0Bh : toutes les pages).
REGISTERS ASSOCIATED WITH ASYNCHRONOUS TRANSMISSION
Addr ess Name Bit 7 GIE PSPIF(1) SPEN PSPIE(1) CSRC Bit 6 PEIE ADIF RX9 ADIE TX9 Bit 5 T0IE RCIF SREN RCIE TXEN Bit 4 INTE TXIF CREN TXIE SYNC Bit 3 RBIE SSPIF Bit 2 T0IF CCP1IF FERR SSPIE CCP1IE BRGH Bit 1 INTF TMR2IF OERR TMR2IE TRMT Bit 0 R0IF TMR1IF RX9D TMR1IE TX9D Value on: POR, BOR
0000 000x 0000 0000 0000 -00x 0000 0000 0000 0000 0000 -010 0000 0000

Value on all ot her RESETS


0000 000u 0000 0000 0000 -00x 0000 0000 0000 0000 0000 -010 0000 0000

0Bh, 8Bh, INTCON 10Bh,18Bh 0Ch 18h 19h 8Ch 98h 99h PIR1 RCSTA TXREG PIE1 TXSTA

USART Transmit Register

SPBRG Baud Rate Generator Register

Legend: x = unknown, - = unimplemented locations read as 0. Shaded cells are not used for asynchronous transmission. Note 1: Bits PSPIE and PSPIF are reserved on the PIC16F873/876; always maintain these bits clear.

Le registre le plus important pour la transmission cest le registre TXSTA.


TXSTA: TRANSMIT STATUS AND CONTROL REGISTER (ADDRESS 98h)
R/W-0 CSRC bit 7 bit 7 CSRC: Clock Source Select bit Asynchronous mode: Dont care Synchronous mode: 1 = Master mode (clock generated internally from BRG) 0 = Slave mode (clock from external source) TX9: 9-bit Transmit Enable bit 1 = Selects 9-bit transmission 0 = Selects 8-bit transmission TXEN: Transmit Enable bit 1 = Transmit enabled 0 = Transmit disabled Note : SREN/CREN overrides TXEN in SYNC mode. bit 4 SYNC: USART Mode Select bit 1 = Synchronous mode 0 = Asynchronous mode Unimp lemente d: Read as '0' BRGH: High Baud Rate Select bit Asynchronous mode: 1 = High speed 0 = Low speed Synchronous mode: Unused in this mode TRMT: Transmit Shift Register Status bit 1 = TSR empty 0 = TSR full TX9D: 9th bit of Transmit Data, can be parity bit Legend: R = Readable bit - n = Value at POR W = Writable bit 1 = Bit is set U = Unimplemented bit, read as 0 0 = Bit is cleared x = Bit is unknown R/W-0 TX9 R/W-0 TXEN R/W-0 SYNC U-0 R/W-0 BRGH R-1 TRMT R/W-0 TX9D bit 0

bit 6

bit 5

bit 3 bit 2

bit 1

bit 0

Philippe LETENNEUR - GRANVILLE - 2003 -

page 42

LES MICROCONTROLEURS PIC 16F87X


12.3) La rception : 12.3.1) Prsentation :
USART RECEIVE BLO CK DIAGRAM
x64 Baud Rate CLK CREN FOSC SPBRG Baud Rate Generator RC7/RX/DT Pin Buffer and Control Data Recovery RX9 64 or 16 MSb STOP (8) 7 RSR Register 1 LSb 0 START OERR FERR

SPEN

RX9D

RCREG Register

FIFO

8 Interrupt RCIF RCIE Data Bus

12.3.2) Fonctionnement :
ASYNCHRONOUS RECEPTION
RX (pin) Rcv Shift Reg Rcv Buffer Reg Read Rcv Buffer Reg RCREG RCIF (Interrupt Flag) OERR bit CREN Note: This timing diagram shows three words appearing on the RX input. The RCREG (receive buffer) is read after the third word, causing the OERR (overrun) bit to be set. START bit bit0 START bit bit0 START bit

bit1

bit7/8 STOP bit

bit7/8 STOP bit

bit7/8

STOP bit

Word 1 RCREG

Word 2 RCREG

Cette fonction utilise un registre RSR dcalage pour les 8 ou 9 bits de linformation recevoir, une fois la rception termine la valeur est stocke dans le registre RCREG. Pour que cette fonction soit oprationnelle, il faut que la broche RC7 du PORTC soit configure en entre et positionne 1 le bit SPEN du registre RCSTA. Dans le cas ou lon utilise une rception sur 9 bits, il faut autoriser la rception sur 9 bits via le bit RX9 du registre RCSTA, la valeur du neuvime bit est rcupre dans le bit RX9D du registre RCSTA. Avant de lire une information dans le registre RCREG, il faut sassurer que linformation est bien reue en testant le bit RCIF (1 Donne reue), ce bit est remis 0 lors de la lecture du registre RCREG. Les bits FERR et OERR peuvent indiquer respectivement une erreur de format et une erreur over run .

Philippe LETENNEUR - GRANVILLE - 2003 -

page 43

LES MICROCONTROLEURS PIC 16F87X


12.3.3) Configuration et registres associs :
La configuration de la transmission de la SCI passe par les registres : PIR1 (adresse 0Ch), PIE1 (adresse 8Ch page 1), RCSTA (adresse 18h page 0), RCREG (adresse 1Ah page 0), TXSTA (adresse 98h page 1), SPBRG (adresse 99h page 1) et INTCON (adresse 0Bh : toutes les pages).
REGISTERS ASSOCIATED WITH ASYNCHRONOUS RECEPTION
Add ress Name Bi t 7 GIE PSPIF(1) SPEN PSPIE(1) CSRC Bit 6 PEIE ADIF RX9 ADIE TX9 Bit 5 T0IE RCIF SREN RCIE TXEN Bit 4 INTE TXIF CREN TXIE SYNC Bit 3 RBIE SSPIF SSPIE Bit 2 T0IF CCP1IF FERR CCP1IE BRGH Bit 1 INTF TMR2IF OERR Bit 0 R0IF TMR1IF RX9D Value on: POR, BOR
0000 000x 0000 0000 0000 -00x 0000 0000 0000 0000 0000 -010 0000 0000

Value o n all ot her RESETS


0000 000u 0000 0000 0000 -00x 0000 0000 0000 0000 0000 -010 0000 0000

0Bh, 8Bh, INTCON 10Bh,18Bh 0Ch 18h 1Ah 8Ch 98h 99h PIR1 RCSTA PIE1 TXSTA SPBRG

RCREG USART Receive Register TMR2IE TMR1IE TRMT TX9D

Baud Rate Generator Register

Legend: x = unknown, - = unimplemented locations read as '0'. Shaded cells are not used for asynchronous reception. Note 1: Bits PSPIE and PSPIF are reserved on PIC16F873/876 devices; always maintain these bits clear.

Le registre le plus important pour la transmission cest le registre RCSTA.


RCSTA: RECEIVE STATUS AND CONTROL REGISTER (ADDRESS 18h)
R/W-0 SPEN bit 7 bit 7 SPEN: Serial Port Enable bit 1 = Serial port enabled (configures RC7/RX/DT and RC6/TX/CK pins as serial port pins) 0 = Serial port disabled RX9: 9-bit Receive Enable bit 1 = Selects 9-bit reception 0 = Selects 8-bit reception SREN: Single Receive Enable bit Asynchronous mode: Dont care Synchronous mode - master: 1 = Enables single receive 0 = Disables single receive This bit is cleared after reception is complete. Synchronous mode - slave: Dont care CREN: Continuous Receive Enable bit Asynchronous mode: 1 = Enables continuous receive 0 = Disables continuous receive Synchronous mode: 1 = Enables continuous receive until enable bit CREN is cleared (CREN overrides SREN) 0 = Disables continuous receive ADDEN: Address Detect Enable bit Asynchronous mode 9-bit (RX9 = 1): 1 = Enables address detection, enables interrupt and load of the receive buffer when RSR<8> is set 0 = Disables address detection, all bytes are received, and ninth bit can be used as parity bit FERR: Framing Error bit 1 = Framing error (can be updated by reading RCREG register and receive next valid byte) 0 = No framing error OERR: Overrun Error bit 1 = Overrun error (can be cleared by clearing bit CREN) 0 = No overrun error RX9D: 9th bit of Received Data (can be parity bit, but must be calculated by user firmware) Legend: R = Readable bit - n = Value at POR W = Writable bit 1 = Bit is set U = Unimplemented bit, read as 0 0 = Bit is cleared x = Bit is unknown R/W-0 RX9 R/W-0 SREN R/W-0 CREN R/W-0 ADDEN R-0 FERR R-0 OERR R-x RX9D bit 0

bit 6

bit 5

bit 4

bit 3

bit 2

bit 1

bit 0

Philippe LETENNEUR - GRANVILLE - 2003 -

page 44

LES MICROCONTROLEURS PIC 16F87X


12.4) Le gnrateur dhorloge: 12.4.1) Prsentation et fonctionnement:
Cest lui qui fixe la vitesse de la rception et transmission de lUSART. Il faut utiliser la formule ci-dessous pour calculer la valeur mettre dans le registre SPBRG. Si la frquence du quartz du microcontrleur est suprieure 10MHz, il est conseill de positionner le bit BGRH 1.
BAUD RATE FORMULA
SYNC 0 1 BRGH = 0 (Low Speed) (Asynchronous) Baud Rate = FOSC/(64(X+1)) (Synchronous) Baud Rate = FOSC/(4(X+1)) BRGH = 1 (High Speed) Baud Rate = FOSC/(16(X+1)) N/A

X = value in SPBRG (0 to 255)

Tableaux de vitesses :
BAUD R ATES FOR AS YNCHRONOUS MODE (BRGH = 0)
BAUD RATE (K) 0.3 1.2 2.4 9.6 19.2 28.8 33.6 57.6 HIGH LOW FOSC = 20 MHz % ERROR 1.75 0.17 1.73 1.72 8.51 3.34 8.51 FOSC = 4 MHz % ERROR 0 0.17 0.17 6.99 8.51 8.51 8.51 SPBRG va lu e (d eci mal) 207 51 25 6 2 1 0 255 0 SPBRG va lu e (d eci mal) 255 129 31 15 9 8 4 255 0 FOSC = 16 MHz % ERROR 0.17 0.17 0.16 0.16 3.55 6.29 8.51 SPBRG va lue (d ecim al) 207 103 25 12 8 6 3 255 0 FOSC = 10 MHz % ERROR 0.17 0.17 1.73 1.72 8.51 6.99 9.58 SPBRG val ue (decim al ) 129 64 15 7 4 4 2 255 0

KBAUD 1.221 2.404 9.766 19.531 31.250 34.722 62.500 1.221 312.500

KBAUD 1.202 2.404 9.615 19.231 27.778 35.714 62.500 0.977 250.000

KBAUD 1.202 2.404 9.766 19.531 31.250 31.250 52.083 0.610 156.250

BAUD RATE (K) 0.3 1.2 2.4 9.6 19.2 28.8 33.6 57.6 HIGH LOW

FOSC = 3.6864 MHz % ERROR 0 0 0 0 0 0 0 SPBRG valu e (dec im al) 191 47 23 5 2 1 0 255 0

KBAUD 0.300 1.202 2.404 8.929 20.833 31.250 62.500 0.244 62.500

KBAUD 0.3 1.2 2.4 9.6 19.2 28.8 57.6 0.225 57.6

BAUD R ATES FOR AS YNCHRONOUS MODE (BRGH = 1)


BAUD RATE (K) 0.3 1.2 2.4 9.6 19.2 28.8 33.6 57.6 HIGH LOW BAUD RATE (K) 0.3 1.2 2.4 9.6 19.2 28.8 33.6 57.6 HIGH LOW FOSC = 20 MHz % ERROR 0.16 0.16 0.94 0.55 3.34 FOSC = 4 MHz % ERROR 0.17 0.17 0.16 0.16 3.55 6.29 8.51 SPBRG va lu e (d eci mal) 207 103 25 12 8 6 3 255 0 SPBRG val ue (decim al ) 129 64 42 36 20 255 0 FOSC = 16 MHz % ERROR 0.16 0.16 2.13 0.79 2.13 SPBRG va lue (decim al) 103 51 33 29 16 255 0 FOSC = 10 MHz % ERROR 1.71 0.16 1.72 1.36 2.10 1.36 SPBRG va lue (d ecim al) 255 64 31 21 18 10 255 0

KBAUD 9. 615 19.231 29.070 33.784 59.524 4.883 1250.000

KBAU D 9.615 19.231 29.412 33.333 58.824 3.906 1000.000

KBAUD 2.441 9.615 19.531 28.409 32.895 56.818 2.441 625.000

FOSC = 3.6864 MHz % ERROR 0 0 0 0 0 2.04 0 SPBRG valu e (d ec im al) 191 95 23 11 7 6 3 255 0

KBAUD 1.202 2.404 9. 615 19.231 27.798 35.714 62.500 0.977 250.000

KBA UD 1.2 2.4 9.6 19.2 28.8 32.9 57.6 0.9 230.4

Philippe LETENNEUR - GRANVILLE - 2003 -

page 45

LES MICROCONTROLEURS PIC 16F87X


12.4.2) Configuration et registres associs :
La configuration du gnrateur dhorloge de la SCI passe par les registres : TXSTA (adresse 98h page 1), RCSTA (adresse 18h page 0) et SPBRG (adresse 99h page 1).
REGISTERS ASSOCIATED WITH BAUD RATE GENERATOR
Add ress 98h 18h 99h Name TXSTA RCSTA SPBRG Bi t 7 CSRC SPEN Bit 6 TX9 RX9 Bit 5 TXEN SREN Bit 4 SYNC CREN Bit 3 ADDEN Bit 2 BRGH FERR Bit 1 TRMT OERR Bit 0 TX9D RX9D Value on: POR, BOR
0000 -010 0000 000x 0000 0000

Value on all ot her RESETS


0000 -010 0000 000x 0000 0000

Baud Rate Generator Register

Legend: x = unknown, - = unimplemented, read as '0'. Shaded cells are not used by the BRG.

Philippe LETENNEUR - GRANVILLE - 2003 -

page 46

LES MICROCONTROLEURS PIC 16F87X


13) LES INTERRUPTIONS. 13.1) Prsentation.
Le C dispose de plusieurs sources d'interruptions. Une interruption externe, action sur la broche INT/RB0. Dbordement du TIMER0. Changement dtat logique sur une des broches du PORTB (RB4 RB7). Une interruption dun des priphriques (PEIE). - Fin de programmation dune case mmoire de lEEPROM. - Changement dtat sur le PORTD (PSPIE). - Fin de conversion analogique numrique (ADIE). - Rception dune information sur la liaison srie (RCIE). - Fin dmission dune information sur la liaison srie (TXIE). - Interruption SPI ou I2C du module MSSP (SSPIE). - Interruption du registre de capture et/ou de comparaison 1 (CCPI1E). - Interruption du registre de capture et/ou de comparaison 2 (CCPI2E). - Dbordement du TIMER1 (TMR1E). - Dbordement du TIMER2 (TMR2E). - Collision de BUS (BCLIE)

13.2) Fonctionnement.
Lors dun vnement dans un ou plusieurs des circuits priphriques (ADC, EEPROM, USART-SCI, MSSP-I2C-SPI, TIMER1, TIMER2) comme par exemple : la fin de conversion, la fin de programmation dun octet dans lEEPROM, la rception dune information, la dtection dun front, etc et si le bit de linterruption concerne a t autorise (EEIE, PSPIE, ADIE, RCIE, TXIE, SSPIE, CCP1IE, TMR2IE, TMR1IE, CCP2IE ou BCLIE : Registres PIE1 et PIE2) alors une interruption priphrique est dclenche. Pour que celle-ci soit prise en compte il faut que le bit dautorisation des interruptions priphriques soit positionn 1 (PEIE) ainsi que le bit GIE dautorisation globale des interruptions du registre INTCON. Pour quune interruption du type TIMER0 ou INT/RB0 ou PORTB soit prise en compte il suffit que le bit local dautorisation dinterruption soit positionn 1 (T0IE ou INTE ou RBIE) ainsi que le bit GIE dautorisation globale des interruptions du registre INTCON. Dans ces conditions le programme en cours dexcution est interrompu et le microcontrleur excute le programme dinterruption partir de ladresse 0x0004. Au dbut de celui-ci il faut que le logiciel vrifie quel priphrique a dclench linterruption.
INTERRUPT LOGIC
EEIF EEIE PSPIF PSPIE ADIF ADIE RCIF RCIE TXIF TXIE SSPIF SSPIE CCP1IF CCP1IE TMR2IF TMR2IE TMR1IF TMR1IE CCP2IF CCP2IE BCLIF BCLIE T0IF T0IE INTF INTE RBIF RBIE PEIE GIE Wake-up (If in SLEEP mode)

Interrupt to CPU

The following table shows which devices have which interrupts.


Devic e PIC16F876/873 PIC16F877/874 T0IF Yes Yes INTF Yes Yes RBIF Yes Yes PSPIF ADIF Yes Yes Yes RCIF Yes Yes TXIF Yes Yes SSPIF Yes Yes CCP1IF TMR2IF TMR1IF EEIF BCLIF CCP2IF Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes

Philippe LETENNEUR - GRANVILLE - 2003 -

page 47

LES MICROCONTROLEURS PIC 16F87X


13.3) Droulement dune interruption.
Chronogramme de la prise en compte dune interruption.
INT Pin and Other External Interrupt Timing

Q1 OSC1 CLKOUT 3 INT pin INTF ag (INTCON<1>) GIE bit (INTCON<7>) INSTRUCTION FLOW PC Instruction fetched Instruction executed

Q2

Q3

Q4

Q1

Q2

Q3

Q4

Q1

Q2

Q3

Q4

Q1

Q2

Q3

Q4

Q1

Q2

Q3

Q4

4 1 1 5 Interrupt Latency 2

PC Inst (PC) Inst (PC-1)

PC+1 Inst (PC+1) Inst (PC)

PC+1 Dummy Cycle

0004h Inst (0004h) Dummy Cycle

0005h Inst (0005h) Inst (0004h)

Note 1: INTF ag is sampled here (every Q1). 2: Interrupt latency = 3-4 TCY where TCY = instruction cycle time. Latency is the same whether Instruction (PC) is a single cycle or a 2-cycle instruction. 3: CLKOUT is available only in RC oscillator mode. 4: For minimum width of INT pulse, refer to AC specs. 5: INTF is enabled to be set anytime during the Q4-Q1 cycles.

Au dbut dune interruption le sous programme dinterruption doit sauvegarder le contexte et le restituer la fin, cest dire les valeurs des registres W, PCLATCH et STATUS. Cela permet au processus interrompu de retrouver ses registres intacts. Pour respecter ce principe il faut ajouter au dbut du sous programme dinterruption quelques instructions pour sauvegarder les registres W, PCLATCH et STATUS. A la fin du sous programme on ajoute des instructions pour restaurer ces valeurs.
SAVING STATUS, W, AND PCLATH REGISTERS IN RAM
M OVWF SWAPF CLRF M OVWF M OVF M OVWF CLRF : : (ISR) : M OVF M OVWF SWAPF M OVWF SWAPF SWAPF W_TEMP STATUS,W STATUS STATUS_TEMP PCLATH, W PCLATH_TEMP PCLATH ;Co py W to TEMP register ;Sw ap status to be saved into W ;ba nk 0, regardless of current bank, Clears IRP,RP1, ;Sa ve status to bank zero STATUS_TEMP register ;On ly required if using pages 1, 2 and/or 3 ;Sa ve PCLATH into W ;Pa ge zero, regardless of current page ;(I nsert user code here) PCLATH_TEMP, W PCLATH STATUS_TEMP,W STATUS W_TEMP,F W_TEMP,W ;Re store PCLATH ;Mo ve W into PCLATH ;Sw ap STATUS_TEMP register into W ;(s ets bank to original state) ;Mo ve W into STATUS register ;Sw ap W_TEMP ;Sw ap W_TEMP into W

RP0

Philippe LETENNEUR - GRANVILLE - 2003 -

page 48

LES MICROCONTROLEURS PIC 16F87X


13.4) Configuration et registres associs :
- Le registre OPTION permet de choisir le type de front pour linterruption INT/RB0.
OPTION_REG REGISTER (ADDRESS 81h, 181h)
R/W-1 RBPU bit 7 bit 6 INTEDG: Interrupt Edge Select bit 1 = Interrupt on rising edge of RB0/INT pin 0 = Interrupt on falling edge of RB0/INT pin R/W-1 INTEDG R/W-1 T0CS R/W-1 T0SE R/W-1 PSA R/W-1 PS2 R/W-1 PS1 R/W-1 PS0 bit 0

- Le registre INTCON permet dautoriser les interruptions globales (GIE), les interruptions des priphriques (PEIE), Linterruption TIMER0 (T0IE), linterruption extrieure (INT/RB0), linterruption de changement dtat du PORTB (RBIE) et les indicateurs associs des interruptions (TIMER0, INT/RB0 et du changement dtat du PORTB : RBIF).
INTCON REGISTER (ADDRESS 0Bh, 8Bh, 10Bh, 1 8Bh)
R/W-0 GIE bit 7 bit 7 GIE: Global Interrupt Enable bit 1 = Enables all unmasked interrupts 0 = Disables all interrupts PEIE: Peripheral Interrupt Enable bit 1 = Enables all unmasked peripheral interrupts 0 = Disables all peripheral interrupts T0IE: TMR0 Overflow Interrupt Enable bit 1 = Enables the TMR0 interrupt 0 = Disables the TMR0 interrupt INTE: RB0/INT External Interrupt Enable bit 1 = Enables the RB0/INT external interrupt 0 = Disables the RB0/INT external interrupt RBIE: RB Port Change Interrupt Enable bit 1 = Enables the RB port change interrupt 0 = Disables the RB port change interrupt T0IF: TMR0 Overflow Interrupt Flag bit 1 = TMR0 register has overflowed (must be cleared in software) 0 = TMR0 register did not overflow INTF: RB0/INT External Interrupt Flag bit 1 = The RB0/INT external interrupt occurred (must be cleared in software) 0 = The RB0/INT external interrupt did not occur RBIF: RB Port Change Interrupt Flag bit 1 = At least one of the RB7:RB4 pins changed state; a mismatch condition will continue to set the bit. Reading PORTB will end the mismatch condition and allow the bit to be cleared (must be cleared in software). 0 = None of the RB7:RB4 pins have changed state R/W-0 PEIE R/W-0 T0IE R/W-0 INTE R/W-0 RBIE R/W-0 T0IF R/W-0 INTF R/W-x RBIF bit 0

bit 6

bit 5

bit 4

bit 3

bit 2

bit 1

bit 0

Philippe LETENNEUR - GRANVILLE - 2003 -

page 49

LES MICROCONTROLEURS PIC 16F87X


- Les registres dautorisations des interruptions priphriques PIE1 et PIE2.
PIE1 REGISTER (ADDRESS 8Ch)
R/W-0 PSPIE(1) bit 7 bit 7 PSPIE(1): Parallel Slave Port Read/Write Interrupt Enable bit 1 = Enables the PSP read/write interrupt 0 = Disables the PSP read/write interrupt ADIE: A/D Converter Interrupt Enable bit 1 = Enables the A/D converter interrupt 0 = Disables the A/D converter interrupt RCIE: USART Receive Interrupt Enable bit 1 = Enables the USART receive interrupt 0 = Disables the USART receive interrupt TXIE: USART Transmit Interrupt Enable bit 1 = Enables the USART transmit interrupt 0 = Disables the USART transmit interrupt SSPIE: Synchronous Serial Port Interrupt Enable bit 1 = Enables the SSP interrupt 0 = Disables the SSP interrupt CCP1IE: CCP1 Interrupt Enable bit 1 = Enables the CCP1 interrupt 0 = Disables the CCP1 interrupt TMR2IE: TMR2 to PR2 Match Interrupt Enable bit 1 = Enables the TMR2 to PR2 match interrupt 0 = Disables the TMR2 to PR2 match interrupt TMR1IE: TMR1 Overflow Interrupt Enable bit 1 = Enables the TMR1 overflow interrupt 0 = Disables the TMR1 overflow interrupt R/W-0 ADIE R/W-0 RCIE R/W-0 TXIE R/W-0 SSPIE R/W-0 CCP1IE R/W-0 TMR2IE R/W-0 TMR1IE bit 0

bit 6

bit 5

bit 4

bit 3

bit 2

bit 1

bit 0

PIE2 REGISTER (ADDRESS 8Dh)


U-0 bit 7 bit 7 bit 6 bit 5 bit 4 Unimp lemente d: Read as '0' Reserved: Always maintain this bit clear Unimp lemente d: Read as '0' EEIE: EEPROM Write Operation Interrupt Enable 1 = Enable EE Write Interrupt 0 = Disable EE Write Interrupt BCLIE: Bus Collision Interrupt Enable 1 = Enable Bus Collision Interrupt 0 = Disable Bus Collision Interrupt Unimp lemente d: Read as '0' CCP2IE: CCP2 Interrupt Enable bit 1 = Enables the CCP2 interrupt 0 = Disables the CCP2 interrupt R/W-0 Reserved U-0 R/W-0 EEIE R/W-0 BCLIE U-0 U-0 R/W-0 CCP2IE bit 0

bit 3

bit 2-1 bit 0

Philippe LETENNEUR - GRANVILLE - 2003 -

page 50

LES MICROCONTROLEURS PIC 16F87X


- Le registre des indicateurs dvnements PIR1.
PIR1 REGISTER (ADDRESS 0Ch)
R/W-0 PSPIF(1) bit 7 bit 7 R/W-0 ADIF R-0 RCIF R-0 TXIF R/W-0 SSPIF R/W-0 CCP1IF R/W-0 TMR2IF R/W-0 TMR1IF bit 0

bit 6

bit 5

bit 4

bit 3

bit 2

bit 1

bit 0

PSPIF(1): Parallel Slave Port Read/Write Interrupt Flag bit 1 = A read or a write operation has taken place (must be cleared in software) 0 = No read or write has occurred ADIF: A/D Converter Interrupt Flag bit 1 = An A/D conversion completed 0 = The A/D conversion is not complete RCIF: USART Receive Interrupt Flag bit 1 = The USART receive buffer is full 0 = The USART receive buffer is empty TXIF: USART Transmit Interrupt Flag bit 1 = The USART transmit buffer is empty 0 = The USART transmit buffer is full SSPIF: Synchronous Serial Port (SSP) Interrupt Flag 1 = The SSP interrupt condition has occurred, and must be cleared in software before returning from the Interrupt Service Routine. The conditions that will set this bit are: SPI - A transmission/reception has taken place. I2C Slave - A transmission/reception has taken place. I2C Master - A transmission/reception has taken place. - The initiated START condition was completed by the SSP module. - The initiated STOP condition was completed by the SSP module. - The initiated Restart condition was completed by the SSP module. - The initiated Acknowledge condition was completed by the SSP module. - A START condition occurred while the SSP module was idle (Multi-Master system). - A STOP condition occurred while the SSP module was idle (Multi-Master system). 0 = No SSP interrupt condition has occurred. CCP1IF: CCP1 Interrupt Flag bit Capture mode: 1 = A TMR1 register capture occurred (must be cleared in software) 0 = No TMR1 register capture occurred Compare mode: 1 = A TMR1 register compare match occurred (must be cleared in software) 0 = No TMR1 register compare match occurred PWM mode: Unused in this mode TMR2IF: TMR2 to PR2 Match Interrupt Flag bit 1 = TMR2 to PR2 match occurred (must be cleared in software) 0 = No TMR2 to PR2 match occurred TMR1IF: TMR1 Overflow Interrupt Flag bit 1 = TMR1 register overflowed (must be cleared in software) 0 = TMR1 register did not overflow Note 1: PSPIF is reserved on PIC16F873/876 devices; always maintain this bit clear.

Philippe LETENNEUR - GRANVILLE - 2003 -

page 51

You might also like