You are on page 1of 7

A0066078X

Lin Shaodun

1. Explain the following. What is a processor? What components does it have? What makes a microprocessor different from the processor of a large computer? What makes a micro-controller different from a general purpose microprocessor? A processor is the logic circuitry that executes instruction sets and performs the basic arithmetical, logical, and I/O operations of the system. Components inside processor includes the following: 1. Arithmetic Logic Unit (ALU): The ALU performs all the numerical computations and logical evaluations for the processor. The ALU receives data from the memory, performs the operations, and, if necessary, writes the result back to the memory. 2. Registers: The registers are responsible for saving the temporarily results obtained from other devices such as ALU and it is used to hold data and/or a memory address during the execution of an instruction. 3. Busses: The main use of the bus is to transfer any type of data between components inside the processor or among the processor and other peripheral devices. The buses are divided into three main subtypes:
a) Data bus: to transfer the data bytes between elements inside the processor. b) Address bus: to carry the address of a unique memory or I/O device. c) Control bus: to transfer control signals that coordinate and synchronize the whole system.

4. Decoders: it is used to tell the processor what must be done based on the instructions in the memory written by users. The difference between a microcontroller and the processor of large computer: 1. Number of bits can be manipulated in one operation: Currently the most widely used microcontroller is 8 ~ 32-bits, while processor of large computer is 64 ~ 512-bits. 2. Clock rate: microcontroller usually runs at 4~80MHz while processor of large computer runs at 2.0~3.6GHz. 3. Number of physical cores: microcontroller only have one core while processor of large computer may have 4~16 cores. 4. Architecture: Microcontroller is RISC while processor of large computer is RISC or CISC. 5. Instruction set: Processor of large computer has much more sophisticated instruction set than microcontroller. The difference between a Microcontroller and a general purpose microprocessor: 1. A microcontroller is a specialized form of microprocessor that is designed to be self-sufficient and cost-effective. Microprocessors generally require external components to implement ROM/RAM and I/O ports, while Microcontrollers incorporate ROM/RAM, Timers, Counters, Interrupts and I/O ports internally to the chip. 2. Microcontrollers are usually designed to perform a small set of specific functions, for example a DSP only performs digital signal processing functions, while microprocessors are designed to perform a wider set of general purpose functions. 3. Microprocessor includes memory management unit and it has lots of cache while microcontroller has Integrated RAM and ROM, no cache. 4. Microprocessor mainly used in desktop machines while microcontroller mainly used in embedded applications and often involves real-time control. 5. Microprocessor focus on high performance (cost and power consumption is secondary) while microcontroller focus on lower cost and power consumption. 1|P age

A0066078X

Lin Shaodun

2. What are the lengths (number of bits) of PIC 18 data and instruction, respectively? How many bits are used to select a data register, and how many bits are used to select a program memory location, respectively? How does that determine the amount of data memory and program memory supported by PIC 18?
Length of PIC18 MCU data is eight bits. PIC18 MCU provides 77 instructions. Seventy-three instructions are 16 bits, four are 32 bits. (MOVFF,CALL,RCALL and LFSR contain 20-bit program address and the lengths are 32 bits) The PIC18 MCU supports 4096 bytes of data memory, it requires 12 bits of address (000h~FFFh) to select one of the data registers, due to the limited length of the PIC18 instruction (most instructions are 16 bits), only eight bits of the PIC18 instruction are used to select a data register. An additional four bits of register address are stored in the bank select register (BSR). PIC18 MCU has 21 bits program counter, which allows it to access up to 221 bits (~ 2 MB) of program memory. Hence 21 bits are used to select a program memory location.

Hence: Amount of data memory supported by PIC 18 MCU = 212 = 4096 byte = 4KB Amount of program memory supported by PIC 18 MCU = 221 = 2097152 byte = 2MB

2|P age

A0066078X

Lin Shaodun

3. Write an instruction sequence to swap the contents of data registers at 0x200 to 0x202 and 0x300 to 0x302, (i.e, [0x200] swaps with [0x300], [0x201] with [0x301] etc) then sum these two 3bytes number and leave the sum in 0x000 to 0x002.
#include <p18F8520.inc> org 0x00 goto start org 0x08 retfie org 0x18 retfie temp set 0x020 sum_temp1 set 0x021 sum_temp2 set 0x022

;temporary data register for swap ;temporary data register for sum calculation ;temporary data register for sum calculation

;Swap the contents of data registers at 0x200 to 0x202 and 0x300 to 0x302 Start movff 0x200,temp ; temp = [0x200] movff 0x300,0x200 ; [0x200] = [0x300] movff temp,0x300 ; [0x300] = temp = [0x200] movff 0x201,temp ; repeat for another two pair of data registers movff 0x301,0x201 movff temp,0x301 movff 0x202,temp movff 0x302,0x202 movff temp,0x302 ;Sum two 3-bytes number and leave the sum in 0x000 to 0x002 movff 0x200, sum_temp1 ; sum_temp1 = [0x200] movfw sum_temp1,A ; WREG = sum_temp1 = [0x200] movff 0x300, sum_temp2 ; sum_temp2 = [0x300] addwf sum_temp2,W ; WREG =[0x200]+ [0x300] movwf 0x000,A ; [0x000]= WREG =[0x200]+ [0x300] movff 0x201, sum_temp1 ; repeat for another two pair of data registers movfw sum_temp1,A movff 0x301, sum_temp2 addwf sum_temp2,W movwf 0x001,A movff 0x202, sum_temp1 movfw sum_temp1,A movff 0x302, sum_temp2 addwf sum_temp2,W movwf 0x002,A end

3|P age

A0066078X

Lin Shaodun

4. Write a program to compute the average of the squares of 32 8-bit numbers stored in a table

labeled array. Save the average in the data memory locations 0x21-0x22. [Notice that if you store the sum of the squares using two bytes, over-flow may happen. This needs to be taken care of.] Explain your idea using pseudo code or flow chart first.
; ; ; ; ; The largest 8-bit number is 0xFF, the square of largest 8-bit number is 0xFE01, the sum of 32 squared numbers is 0x1FC020, which is a 24-bits number, it needs to be stored using three bytes. Division by 32 can be done by rotate the register to right 5 times, the digits after radix point is ignored in this case. #include <p18F8720.inc> no_of_loop set 0x30 ;loop index ;temporary data register for sum calculation sum_temp1 set 0x31 sum_temp2 set 0x32 ;temporary data register for sum calculation sum_temp3 set 0x33 ;temporary data register for sum calculation org 0x00 start goto org 0x08 retfie org 0x18 retfie ;Read array into table start movlw D32 movwf no_of_loop movlw upper array movwf TBLPTRU,A movlw high array movwf TBLPTRH,A movlw low array movwf TBLPTRL,A

; WREG = array size (32) ; set number of loops equal to array size ; read array to table

;Add the sum of squared 8-bits numbers loop TBLRD*+ ; read an array element into TABLAT movf TABLAT,W,A ; WREG = array element mulwf TABLAT,A ; square the array element movf PRODL,W,A ; WREG = lower byte of squared number addwf sum_temp1,F,A ; sum_temp1 = sum_temp1 + lower byte movf PRODH,W,A ; WREG = upper byte of squared number addwfc sum_temp2,F,A ; sum_temp2 = sum_temp2 + upper byte BNC next ; branch if no carry incf sum_temp3,F,A ; sum_temp3 = sum_temp3 + 1 (from carry) next decfsz no_of_loop,F,A bra loop ;Divide the movlw div bcf rrcf rrcf result by 32 0x05 STATUS, C, A sum_temp3, F, A sum_temp2, F, A

; ; ; ;

set loop count to 5 (25=32) clear the C flag shift the bits to right 1 place

4|P age

A0066078X
rrcf sum_temp1, F, A decfsz WREG, W, A bra div movff sum_temp2,0x22 movff sum_temp1,0x21 nop ; ; ; ;

Lin Shaodun
have we shifted right five places yet? not yet, continue move final result to 0x21~0x22

array db 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09 db 0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x10,0x11,0x12,0x13 db 0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D db 0x1E,0x1F end


Start

No
Is no_of_loop = 0?
move 2nd byte of temp storage to 0x22

Set no_of_loop =32

Yes
Read array to table Set rotate count =5 Read element from table move 1st byte of temp storage to 0x21

Clear carry flag

End

WREG = array element Shift the bits of 3rd byte of temp storage to right 1 place (with carry)

Square the array element

Add PRODL to 1st byte of temporary storage

Shift the bits of 2nd byte of temp storage to right 1 place (with carry)

Add PRODH to 2nd byte of temporary storage with carry

Shift the bits of 1st byte of temp storage to right 1 place (with carry)

No
Any carry after this?

Yes
Rotate count - 1

Increment 1 for 3rd byte of temp storage

No

Is rotate count =0?

Yes

no_of_loop - 1

5|P age

A0066078X

Lin Shaodun

5. Write a program that after a delay of 100 ms, find the smallest element that is a multiplier of 8, from an array of 20 8-bit numbers stored in program memory starting with a label array. Explain your idea using pseudo code or flow chart first. Assume the clock frequency is 40MHz
#include <p18F8720.inc> no_of_loop set 0x20 delay_cnt1 set 0x21 delay_cnt2 set 0x22 delay_cnt3 set 0x23 temp set 0x24 result set 0x25 org goto org retfie org retfie 0x00 start 0x08 0x18 ; ; ; ; ; ; loop index delay counter 1 delay counter 2 delay counter 3 temporary data register to store the final result

; clock frequency is 40MHz, delay 100ms = 200x250x20x4x25nS start movlw D'200' movwf delay_cnt1,A delay_loop1 movlw D'250' movwf delay_cnt2,A delay_loop2 movlw D'17' movwf delay_cnt3,A delay_loop3 nop decfsz delay_cnt3,F,A bra delay_loop3 decfsz delay_cnt2,F,A bra delay_loop2 decfsz delay_cnt1,F,A bra delay_loop1 ;Read array into table movlw D20 ; WREG = array size (20) movwf no_of_loop ; set number of loops equal to array size movlw upper array ; read array to table movwf TBLPTRU,A movlw high array movwf TBLPTRH,A movlw low array movwf TBLPTRL,A movlw 0xff ; set [result] data register to highest movwf result ; 8-bits number loop movlw D7 ; load mask 0000 0111 to WREG TBLRD*+ ; read an array element into TABLAT movff TABLAT,temp ; copy TABLAT to temporary data register andwf TABLAT,F,A ; AND WREG with 0000 0111

6|P age

A0066078X
bnz next movf CPFSLT movff next decfsz bra nop db db end

Lin Shaodun
; branch if not a multiple of 8 temp,W,A ; WREG = array element result,A ; compare array element with result data temp,result ; register, if the array element is smaller, ; put the array element into result register no_of_loop,F,A loop

array

0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09 0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x10,0x11,0x12,0x13

Start

Read Array to Table

delay_cnt1=200

No
delay_cnt2= 250 No_of_loop = 20 Read table element

Is no_of_loop = 0?

delay_cnt3= 17

AND table element with 0000 0111

Yes
END

NOP

No
Is result zero?

Yes

delay_cnt3 - 1 Compare with result register

No
Is delay_cnt3 = 0?

Yes

No
delay_cnt2 - 1

Is it smaller than result register?

Yes

No
Is delay_cnt2 = 0?

Yes
Put the smaller element into result register

delay_cnt1 - 1 no_of_loop - 1

No
Is delay_cnt1 = 0?

Yes

7|P age

You might also like