fundamentos taller de interrupciones - dc.uba.ar · fundamentos taller de interrupciones ......

24
Fundamentos Taller de interrupciones Francisco Garc´ ıa Eij´ o Segundo Cuatrimestre de 2011 - Organizaci´ on del Computador I

Upload: doanhuong

Post on 21-Aug-2018

240 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Fundamentos Taller de interrupciones - dc.uba.ar · Fundamentos Taller de interrupciones ... Organizaci on del Computador I. ... Eight-level deep hardware stack

Fundamentos Taller de interrupciones

Francisco Garcıa Eijo

Segundo Cuatrimestre de 2011 - Organizacion del Computador I

Page 2: Fundamentos Taller de interrupciones - dc.uba.ar · Fundamentos Taller de interrupciones ... Organizaci on del Computador I. ... Eight-level deep hardware stack

El PIC 16F84

El PIC16F84 es un microcontrolador de la empresa Microchip.Cuenta con las siguientes caracterısticas:

Caracterıstica DescripcionArquitectura Harvard

Frecuencia de operacion 20 MHz

Memoria FLASH de programa 1024 words

Memoria de datos 68 Bytes

Memoria de datos EEPROM 64 Bytes

Interrupciones 4

Puertos de E/S 2 (A,B)

Conjunto de Instrucciones 35

Page 3: Fundamentos Taller de interrupciones - dc.uba.ar · Fundamentos Taller de interrupciones ... Organizaci on del Computador I. ... Eight-level deep hardware stack

Encapsulado

2001 Microchip Technology Inc. DS35007B-page 1

M PIC16F84A

High Performance RISC CPU Features:

• Only 35 single word instructions to learn

• All instructions single-cycle except for program branches which are two-cycle

• Operating speed: DC - 20 MHz clock inputDC - 200 ns instruction cycle

• 1024 words of program memory• 68 bytes of Data RAM• 64 bytes of Data EEPROM

• 14-bit wide instruction words• 8-bit wide data bytes• 15 Special Function Hardware registers

• Eight-level deep hardware stack• Direct, indirect and relative addressing modes • Four interrupt sources:

- External RB0/INT pin- TMR0 timer overflow - PORTB<7:4> interrupt-on-change

- Data EEPROM write complete

Peripheral Features:

• 13 I/O pins with individual direction control• High current sink/source for direct LED drive

- 25 mA sink max. per pin

- 25 mA source max. per pin• TMR0: 8-bit timer/counter with 8-bit

programmable prescaler

Special Microcontroller Features:

• 10,000 erase/write cycles Enhanced FLASH Program memory typical

• 10,000,000 typical erase/write cycles EEPROM Data memory typical

• EEPROM Data Retention > 40 years• In-Circuit Serial Programming™ (ICSP™) - via

two pins• Power-on Reset (POR), Power-up Timer (PWRT),

Oscillator Start-up Timer (OST)• Watchdog Timer (WDT) with its own On-Chip RC

Oscillator for reliable operation• Code protection• Power saving SLEEP mode

• Selectable oscillator options

Pin Diagrams

CMOS Enhanced FLASH/EEPROM Technology:

• Low power, high speed technology

• Fully static design• Wide operating voltage range:

- Commercial: 2.0V to 5.5V

- Industrial: 2.0V to 5.5V• Low power consumption:

- < 2 mA typical @ 5V, 4 MHz

- 15 µA typical @ 2V, 32 kHz- < 0.5 µA typical standby current @ 2V

RA1

RA0

OSC1/CLKIN

OSC2/CLKOUT

VDD

RB7

RB6

RB5

RB4

RA2

RA3

RA4/T0CKI

MCLR

VSS

RB0/INT

RB1

RB2

RB3

•1

2

3

4

5

6

7

8

9

18

17

16

15

14

13

12

11

10

PDIP, SOIC

PIC

16F84A

RA1

RA0

OSC1/CLKIN

OSC2/CLKOUT

VDD

RB7

RB6

RB5

RB4

RA2

RA3

RA4/T0CKI

MCLR

VSS

RB0/INT

RB1

RB2

RB3

•1

2

3

4

5

6

7

8

9

20

19

18

17

16

15

14

13

12

SSOP

PIC

16F84A

10 11

VSS

VDD

18-pin Enhanced FLASH/EEPROM 8-Bit Microcontroller

Page 4: Fundamentos Taller de interrupciones - dc.uba.ar · Fundamentos Taller de interrupciones ... Organizaci on del Computador I. ... Eight-level deep hardware stack

El PIC 16F84 internamente

2001 Microchip Technology Inc. DS35007B-page 3

PIC16F84A

1.0 DEVICE OVERVIEW

This document contains device specific information forthe operation of the PIC16F84A device. Additionalinformation may be found in the PICmicro™ Mid-Range Reference Manual, (DS33023), which may bedownloaded from the Microchip website. The Refer-ence Manual should be considered a complementarydocument to this data sheet, and is highly recom-mended reading for a better understanding of thedevice architecture and operation of the peripheralmodules.

The PIC16F84A belongs to the mid-range family of thePICmicro® microcontroller devices. A block diagram ofthe device is shown in Figure 1-1.

The program memory contains 1K words, which trans-lates to 1024 instructions, since each 14-bit programmemory word is the same width as each device instruc-tion. The data memory (RAM) contains 68 bytes. DataEEPROM is 64 bytes.

There are also 13 I/O pins that are user-configured ona pin-to-pin basis. Some pins are multiplexed with otherdevice functions. These functions include:

• External interrupt• Change on PORTB interrupt

• Timer0 clock input

Table 1-1 details the pinout of the device with descrip-tions and details for each pin.

FIGURE 1-1: PIC16F84A BLOCK DIAGRAM

FLASHProgramMemory

Program Counter13

ProgramBus

Instruction Register

8 Level Stack(13-bit)

Direct Addr

8

InstructionDecode &

Control

TimingGeneration

OSC2/CLKOUTOSC1/CLKIN

Power-upTimer

OscillatorStart-up Timer

Power-onReset

WatchdogTimer

MCLR VDD, VSS

W reg

ALU

MUXI/O Ports

TMR0

STATUS reg

FSR reg

IndirectAddr

RA3:RA0

RB7:RB1

RA4/T0CKI

EEADR

EEPROMData Memory

64 x 8EEDATA

Addr Mux

RAM Addr

RAMFile Registers

EEPROM Data Memory

Data Bus

5

7

7

RB0/INT

14

8

8

1K x 14

68 x 8

Page 5: Fundamentos Taller de interrupciones - dc.uba.ar · Fundamentos Taller de interrupciones ... Organizaci on del Computador I. ... Eight-level deep hardware stack

Organizacion de la memoria

En este PIC la memoria esta divida en:

Memoria FLASH de programa.

Memoria RAM de datos.

Memoria EEPROM de datos.

Page 6: Fundamentos Taller de interrupciones - dc.uba.ar · Fundamentos Taller de interrupciones ... Organizaci on del Computador I. ... Eight-level deep hardware stack

Memoria de Programa

2001 Microchip Technology Inc. DS35007B-page 5

PIC16F84A

2.0 MEMORY ORGANIZATION

There are two memory blocks in the PIC16F84A.These are the program memory and the data memory.Each block has its own bus, so that access to eachblock can occur during the same oscillator cycle.

The data memory can further be broken down into thegeneral purpose RAM and the Special FunctionRegisters (SFRs). The operation of the SFRs thatcontrol the “core” are described here. The SFRs usedto control the peripheral modules are described in thesection discussing each individual peripheral module.

The data memory area also contains the dataEEPROM memory. This memory is not directly mappedinto the data memory, but is indirectly mapped. That is,an indirect address pointer specifies the address of thedata EEPROM memory to read/write. The 64 bytes ofdata EEPROM memory have the address range0h-3Fh. More details on the EEPROM memory can befound in Section 3.0.

Additional information on device memory may be foundin the PICmicro™ Mid-Range Reference Manual,(DS33023).

2.1 Program Memory Organization

The PIC16FXX has a 13-bit program counter capableof addressing an 8K x 14 program memory space. Forthe PIC16F84A, the first 1K x 14 (0000h-03FFh) arephysically implemented (Figure 2-1). Accessing a loca-tion above the physically implemented address willcause a wraparound. For example, for locations 20h,420h, 820h, C20h, 1020h, 1420h, 1820h, and 1C20h,the instruction will be the same.

The RESET vector is at 0000h and the interrupt vectoris at 0004h.

FIGURE 2-1: PROGRAM MEMORY MAP AND STACK - PIC16F84A

PC<12:0>

Stack Level 1•

Stack Level 8

RESET Vector

Peripheral Interrupt Vector

••

Use

r M

emor

yS

pace

CALL, RETURNRETFIE, RETLW

13

0000h

0004h

1FFFh

3FFh

Page 7: Fundamentos Taller de interrupciones - dc.uba.ar · Fundamentos Taller de interrupciones ... Organizaci on del Computador I. ... Eight-level deep hardware stack

Memoria de DatosPIC16F84A

DS35007B-page 6 2001 Microchip Technology Inc.

2.2 Data Memory Organization

The data memory is partitioned into two areas. The firstis the Special Function Registers (SFR) area, while thesecond is the General Purpose Registers (GPR) area.The SFRs control the operation of the device.

Portions of data memory are banked. This is for boththe SFR area and the GPR area. The GPR area isbanked to allow greater than 116 bytes of generalpurpose RAM. The banked areas of the SFR are for theregisters that control the peripheral functions. Bankingrequires 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 fromthe W register to any location in the register file (“F”),and vice-versa.

The entire data memory can be accessed eitherdirectly using the absolute address of each register fileor indirectly through the File Select Register (FSR)(Section 2.5). Indirect addressing uses the presentvalue of the RP0 bit for access into the banked areas ofdata memory.

Data memory is partitioned into two banks whichcontain the general purpose registers and the specialfunction registers. Bank 0 is selected by clearing theRP0 bit (STATUS<5>). Setting the RP0 bit selects Bank1. Each Bank extends up to 7Fh (128 bytes). The firsttwelve locations of each Bank are reserved for theSpecial Function Registers. The remainder are Gen-eral Purpose Registers, implemented as static RAM.

2.2.1 GENERAL PURPOSE REGISTER FILE

Each General Purpose Register (GPR) is 8-bits wideand is accessed either directly or indirectly through theFSR (Section 2.5).

The GPR addresses in Bank 1 are mapped toaddresses in Bank 0. As an example, addressing loca-tion 0Ch or 8Ch will access the same GPR.

FIGURE 2-2: REGISTER FILE MAP - PIC16F84A

File Address

00h

01h

02h

03h

04h

05h

06h

07h

08h

09h

0Ah

0Bh

0Ch

7Fh

80h

81h

82h

83h

84h

85h

86h

87h

88h

89h

8Ah

8Bh

8Ch

FFhBank 0 Bank 1

Indirect addr.(1) Indirect addr.(1)

TMR0 OPTION_REG

PCL

STATUS

FSR

PORTA

PORTB

EEDATA

EEADR

PCLATH

INTCON

68 GeneralPurposeRegisters(SRAM)

PCL

STATUS

FSR

TRISA

TRISB

EECON1

EECON2(1)

PCLATH

INTCON

Mapped

in Bank 0

Unimplemented data memory location, read as ’0’.

File Address

Note 1: Not a physical register.

CFhD0h

4Fh50h

(accesses)

— —

Page 8: Fundamentos Taller de interrupciones - dc.uba.ar · Fundamentos Taller de interrupciones ... Organizaci on del Computador I. ... Eight-level deep hardware stack

Seleccion de Bancos

Para seleccionar el banco con el cual queremos trabajar tendremosque agregar la siguiente instruccion.

banksel NombreDelRegistro

Page 9: Fundamentos Taller de interrupciones - dc.uba.ar · Fundamentos Taller de interrupciones ... Organizaci on del Computador I. ... Eight-level deep hardware stack

Registro: W

El registro de trabajo W (Working Register) es un registro de8 bits que participa en la mayorıa de las instrucciones.

Es uno de los registro mas importantes del microcontrolador.

Puede ser accedido tanto para lectura como para escritura.

Page 10: Fundamentos Taller de interrupciones - dc.uba.ar · Fundamentos Taller de interrupciones ... Organizaci on del Computador I. ... Eight-level deep hardware stack

Registro: STATUS

PIC16F84A

DS35007B-page 8 2001 Microchip Technology Inc.

2.3.1 STATUS REGISTER

The STATUS register contains the arithmetic status ofthe ALU, the RESET status and the bank select bit fordata memory.

As with any register, the STATUS register can be thedestination for any instruction. If the STATUS register isthe destination for an instruction that affects the Z, DCor C bits, then the write to these three bits is disabled.These bits are set or cleared according to device logic.Furthermore, the TO and PD bits are not writable.Therefore, the result of an instruction with the STATUSregister as destination may be different than intended.

For example, CLRF STATUS will clear the upper threebits and set the Z bit. This leaves the STATUS registeras 000u u1uu (where u = unchanged).

Only the BCF, BSF, SWAPF and MOVWF instructionsshould 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)

Note 1: The IRP and RP1 bits (STATUS<7:6>)are not used by the PIC16F84A andshould be programmed as cleared. Use ofthese bits as general purpose R/W bits isNOT recommended, since this may affectupward compatibility with future products.

2: The C and DC bits operate as a borrowand digit borrow out bit, respectively, insubtraction. See the SUBLW and SUBWFinstructions for examples.

3: When the STATUS register is thedestination for an instruction that affectsthe Z, DC or C bits, then the write to thesethree bits is disabled. The specified bit(s)will be updated according to device logic

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 bit1 = After power-up, CLRWDT instruction, or SLEEP instruction0 = A WDT time-out occurred

bit 3 PD: Power-down bit1 = After power-up or by the CLRWDT instruction0 = By execution of the SLEEP instruction

bit 2 Z: Zero bit1 = The result of an arithmetic or logic operation is zero0 = 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 polarityis reversed)1 = A carry-out from the 4th low order bit of the result occurred0 = 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 isreversed)1 = A carry-out from the Most Significant bit of the result occurred0 = 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 orderbit of the source register.

Legend:

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

Page 11: Fundamentos Taller de interrupciones - dc.uba.ar · Fundamentos Taller de interrupciones ... Organizaci on del Computador I. ... Eight-level deep hardware stack

Puertos de Entrada/Salida

Los PICs tienen la caracterıstica de que sus pines pueden ser todosconfigurados de acuerdo a la necesidad de la aplicacion, es decir,que los pines de un mismo puerto pueden ser usados comoentradas 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.

Page 12: Fundamentos Taller de interrupciones - dc.uba.ar · Fundamentos Taller de interrupciones ... Organizaci on del Computador I. ... Eight-level deep hardware stack

Registros TRIS

Se utilizan para configurar los pines del PORTA y PORTBcomo 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.

Page 13: Fundamentos Taller de interrupciones - dc.uba.ar · Fundamentos Taller de interrupciones ... Organizaci on del Computador I. ... Eight-level deep hardware stack

Set de instruccionesPIC16F84A

DS35007B-page 36 2000 Microchip Technology Inc.

TABLE 7-2: PIC16CXXX INSTRUCTION SET

Mnemonic,Operands

Description Cycles14-Bit Opcode Status

AffectedNotes

MSb LSb

BYTE-ORIENTED FILE REGISTER OPERATIONS

ADDWFANDWFCLRFCLRWCOMFDECFDECFSZINCFINCFSZIORWFMOVFMOVWFNOPRLFRRFSUBWFSWAPFXORWF

f, df, d

f-

f, df, df, df, df, df, df, d

f-

f, df, df, df, df, d

Add W and fAND W with fClear fClear WComplement fDecrement fDecrement f, Skip if 0Increment fIncrement f, Skip if 0Inclusive OR W with fMove fMove W to fNo OperationRotate Left f through CarryRotate Right f through CarrySubtract W from fSwap nibbles in fExclusive OR W with f

111111

1 (2)1

1 (2)111111111

000000000000000000000000000000000000

011101010001000110010011101110101111010010000000000011011100001011100110

dfffdffflfff0xxxdfffdfffdfffdfffdfffdfffdffflfff0xx0dfffdfffdfffdfffdfff

ffffffffffffxxxxffffffffffffffffffffffffffffffff0000ffffffffffffffffffff

C,DC,ZZZZZZ

Z

ZZ

CC

C,DC,Z

Z

1,21,22

1,21,2

1,2,31,2

1,2,31,21,2

1,21,21,21,21,2

BIT-ORIENTED FILE REGISTER OPERATIONS

BCFBSFBTFSCBTFSS

f, bf, bf, bf, b

Bit Clear fBit Set fBit Test f, Skip if ClearBit Test f, Skip if Set

11

1 (2)1 (2)

01010101

00bb01bb10bb11bb

bfffbfffbfffbfff

ffffffffffffffff

1,21,233

LITERAL AND CONTROL OPERATIONS

ADDLWANDLWCALLCLRWDTGOTOIORLWMOVLWRETFIERETLWRETURNSLEEPSUBLWXORLW

kkk-kkk-k--kk

Add literal and WAND literal with WCall subroutineClear Watchdog TimerGo to addressInclusive OR literal with WMove literal to WReturn from interruptReturn with literal in W Return from SubroutineGo into standby modeSubtract W from literalExclusive OR literal with W

1121211222111

11111000101111001100001111

111x10010kkk00001kkk100000xx000001xx00000000110x1010

kkkkkkkkkkkk0110kkkkkkkkkkkk0000kkkk00000110kkkkkkkk

kkkkkkkkkkkk0100kkkkkkkkkkkk1001kkkk10000011kkkkkkkk

C,DC,ZZ

TO,PD

Z

TO,PDC,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’.

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.

Note: Additional information on the mid-range instruction set is available in the PICmicro™ Mid-Range MCUFamily Reference Manual (DS33023).

Page 14: Fundamentos Taller de interrupciones - dc.uba.ar · Fundamentos Taller de interrupciones ... Organizaci on del Computador I. ... Eight-level deep hardware stack

Configuracion Inicial

config CP OFF & XT OSC & WDT OFF & PWRTE OFF

OSC: Controla el modo de oscilacion que usara el PIC parafuncionar.

WDT: (Watchdog Timer) Es una capacidad delmicrocontrolador de autoresetearse

PWRTE: Permite generar un retardo en la inicializacion delPIC.

CP: Proteccion de codigo. Permite que el mismo no pueda serleıdo por otra persona.

Page 15: Fundamentos Taller de interrupciones - dc.uba.ar · Fundamentos Taller de interrupciones ... Organizaci on del Computador I. ... Eight-level deep hardware stack

Declaracion de constantes

LED equ 0x00PULSADOR equ 0x01

Page 16: Fundamentos Taller de interrupciones - dc.uba.ar · Fundamentos Taller de interrupciones ... Organizaci on del Computador I. ... Eight-level deep hardware stack

Declaracion de variables

Es necesario chequear cuales son las posiciones de memoria quetenemos disponibles para almacenar nuestras variables.

Banco 0: 0x0C hasta 0x4F

Banco 1: 0x8C hasta 0xCF

LED STATUS equ 0x20

Page 17: Fundamentos Taller de interrupciones - dc.uba.ar · Fundamentos Taller de interrupciones ... Organizaci on del Computador I. ... Eight-level deep hardware stack

MPLab

Descarga: www.microchip.com

Page 18: Fundamentos Taller de interrupciones - dc.uba.ar · Fundamentos Taller de interrupciones ... Organizaci on del Computador I. ... Eight-level deep hardware stack

MPLab - Primeros pasos

Project → Project Wizard

Next...Device: 16F84AToolsuite: Microchip MPASM ToolsuiteCreate New Project FileNext...Finish...

Project → Add Files to Project

Probemoslo con ejemplo1.asm

Page 19: Fundamentos Taller de interrupciones - dc.uba.ar · Fundamentos Taller de interrupciones ... Organizaci on del Computador I. ... Eight-level deep hardware stack

MPLab

Page 20: Fundamentos Taller de interrupciones - dc.uba.ar · Fundamentos Taller de interrupciones ... Organizaci on del Computador I. ... Eight-level deep hardware stack

Ejemplo 1

Controlar mediante un pulsador conectado RB0, el estado de 6 ledsconectados en el PORTB.

Page 21: Fundamentos Taller de interrupciones - dc.uba.ar · Fundamentos Taller de interrupciones ... Organizaci on del Computador I. ... Eight-level deep hardware stack

Ejemplo 2

Encender y apagar 6 Leds conectados al PORTB con retardo.

Page 22: Fundamentos Taller de interrupciones - dc.uba.ar · Fundamentos Taller de interrupciones ... Organizaci on del Computador I. ... Eight-level deep hardware stack

Ejercicio

Parte 1

Alternar el encendido entre dos leds conectados a RB1 y RB2 conun retardo igual al del ejemplo 1.

Parte 2

Deshabilitar o habilitar el sistema mediante un pulsador conectadoa RB0.

Page 23: Fundamentos Taller de interrupciones - dc.uba.ar · Fundamentos Taller de interrupciones ... Organizaci on del Computador I. ... Eight-level deep hardware stack

Ejercicio

Parte 1

Alternar el encendido entre dos leds conectados a RB1 y RB2 conun retardo igual al del ejemplo 1.

Parte 2

Deshabilitar o habilitar el sistema mediante un pulsador conectadoa RB0.

Page 24: Fundamentos Taller de interrupciones - dc.uba.ar · Fundamentos Taller de interrupciones ... Organizaci on del Computador I. ... Eight-level deep hardware stack

¿Consultas?