Professional Documents
Culture Documents
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
page 1
page 2
page 3
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.
page 4
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.
page 5
Program Bus
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
W reg
MCLR
VDD, VSS
Timer0
Timer1
Timer2
10-bit A/D
Data EEPROM
CCP1,2
USART
page 6
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
MCLR
VDD, VSS
Timer0
Timer1
Timer2
10-bit A/D
Data EEPROM
CCP1,2
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.
page 7
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
2 3 4 5 6 7
2 3 4 5 6 7
21 22 23 24 25 26 27 28
21 22 23 24 25 26 27 28
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
ST ST ST ST ST ST ST ST
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.
page 8
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
2 3 4 5 6 7
3 4 5 6 7 8
19 20 21 22 23 24
33 34 35 36 37 38 39 40
36 37 38 39 41 42 43 44
8 9 10 11 14 15 16 17
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.
15 16 17 18 23 24 25 26
16 18 19 20 25 26 27 29
32 35 36 37 42 43 44 1
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
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.
page 9
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
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
Time-out Reset
BODEN
Enable PWRT
Enable OST
page 11
TABLE 12-1:
CERAMIC RESONATORS
Ranges Teste d:
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:
Osc Type LP XT
Cry stal Freq . 32 kHz 200 kHz 200 kHz 1 MHz 4 MHz
HS
Un oscillateur externe.
VDD
Un rseau RC.
REXT
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).
page 12
page 13
13
Stack Level 8
RESET Vector
0000h
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 ! !).
page 14
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
TXSTA SPBRG
ADRESL ADCON1
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.
page 15
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.
page 16
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
page 17
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
C C C,DC,Z Z
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
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.
page 18
; 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
page 19
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.
page 20
SINON FSI
page 21
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.
page 22
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
page 23
I/O pin(1)
TRIS Latch D Q Q
CK
CK
RD TRIS
RD TRIS
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
ADCON1 ADFM
Legend: x = unknown, u = unchanged, - = unimplemented locations read as '0'. Shaded cells are not used by PORTA.
page 24
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
I/O pin(1)
ST Buffer
Latch D EN Q1
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.
page 25
Data Latch WR TRIS D CK Q Q N Vss Schmitt Trigger Q D EN 0 Schmitt Trigger with SMBus levels
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.
page 26
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
RD Port
PORTD Data Direction Register OBF IBOV PSPMODE PORTE Data Direction Bits
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.
Bi t 7 IBF ADFM
Bit 6 OBF
Bit 5 IBOV
Bit 4 PSPMODE
Bit 3 PCFG3
Legend: x = unknown, u = unchanged, - = unimplemented, read as 0. Shaded cells are not used by PORTE.
page 27
000
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.
page 28
TAD2 b9
TAD3 b8
TAD4
b7
TAD5 b6
TAD6 b5
TAD7
b4
TAD8
b3
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
page 29
U-0
R/W-0
U-0
R/W-0 PCFG3
R/W-0 PCFG2
R/W-0 PCFG1
AN 7(1) RE2 A A D D D D D A D D D 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+
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
Right Justified
Left Justified
page 30
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).
page 31
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
page 32
M U X
1 0
TMR0 Reg
T0CS
PSA PRESCALER
0 1
Watchdog Timer
M U X
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).
page 33
FOSC/4
0
1
PSout
Programmable Prescaler
3
TMR0
PSout
PSA
T0CS
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
bit 4
bit 3
bit 2-0
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.
page 34
TMR1 TMR1L
0 1
T1OSC RC0/T1OSO/T1CKI
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
page 35
bit 3
bit 2
bit 1
bit 0
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.
page 36
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
page 37
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
page 38
page 39
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).
page 40
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
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).
page 41
0Bh, 8Bh, INTCON 10Bh,18Bh 0Ch 18h 19h 8Ch 98h 99h PIR1 RCSTA TXREG PIE1 TXSTA
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.
bit 6
bit 5
bit 3 bit 2
bit 1
bit 0
page 42
SPEN
RX9D
RCREG Register
FIFO
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
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 .
page 43
0Bh, 8Bh, INTCON 10Bh,18Bh 0Ch 18h 1Ah 8Ch 98h 99h PIR1 RCSTA PIE1 TXSTA SPBRG
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.
bit 6
bit 5
bit 4
bit 3
bit 2
bit 1
bit 0
page 44
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
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
page 45
Legend: x = unknown, - = unimplemented, read as '0'. Shaded cells are not used by the BRG.
page 46
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
page 47
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
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
page 48
- 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
page 49
bit 6
bit 5
bit 4
bit 3
bit 2
bit 1
bit 0
bit 3
page 50
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.
page 51