02 The PIC16F877 Memory Map and Programing
02 The PIC16F877 Memory Map and Programing
Programming
Data RAM Organisation
The data RAM address space is partitioned into 4 banks of 128 bytes (known as file registers) each:
Addresses 00 to 7F form bank 0
Addresses 80 to FF form bank 1
Addresses 100 to 17F form bank 2
Addresses 180 to 1FF form bank 3
File register addresses are 9 bits wide. Each file register can be accessed using direct or indirect addressing:
In direct addressing, the instruction supplies a 7-bit address that corresponds to the 7 lowest address
bits. These 7 bits specify the address of the file register within a bank. The two MSBs are supplied by
separate bank selector bits, i.e., the bank selector bits select the bank in which the register is located.
In indirect addressing, the memory pointer supplies an 8-bit address corresponding to the 8 lowest
address bits. The MSB is supplied by a separate bank selector bit. The bank selector bit and the MSB
of the memory pointer select the bank in which the register is located.
7
Figure 2.1 RAM banks in the PIC16F877
8
Core SFR Descriptions
9
T0SE: TMR0 Source Edge Select bit
1 – select timer register to increment on high-to-low transition on RA4/T0CKI pin
0 – selects timer register to increment on low-to-high transition on RA4/T0CKI pin
PSA: Prescaler Assignment bit
1 – assigns prescaler to the watchdog timer (WDT)
0 – assigns prescaler to the Timer0 module
PS2:PS0: Prescaler Rate Select bits as shown in table
10
The PIE1 Register (address 8Ch)
It contains the individual interrupt enable bits for the various peripheral interrupts. An interrupt enable bit is
set to enable the interrupt and cleared to disable the interrupt.
PSPIF: Parallel Slave Port Read/Write Interrupt Flag bit (flags that a write or read has occurred)
ADIF: A/D Converter Interrupt Flag bit (flags A/D conversion has been completed)
RCIF: USART Receive Interrupt Flag bit (flags that the USART buffer is full)
TXIF: USART Transmit Interrupt Flag bit (flags the USART buffer is empty)
SSPIF: Synchronous Serial Port (SSP) Interrupt Flag bit (flags transmission/reception has occurred)
CCP1IF: CCP1 Interrupt Flag bit (flags a capture or a compare match has occurred)
TMR2IF: TMR2 to PR2 Match Interrupt Flag bit (flags a match has occurred)
TMR1IF: TMR1 Overflow Interrupt Flag bit (flags an overflow occurred)
11
The PIR2 (address 0Dh)
It contains the individual interrupt flag bits for the peripheral interrupts. An interrupt flag is set when the
condition causing the interrupt is present and is cleared when it is not absent.
Program Memory
The program memory is divided into four pages of 2k words each.
By design, branch instructions (CALL and GOTO) supply an 11-bit branch offset. This is the offset of the
instruction to an instruction to be branched to from the start of the page. The offset, thus, allows
branching only within a page.
12
The offset becomes the lower 11 bits of the PC. The upper two bits are provided by PCLATH bits 4 and
3. The full address to be branched to is thus, formed from concatenating the 11-bit offset provided by
the instruction with PCLATH bits 4 and 3.
When branching across a 2k boundary, the programmer must select the page by writing to bits 3 and
4 of the PCLATH before the call instruction is executed.
For return instructions, PCLATH and PCL are loaded with whole 13-bit address from stack, hence it is
not necessary to select the page to return to.
PIC16F877 Instructions
The PIC16F877 instructions can perform the following operations: data transfer; arithmetic (add, subtract,
increment, decrement); logic (AND, OR, XOR, NOT); rotates; bit operations (set, clear, test and skip is set, test
and skip if set); program flow (unconditional branch, conditional branch, call, return).
The PIC16F877 has 35 instructions that are all 14-bits wide. The bits are distributed into opcode and operand
bits. The instruction structures are grouped into byte-oriented, bit-oriented, literal and control instructions.
The respective instruction encodings for the instruction groups are shown in figure 2.3.
13
Byte-oriented file Operations
They perform various operations on values in file registers – data transfer, arithmetic, logic and
rotates. In two-operand instructions, one of the operands is always held in the working register. The
destination of the operation is either the working register or a file register.
The file register to be operated on is specified using a direct address (lowest 7 bits). The destination
of the result is specified using a destination specifier bit.
Control Operations
They perform control operations on the PIC. They do not need any operand to be specified and all bits
represent the opcode. The opcode implicitly imply a particular register, bit or operation.
14
Figure 2.4 Instruction set Summary
Instruction Examples
ADDWF 27,1 ; add contents of file register at 27 (decimal) to accumulator, store in file register.
DECF 0x63,0 ; decrement contents of file register at 27 (hex), store result in file register.
MOVWF 0x7E ; move contents of working register into a file register at 7E (hex).
INFSZ 78,0 ; increment contents of file register at 78 (decimal), store result in file register,
; skip next instruction if result is zero.
15
BSF 0x77,5 ; set bit 5 in file register 77 (hex)
BTFSC STATUS,C ; test the carry flag, skip next instruction if carry flag is set.
XORLW 0x5A ; exclusive OR the number 5A (hex) with contents of working register.
CALL 0x2C5 ; call subroutine at address 2C5 (hex).
RETLW 0xC5 ; return with the literal C5 (hex) in the working register.
If file register 51 does not contain a zero, the GOTO is executed causing branching. If it contains a zero, the
GOTO is skipped and execution continues at the instruction following the GOTO hence no branching done.
Delay Program
In software, a delay can be achieved by decrementing a register in a loop until it gets to zero or incrementing
a register in a loop until it gets to some maximum value. Between the point of entry and the point of leaving
the loop, a delay lapses. The duration of the delay is determined by the initial value loaded into the register to
be decremented or incremented. Longer delays may be generated by nesting one-register delay routines.
The program for a delay using two nested dummy decrement loops is:
delay: MOVLW 0xff ; number of times to decrement outer register
MOWVF fr1 ; load into file register
d1: MOVLW 0xff ; number of times to decrement inner register
MOVWF fr2 ; load into file register
d2: DECFSZ fr2,1 ; decrement inner register until zero
GOTO d2
DECFSZ fr1,d1 ; decrement outer register until zero
The program for a delay using increment loops will be seen in a later chapter.
16
Indirect Addressing Program
A program to fill a block of file registers from addresses 0x21 to 0x91 with the value 0x3E will be used to
illustrate indirect addressing. The program that may be used is:
MOVLW 0x21 ; starting address of memory block
MOVWF fsr ; store in file select register
MOVLW 0xaa ; number of locations to be filled with values
MOVWF 0x70 ; store in file register designated as counter
MOVLW 0x3e ; the number to be stored in memory block
loop: MOVWF indf ; effect indirect move
DECFSZ 0x70 ; repeat till all locations filled up
GOTO loop
The approach can be adapted to decode any other values that may have a sequential behaviour like BCD
codes.
Handling Interrupts
There are 15 sources of interrupts and all interrupts cause branching to address 04h.
17
Figure 2.5 summarises the various interrupt sources and also indicates how they may be managed.
Figure 2.5
org 04h
btfsc INTCON,INTF
goto extern ; external interrupt
btfsc INTCON,TMR0IF
goto tim_1 ; timer interrupt
btfsc PIR1.ADIF
goto adconv ; ADC interrupt
btfsc PIR2,EEIF
goto eprom ; EPROM interrupt
bcf INTCON,RBIF ; clear flags for undesired interrupts
clrf PIR1 ; clear flags for
clrf PIR2 ; undesired interrupts
retfie ; return from interrupt code for
; checking interrupts
18
.
The reset vector is at 00h. There are only four locations before running into the interrupt vector. Hence, a
jump is located at 00h so that the main program is located at a place where there is enough space.
Conditional jumps are used within the ISR to branch to a program section that handles the particular interrupt
condition.
19
MOVLW 0x9D
MOVF 0x71,1
MOVLW 0x35
MOVF FSR,1
MOVLW 0x23
SUBWF 0x70,1
RRF 0x71,0
XORW 0x55
MOVF INDF,1
BSF OPTION_REG,RP0
BCF 0x71,2
MOVLW 0x71,0
MOVF 0x35,1
Draw a table showing the contents of the file registers W, 35h, 70h, 71h, 115h and FSR after execution
of each instruction in the program. Also indicate contents of the carry flag as well as the current page
for each instruction.
MOVF PORTD,0
MOVF TEMP0,1
MOVLW 0x33
MOVF TEMP1,1
BSF TEMP1,0
BCF TEMP1,7
MOVF TEMP1,0
SUBLW 0XCF
BTFSS OPTION_REG,C
GOTO NEXT
MOVF TEMP1,0
MOVF PORTC,1
NEXT: CLRF TEMP0
Port D pins have the following voltages from the MS pin to LS pin: 0V, 5V, 0V, 0V, 0V, 0V, 5V, 5V.
Draw a table indicating which instructions will be executed and also showing the contents of the
working register W, file registers 2DH and 2Eh, and port C after execution of each instruction in the
program.
20
d) Store the number 3Dh in file registers 21h – 40h
e) Provide a delay of 1 microsecond for a clock frequency of 10Mhz.
f) Provide a delay of 1 millisecond for a clock frequency of 10Mhz.
g) Provide a delay of 1 second for a clock frequency of 10Mhz.
h) Add 16 bit numbers.
i) Subtract 16 bit numbers.
j) Multiply two 8 bit numbers.
k) Divide a 16-bit number by an 8-bit number (give result as a quotient and a remainder).
21