You are on page 1of 49

MICROCONTROLLER ARCHITECTURE & ASSEMBLY LANGUAGE PROGRAMMING

Part 2 PIC Assembly Language fundamental

Instruction set of PIC18


PIC18F2455/2550/4455/4550 devices incorporate the standard set of 75 PIC18 core instructions. Most instructions are a single program memory word (16 bits) but there are four instructions that require two program memory locations. Each single-word instruction is a 16-bit word divided into an opcode, which specifies the instruction type and one or more operands, which further specify the operation of the instruction.

The instruction set is highly orthogonal and is grouped into four basic categories:

Byte-oriented operations Bit-oriented operations Literal operations Control operations

An Assembly language instruction consists of four fields: [label] mnemonic [operands] [; comment]

Byte-oriented operations
Most byte-oriented instructions have three operands: 1. The file register (specified by f) 2. The destination of the result (specified by d) 3. The accessed memory (specified by a) The file register designator f specifies which file register is to be used by the instruction. The destination designator d specifies where the result of the operation is to be placed. If d is zero, the result is placed in the WREG register. If d is one, the result is placed in the file register specified in the instruction.

Byte-oriented operations

Bit-oriented operations
All bit-oriented instructions have three operands: 1. The file register (specified by f) 2. The bit in the file register (specified by b) 3. The accessed memory (specified by a)

The bit field designator b selects the number of the bit affected by the operation, while the file register designator f represents the number of the file in which the bit is located.

Bit-oriented operations

Literal operations
The literal instructions may use some of the following operands: A literal value to be loaded into a file register (specified by k) The desired FSR register to load the literal value into (specified by f) No operand required (specified by )

Literal operations

Control operations
The control instructions may use some of the following operands: A program memory address (specified by n) The mode of the CALL or RETURN instructions (specified by s) The mode of the table read and table write instructions (specified by m) No operand required (specified by )

Control operations

All instructions are a single word, except for four double-word instructions. All single-word instructions are executed in a single instruction cycle, unless a conditional test is true or the program counter is changed as a result of the instruction. In these cases, the execution takes two instruction cycles with the additional instruction cycle(s) executed as a NOP.

The double-word instructions execute in two instruction cycles.


Please refer to PIC datasheet for detail information.

Example
ORG REG0 REG1 REG2 MOVLW MOVWF MOVLW MOVWF ADDWF MOVWF SLEEP 0x20 EQU 0x00 EQU 0x01 EQU 0x02 0x37 REG0,0 0x92 REG1,0 REG0,0 REG2, 0

Code assembly language using PIC18 instruction set


To be more precise, the instruction set can be divide to 7 group: Move (Data Copy) and Load Arithmetic Logic Program Redirection (Branch/Jump) Bit Manipulation Table Read/Write Machine Control

Move and Load Instructions

Arithmetic Instructions (1 of 3)

Arithmetic Instructions (2 of 3)

Arithmetic Instructions (3 of 3)

Logic Instructions

Branch Instructions

Call and Return Instructions

Bit Manipulation Instructions

Test and Skip Instructions

Increment/Decrement and Skip Next Instruction

Table Read/Write Instructions (1 of 2)

Table Read/Write Instructions (2 of 2)

Machine Control Instructions

Assembling and linking process in PIC18 Program


The steps to create an executable Assembly language program

First we use a text editor to type in a program. PIC microcontrollers, we use the MPLAB IDE, which has a text editor, assembler, linker, simulator, and much more all in one software package. Source file has the extension "asm". The "asm" extension for the source file is used by an assembler in the next step.

The assembler converts the instructions into machine code. The assembler will produce an object file and an error file.

The extension for the object file is "0". The extension for the error file, which contains any syntax errors and their line numbers, is "err". Assemblers require a third step called linking. The link program takes one or more object files and produces a hex file, a list file, a map file, an intermediate object file, and a debug file. After a successful link, the hex file is ready to be burned into the PIC's program ROM and is downloaded into the PIC Trainers

PIC assembler provides us the error file with the extension of "err" and this is the file we examine to see the nature of syntax errors. The 1st (list) and map files are very useful to the programmer. The list shows the binary and source code. The map file shows the memory layout of used and unused memory locations.

Program counter in the PIC


The program counter is used by the CPU to point to the address of the next instruction to be executed. As the CPU fetches the opcode from the program ROM, the program counter is incremented automatically to point to the next instruction. The wider the program counter, more the memory locations a CPU can access. The program counter in the PIC 18 family is 21-bit. This means that the PIC 18 family can access program addresses 000000 to I FFFFFH, a total of 2M of code.

ROM memory map in the PIC18 family


It must be noted that while the first location of program ROM inside the PIC has the address of 000000, the last location can be different depending on the size of the ROM on the chip. Find the ROM memory address of each of the following PIC chips: (a) PIC I 8F2220 with 4 KB (b) PICI8F2410 with 16 KB (c) PICI8F458 with 32 KB

(a) PIC I 8F2220 with 4 KB With 4K of on-chip ROM memory space, we have 4096 bytes (4 x 1024 = 4096). This maps to address locations of 0000 to OFFFH. Notice that 0 is always the first location.

(b) PICI8F2410 with 16 KB With 16K of on-chip ROM memory space, we have 16,384 bytes (16 x 1024 = 16,384),which gives 0000-3FFFH.

(c) PICI8F458 with 32 KB With 32K we have 32,768 bytes (32 x 1024 = 32,768). Converting 32,768 to hex, we get 8000H; therefore, the memory space is 0000 to 7FFFH.

Where the PIC wakes up when it is powered up?


At what address does the CPU wake up when power is applied? the microcontroller wakes up at memory address 0000 when it is powered up. In other words, when the PIC is powered up, the PC (program counter) has the value of 00000 in it. This means that it expects the first opcode to be stored at ROM address OOOOOH.

For this reason, in the PIC system, the first opcode must be burned into memory location OOOOOH of program ROM because this is where it looks for the first instruction when its booted. We achieve this by using the ORG statement in the source program.

Placing code in program ROM


To get a better understanding of the role of the program counter in fetching and executing a program, we examine the action of the program counter as each instruction is fetched and executed.

;PIC Assembly Language Program To Add Some Data. ;store sum in fileReg location lOH. SUM EQU 10H ; RAM loe 10H for sum ORG OH ;start at address 0 MOVLW 25H ;WREG = 25 ADDLW Ox34 ;add 34H to WREG ADDLW 11H ;add 11H to WREG ADDLW D'18' ;W W + 12H = 7CH ADDLW lCH ;W = W + lCH = 98H ADDLW B' 00000110' ;W = W + 6 = 9EH MOVWF SUM ;save the sum in loc 10H HERE GOTO HERE ;stay here forever END ;end of asm source file

For the PICI8, the internal data bus between the code ROM and the CPU is 16 bits.

End of part 2, to be continued in part 3

You might also like