Sei sulla pagina 1di 6

Lecture 3

Small assembly program Instruction code The data memory of PIC16F877A Data memory banks Ports and data direction of ports A complete small program

A small assembly program


An 8 bit variable x1 is located in data memory location 0x020. Write an assembly program which adds x1 with 5 and puts the result in a variable y1. y1 is located in data memory location 0x024. The program starting address must be 0x0000.

; name: xplus5.asm ; date: 2006-10-10 ;author: ; register definitions LIST P=16F877A INCLUDE "P16F877a.inc __CONFIG _CP_OFF & _WDT_OFF & _XT_OSC & _PWRTE_ON x1 equ y1 equ ; program org main NOP MOVF x1,w ADDLW 0x5 MOVWF y1 SLEEP end 0x20 0x24 0x0000

Everything after a semicolon on a line is interpreted as a comment and will be ignored by the assembler. Always start a program with comments with a program name, a date and an author. If something is written starting from the first position of a line it will be interpreted as a label. A label is a name given to a memory address in the microcontroller. x1 and y1 are memory addresses or registers in the data memory.

equ tells the assembler that it must exchange x1and y1 with 0x20 and 0x24 in all the following lines. equ (equals) is not an instruction but a directive to the assembler
org 0x0000

main

NOP

is another directive. It tells that all the following code should be programmed into program memory locations starting with address 0. This is important because as soon as the supply voltage is connected to the controller it always starts executing code from address 0.

is the first real instruction. Since this instruction is placed in the program memory location with the address 0, the label main means address 0. Remember to distinguish directives from instructions. The instructions are given to the microcontroller and the directives to the assembler. Try to write instructions with capital letters. NOP means no operation.

MOVF x1, W means move the contents of x1. The W means move it to the working register W in the CPU. ADDLW 0x5 means add 5 to W and put the result back into W. The number after ADDLW must be a number between 0x00 and 0xFF. MOVWF y1 means move the contents of W into y1.

If a program does not run in an infinite loop the last instruction must be SLEEP. (or goto $) All assembly programs must end with the directive end.

Instruction code
In the document instructions.pdf you find the 14 bit code for all instructions. The 7 f:s in 20 of the instructions make up a data memory address. We know from before that we actually need 9 bits to be able to access all data memory locations. Where we get the remaining 2bits in these instructions will be explained soon. The d in 14 instructions means that the result is put in W if d is 0 and in f if d is 1. The 8 k:s in 7 instructions must be a number between 0x00 and 0xFF. 2 instructions have 11 k:s. They indicate a program memory address. The 3 b:s in 4 instructions identify a bit in a register. An x means that the value of a bit is ignored.

address 0x00000 0x001 0x0002 0x0003

assembly code NOP MOVF x1, ADDLW 0x5 MOVWF y1

machine code 00 0000 0xx0 0000 00 1000 0010 0000 11 111x 0000 0101 00 0000 1010 0100
Op-code d Data/operand

The data memory of PIC16F877A


A data memory location is called a register. Some registers have a predefined name. Such a register has a special function and is referred to as a Special Function Register, SFR. All other registers are called General Purpose Registers. In the document registers.pdf you find a table with all the registers.

Data memory banks


There is a difficulty when accessing the data memory in 20 of the instructions. We need 9 bits to completely identify a memory location. There is however only room for 7 bits in an instruction to identify a memory address. The possible addresses in an instruction are from 0x00 to 0x7F.

To be able to access all the data memory location this memory is divided into 4 groups called banks. Bank0 has addresses from 0x000 to 0x07F. Bank1 has addresses from 0x080 to 0x0FF. Bank2 has addresses from 0x100 to 0x17F. Bank3 has addresses from 0x180 to 0x1FF.

Let us look at four such registers and their addresses: PIR1 0x00C B0 0000 1100 PIE1 0x08C B0 1000 1100 EEDATA 0x10C B1 0000 1100 EECON1 0x18C B1 1000 1100 Only the 7 least significant bits of these addresses are used in an instruction. This means that there is no point in identifying a memory location with more that 7 bits.

The four registers on each row on the previous page have a common address in the instructions.

STATUS REGISTER

The two bits RP1 (bit 6) and RP0 (bit 5) make up the two most significant bits of the data memory address. Let us look at some code (note that it is not a complete program): STATUS equ 0x3 TRISD equ 0x8 PORTD equ 0x8 RP0 equ 0x5 RP1 equ 0x6 BCF STATUS,RP1 BSF STATUS,RP0 ;bank1 CLRF TRISD BCF STATUS,RP0 ;bank0 MOVLW 0X62 MOVWF PORTD

There is a Special Function Register called STATUS which is accessed with the address B000 0011 regardless of register bank.

Ports and data direction of ports BCF STATUS,RP1 clears bit RP1 in STATUS BSF STATUS,RP0 sets bit RP0 in STATUS CLRF TRISD clears all bits in TRISD (all bits of PORTD become outputs) MOVLW 0x62 inserts 0x62 into W MOVWF PORTD moves the contents of W to PORTD
There are 5 ports in PIC16F877A. PORTA has 6 bits, RA0 RA5. PORTB to PORTD have 8 bits. PORTE has 3 bits, RE0 RE2. These registers are accessible on pins on the controller chip.

The pins are bi-directional. Each port register has a data direction register. TRISB belongs to PORTB. If a bit in TRISB is set to 1, the corresponding PORT bit becomes an input. Let us make pins RB0-RB3 inputs and pinsRB4RB7 outputs: BCF STATUS,RP1 BSF STATUS,RP0 ;bank1 MOVLW B00001111 MOVWF TRISB

A complete small program


On the PCB we use in the practical exercises three pushbuttons are connected to bits 0,1 and 2 of PORTB. Zeros (0 V) are normally connected to these bits. When a pushbutton is pressed a one (5V) is connected to the corresponding bit of PORTB. The three bits 0, 1 and 2 of PORTD are connected to three light emitting diodes, LEDs. PORTD is connected to a display

Here is an assembly program which will make a LED illuminate when the corresponding pushbutton is pressed:

; name: butleds ; date: 2006-10-10 ;author ;register definitions TRISB equ PORTB equ TRISE equ PORTE equ STATUS equ ; bit definitions RP0 equ RP1 equ

0x6 0x6 0x9 0x9 0x3 0x5 0x6

; program org main BCF BSF MOVLW MOVLW CLRF BCF Again MOVF MOVWF GOTO Again END

0x0000 STATUS,RP1 STATUS,RP0 0XFF TRISB TRISE STATUS,RP0 PORTB,0 PORTE

Thank you

Potrebbero piacerti anche