+ All Categories
Home > Documents > Chapter 2 PIC ARCHITECTURE & ASSEMBLY LANGUAGE...

Chapter 2 PIC ARCHITECTURE & ASSEMBLY LANGUAGE...

Date post: 18-Oct-2020
Category:
Upload: others
View: 3 times
Download: 1 times
Share this document with a friend
14
Islamic University – Gaza Engineering Faculty Department of Computer Engineering ECOM 3022: Embedded Systems Discussion Chapter 2 PIC ARCHITECTURE & ASSEMBLY LANGUAGE PROGRAMMING Eng. Eman R. Habib February, 2014
Transcript
Page 1: Chapter 2 PIC ARCHITECTURE & ASSEMBLY LANGUAGE …site.iugaza.edu.ps/ehabib/files/ES-chapter2.pdf · Give the value in fileReg 0x63 for the following: MYDATA EQU D'12' MYREG EQU 0x63

Islamic University – Gaza Engineering Faculty

Department of Computer Engineering ECOM 3022: Embedded Systems Discussion

Chapter 2

PIC ARCHITECTURE & ASSEMBLY LANGUAGE PROGRAMMING

Eng. Eman R. Habib

February, 2014

Page 2: Chapter 2 PIC ARCHITECTURE & ASSEMBLY LANGUAGE …site.iugaza.edu.ps/ehabib/files/ES-chapter2.pdf · Give the value in fileReg 0x63 for the following: MYDATA EQU D'12' MYREG EQU 0x63

2 Embedded Systems Discussion

The WREG Register - The WREG (Working Register) Register is one of the most widely used registers of the

PIC. - 8-bit register any data larger than 8 bits must be broken into 8-bits chunks before it is

processed. - There is only one.

MOVLW k - Moves 8-bit data into WREG (00H ≤ k ≤ FFH) - Move literal value k into WREG (WREG = k) - Example:

MOVLW 25H WREG = 25H MOVLW 5AH WREG = 5AH

ADDLW k

- Add literal value k to WREG (WREG = WREG + k) - Example:

MOVLW 12H WREG = 12H ADDLW 16H WREG = 28H

When programming the WREG of PIC, the following points should be noted: - Values can be loaded directly into the WREG. - If values 0 to F are moved into an 8-bit register such as WREG, the rest of the bits are

assumed to be all zeros. - Moving a value larger than 255 (FF in hex) into the WREG register will truncate the

upper byte and cause a warning in the .err file. MOVLW 7F2H; Illegal , becomes F2H

The PIC File Register - It is the data memory: Read/Write Static RAM - Used for data storage, scratch pad and registers for internal use and function - 8-bit width - The file register data RAM in PIC is - Divided into two sections:

(a) Special Function Registers (SFR). (b) General Purpose Registers (GPR) or General-Purpose RAM (GP RAM).

Special Function Registers

- Dedicated to specific functions such as ALU status, timers, serial communication, I/O ports, ADC,…

- The function of each SFR is fixed by the CPU designer at the time of design

Page 3: Chapter 2 PIC ARCHITECTURE & ASSEMBLY LANGUAGE …site.iugaza.edu.ps/ehabib/files/ES-chapter2.pdf · Give the value in fileReg 0x63 for the following: MYDATA EQU D'12' MYREG EQU 0x63

3 Embedded Systems Discussion

- it is used for control of the microcontroller or peripheral - 8-bit registers - Their numbers varies from one chip to another.

General Purpose Register - Group of RAM locations that are used for data storage and scratch pad - 8-bit registers - Larger than SFR, difficult to manage them by using Assembly language, easier to handle

them by C Compiler.

GP RAM vs. EEPROM in PIC chips - GPRs are used by the CPU for internal data storage. - EEPROM are considered as add-on memory that one can add externally to the ship. - So PIC chip may have zero byte of EEPROM data memory, but impossible for a PIC have

zero size for the file register.

File Register and access bank in the PIC18 - The PIC18 Family can have a max. of 4096 Bytes. - The File Register has addresses of 000- FFFH - divided into 256-byte banks - Max. 16 banks (16 bank x 256 byte) - At least there is one bank known as default access bank:

o 128 byte GP RAM, from 0 to 7FH o 128 byte SFR, from F80H to FFFH

- Bank switching is a method used to access all the banks.

MOVWF File Reg. Address - F indicates for a file register - It tells the CPU to copy the source register, WREG, to a destination in the file register

(File Reg. Address =WREG): o A location in the SPR o A location in GP RAM

- Example: MOVLW 55H WREG = 55H MOVWF 12H 12H = 55H MOVWF PORTB PORTB = 55H

ADDWF File Reg. Address, D

- Adds together the content of WREG and a file register location - The result will be placed in either the WREG or in the file register location - D indicates the destination bit:

Page 4: Chapter 2 PIC ARCHITECTURE & ASSEMBLY LANGUAGE …site.iugaza.edu.ps/ehabib/files/ES-chapter2.pdf · Give the value in fileReg 0x63 for the following: MYDATA EQU D'12' MYREG EQU 0x63

4 Embedded Systems Discussion

o If D=0 or (D=w) The result will be placed in the WREG (WREG = WREG + File Reg.)

o If D=1 or (D=f) The result will be placed in the file register (File Reg. = WREG + File Reg.)

- Example: MOVLW 55H WREG = 55H MOVWF 12H 12H = 55H MOVLW 11H WREG = 11H ADDWF 12H, W WREG = 11H + 55H = 66H ADDWF 12H, F 12H = 66H + 55H = BBH

COMF File Reg. Address, D

- It tells the CPU to complement the content of fileReg and places the results in WREG or in fileReg.

- Example: MOVLW 55H WREG = 55H MOVWF PORTB PortB = 55H COMF PORTB, F PortB = AAH COMF PORTB, W WREG = 55H

DECF File Reg. Address, D - It tells the CPU to decrement the content of fileReg and places the results in WREG or in

fileReg. - Example:

MOVLW 3 WREG = 3H MOVWF 12H 12H = 3H DECF 12H, F 12H = 2H DECF 12H, W WREG = 1H, but 12H = 2H

MOVF File Reg. Address, D - It is intended to perform MOVFW, MOVFW isn’t existed - If D=0, copies the content of fileReg (from I/O pin) to WREG - If D=1, the content of the fileReg is copied to itself. - Example: get data from the SFRs of PortB. Add the value 5 to it and send it to the SFRs

of PORTC: (If PORTB = 1AH) MOVF PORTB, W WREG = 1AH ADDLW 05H WREG = 1AH + 5H = 1FH MOVWF PORTC PORTC = 1FH

MOVFF Source FileReg, destination FileReg - It copies data from one location in FileReg to another location in FileReg.

Page 5: Chapter 2 PIC ARCHITECTURE & ASSEMBLY LANGUAGE …site.iugaza.edu.ps/ehabib/files/ES-chapter2.pdf · Give the value in fileReg 0x63 for the following: MYDATA EQU D'12' MYREG EQU 0x63

5 Embedded Systems Discussion

- Example: get data from the SFRs of Port B and send it to the SFRs of PORT C: MOVFF PORTB, PORTC PORTC = PORTB

SECTON 2.3, R.Q. - 2: Write instructions to add the values 16H and CDH. Place the result in location 0 of the file register.

MOVLW 0x16 ADDW 0x0CD MOVWF 0

PIC Status Register - To indicate arithmetic conditions - It is a 8-bit register - Five bits are used

D0: C Carry Flag D1: DC Digital Carry Flag D2: Z Zero Flag D3: OV Overflow Flag D4: N Negative Flag

- Example: Show the status of the C, DC, Z flags after the following addition instruction MOVLW 9CH ADDLW 64H

9CH + 64H = 100H WREG = 00H 9CH 1001 1100 C=1 + 64H 0110 0100 DC=1 100H 0000 0000 Z=1

Page 6: Chapter 2 PIC ARCHITECTURE & ASSEMBLY LANGUAGE …site.iugaza.edu.ps/ehabib/files/ES-chapter2.pdf · Give the value in fileReg 0x63 for the following: MYDATA EQU D'12' MYREG EQU 0x63

6 Embedded Systems Discussion

Flag Bits and Decision Making There are instructions that will make a conditional jump (branch) based on the status of the flag bits.

PIC Data Format and Directives - There is one data type: 8 bits, it is the job of the programmer to break down data larger

8 bits - Data type can be positive or negative - Data format are:

o Hex (default in PIC) 12 or 0x12 or H'12' or 12H If the value starts with the hex digits A-F, then it must be preceded with a zero. MOVLW E5H; invalid, it must be MOVLW 0E5H ADDLW C6; invalid, it must be ADDLW 0C6

o Binary B'00010010' o Decimal .12 or D'12' o ASCII A'c' o H, B, D, A: capital or small

- To define ASCII strings (more than one character), we use the DB (define byte) directive

Assembler Directives - The Instruction tell the CPU what to do, while Directives (pseudo instructions) give

direction to assembler. - Directives: EQU, SET, ORG (Origin), END, LIST - Directive don’t generate any machine code and used by assembler.

EQU (equate) o It is used to define a constant value or a fixed address. o It associates a constant number with a data or an address label so that when the

label appears in the program, its constant will be substituted for the label. o Example:

COUNT EQU 0x25 MOVLW COUNT WREG = 25H

Page 7: Chapter 2 PIC ARCHITECTURE & ASSEMBLY LANGUAGE …site.iugaza.edu.ps/ehabib/files/ES-chapter2.pdf · Give the value in fileReg 0x63 for the following: MYDATA EQU D'12' MYREG EQU 0x63

7 Embedded Systems Discussion

SET It’s used to define a constant value or a fixed, it’s the identical to EQU, but the SET may be reassigned later.

ORG (origin) It is used to indicate the beginning of the address.

END This indicates to assembler the end of the source (asm) file.

LIST It indicate to the assembler the specific PIC chip for which the program should be assembled LIST P=18F458

#include It tells the PIC assembler to use the libraries associated with the specific chip for which we are compiling the program.

_config It tells the assembler the configuration bits for the targeted PIC chip.

radix directive It indicates whether the numbering system is hexadecimal or decimal. The default is hex if we do not use the radix directive. If we use "radix dec", the default representation will change to decimal.

Rules for labels in Assembly Language - Unique name - Alphabetic letters (upper, lower), digits (0-9), special char. (? . @_ $) - The first letter must be Alphabetic letters - Not a reserved word

SECTON 2.4, R.Q. - 7: Give the value in fileReg 0x63 for the following: MYDATA EQU D'12' MYREG EQU 0x63 FACTOR EQU 0x10 MOVLW MYDATA WREG = D'12' ADDLW FACTOR WREG = D'12' + 0x10 = 12 + 16 = 28 MOVWF MYREG MYREG = D'28' = 0x1C 0x63 = D'28' = 0x1C

Page 8: Chapter 2 PIC ARCHITECTURE & ASSEMBLY LANGUAGE …site.iugaza.edu.ps/ehabib/files/ES-chapter2.pdf · Give the value in fileReg 0x63 for the following: MYDATA EQU D'12' MYREG EQU 0x63

8 Embedded Systems Discussion

Assembling and Linking A PIC Program

The Program Counter and Program ROM Space

- Program Counter (PC) is used by the CPU to point to the address of the next instruction to be executed

- The wider the program counter, more the memory locations can be accessed - The program counter in the PIC 18 family is 21-bit. - Program addresses from 000000 to 1FFFFFH, a total of 221 = 2M of code. - With 4K of on-chip ROM memory space, we have 4096 bytes (4 x 1024 = 22 x 210 = 212 =

4096). PC is 12 bit, address locations from 0000 to 0FFFH.

Page 9: Chapter 2 PIC ARCHITECTURE & ASSEMBLY LANGUAGE …site.iugaza.edu.ps/ehabib/files/ES-chapter2.pdf · Give the value in fileReg 0x63 for the following: MYDATA EQU D'12' MYREG EQU 0x63

9 Embedded Systems Discussion

Powering UP - The uC wakes up at memory address 0000 - The PC has the value 0000 - ORG directive put the address of the first op code at the memory location 0000

Program ROM Width for the PIC18 - All the instructions are 2-byte or 4-byte instruction. - In 2-byte instruction: 1 byte for opcode and the other byte for the operand. - Byte addressable: each location holds only one byte - CPU with 8-Bit will fetch one byte a time - Increasing the data bus will bring more information. - Solution: Data bus between CPU and ROM is 16 bit - Therefore, the 2M ROM space is 1M x 16 using a 16-bit word data size. - Increase the processing power - CPU brings in an instruction from ROM in single cycle.

Little endian VS big endian war - The low byte goes to the low memory location - The high byte goes to the high memory location - Intel uP and many uCs use little endian

Page 10: Chapter 2 PIC ARCHITECTURE & ASSEMBLY LANGUAGE …site.iugaza.edu.ps/ehabib/files/ES-chapter2.pdf · Give the value in fileReg 0x63 for the following: MYDATA EQU D'12' MYREG EQU 0x63

10 Embedded Systems Discussion

Instruction size of the PIC18 - PIC Instructions are 2-Byte or 4-Byte - The first seven or eight bits represents the op-code - Most of PIC18 instructions are 2-Byte

o MOVLW 0000 1110 kkkk kkkk (0E XX), (00 ≤ k ≤ FF) o ADDLW 0000 1111 kkkk kkkk (0F XX), (00 ≤ k ≤ FF) o MOVWF 0110 111a ffff ffff (6E XX or 6F XX), (00 ≤ f ≤ FF)

a specifies the default access bank if it is 0 and if a = 1 we have to use bank switching

- 4-Byte instructions include: o MOVFF (move data within RAM, which is 4k)

1100 ssss ssss ssss (0 ≤ fs ≤ FFF) 1111 dddd dddd dddd (0 ≤ fd ≤ FFF)

o GOTO (go to address in ROM, which is 2M) 1110 1111 k7kkk kkkk0 1111 k19kkk kkkk kkkk8

k is 20-bit, gives us only 1M of address space and the PICI8 has 2M of ROM space.

This is solved by making the least-significant bit (LSB) of the GOTO instruction 0.

Page 11: Chapter 2 PIC ARCHITECTURE & ASSEMBLY LANGUAGE …site.iugaza.edu.ps/ehabib/files/ES-chapter2.pdf · Give the value in fileReg 0x63 for the following: MYDATA EQU D'12' MYREG EQU 0x63

11 Embedded Systems Discussion

PROBLEMS 7. Which of the following is (are) illegal?

(a) MOVLW 500 the operand exceeds allowed range. (b) MOVLW 50 (c) MOVLW 00 (d) MOVLW 255H the operand exceeds allowed range. (e) MOVLW 25H (f) MOVLW F5H The operand should start with 0. (g) MOVLW mybyte, 50H The syntax is wrong.

8. Which of the following is (are) illegal? (a) ADDLW 300H the operand exceeds allowed range (b)ADDLW 50H

(c) ADDLW $500 the operand exceeds allowed range (d) ADDLW 255H the operand exceeds allowed range (e) ADDLW 12H (f) ADDLW 0F5H (g) ADDWL 25H 12. True or false. We have many WREG registers in the PICI8.

13. PIC data RAM consists of (EEPROM, SRAM). 14. True or false. Data RAM in PIC is also called the file register. 17. True or false. All members of PICl8 family have the same size file register.

28. Show a simple code to load values 30H and 97H into locations 5 and 6 respectively.

MOVLW 30H MOVWF 5H MOVLW 97H MOVWF 6H

29. Show a simple code to load value 55H into locations 0-8. MOVLW 55H MOVWF 0H MOVWF 1H MOVWF 2H MOVWF 3H MOVWF 4H MOVWF 5H MOVWF 6H MOVWF 7H MOVWF 8H

Page 12: Chapter 2 PIC ARCHITECTURE & ASSEMBLY LANGUAGE …site.iugaza.edu.ps/ehabib/files/ES-chapter2.pdf · Give the value in fileReg 0x63 for the following: MYDATA EQU D'12' MYREG EQU 0x63

12 Embedded Systems Discussion

30. Show a simple code to load value 5FH into Port B SFR. MOVLW 5FH MOVWF PORTB

31. True or false. We can not load literal values into the scratch pad area directly.

35. Show a simple code to (a) load value 11H into locations 0-5, and (b) add the values together and place the result in WREG as they are added.

(a) MOVLW 11H MOVWF 0H MOVWF 1H MOVWF 2H MOVWF 3H MOVWF 4H MOVWF 5H (b) MOVLW 0x00 ADDWF 0H, W ADDWF 1H, W ADDWF 2H, W ADDWF 3H, W ADDWF 4H, W ADDWF 5H, W

36. Repeat Problem 35, except place the result in location 5 after the addition is done. MOVLW 11H MOVWF 0H MOVWF 1H MOVWF 2H MOVWF 3H MOVWF 4H MOVWF 5H MOVLW 0x00 ADDWF 0H, W ADDWF 1H, W ADDWF 2H, W ADDWF 3H, W ADDWF 4H, W ADDWF 5H, F

Page 13: Chapter 2 PIC ARCHITECTURE & ASSEMBLY LANGUAGE …site.iugaza.edu.ps/ehabib/files/ES-chapter2.pdf · Give the value in fileReg 0x63 for the following: MYDATA EQU D'12' MYREG EQU 0x63

13 Embedded Systems Discussion

37. Show a simple code to (a) load value 15H into location 7, and (b) add it to WREG five times and place the result in WREG as the values are added. WREG should be zero before the addition starts.

(a) MOVLW 15H MOVWF 7H (b) MOVLW 0H ADDWF 7H, W ADDWF 7H, W ADDWF 7H, W ADDWF 7H, W ADDWF 7H, W

38. Repeat Problem 37, except place the result in location 7 as numbers are being added together.

(a) MOVLW 15H MOVWF 7H (b) MOVLW 0H ADDWF 7H, F ADDWF 7H, F ADDWF 7H, F ADDWF 7H, F ADDWF 7H, F

42. Write a simple code to copy data from location 8 to PORTC (a) using WREG and (b) without using WREG.

(a) MOVF 8H, W MOVWF PORTC (b) MOVFF 8H, PORTC

53. Show a simple code to (a) load value 11H into locations 0-5, and (b) add them together and place the result in WREG as the values are added. Use EQU to assign the names R0-R5 to locations 0-5.

R0 EQU 0H R1 EQU 1H R2 EQU 2H R3 EQU 3H R4 EQU 4H R5 EQU 5H

Page 14: Chapter 2 PIC ARCHITECTURE & ASSEMBLY LANGUAGE …site.iugaza.edu.ps/ehabib/files/ES-chapter2.pdf · Give the value in fileReg 0x63 for the following: MYDATA EQU D'12' MYREG EQU 0x63

14 Embedded Systems Discussion

(a) MOVLW 11H MOVWF R0 MOVWF R1 MOVWF R2 MOVWF R3 MOVWF R4 MOVWF R5 (b) MOVLW 0H ADDWF R0, W ADDWF R1, W ADDWF R2, W ADDWF R3, W ADDWF R4, W ADDWF R5, W

75. A given PIC has 7FFFH as the address of the last location of its on-chip ROM. What is the size of on-chip ROM for this PIC?

7FFF 15 bit 215 = 32768 bytes = 25 x 210 = 32KB

79. How wide is the ROM in the PICI8 chip?

16 bits 80. How wide is the data bus between the CPU and the program ROM in PICI8?

16 bits 91. In the instruction "GOTO target-addr" explain why the lowest bit of the program counter is 0.

Because the data bus between the CPU and ROM of PIC18 microcontrollers is 16 bits wide. Whenever CPU fetches the data from ROM, it receives 2 bytes. Therefore data is always fetched starting from even addresses and because of this, program counter always holds even values and its LSB is 0.

92. Explain why the instruction "GOTO target-addr" will not land at an odd address. Because the LSB of program counter is always 0.

93. In Question 92, explain why it should not. Because there are only 2-byte and 4-byte instructions in PIC18 microcontrollers and all instructions are stored in ROM starting from even addresses. If program counter could get odd values, it would land at an odd address (in the middle of an instruction).

Best Wishes


Recommended