Ch2 microcontroller architecture
Embed Size (px)
Transcript of Ch2 microcontroller architecture
- 1. Copyright Politeknik Kota Bharu Page 1 CHAPTER 2 MICROCONTROLLER ARCHITECTURE & ASSEMBLY LANGUAGE PROGRAMMING PIC18 Microcontroller families PIC is a family of modified Harvard architecture microcontrollers made by Microchip Technology, derived from the PIC1650 originally developed by General Instrument's Microelectronics Division. The name PIC initially referred to "Peripheral Interface Controller". 1989 Microchip Technology Corporation introduced its first 8-bit microcontroller PIC18 Architecture PIC microcontrollers are based on advanced RISC architecture. RISC stands for Reduced Instruction Set Computing. In this architecture, the instruction set of hardware gets reduced which increases the execution rate (speed) of system. CPUs use many register to store data temporarily. To program in Assembly Language, we must understand the register and architecture PIC microcontrollers follow Harvard architecture for internal data transfer PIC microcontrollers are designed using the Harvard Architecture which includes: Microprocessor unit (MPU) Program memory for instructions Data memory for data I/O ports Support devices such as timers
- 2. Copyright Politeknik Kota Bharu Page 2
- 3. Copyright Politeknik Kota Bharu Page 3 The data RAM file Register CPUs use many registers to store data temporarily. To program in assembly language, we must understand the registers and architecture of given CPU and the role they play in processing data. WREG register Registers are used to store information temporarily The information could be a byte of data to be processed, or and address pointing to the data to be fetched The WREG is the most widely used register in PIC microcontroller
- 4. Copyright Politeknik Kota Bharu Page 4 The 8-bit WREG register is the most widely used register in the PIC micro controller. WREG stands for working register, as there is only one. The WREG register is the same as the accumulator in other microprocessors. The WREG register is used for all arithmetic and logic instructions. To understand the use of the WREG register, we will show it in the context of two simple instructions: MOVLW and ADDWL. MOVLW Instruction Notice that in MOVLW, the letter L (literal) comes first and then the letter W (WREG), which means "move a literal value to WREG, " the destination. The following instruction loads the WREG register with a literal value of 25H (i.e., 25 in hex). o MOVLW 25H ; move value 25H into WREG o MOVLW 87H ; load 87H into WREG ADDLW Instruction The ADD instruction tells the CPU to add the literal value K to register WREG and put the result back in the WREG register. Notice that in ADDLW, first comes the letter L (literal) and then the letter W (WREG), which means "add a literal value to WREG," the destination MOVLW 25H ; load 25H into WREG ADDLW 34H ; add value 34 to WREG W = W + 34H Executing the above lines results in WREG = 59H (25H + 34H = 59H) File register (SFRs and GPR) File Register (data RAM) is read/write memory used by CPU for data storage, scratch pad and register for internal use and function Divided into two sections: o SFRs Special Function Registers o GPR General-Purpose Register
- 5. Copyright Politeknik Kota Bharu Page 5 Special Function Register (SFRs) Special-Function registers are RAM memory locations, their purpose is predetermined during manufacturing process and cannot be changed. Dedicated to specific functions ALU status, timers, serial communication, I/O ports ADC etc. Fixed by CPU designer at the time of design 8 bits register
- 6. Copyright Politeknik Kota Bharu Page 6 General Purpose Register (GPR) 8-bit registers are a group of RAM locations in the file register that are used for data storage and scratch pad. the space that is not allocated to the SFRs typically used for general-purpose registers MOVWF Instruction Notice that in MOVWF, the letter F stands for a location in the file register, while W means WREG. The MOVWF instructions tells the CPU to move (in reality, copy) the source register of WREG to a destination in the file register (F) MOVLW 55H ; WREG = 55H MOVWF PORTB ; Copy WREG to Port B (Port B = 55H) MOVWF PORTC ; Copy WREG to Port C (Port C = 55H) MOVWF PORTD ; Copy WREG to Port D (Port D = 55H)
- 7. Copyright Politeknik Kota Bharu Page 7 MOVF Instruction The MOVF mnemonic is intended to perform MOVFW. It move the contents of file register into WREG or to itself MOVF PORTB,W ; move from file register of Port B to WREG MOVF PORTB,0 ; move content of file register into WREG MOVF PORTB,1 ; move content of file register into itself Review Question 1. State five example of SFR 2. What is GPR 3. Write an instruction to move value of 58H into WREG 4. Write an instruction to move value of 58H into PORTB 5. What is the difference between the MOVWF and MOVF instruction 6. Explain each instruction below: a. MOVLW 55H b. MOVF PORTB,W Status Register One of the most important register in PIC. Most of PIC has this register. 8-Bit register. Sometime referred as flag register. Only 5 bits of it are used by the PIC18. o called conditional flags The three unused bits are unimplemented and read as 0.
- 8. Copyright Politeknik Kota Bharu Page 8 C (Carry/Borrow Flag) set when an addition generates a carry and a subtraction generates a borrow. DC(Digit Carry Flag):also called Half Carry flag;set when carry generated from Bit3 to Bit4 an arithmetic operatio.n Z(Zero Flag):set when result of an operation is zero. OV(Overflow Flag):set when result of an operation of signed numbers goes beyond seven bits-if the results fall outside 127(0x7F)and -128(0x80). N(Negative Flag):set when bit B7 is one of the result of an arithmetic/logic operation. Example 1. Show the status of the C, DC and Z flag after the addition of 38H and 2FH. Solution: 38H 00111000 + 2FH + 00101111 67H 01100111 C = 0 ; DC = 1; Z = 0
- 9. Copyright Politeknik Kota Bharu Page 9 2. Show the status of the C, DC and Z flags after the addition of 9CH and 64H in the following instructions: MOVLW 9CH ADDLW 64H 3. Write the instruction for addition operation of 88H and 93H and show the status of the C, DC and Z flags. Solution: MOVLW 88H ADDLW 93H Review Question 1. Determine the state of status register after the instruction below: a. MOVLW 0F5H ADDLW 0BH b. MOVLW 9FH ADDLW 61H
- 10. Copyright Politeknik Kota Bharu Page 10 2. Show the status of the C, DC and Z flags after the addition of 9CH and 64H in the following instructions: a. MOVLW 9CH ADDLW 64H b. MOVLW 67H ADDLW 99H c. MOVLW 87H ADDLW 22H Data format and directives PIC data type has only one data type. 8-bits. Size of register also 8-bits ( 00-FF, or 0-255) Data format representation. o Hex, o Binary, o Decimal, o ASCII HEX number Use h, or H right after the number. Example MOVLW 12H Put 0x. Example MOVLW 0x12; Put nothing. Eg. MOVLW 12 Put h in front of number. MOVLW h12 Binary number Only one way to represent binary numbers, Example: MOVLW B00010010 1 2 in hex Decimal number Two ways to represent. MOVLW D12 , (in hex 0x0C) Or MOVLW .12
- 11. Copyright Politeknik Kota Bharu Page 11 ASCII code To represent ASCII data in PIC assemble we use the letter A as follows MOVLW A2 ; WREG = 00110010 or 32 in hex ( see ASCII chart) MOVLW Ac ; WREG = 01100011 or 63 in hex (see ASCII chart) MOVLW 9 ; WREG = 39H another way for ASCII
- 12. Copyright Politeknik Kota Bharu Page 12 ASSEMBLER Directive Directive give direction to he assembler.. Example EQU, ORG, END, #INCLUDE, LIST, _CONFIG EQU (equate) Used to define constant value or fixed address. Associates a constant number / address label Eg. COUNT EQU 0x25 MOVLW COUNT ; WREG = 25 ORG (Origin) Indicate the beginning of the address. Example ORG 0x00 END Important to indicate End of code, last line LIST Indicates to the assembler the specific PIC chip for which the program should be assembled. Eg. LIST P=18F458 #INLCUDE Tell the assembler to use the libraries associated with the specific chip _CONFIG To configure bits for the targeted PIC. Read during the power-up, wrong configuration makes PIC unusable.
- 13. Copyright Politeknik Kota Bharu Page 13 Example code Ins