A0066078X
Lin Shaodun
�� ������� ��� ���������� ���� �� � ���������� ���� ���������� ���� �� ����� ���� ����� � �������������� ��������� ���� ��� ��������� �� � ����� ��������� ���� ����� � ���������������� ��������� ���� � ������� ������� ��������������� ���������������
� ��������� �� � � ��� ����� ��������� ���� �������� ����������� ���� ��� �������� ��� ����� ������������� �������� ��� ��� ���������� �� ��� �������
���������� ������ ��������� �������� ��� ����������
1. ���������� ����� ���� ������ ��� ��� �������� ��� ��� ��������� ������������ ��� ������� ����������� ��� ��� ���������. ��� ��� �������� ���� ���� ��� ������, �������� ��� ����������, ���, �� ���������, ������ ��� ������ ���� �� ��� ������. 2. ���������� ��� ��������� ��� ����������� ��� ������ ��� ����������� ������� �������� ���� ����� ������� ���� �� ��� ��� �� �� ���� �� ���� ���� ���/�� � ������ ������� ������ ��� ��������� �� �� �����������. 3. ������� ��� ���� ��� �� ��� ��� �� �� �������� ��� ���� �� ���� ������� ���������� ������ ��� ��������� �� ����� ��� ��������� ��� ����� ���������� �������. ��� ����� ��� ������� ���� ����� ���� ��������: �� ���� ���� �� �������� ��� ���� ����� ������� �������� ������ ��� ���������� �� ������� ���� �� ����� ��� ������� �� � ������ ������ �� ��� ������� �� ������� ���� �� �������� ������� ������� ���� ���������� ��� ����������� ��� ����� �������
4. ��������� �� �� ���� �� ���� ��� ��������� ���� ���� �� ���� ����� �� ��� ������������ �� ��� ������ ������� �� �����.
��� ���������� ������� � ��������������� ��� ��� ��������� �� ����� ���������
1. ������ �� ���� ��� �� ����������� �� ��� ���������: ��������� ��� ���� ������ ���� ��������������� �� 8 � 32�����, ����� ��������� �� ����� �������� �� 64 � 512� ����. 2. ����� ����: ��������������� ������� ���� �� 4�80��� ����� ��������� �� ����� �������� ���� �� 2.0�3.6���. 3. ������ �� �������� �����: ��������������� ���� ���� ��� ���� ����� ��������� �� ����� �������� ��� ���� 4�16 �����. 4. ������������: ��������������� ��������������� �� ���� ����� ��������� �� ����� �������� �� ���� �� ����. 5. ����������� ���: ��������� �� ����� �������� ��� ���� ���� ������������� ����������� ��� ���� ���������������.
��� ���������� ������� � ��������������� ��� � ������� ������� ���������������
1. � ��������������� �� � ����������� ���� �� �������������� ���� �� �������� �� �� ��������������� ��� ��������������. ��������������� ��������� ������� �������� ���������� �� ��������� ���/��� ��� �/� �����, ����� ���������������� ����������� ���/���, ������, ��������, ���������� ��� �/� ����� ���������� �� ��� ����. 2. ���������������� ��� ������� ������� �������� �� ������� � ����� ����� ��� �� �������� ���������, ���������, ��� ������� � ��� ���� �������� ������� ������ ���������� ���������, ����� ��������������� ��� �������� �� ������� � ����� ��� �� ������� ������� ���������. 3. �������������� �������� ������ ���������� ���� ��� �� ��� ���� �� ����� ����� ��������������� ��� ���������� ��� ��� ���, �� �����. 4. �������������� ������ ���� �� ������� �������� ����� ��������������� ������ ���� �� ���������� ������������ ��� ����� �������� � ������� ��������� �������. 5. �������������� ����� �� ���� ����������� (���� ��� ����� ����������� �� ���������) ����� ��������������� ����� �� ����� ���� ��� ����� �����������. 1�� � � �
A0066078X
Lin Shaodun
�� ���� ��� ��� ������� ������� �� ����� �� ��� �� ���� ��� ������������ ������������� ������������� ��� ���� ���� ��� ���� �� ������ � ���� ��������� ��� ��� ���� ���� ��� ���� �� ������ � ������� ������ ��������� ������������� ��� ���� ���� ��������� ��� ������ �� ���� ������ ��� ������� ������ ��������� �� ��� ���
������ �� ���18 ��� ���� �� ����� ����.
���18 ��� �������� 77 ������������. ������������� ������������ ��� �� ����� ���� ��� �� ����� (MOVFF,CALL,RCALL ��� LFSR ������� 20���� ������� ������� ��� ��� ������� ��� 32 ����)
��� ���18 ��� �������� 4096 ����� �� ���� ������, �� �������� 12 ���� �� ������� (000������) �� ������ ��� �� ��� ���� ���������, ��� �� ��� ������� ������ �� ��� ���18 ����������� (���� ������������ ��� 16 ����), ���� ����� ���� �� ��� ���18 ����������� ��� ���� �� ������ � ���� ��������. �� ���������� ���� ���� �� �������� ������� ��� ������ �� ��� ���� ������ �������� (���).
��
���18 ��� ��� 21 ���� ������� �������, ����� ������ �� �� ������ �� �� 2 ���� (� 2 ��) �� ������� ������. ����� 21 ���� ��� ���� �� ������ � ������� ������ ��������.
������ ��
������ �� ���� ������ ��������� �� ��� 18 ��� � 2 � 4096 ���� � 4��
������ �� ������� ������ ��������� �� ��� 18 ��� � 2 � 2097152 ���� � 2��
��
2�����
A0066078X
Lin Shaodun
�. ����� �� ����������� ����������� �������� �� ���� ��� �������� �������� �� ���� ��������� �� 0�200 �� 0�202 ��� 0��00 �� 0��02, (�.�, �0�200� ����� ���� �0��00�, � 0��00�, �0�201� ���� �0��01� ���) ���� ��� ����� ��� �� ����� ������ ��� ����� ��� ��� �� 0�000 �� 0�002.
#include
org
0x00
goto
start
org
0x08
retfie org
0x18
retfie temp
set
0x020
;temporary data register for swap
sum_temp1
set
0x021
;temporary data register for sum calculation
sum_temp2
set
0x022
;temporary data register for sum calculation
;Swap the contents of data registers at 0x200 to 0x202 and 0x300 to 0x302 Start movff movff 0x200,temp 0x200,temp
; temp = [0x200]
movff 0x300,0x200 movff 0x300,0x200
; [0x200] = [0x300]
movff temp,0x300 movff temp,0x300
; [0x300] = temp = [0x200]
movff 0x201,temp movff 0x201,temp
; repeat for another two pair of data registers
movff 0x301,0x201 movff 0x301,0x201 movff temp,0x301 movff temp,0x301 movff 0x202,temp movff 0x202,temp movff 0x302,0x202 movff 0x302,0x202 movff temp,0x302 movff temp,0x302 ;Sum two 3-bytes number and leave the sum in 0x000 to 0x002 movff 0x200, movff 0x200, sum_temp1
; sum_temp1 = [0x200]
movfw sum_temp1,A movfw sum_temp1,A
; WREG = sum_temp1 = [0x200]
movff 0x300, movff 0x300, sum_temp2
; sum_temp2 = [0x300]
addwf sum_temp2,W addwf sum_temp2,W
; WREG =[0x200]+ [0x300]
movwf 0x000,A movwf 0x000,A
; [0x000]= WREG =[0x200]+ [0x300]
movff 0x201, movff 0x201, sum_temp1
; repeat for another two pair of data registers
movfw sum_temp1,A movfw sum_temp1,A movff 0x301, movff 0x301, sum_temp2 addwf sum_temp2,W addwf sum_temp2,W movwf 0x001,A movwf 0x001,A movff 0x202, movff 0x202, sum_temp1 movfw sum_temp1,A movfw sum_temp1,A movff 0x302, movff 0x302, sum_temp2 addwf sum_temp2,W addwf sum_temp2,W movwf 0x002,A movwf 0x002,A end
3�� � � �
A0066078X
Lin Shaodun
�. ����� � ������� �� ������� ��� ������� �� ��� ������� �� �2 ����� ������� ������ �� � �����
������� �������. ���� ��� ������� �� � � ��� ���� ������ ��������� 0�21�0�22. ������� ���� �� ��� ����� ��� ��� �� ��� ������� ����� ��� �����, ��������� ��� ������. ���� ����� �� �� ����� ���� ��.� ������� ���� ���� ���� ����� ������ ���� ���� �� ���� ����� ����� �����. ; 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 no_of_loop
set
0x30
;loop index
sum_temp1
set
0x31
;temporary data register for sum calculation
sum_temp2
set
0x32
;temporary data register for sum calculation
sum_temp3
set
0x33
;temporary data register for sum calculation
org
0x00
goto
start
org
0x08
retfie org
0x18
retfie ;Read array into table start movlw movlw D’32’ D’32’
; WREG = array size (32)
movwf no_of_loop movwf no_of_loop
; set number of loops equal to array size
movlw upper movlw upper array
; read array to table
movwf TBLPTRU,A movwf TBLPTRU,A movlw high movlw high array movwf TBLPTRH,A movwf TBLPTRH,A movlw low movlw low array movwf TBLPTRL,A movwf TBLPTRL,A ;Add the sum of squared 8-bits numbers loop
next
TBLRD*+
; read an array element into TABLAT
movf movf TABLAT,W,A
; WREG = array element
mulwf TABLAT,A mulwf TABLAT,A
; square the array element
movf movf PRODL,W,A
; WREG = lower byte of squared number
addwf sum_temp1,F,A addwf sum_temp1,F,A
; sum_temp1 = sum_temp1 + lower byte
movf
; WREG = upper byte of squared number
PRODH,W,A
addwfc sum_temp2,F,A addwfc sum_temp2,F,A
; sum_temp2 = sum_temp2 + upper byte
BNC next
; branch if no carry
incf incf sum_temp3,F,A
; sum_temp3 = sum_temp3 + 1 (from carry)
decfsz no_of_loop,F,A decfsz no_of_loop,F,A bra loop
;Divide the result by 32 div div
movlw 0x05
; set loop count to 5 (2 5=32)
bcf
STATUS, C, A
; clear the C flag
rrcf
sum_temp3, F, A
; shift the bits to right 1 place
rrcf
sum_temp2, F, A
; “
4�� � � �
A0066078X
Lin Shaodun
rrcf
sum_temp1, F, A
; “
decfsz WREG, W, A
; have we shifted right five places yet?
bra div
; not yet, continue
movff sum_temp2,0x22
; move final result to 0x21~0x22
movff sum_temp1,0x21 nop array db db 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09 db db 0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x10,0x11,0x12,0x13 db db 0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D db 0x1E,0x1F db 0x1E,0x1F end
Start
No nd
move 2 byte of temp storage to 0x22
Is no_of_loop = 0?
Set no_of_loop =32
Yes
Read array to table
st
Read element from table
Set rotate count =5
move 1 byte of temp storage to 0x21
Clear carry flag
End
WREG = array element Shift the bits of rd 3 byte of temp storage to right 1 place (with carry)
Square the array element
Shift the bits of nd 2 byte of temp storage to right 1 place (with carry)
st
Add PRODL to 1 byte of temporary storage
nd
Add PRODH to 2 byte of temporary storage Shift the bits of st 1 byte of temp storage to right 1 place (with carry)
with carry
No
Yes
Any carry after this?
Rotate count - 1
rd
Increment 1 for 3
byte of temp storage
No
Is rotate count =0?
Yes
no_of_loop - 1
5�� � � �
A0066078X
Lin Shaodun
�. ����� � ������� ���� ���� ����� � ����� �� 100 ��, ���� ��� �������� ������� ������� ���� �� � ���������� �� �, ���� �� ����� �� 20 ����� ������� ������ �� ������� ������ ������ �������� ���� � ����� �������. ������� ���� ���� ����� ������ ���� �� ���� ����� �����. � ����. ������ ��� ����� ��������� �� �0��� #include no_of_loop
set
0x20
; loop index
delay_cnt1
set
0x21
; delay counter 1
delay_cnt2
set
0x22
; delay counter 2
delay_cnt3
set
0x23
; delay counter 3
temp
set
0x24
; temporary data register
result
set
0x25
; to store the final result
org
0x00
goto
start
org org
0x08
retfie org
0x18
retfie ; clock frequency is 40MHz, delay 100ms = 200x250x20x4x25nS start
movlw movlw
D'200'
movwf
delay_cnt1,A
delay_loop1 movlw movwf movwf delay_loop2 movlw movlw movwf movwf
D'250' delay_cnt2,A D'17' delay_cnt3,A
delay_loop3 nop decfsz decfsz
delay_cnt3,F,A
bra
delay_loop3
decfsz decfsz
delay_cnt2,F,A
bra
delay_loop2
decfsz decfsz
delay_cnt1,F,A
bra
delay_loop1
;Read array into table
loop
movlw movlw
D’20’
; WREG = array size (20)
movwf movwf
no_of_loop
; set number of loops equal to array size
movlw
upper array ; read array to table
movwf movwf
TBLPTRU,A
movlw
high array
movwf movwf
TBLPTRH,A
movlw
low array
movwf movwf
TBLPTRL,A
movlw movlw
0xff
; set [result] data register to highest
movwf movwf
result
; 8-bits number
movlw movlw
D’7’
; load mask ‘0000 0111’ to WREG
TBLRD*+
; read an array element into TABLAT
movff
TABLAT,temp ; copy TABLAT to temporary data register
andwf andwf
TABLAT,F,A
; AND WREG with ‘0000 0111’
6�� � � �
A0066078X
Lin Shaodun
bnz next bnz next
; branch if not a multiple of 8
movf movf
temp,W,A
; WREG = array element
CPFSLT CPFSLT
result,A
; compare array element with result data
movff
temp,result ; register, if the array element is smaller, ; put the array element into result register
next next
decfsz
no_of_loop,F,A
bra
loop
nop array
db db
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09
db db
0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x10,0x11,0x12,0x13
end
Start
Read Array to Table
delay_cnt1=200
No No_of_loop = 20 Read table element
delay_cnt2= 250
Is no_of_loop = 0?
AND table element delay_cnt3= 17
Yes
with ‘0000 0111’
END NOP
Yes
No Is result zero?
delay_cnt3 - 1
Compare with
Yes
No
result register
Is delay_cnt3 = 0?
No delay_cnt2 - 1
Is it smaller than result register?
Yes
Yes
No
Put the smaller
Is delay_cnt2 = 0?
element into result register delay_cnt1 - 1 no_of_loop - 1
No
Yes Is delay_cnt1 = 0?
7�� � � �