Fundamentos Taller de interrupciones Francisco Garcı́a Eijó Segundo Cuatrimestre de 2011 - Organización del Computador I El PIC 16F84 El PIC16F84 es un microcontrolador de la empresa Microchip. Cuenta con las siguientes caracterı́sticas: Caracterı́stica Arquitectura Frecuencia de operación Memoria FLASH de programa Memoria de datos Memoria de datos EEPROM Interrupciones Puertos de E/S Conjunto de Instrucciones Descripción Harvard 20 MHz 1024 words 68 Bytes 64 Bytes 4 2 (A,B) 35 Encapsulado SSOP RA2 •1 20 RA1 2 19 RA0 3 18 OSC1/CLKIN MCLR VSS 4 17 OSC2/CLKOUT 16 VDD VSS 6 15 RB0/INT 7 14 VDD RB7 RB1 8 13 RB6 RB2 9 12 RB5 RB3 10 11 RB4 5 PIC16F84A RA3 RA4/T0CKI ence Manual should be considered a complementary document to this data sheet, and is highly recommended reading for a better understanding of the device architecture and operation of the peripheral modules. device functions. These functions include: • External interrupt • Change on PORTB interrupt • Timer0 clock input El PIC 16F84 internamente The PIC16F84A belongs to the mid-range family of the PICmicro® microcontroller devices. A block diagram of the device is shown in Figure 1-1. FIGURE 1-1: Table 1-1 details the pinout of the device with descriptions and details for each pin. PIC16F84A BLOCK DIAGRAM Data Bus 13 8 Program Counter EEPROM Data Memory FLASH Program Memory 8 Level Stack (13-bit) 1K x 14 Program Bus 14 RAM File Registers 68 x 8 7 EEDATA RAM Addr EEPROM Data Memory 64 x 8 EEADR Addr Mux Instruction Register 7 Direct Addr 5 TMR0 Indirect Addr FSR reg RA4/T0CKI STATUS reg 8 MUX Power-up Timer Instruction Decode & Control Oscillator Start-up Timer Timing Generation Watchdog Timer 8 I/O Ports ALU Power-on Reset RA3:RA0 W reg RB7:RB1 RB0/INT OSC2/CLKOUT OSC1/CLKIN MCLR VDD, VSS Organización de la memoria En este PIC la memoria esta divida en: Memoria FLASH de programa. Memoria RAM de datos. Memoria EEPROM de datos. PIC16F84A Memoria de Programa memory blocks in the PIC16F84A. ogram memory and the data memory. its own bus, so that access to each during the same oscillator cycle. y can further be broken down into the e RAM and the Special Function ). The operation of the SFRs that are described here. The SFRs used ipheral modules are described in the g each individual peripheral module. ory area also contains the data y. This memory is not directly mapped mory, but is indirectly mapped. That is, ss pointer specifies the address of the memory to read/write. The 64 bytes of memory have the address range ails on the EEPROM memory can be 3.0. FIGURE 2-1: PROGRAM MEMORY MAP AND STACK - PIC16F84A PC<12:0> 13 CALL, RETURN RETFIE, RETLW Stack Level 1 • • • Stack Level 8 RESET Vector 0000h Peripheral Interrupt Vector 0004h User Memory Space ORY ORGANIZATION ation on device memory may be found o™ Mid-Range Reference Manual, 3FFh am Memory Organization as a 13-bit program counter capable 8K x 14 program memory space. For the first 1K x 14 (0000h-03FFh) are mented (Figure 2-1). Accessing a locaphysically implemented address will und. For example, for locations 20h, h, 1020h, 1420h, 1820h, and 1C20h, 1FFFh Memoria de Datos PIC16F84A 2.2 Data Memory Organization The data memory is partitioned into two areas. The first is the Special Function Registers (SFR) area, while the second is the General Purpose Registers (GPR) area. The SFRs control the operation of the device. Portions of data memory are banked. This is for both the SFR area and the GPR area. The GPR area is banked to allow greater than 116 bytes of general purpose RAM. The banked areas of the SFR are for the registers that control the peripheral functions. Banking requires the use of control bits for bank selection. These control bits are located in the STATUS Register. Figure 2-2 shows the data memory map organization. Instructions MOVWF and MOVF can move values from the W register to any location in the register file (“F”), and vice-versa. The entire data memory can be accessed either directly using the absolute address of each register file or indirectly through the File Select Register (FSR) (Section 2.5). Indirect addressing uses the present value of the RP0 bit for access into the banked areas of data memory. Data memory is partitioned into two banks which contain the general purpose registers and the special function registers. Bank 0 is selected by clearing the RP0 bit (STATUS<5>). Setting the RP0 bit selects Bank 1. Each Bank extends up to 7Fh (128 bytes). The first twelve locations of each Bank are reserved for the Special Function Registers. The remainder are General Purpose Registers, implemented as static RAM. 2.2.1 FIGURE 2-2: REGISTER FILE MAP PIC16F84A File Address File Address 00h Indirect addr.(1) Indirect addr.(1) 80h 01h TMR0 OPTION_REG 81h 02h PCL PCL 82h 03h STATUS STATUS 83h 04h FSR FSR 84h 05h PORTA TRISA 85h 06h PORTB TRISB 86h 07h — — 87h 08h EEDATA EECON1 88h 09h EEADR EECON2(1) 89h 0Ah PCLATH PCLATH 8Ah 0Bh INTCON INTCON 8Bh 8Ch 0Ch 68 General Purpose Registers (SRAM) Mapped (accesses) in Bank 0 4Fh 50h CFh D0h GENERAL PURPOSE REGISTER FILE Each General Purpose Register (GPR) is 8-bits wide and is accessed either directly or indirectly through the FSR (Section 2.5). The GPR addresses in Bank 1 are mapped to addresses in Bank 0. As an example, addressing location 0Ch or 8Ch will access the same GPR. 7Fh FFh Bank 0 Bank 1 Unimplemented data memory location, read as ’0’. Note 1: Not a physical register. Selección de Bancos Para seleccionar el banco con el cual queremos trabajar tendremos que agregar la siguiente instrucción. banksel NombreDelRegistro Registro: W El registro de trabajo W (Working Register) es un registro de 8 bits que participa en la mayorı́a de las instrucciones. Es uno de los registro más importantes del microcontrolador. Puede ser accedido tanto para lectura como para escritura. instructions for examples. Furthermore, the TO and PD bits are not writable. Therefore, the result of an instruction with the STATUS register as destination may be different than intended. 3: When the STATUS register is the destination for an instruction that affects the Z, DC or C bits, then the write to these three bits is disabled. The specified bit(s) will be updated according to device logic For example, CLRF STATUS will clear the upper three bits and set the Z bit. This leaves the STATUS register as 000u u1uu (where u = unchanged). Registro: STATUS Only the BCF, BSF, SWAPF and MOVWF instructions should be used to alter the STATUS register (Table 7-2), because these instructions do not affect any status bit. REGISTER 2-1: STATUS REGISTER (ADDRESS 03h, 83h) R/W-0 R/W-0 R/W-0 R-1 R-1 R/W-x R/W-x R/W-x IRP RP1 RP0 TO PD Z DC C bit 7 bit 0 bit 7-6 Unimplemented: Maintain as ‘0’ bit 5 RP0: Register Bank Select bits (used for direct addressing) 01 = Bank 1 (80h - FFh) 00 = Bank 0 (00h - 7Fh) bit 4 TO: Time-out bit 1 = After power-up, CLRWDT instruction, or SLEEP instruction 0 = A WDT time-out occurred bit 3 PD: Power-down bit 1 = After power-up or by the CLRWDT instruction 0 = By execution of the SLEEP instruction bit 2 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 bit 1 DC: Digit carry/borrow bit (ADDWF, ADDLW,SUBLW,SUBWF instructions) (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 bit 0 C: Carry/borrow bit (ADDWF, ADDLW,SUBLW,SUBWF instructions) (for borrow, the polarity is reversed) 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: A subtraction is executed by adding the two’s 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. Legend: DS35007B-page 8 R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’ - n = Value at POR ’1’ = Bit is set ’0’ = Bit is cleared x = Bit is unknown 2001 Microchip Technology Inc. Puertos de Entrada/Salida Los PICs tienen la caracterı́stica de que sus pines pueden ser todos configurados de acuerdo a la necesidad de la aplicación, es decir, que los pines de un mismo puerto pueden ser usados como entradas o como salidas. Los PIC 16F84 cuentan con tres puertos de E/S. Puerto A: Puerto bidireccional de 5 bits. Puerto B: Puerto bidireccional de 8 bits. Registros TRIS Se utilizan para configurar los pines del PORTA y PORTB como Entradas o Salidas. Cada bit se corresponde con un pin del PORTA/PORTB Donde se escribe un 1 el pin correspondiente sera entrada. Donde se escribe un 0 el pin correspondiente sera salida. Set de instrucciones PIC16F84A TABLE 7-2: PIC16CXXX INSTRUCTION SET Mnemonic, Operands 14-Bit Opcode Description Cycles MSb LSb Status 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 BCF BSF BTFSC BTFSS f, b f, b f, b f, b Bit Clear f Bit Set f Bit Test f, Skip if Clear Bit Test f, Skip if Set 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 dfff dfff lfff 0xxx dfff dfff dfff dfff dfff dfff dfff lfff 0xx0 dfff dfff dfff dfff dfff ffff ffff ffff xxxx ffff ffff ffff ffff ffff ffff ffff ffff 0000 ffff ffff ffff ffff ffff 00bb 01bb 10bb 11bb bfff bfff bfff bfff ffff ffff ffff ffff 111x 1001 0kkk 0000 1kkk 1000 00xx 0000 01xx 0000 0000 110x 1010 kkkk kkkk kkkk 0110 kkkk kkkk kkkk 0000 kkkk 0000 0110 kkkk kkkk kkkk kkkk kkkk 0100 kkkk kkkk kkkk 1001 kkkk 1000 0011 kkkk kkkk C,DC,Z Z Z Z Z Z Z Z Z C C C,DC,Z Z 1,2 1,2 2 1,2 1,2 1,2,3 1,2 1,2,3 1,2 1,2 1,2 1,2 1,2 1,2 1,2 BIT-ORIENTED FILE REGISTER OPERATIONS 1 1 1 (2) 1 (2) 01 01 01 01 1,2 1,2 3 3 LITERAL AND CONTROL OPERATIONS ADDLW ANDLW CALL CLRWDT GOTO IORLW MOVLW RETFIE RETLW RETURN SLEEP SUBLW XORLW k k k k k k k k k 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 2 1 2 1 1 2 2 2 1 1 1 11 11 10 00 10 11 11 00 11 00 00 11 11 C,DC,Z Z TO,PD Z TO,PD C,DC,Z Z Note 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’. Configuración Inicial config CP OFF & XT OSC & WDT OFF & PWRTE OFF OSC: Controla el modo de oscilación que usará el PIC para funcionar. WDT: (Watchdog Timer) Es una capacidad del microcontrolador de autoresetearse PWRTE: Permite generar un retardo en la inicialización del PIC. CP: Protección de código. Permite que el mismo no pueda ser leı́do por otra persona. Declaración de constantes LED equ 0x00 PULSADOR equ 0x01 Declaración de variables Es necesario chequear cuales son las posiciones de memoria que tenemos disponibles para almacenar nuestras variables. Banco 0: 0x0C hasta 0x4F Banco 1: 0x8C hasta 0xCF LED STATUS equ 0x20 MPLab Descarga: www.microchip.com MPLab - Primeros pasos Project → Project Wizard Next... Device: 16F84A Toolsuite: Microchip MPASM Toolsuite Create New Project File Next... Finish... Project → Add Files to Project Probemoslo con ejemplo1.asm MPLab Ejemplo 1 Controlar mediante un pulsador conectado RB0, el estado de 6 leds conectados en el PORTB. Ejemplo 2 Encender y apagar 6 Leds conectados al PORTB con retardo. Ejercicio Parte 1 Alternar el encendido entre dos leds conectados a RB1 y RB2 con un retardo igual al del ejemplo 1. Ejercicio Parte 1 Alternar el encendido entre dos leds conectados a RB1 y RB2 con un retardo igual al del ejemplo 1. Parte 2 Deshabilitar o habilitar el sistema mediante un pulsador conectado a RB0. ¿Consultas?