ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 12.06.2025

Просмотров: 1403

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

Chapter 2 - Microcontroller PIC16F84

Previous page

Table of contents

Chapter overview

Next page

2.4 Ports

Port refers to a group of pins on a microcontroller which can be accessed simultaneously, or on which we can set the desired combination of zeros and ones, or read from them an existing status. Physically, port is a register inside a microcontroller which is connected by wires to the pins of a microcontroller. Ports represent physical connection of Central Processing Unit with an outside world. Microcontroller uses them in order to monitor or control other components or devices. Due to functionality, some pins have twofold roles like PA4/TOCKI for instance, which is simultaneously the fourth bit of port A and an external input for free-run counter. Selection of one of these two pin functions is done in one of the configurational registers. An illustration of this is the fifth bit T0CS in OPTION register. By selecting one of the functions the other one is disabled.

All port pins can be defined as input or output, according to the needs of a device that's being developed. In order to define a pin as input or output pin, the right combination of zeros and ones must be written in TRIS register. If at the appropriate place in TRIS register a logical "1" is written, then that pin is an input pin, and if the opposite is true, it's an output pin. Every port has its proper TRIS register. Thus, port A has TRISA at address 85h, and port B has TRISB at address 86h.

PORTB

PORTB has 8 pins joined to it. The appropriate register for direction of data is TRISB at address 86h. Setting a bit in TRISB register defines the corresponding port pin as an input pin, and resetting a bit in TRISB register defines the corresponding port pin as the output pin. Each pin on PORTB has a weak internal pull-up resistor (resistor which defines a line to logic one) which can be activated by resetting the seventh bit RBPU in OPTION register. These 'pull-up' resistors are automatically being turned off when port pin is configured as an output. When a microcontroller is started, pull-up's are disabled.

Four pins PORTB, RB7:RB4 can cause an interrupt which occurs when their status changes from

http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_05Poglavlje.htm (1 of 2) [4/2/2003 16:17:47]

Chapter 2 - Microcontroller PIC16F84

logical one into logical zero and opposite. Only pins configured as input can cause this interrupt to occur (if any RB7:RB4 pin is configured as an output, an interrupt won't be generated at the change of status.) This interrupt option along with internal pull-up resistors makes it easier to solve common problems we find in practice like for instance that of matrix keyboard. If rows on the keyboard are connected to these pins, each push on a key will then cause an interrupt. A microcontroller will determine which key is at hand while processing an interrupt It is not recommended to refer to port B at the same time that interrupt is being processed.

The above example shows how pins 0, 1, 2, and 3 are declared for input, and pins 4, 5, 6, and 7 for output.

PORTA

PORTA has 5 pins joined to it. The corresponding register for data direction is TRISA at address 85h. Like with port B, setting a bit in TRISA register defines also the corresponding port pin as an input pin, and clearing a bit in TRISA register defines the corresponding port pin as an output pin. The fifth pin of port A has dual function. On that pin is also situated an external input for timer TMR0. One of these two options is chosen by setting or resetting the T0CS bit (TMR0 Clock Source Select bit). This pin enables the timer TMR0 to increase its status either from internal oscillator or via external impulses on RA4/T0CKI pin.

Example shows how pins 0, 1, 2, 3, and 4 are declared to be input, and pins 5, 6, and 7 to be output pins.

Previous page

Table of contents

Chapter overview

Next page

© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.

http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_05Poglavlje.htm (2 of 2) [4/2/2003 16:17:47]


Chapter 2 - Microcontroller PIC16F84

Previous page

Table of contents

Chapter overview

Next page

2.5 Memory organization

PIC16F84 has two separate memory blocks, one for data and the other for program. EEPROM memory and GPR registers in RAM memory make up a data block, and FLASH memory makes up a program block.

Program memory

Program memory has been realized in FLASH technology which makes it possible to program a microcontroller many times before it's installed into a device, and even after its installment if eventual changes in program or process parameters should occur. The size of program memory is 1024 locations with 14 bits width where locations zero and four are reserved for reset and interrupt vector.

Data memory

Data memory consists of EEPROM and RAM memories. EEPROM memory consists of 64 eight bit locations whose contents is not lost during loosing of power supply. EEPROM is not directly addressible, but is accessed indirectly through EEADR and EEDATA registers. As EEPROM memory usually serves for storing important parameters (for example, of a given temperature in temperature regulators) , there is a strict procedure for writing in EEPROM which must be followed in order to avoid accidental writing. RAM memory for data occupies space on a memory map from location 0x0C to 0x4F which comes to 68 locations. Locations of RAM memory are also called GPR registers which is an abbreviation for General Purpose Registers. GPR registers can be accessed regardless of which bank is selected at the moment.

SFR registers

Registers which take up first 12 locations in banks 0 and 1 are registers of specialized function assigned with certain blocks of the microcontroller. These are called Special Function Registers.

http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_06Poglavlje.htm (1 of 5) [4/2/2003 16:17:50]

Chapter 2 - Microcontroller PIC16F84

Memory Banks

Beside this 'length' division to SFR and GPR registers, memory map is also divided in 'width' (see preceding map) to two areas called 'banks'. Selecting one of the banks is done via RP0 and RP1 bits in STATUS register.

Example:

bcf STATUS, RP0

Instruction BCF clears bit RP0 (RP0=0) in STATUS register and thus sets up bank 0.

http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_06Poglavlje.htm (2 of 5) [4/2/2003 16:17:50]

Chapter 2 - Microcontroller PIC16F84

bsf STATUS, RP0

Instruction BSF sets the bit RP0 (RP0=1) in STATUS register and thus sets up bank1.

Usually, groups of instructions that are often in use, are connected into one unit which can easily be recalled in a program, and whose name has a clear meaning, so called Macros. With their use, selection between two banks becomes more clear and the program itself more legible.

BANK0 macro

;Select memory bank 0

Bcf STATUS, RP0

Endm

BANK1 macro

;Select memory bank 1

Bsf STATUS, RP0

Endm

Locations 0Ch - 4Fh are general purpose registers (GPR) which are used as RAM memory. When locations 8Ch - CFh in Bank 1 are accessed, we actually access the exact same locations in Bank 0. In other words , whenever you wish to access one of the GPR registers, there is no need to worry about which bank we are in!

Program Counter

Program counter (PC) is a 13 bit register that contains the address of the instruction being executed. By its incrementing or change (ex. in case of jumps) microcontroller executes program instructions step-by-step.

Stack

PIC16F84 has a 13-bit stack with 8 levels, or in other words, a group of 8 memory locations of 13 - bits width with special function. Its basic role is to keep the value of program counter after a jump from the main program to an address of a subprogram . In order for a program to know how to go back to the point where it started from, it has to return the value of a program counter from a stack. When moving from a program to a subprogram, program counter is being pushed onto a stack (example of this is CALL instruction). When executing instructions such as RETURN, RETLW or RETFIE which were executed at the end of a subprogram, program counter was taken from a stack so that program could continue where was stopped before it was interrupted. These operations of placing on and taking off from a program counter stack are called PUSH and POP, and are named according similar instructions on some bigger microcontrollers.

In System Programming

In order to program a program memory, microcontroller must be set to special working mode by bringing up MCLR pin to 13.5V, and supply voltage Vdd has to be stabilized between 4.5V to 5.5V. Program memory can be programmed serially using two 'data/clock' pins which must previously be separated from device lines, so that errors wouldn't come up during programming.

Addressing modes

RAM memory locations can be accessed directly or indirectly.

Direct Addressing

Direct Addressing is done through a 9-bit address. This address is obtained by connecting 7th bit of direct address of an instruction with two bits (RP1, RP0) from STATUS register as is shown on the following picture. Any access to SFR registers can be an example of direct addressing.

http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_06Poglavlje.htm (3 of 5) [4/2/2003 16:17:50]


Chapter 2 - Microcontroller PIC16F84

Bsf STATUS, RP0 ;Bankl

movlw

0xFF

;w=0xFF

movwf

TRISA

;address of TRISA register is taken from

;instruction movwf

Direct addressing

Indirect Addressing

Indirect unlike direct addressing does not take an address from an instruction but makes it with the help of IRP bit of STATUS and FSR registers. Addressed location is accessed via INDF register which in fact holds the address indicated by a FSR. In other words, any instruction which uses INDF as its register in reality accesses data indicated by a FSR register. Let's say, for instance, that one general purpose register (GPR) at address 0Fh contains a value of 20. By writing a value of 0Fh in FSR register we will get a register indicator at address 0Fh, and by reading from INDF register, we will get a value of 20, which means that we have read from the first register its value without accessing it directly (but via FSR and INDF). It appears that this type of addressing does not have any advantages over direct addressing, but certain needs do exist during programming which can be solved smoothly only through indirect addressing.

http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_06Poglavlje.htm (4 of 5) [4/2/2003 16:17:50]

Chapter 2 - Microcontroller PIC16F84

An of such example can be sending a set of data via serial communication, working with buffers and indicators (which will be discussed further in a chapter with examples), or erasing a part of RAM memory (16 locations) as in the following instance.

Reading data from INDF register when the contents of FSR register is equal to zero returns the value of zero, and writing to it results in NOP operation (no operation).

Previous page

Table of contents

Chapter overview

Next page

© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.

http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_06Poglavlje.htm (5 of 5) [4/2/2003 16:17:50]


Chapter 2 - Microcontroller PIC16F84

Previous page

Table of contents

Chapter overview

Next page

2.6 Interrupts

Interrupts are a mechanism of a microcontroller which enables it to respond to some events at the moment when they occur, regardless of what microcontroller is doing at the time. This is a very important part, because it provides connection between a microcontroller and environment which surrounds it. Generally, each interrupt changes the program flow, interrupts it and after executing an interrupt subprogram (interrupt routine) it continues from that same point on.

One of the possible sources of an interrupt and how it affects the main program

Control register of an interrupt is called INTCON and is found at 0Bh address. Its role is to allow or disallowed interrupts, and in case they are not allowed, it registers single interrupt requests through its own bits.

INTCON Register

http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_07Poglavlje.htm (1 of 7) [4/2/2003 16:17:55]

Chapter 2 - Microcontroller PIC16F84

bit 0 RBIF (RB Port Change Interrupt Flag bit) Bit which informs about changes on pins 4, 5, 6 and 7 of port B.

1=at least one pin has changed its status 0=no change occured on any of the pins

bit 1 INTF (INT External Interrupt Flag bit) External interrupt occured. 1=interrupt occured

0=interrupt did not occur

If a rising or falling edge was detected on pin RB0/INT, (which is defined with bit INTEDG in OPTION register), bit INTF is set. Bit must be cleared in interrupt subprogram in order to detect the next interrupt.

bit 2 T0IF (TMR0 Overflow Interrupt Flag bit) Overflow of counter TMR0. 1= counter changed its status from FFh to 00h

0=overflow did not occur

Bit must be cleared in program in order for an interrupt to be detected.

bit 3 RBIE (RB port change Interrupt Enable bit) Enables interrupts to occur at the change of status of pins 4, 5, 6, and 7 of port B.

1= enables interrupts at the change of status 0=interrupts disabled at the change of status

If RBIE and RBIF were simultaneously set, an interrupt would occur.

bit 4 INTE (INT External Interrupt Enable bit) Bit which enables external interrupt from pin RB0/INT. 1=external interrupt enabled

0=external interrupt disabled

If INTE and INTF were set simultaneously, an interrupt would occur.

bit 5 T0IE (TMR0 Overflow Interrupt Enable bit) Bit which enables interrupts during counter TMR0 overflow.

1=interrupt enabled 0=interrupt disabled

If T0IE and T0IF were set simultaneously, interrupt would occur.

Bit 6 EEIE (EEPROM Write Complete Interrupt Enable bit) Bit which enables an interrupt at the end of a writing routine to EEPROM

1=interrupt enabled 0=interrupt disabled

If EEIE and EEIF (which is in EECON1 register) were set simultaneously , an interrupt would occur.

Bit 7 GIE (Global Interrupt Enable bit) Bit which enables or disables all interrupts. 1=all interrupts are enabled

0=all interrupts are disabled

PIC16F84 has four interrupt sources:

1.Termination of writing data to EEPROM

2.TMR0 interrupt caused by timer overflow

3.Interrupt during alteration on RB4, RB5, RB6 and RB7 pins of port B.

4.External interrupt from RB0/INT pin of microcontroller

http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_07Poglavlje.htm (2 of 7) [4/2/2003 16:17:55]


Chapter 2 - Microcontroller PIC16F84

Generally speaking, each interrupt source has two bits joined to it. One enables interrupts, and the other detects when interrupts occur. There is one common bit called GIE which can be used to disallow or enable all interrupts simultaneously. This bit is very useful when writing a program because it allows for all interrupts to be disabled for a period of time, so that execution of some important part of a program would not be interrupted. When instruction which resets GIE bit was executed (GIE=0, all interrupts disallowed), any interrupt that remained unsolved should be ignored.

Interrupts which remained unsolved and were ignored, are processed when GIE bit (GIE=1, all interrupts allowed) would be cleared. When interrupt was answered, GIE bit was cleared so that any additional interrupts would be disabled, return address was pushed onto stack and address 0004h was written in program counter - only after this does replying to an interrupt begin! After interrupt is processed, bit whose setting caused an interrupt must be cleared, or interrupt routine would automatically be processed over again during a return to the main program.

Keeping the contents of important registers

Only return value of program counter is stored on a stack during an interrupt (by return value of program counter we mean the address of the instruction which was to be executed, but wasn't because interrupt occured). Keeping only the value of program counter is often not enough. Some registers which are already in use in the main program can also be in use in interrupt routine. If they were not retained, main program would during a return from an interrupt routine get completely different values in those registers, which would cause an error in the program. One example for such a case is contents of the work register W. If we suppose that main program was using work register W for some of its operations, and if it had stored in it some value that's important for the following instruction, then an interrupt which occurs before that instruction would change the value of work register W which would directly be influenced the main program.

Procedure of recording important registers before going to an interrupt routine is called PUSH, while the procedure which brings recorded values back, is called POP. PUSH and POP are instructions with some other microcontrollers (Intel), but are so widely accepted that a whole operation is named after them. PIC16F84 does not have instructions like PUSH and POP, and they have to be programmed.

http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_07Poglavlje.htm (3 of 7) [4/2/2003 16:17:55]

Chapter 2 - Microcontroller PIC16F84

One of the possible cases of errors if saving was not done when going to a subprogram of an interrupt

Due to simplicity and frequent usage, these parts of the program can be made as macros. The concept of a Macro is explained in "Program assembly language". In the following example, contents of W and STATUS registers are stored in W_TEMP and STATUS_TEMP variables prior to interrupt routine. At the beginning of PUSH routine we need to check presently selected bank because W_TEMP and STATUS_TEMP are found in bank 0. For exchange of data between these registers, SWAPF instruction is used instead of MOVF because it does not affect the status of STATUS register bits.

Example is a program assembler for following steps:

1.Testing the current bank

2.Storing W register regardless of the current bank

3.Storing STATUS register in bank 0.

4.Executing interrupt routine for interrupt processing (ISR)

5.Restores STATUS register

6.Restores W register

If there are some more variables or registers that need to be stored, then they need to be kept after storing STATUS register (step 3), and brought back before STATUS register is restored (step 5).

http://www.mikroelektronika.co.yu/english/product/books/PICbook/2_07Poglavlje.htm (4 of 7) [4/2/2003 16:17:55]