G.R. Vossoughi, Sharif Univ. of Tech.sina.sharif.edu/~narimani/appelec/8051.pdfMechatronics...
Transcript of G.R. Vossoughi, Sharif Univ. of Tech.sina.sharif.edu/~narimani/appelec/8051.pdfMechatronics...
Mechatronics Spring 1387
G.R. Vossoughi, Sharif Univ. of Tech. 5
5O+P+K�J1+@8$ 8051
5O1O28P1� -.S� �>�.K8�$�J1+@8$ %+@F(!
"FS� ��Dc� �.K8�$�J1+@8$ u' )+'� l@C E,�G$ (�� ����� ! CPU YRAM YROM YData bus Y
Address bus YControl bus YInterface Ciruits &Peripheral
ROMRAM
CPU
Interface circuitry
Peripheral devices
Address busData bus
Countrol bus
!" #$%&�'��( )$*�� + �, :(CPU) Central Processing Unit
�Dc 9'+hh� "3�3� +�.8�$�J1+@8$ u' "$ �.`T$ 78P�/^ %.C *�> "FS� u'CPU �� ����� !
(1 *�hh>�J 7^�'��, "��h8F(! )98$�+h^ �' (��1 2[^�y �hhhhm); *�+hc� (Fetch & Execute Instructions) (2 w�hhh<)� "hh?G�$ 1 "��hh 3�T$ R�hh8F(! (Arithmatic/Logic Operations) (3-+K�J jFKk$ ��Dc� jFKk$ R�8F(! *��, B�$� 1 �8�+� +�.8�$�J (Sequence/ Timing Control)
"F:�% ��Dc� u' CPU �� ����� ! :
�Registers 2[^�y �' *�hh> "F:�% CPU:*%��/� *�+, �m); �� *%��/� 1 "$.(! \��hh�$ *�+, +p'% \��hh�$ "S�: "$��C�h,
)�hh�)�$Status Register �'Stack pointer (
�(PC) program Counter:a�%; I�)���mp) *�/, v: 2$�)+, *�/, "��h8F(! �J �' (next instrudion) 2[^�y �%
�(IR) Instruction Register :�Jbinary *�+, B�$+^ (instruction) -�y�% �% Y�+c� IR*���mp) "$ %.C .
Instruction Register (IR)
Special purpose RegistersInstruction
decode and countrol unit
Arithmaticand Logic unit
Program Counter (PC)
General purpose Registers
Mechatronics Spring 1387
G.R. Vossoughi, Sharif Univ. of Tech. 6
�Arithmatic Logic Unit (ALU) :"_�'� 1 "?G�$ R�8F(! B%�1;�% �+hhc� 2, l$�!)98$�+^ E z +, �� "K^�'�% Instruction decoder / Control unit(
�Instruction Decode/Control unit:+8 /� lh$�!)2h(c+� (�hJbinary *�+,instruction �% %.c.$IR1w�<)� %�.$ R�8F(! ��8) )R�8F(!"hhP+K�J 18�+I(7mc �+hhc� 2, B%�1;�% instruction +[)%�.$
��J 9'+� I�(! �C I��hhC� �A H 2J �.G)�hh(>CPU *�+c� 1 2[^�y �� 98$�+^ �' "��8F(! *�hhhh>�J ",�'��, �C�, "$ �m); .",�'��, 2,instruction 2[^�y �� "��8F(! *�>�J �'fetching %.C "$ 2K#W .
Address bus
Data bus
CPU
RAM
NProgram Counter
OpcodeInstruction
RegisterClockRead
Control busN+2N+1NN-1
Opcode
�R�8F(! 2Fy+$ �' ",�'��, (Fetch) B%+J 98$�+^ ��2[^�y %.: �C�, "$ +'� ly�+$ l$�C .
1O*�.KT$ )Program Counter (PC *1� +,Address bus 2KC�VW "$ %.C.2O"P+K�J -��p83 READ %%+W "$ -�/^ .3O98$�+h^ (instruction) �K'% �'(data) I�h)�.: 2hz.,+$ a�%; �� *1� +, 1 I�C data bus ��+H I%�%
%.C "$ .
4OB�hh$+^ �.hh,D$(instruction code) ��*1� data bus 2,+hhhK`8c� B1�% "hhF:�% IR(Instruction Register) %%+W "$ l?K�$ .
5O*�.KT$ Program Counter) PC (I%�% &'�D^� "$ %.C)*�+, "hhhW%�$; 7mc l@83 Fetch *�/, (
Mechatronics Spring 1387
G.R. Vossoughi, Sharif Univ. of Tech. 7
�2Fy+$ �' R�h8F(! *�+c� 98$�+^ (Execution stage) B�$+^ �' �J +8 /� l$�C ",�'��, I�C (instruction decoding) Y�8P.� 7mc ��h8)%�.$ "hP+K�J *�hhhmP��p83 -�3�� *�.hhKT$
register*�h> 2, "F:�% B1�% ALU �8P.� 1 e@/P�, 1*�+c� 7mc ��8)%�.$ "P+K�J *�hhmP��p83 v3.� "_�'� 1 "?G�$ R�8F(!ALU �C�, "$ .
�2!.(<$ *�>�J Y 98$�+^ 1 "��8F(! �� 2$�)+, u' "$ l8@n� �>%1%+@F(! CPU v3.� l$�J �.G, 2$�)+, }kn$ %%+W "$:Program “drives” the CPU
�u'I�W�VW (bus) , "J+Kn$ \�> �, 2J -�h�p83 7'��> �.G: �' Z83 *%��/� �� 73� l@nK$ 2K^+W ��@ �).C "$ .23 �� u' +> &?) 1 %+@F(! I�W�VW &k, �% *�y �� �.,D$ instruction Fetch �'%+W }kn$.
-#$%&�'��()$*�� + �, :.�/�0/ "$��( ) 1��2 3���� ��� (Address, Data & Control bus)
��%a�%; 2@8��.S a�, u' *�+, cpu *���%n78, Y�C�, cpu a�%; "'�hh)�.� �.,D$">% +tJ��y 2n7'�, 7C�% �hh>�.: �� .+hhhP+K�J1+@8$ *�+, 8051a�%; a�, 169'� �VhP 1 73� "K8,
+P+K�J1+@8$ "$ +tJ��y �� �)�.�65535=216 �' 2[^�y 2KC�% �C�, .
��%2hh@8��.S a�, �hK'% u' *�+, CPU *���%n78, 73� w.m#$ 9'�, 9'� Y�C�, 2Jlh@83 +> �% Read �'write v?^n78, I%�% 98, CPU Y" )�c R�D8m<� 1 2[^�hhyI/O)��)�h$HD1�.hK8)�$1(... %%+W "$ l?K�$ .�+! �VPdata bus)�.G: %��/� �' a�,�K'% (�� "@' Zm$ l$�.! 7!+3-%�hh � +�.8�$�J1+@8$ u' B1�% R�!Az� �.`T$ �'%+W 1��2�kn$ *�h> 988/� Zm$ I���J
R�!Az� 0��%+f 7!+3 v3.� CPU %%+W "$ �.`T$ ."$ 2K#W "KH1 �Aht$ �.`31+f1+@8$ 2J %.C P-IV Y3273� w.m#$ 9'�, 9'� 73� "K8, 2J9'� �% �.`31+f1+hh@8$ data bus 3273� "K8, .
�%8051 a�, �K'% 873� "K8, .
�a�%; a�, 2^+hhhG@' I��.(> 73� (uni-directional) 1% I��.(> a�,�K'% 2@8��.S�% 73� 2^+z .
65535641024
Kb =
Mechatronics Spring 1387
G.R. Vossoughi, Sharif Univ. of Tech. 8
�-+K�J *� 2!.(<$ l$�C a�, "'�hhhmP��p83 �� 73� 2J&?) I���J ��>�(> -�hhh?K)� jFKk$ R�8F(! R�!Az� *1� +, a�%; 1 a�, �� a�,�K'% "$ ��% I�m! ��C�, .*�2hh).() �mP��p83 2hhh).p�'� �� B�.� "$ Clock 1READ 1WRITE ��%+, w�) .w�)1%��/� "P+K�J *�hhmP��p83 &?) 1�m); 1 I%., �.�K$ ��8`,
*%�'� "pK`, �.) 2, CPU %��%.
4#$%&�'��()$*�� + �, :5 ��67% ��)�) ,)$8-R�Dh8m<�I+8:� *��hh3 I. )� (mass storage) :��)�$FDD YHDD YCD-ROM
-�� ��� R�D8m<� �,B�`)� (Human interface) :-��8$+� ��)�$Y+hhK�'+f YKeyboard YMouse Yspeaker
-"��z� ��� R�D8m<�(Communication) :2@ C 7)+K�'� *�hhh> Yprofi 1R+f *�> 1 *��.$ �'+3-
-R�D8m<� "P+K�J 1��'�.K8).$ :��)�$ 1 �>�.`�3 ��.)� �>+pF(!
�� ��� l$�.!:*�>%+, �' �>���$
2G3�1
�#$%&�'��()$*�� + �, :9$� � �: �w+) i].(/$ 2J *��Dhh^� Z8?K`$ ��hh ��� �,user ��+H+, �� "$ %��hh3 B�.�/, Application
Software 2K:�hh�C "$ %.C w+) 9'� Y�� ��D^� E'+z 2'] 1% w+) �, *��D^� 7k3 ��D^� �� ��� �% �C�, "$ .9'� ��K:�3 v3.� w�+W�'% 73� �'+n� l,�H l,�?$ .
�Input/output Subroutines Z8?K`$ �.hhhhG, �hh, 7k3 ��Dh^� )��)�$keyboard YCRT YHD%+, 1D/A 1A/D 1(... a�(� �%
*%1�1 28FJ 1 I%., �>"c1+: 1�� +h�.8�$�J u' *�> 98�� E'+z Subroutine w�<)� "'�hhh>%+8W "$ .2hh).p�'� Subroutine �hh>
“��$.(! “�%ROM *���mp) I�C )��)�hh$Basic I/O System �' BIOS �%IBM-PC (2, ��$.(! 1B�,� assembly 2KC.) "$ �).C.
��� I%�hhh#K3�I/O subroutines ��$.(! �,%1�1 *�hhh>+K$���f 2hhhz.,+$)(Subroutine parameters ��E'+z initialize B%+J
Application Software (user interface)
Operating System
Input/output routines(access to hardware)
Hardware
*�hhhh>+K`8c� �� *%��/�CPU w�<)� �� ef 1 I�'%+hW ��h�;subroutine call YE'+z �� 2FS�y �'�K) *�hhhh>+K`8c� �� +p'% *%��/�CPU �% "�kn$ *�h> a�%; E'+z �� �' 1RAM %.C "$ 7^�'��,.
Mechatronics Spring 1387
G.R. Vossoughi, Sharif Univ. of Tech. 9
�I1A! +, I/O subroutine �h>YROM *1�y 2$�)+, u' start-up 73� 2n8(> 2J ef��reset B%+hhhJ CPU %%+hhhW "$ -�hhh/^ .ef��reset B%+hhhhhJ CPU YI��.hhhh(>(pc) program counter 2,a�%;"hh�kn$ B1�% ROM –�].(/$0000H –+88��2K^�h' �VP 12h$�)+, �% %.c.$ a�%; .,D$�)B�(> 2$�)+,startup(v3.�CPU �+c� �'%+W �>�.: )�% �F�� 2K P� a�%; 0000H u'jmp 2,*+p'% a�%;
�%ROM w�<)� 1 %+8W "$ ( ... 2$�hhhhh)+, Start up I1A! +, w�<)� "��hh8F(! B.�(> u�B%+J 1 2[^�y �8! ",�' *�hh> Y-.(/$ �,B%� ��S u'routine B1�% +p'%ROM w��,Bootstrap Loader YTrack -1� uh`'% �� �� lh:�% 2, RAM I�)�.:�+^1�� -+K�J "$ B��, �)���3 .9'� 2$�)+, "K(`H ��Operating
system �� ��*1� u`'% 2[^�y l:�% 2, (RAM) I�)�.:�+^1�� -+K�J "$ B; 2, �)���3 .
�Operating Sytem I1A! 9h8$�h� +, interface user)�hhh�)�hh$DOS Command Language (*���% B�8nk, 7P.m3 7mc "'�hh>��D,� 2,2$�)+, "`'.) �8P.� 1Application Software �C�, "$ .
5O2O98, R1�#� �.`31+f1+@8$ 1+P+K�J1+@8$
��].S�*D8� �.h`31+f1+@8$ Dcu'CPU 2J*1� +, u'IC��+H I%�% I�C 7`8) )�%"�8$ �>+�.�8$�J 1main frame �>CPU %.:l@nK$ ��9'��� IC1"Ky 73� %+, 9'��� (
��� +�.8�$�J1+@8$ u' +W� l@nK$ ��B�n) ��Dc� I%�% �% I�C w�+W�'% +'� Z8)��, �%+P+K�J1+@8$ u' ��Dc� 28FJ I%+ $�) �%u'ICI%�% *�c 73� I�C .��8`, �% ��$.(! 2K P� �� R�hhh8S.�: )�hh�)�$ &'�<�WRAM 1ROM Y%��/�R+f -�'+3 *�hhhhhh> �' 2#H1 �.G: %��/� 1interrupt (u'
+P+K�J1+@8$ ��8`, 73� 9@($ +�.8�$�J1+@8$ u' �� +�%1�T$ �C�, .2K P� 7'%1�T$ �h, �.,D$ *�> %��.$ 2, 2c.� �>+P+K�J1+@8$ %+,��J )�%ZK`83 "P+K�J *�> (73� 28c.� l,�H .
ROMRAM
CPU
TimersInterruptcontrol
Serial interface
Parallelinterface
ParallelDevice
Serial Device
External interrupts
External Clocks
InternalClocks
Mechatronics Spring 1387
G.R. Vossoughi, Sharif Univ. of Tech. 10
��,2, 2c.� 2@�'� %�.$ ��Dc� �F�� *�+, ��8) 7:�3 u'%+, "P+K�J )%+,+hhhP+K�J1+@8$ (�%u'IC*�cI%�% �VP I�C �, +P+K�J1+@8$ *�>%+, "(J ��8`, %��/� IC"$ 7:�3 l,�H 2J ��C�, 9'� "y�+z 2�'D> 1
7:�3 2).p�'� �>%+, ����8`, 2p) 98'�f "$%��%.
��� "@' 2�kn$ *�> "FJ �>+P+K�J1+@8$ Y�m); "'�).� �%0��%+f 2#H1 (interrupts) 73�.�h, 9'� "'�)�.�
"P+K�J ZhhK`83)+hhhP+K�J1+@8$ ("$�)�.� 2, b'�H1 (events) jFKk$)eP�f ��)�$ *�hhhhh>clock �'
78/_1 *�> *��+G_� (��/'+3 .pk3�f �C�, .
��� +p'% "@' R1�#� *�> 98, �.hhh`31+f1+@8$ u' 1+P+K�J1+@8$ 98$�+^ �' "��8F(! �J %��/� 1 j8z �% (instruction set) 73�.�, �.hhh`31+f1+@8$ u' \�> R�!Az� 0��%+f )98p�3(�, B%+J��J "'�).� Y
I%�% *�> 2Y"K'�,4"K'�, 18"K'�, Y7P.m3 ��J 2'��; �, B%+J *�> ��D, (arrays) 1... I�'%+W "y�+z 1*���% �VP *�>�J "��8F(! (instruction) �>%+,��J 2).p�'� *�+, "S�: �C�, "$ .
�
P� u' B%+J 0.$�: 1 9C1� \�> 73� 9@($ +hP+K�J1+@8$ u' �% 2@); -�y �� �C�, 2,�n$ *�>��J 1 2�� *��8`, Y2hh�'D> 9KC��mp) 98'�f �.[�(, Y1� 9'�instruction u' �% %.c.$ ">% a�%; *�>�$ 1 �>
"'�)�.� 2@); -�y Y%.C "() I�'% +hhhP+K�J1+@8$ u' �% �.`31+f1+@8$ v3.� l?K`$ �.G, �> 78, -+K�J instruction 73� +'Vf B�@$� +P+K�J1+@8$ �% "S�: *�hhhhh> ) .%��/� &'�D^� �,instruction 1 �hhhh>
1 7:�3 1 "y�+z *�hh> 2�'D> ... �,�' "$ &'�D^� ��/,+3 .+P+K�J1+@8$ u' �% -�t$ *�+,8051 %1�y 000/60�% 2K^� ��@, �.K`'D)�+� %��/� 2@); -�y 1 2K^� ��@, �.hK`'D)�+� �.hh`31+f1+@8$ u'80286 %1�y000/134 u' �% 180486 %1�y 2/1u' �% 1 B.8F8$pentuim %1�y1/3�.K`'D)�+� B.8F8$ �% 1pentiume pro %1�y5/5�.K`'D)�+� B.8F8$ Pentium IV %1�y 42B.8F8$ 73� I�C \+�$ (.
Mechatronics Spring 1387
G.R. Vossoughi, Sharif Univ. of Tech. 11
5O3O7k3 R��kn$ I%�.)�: *�>+P+K�J1+@8$ *��D^� MSC-51
�MSC-51 I%�.)�: *� 2J ��K`> �>+P+K�J1+@8$ �� �%2>% l'�1� 1980 7J+C v3.� INTEL ����, 2,1 I�C 2_+!e�3 7J+C *�hhh> jFKk$ e�('� ��)�$ YAMD YFujitsu 1philips �,��8K$� 9hK^+W
Y 7:�3 "'�mP�$ jhFKk$ ��B;��)+>"'�)�.� �, u' *�> ��: %.: (2_+! ����, 2, �)%.() :
63x16-bit256 x 8-bit8K x 8 UV-EPROM8752
52x16-bit128 x 8-bit4K x 8 UV-EPROM8751
63x16-bit256 x 8-bit8K x 8 ROM8052
52x16-bit128 x 8-bit4K x 8 ROM8051
63x16-bit256 x 8-bit-8032
52x16-bit128 x 8-bit-8031
InterruptsTimers/event Counters
Memory RAMInternal ROMDevice
���Dc� ")1�% ��K:�3 7k3 R��kn$ 1I%�.)�: *��D^�MCS-51 �C�, "$ +'� l@C E,�G$.
Internalbus
Mechatronics Spring 1387
G.R. Vossoughi, Sharif Univ. of Tech. 12
�8051)��$.(! 1I%�.)�: MCS-51( �%1%w+^ DIP 40"�8f 1PLCC 44"$ 2_+! "�8f 2J %.C I�(! R1�#� �m); *�hhh�, 2K`, I.T) �% (packaging) �C�, "$ .�.) *�hh�, 2K`, �%PLCC 498f ��
44 pin A,I%�#K3� 73� (NC ≡No connection)
T2T2EX
8032/8052 only
+> &?) �� u' 40�% 2K^� ��@, 98f +P+K�J1+@8$ 8051 73� +'� �+n, ���K:� 2, :
�� ��^+S 2@8��.S �%RAM 1ROM Y%%+W I%�h#K3� "F:�% P∅u' B�.�/,Port 8"$.(! \���$ 7mc *��.$ "K8,
73� I%�#K3� l,�H .R�.��'� +8� �%P∅B�.�/, ��J+Kn$878, a�, a�%; -1�)2[^�hy -1� l@83 Z8) �% (a�,�K'% u' 18
"K8,)2[^�y w1% l@83 Z8) �% (�, �� ��� 7mcRAM 1ROM �'�() "$ l(! "c��hhh: .-��p83 ��“ALE” 7mc
demultiplex %.C "$ I%�#K3� �K'% 1 a�%; B%+J.
R+f P1u'Port 81 "$.(! \���$ 7mc *��.$ "K8, "c��: *�>��D^� 7k3 �, �� ���(bit addressable)
�� ��^+S 2@8��.S �%RAM 1ROM Y%%+W I%�hhh#K3� "F:�%P2 u' B�.�/,Port 8l,�H "$.(! \���$ 7mc *��.$ "K8,
73� I%�#K3� .R�.��'�+8� �%P2�.�/,B8a�%; w1% 78, a�, )�, �� ��� 7mcRAM 1ROM "c��: (�'�() "$ l(!.
Mechatronics Spring 1387
G.R. Vossoughi, Sharif Univ. of Tech. 13
9'� 98f 2, 98fOE(Output enable) bH�1 *1� +, ROM %%+W "$ l�K$ "c��h: .w�hhp�m, Instruction Fetch)",�'��, "��8F(! �J ��ROM (9'� -�hhh�p83 Low "$%.C .
e)�J+^Oscillator �].(/$MHz 12��MHz 16�C�, "$.
Program Store Enable
*�+,demultiplex 1 B%+JLatch u' 2, a�%; B%+J "c��: +hK`8c�)*1� ��P0(%1� "$ ��hh@, .�� ef
*1� +hh, B; B�C ���h'�f 1 a�%; �.�yP0YALE -�hhhhhhh�p83 B�.�/,address latch YHi%%+W "$.
Address Latch Enable
�hhhhh,Hi9hh'� B�hhCPin *�>+K`8c� +hhf I�C 988/� &8f �� *+'%�?$ �, "F:�%
%+W "$ )�)R�8F(! *�+, start-up(
Master reset
+hhhhW� EAYHi%.C 2KC��mp) Y8051 �� ROM "hhhhhF:�% 2$�)+, B�)�.: 7mc )��K4"�8'�f
2hh[^�y (I%�hhh#K3� "$�'�() .�%2h@8��.S EALow %.C 2KC��mp)Y-�h�p83 �� I%�#K3� �, PSEN ��
ROM :��"c2$�h)+, ")�.:��, 7mc )�%K4"�8'�f 2[^�y (I%�hh#K3� "$�'�() ) ."���hhh /,ROM "F:�%
-�/^+8� %%+W "$ (External access
Timer/counter 2 capture/ reload91HT2EXP1.1Timer/counter 2 external input90HT2P1.0External data memory read strobeB7HRDP3.7External data memory write strobeB6HWRP3.6Timer/counter 1 external inputB5HT1P3.5Timer/counter 0 external inputB4HT0P3.4External intereupt 1B3HINT1P3.3External Exterrupt 0B2HINT0P3.2Transmit data for serial portB1HTXDP3.1Receive data for serial portB0HRXDP3.0
ALTERNATE FUNCTIONBIT ADDRESSNAMEBIT
Mechatronics Spring 1387
G.R. Vossoughi, Sharif Univ. of Tech. 16
128
7'�,
RA
M"F
:�%)
*�+,
\���
$"$
.(!
((B-register) used inMUL AB; DIV ABAccumulator(A register)
Program Status Word)
Interrupt Priority Reister
Port P3
Interrupt Enable Reister
Port P2
Serial Port BufferSerial Port Control Register
Port P1
Timer 0 & 1High and Low byte
Timer Mode RegisterTimer Control RegisterPower Control Register
Data Pointer High and Low byteStack Pointer Port P0
RAM (128 Bytes) (21 Bytes)
128b
its
Used By serial Communication Prog. in ROM
21+K`
8c�7
'�,"F
:�%*�
hhhhhh
>)
��:
\���
$�,
(
���8��RAM )�8 � ���$���,�
�128 7'�, RAM Y"F:�%%.: 23 2, 28y�) Z8`?� I�'%+W 73� .
•General purpose RAM)a�%;H30"P�F H7:(1%01� �� I%�#K3� 9'� �� 28y�) %.c1 2[^�y %��% .
)1(a�%; Z8?K`$ ">% (Direct Addressing) ��)�$ :mov A, 5FH ; Move the content of the 5FH address into Accumulatormov 5FH, A ; Move the content of the Accumulator into 5FH addressmov R1, 5FH ; Move the content of the 5FH address into the R1 registermov 5FH, R1 ; Move the content of the R1 register into 5FH address
)2(a�%;">% Z8?K`$+8� (Indirect Addressing) ��)�$ :mov R0, #5FH ; Move number 5FH into the R0 Register mov A, @R0 ; Move the content of the memory location whose address is inside
R0 into the Accumulator (i.e. move content of 5FH into Accum.)
2K P� lJ 128 7'�, RAM 2,1% 98(> l,�H E'+z ��K`> I%�#K3� .
•bit addressable RAM)a�%;H20"hhhP�FH2:(At$ 7mcSET B%+J �hhh' CLEAR B%+Jbit I��(C 7,�, �� ZK#>CH2)��RAM "F:�% (��98$�+^ “SETB 67H” 1“CLR 67H” I%�#K3� "$ %.C .
Mechatronics Spring 1387
G.R. Vossoughi, Sharif Univ. of Tech. 23
8051 instruction set summary
Mechatronics Spring 1387
G.R. Vossoughi, Sharif Univ. of Tech. 24
; This program turns on the LEDs attached to port P1 in a decreasing-binary-number order
ORG 2000H
DEL EQU 040H ;DELAY TIME OR SPEED FACTOR
START:MOV DEL,#01
MOV P1,#0FFHL: DEC P1
CALL DELAYSJMP L
DELAY:MOV R0,DEL
DEL1: MOV R1,#0FFHDEL2: DJNZ R1,DEL2 ; Internal Loop
DJNZ R0,DEL1 ; External LoopRET
END.
A Sample Program: test.asm
C> T51 tstC> simC> EVTRNC2
; Assembles and Locates the program; program simulator …followed by F7….”P”….”N”….”test”….”F5”; Download the program from COM2: port onto the 8051 board RAM
5O6O%+@F(! Timer �>
�%+h@F(! Timer 2,�n$ ��hhh8`, u' %+@F(! “ripple counter” 73� 2hhhJ �%&k, 6O5O4B��, 2K:�%+f �C .�,*%1�1 -���� Timer 2,eP�f Clock e)�J+^ �, B�D8$ Y98/$ 0��hh(C "$�)�.� B�.�/,
*�+, "'�� $ %�.$ B�$� 2 3�T$ I%�hhh#K3� %+8W ��+H .��"hh^+z 2@8��.S�% 2, *%1�1 +hpC��(C )2 P �At$ -��p83 u' I�)1� 98'�f (�.H1 2/H�1 u' �C�, +hhhpC��(C I�hhhp); B�.�/, u'Event Counter l,�H
�>�.: I%�#K3� %., .���(_ +pC��(C B�.hh�/, u'Z`?$ e)�J+^ "$98'�f e)�J+^ �, "P��p83 �)�.�+���7mc \���$ +p'% �8P.� �'�() .
��%8051 YTimer *�> "F:�% *�+, 23\�> l,�H jFKk$ ��K`> I%�#K3� .
jP� (Interval Timing ... 7mc �8P.� -��p83 "c1+: )B�$� \+/$ (98/$ lS�.^ �%
�(Event Counting ... b'�H1 0��(C *�+,)B�(> 2J 2 P I�)1� 98'�f *�> *1� +, u'pin 73� ��: (
¬(Baud rate generation for built-in serial port ... �8P.� e)�J+^ �, "P�hhh�p83 98/$ 2J\+/$ baud rate -�'+3 R+f *�+, �C�, "$ .
�8051*���% +('�� 1% 16"K8, *�m$��, timer 0 1timer 1 �C�, "$ .
Mechatronics Spring 1387
G.R. Vossoughi, Sharif Univ. of Tech. 29
•�� I%�h#K3� 7mctimer ��K,� �> 78, �'�, 2z.,+$ *�hhh> �% +K`8c� TMOD )*�+, I.T) Z8[�� �>+('�� %+@F(! (+'%�?$ 2, +[)%�.$ set �).C.��h@�'� v?^ �� @' �% 1w�<)� 2$�)+, *��K,� "$%.C.
•%+, �% "C�.$; %�.$ I%�#K3� 9'� �% +hh('�� �� aAJ 1(Timer 1) *�+, Z8[�� baud rate �%R+f I%�h#K3� -�'+3 "$ %.C )*�+, 98, �� ��� %�<'� +hh�.8�$�J PC%+, 1+P+K�J1+@8$ .(�VP �'� ) 2c1 8m,
78, 2z.,+$ *�hhh> 2, Timer 1 �%+K`8c� TMOD )*�mK8, 4"P� 7(�).C *��hhh@K3% .
57-OInterrupts•u'interuupt 2hh#H1 �' �� 7h`��� ! �1+, 2/H�1 u' (event))")�$� �% ��$.(!}kn$+8� (2J+d� �%
B; X$�)+, ��KH.$ Y�+c� -�y �% �hh>� 1 I�C *+p'% X$�)+, w��, (ISR) Interrupt service routine 2J7mc 2#H1 2, ">% ´3�f (event) 2KC.) �+c� 2, I�C "$�% �'� .2$�)+, ISR 2Jw��, Interrupt
Handler 2K:�h�C D8) "$ %.C ef ��Y�+c� �� -+K�J 2,2$�)+, X8P1� )2$�)+, l H �+c� -�hhy �% ���.H1 2#H1 ("$+, �)�%+W.
main
ISR
main
ISR
main* ** * **
Time
* Interrupt ** Return from Interrupt service routine
Interrupt level Execution
Base level Execution
��hC�, 2hKC�% �� 2#H1 2, "'.pk3�f "'�)�.� 2J *+hh�.8�$�J ZK`83 u' %.hc., �� "h(>.� 98�� Y�>% "$ w�<)� �� jFKk$ ��J 9'��� B�h$D(> �.G, +�.8�$�J 2J %�1; "$ .2J %.C "$ 2K#W ��hh$.(!
�% �+hhhhc� 73% �% "FS� X$�hhhh)+,base-level )�'Foreground (*�> 2$�hhhh)+, 1ISR �%Interrupt-level)�'background (�).C "$ �+c�:
��%8051 Y5jFKk$ b �$ *�+, �8P.� interrupt %.c1 2#H1 �' %��% :
Two Ext. Interrupt1% 2#H1 "c��:
INT 0 , INT1
Two Timer interrupt1%I�C �8P.� 2#H1 2F83.,
Timer 0 1 Timer1w�p�m, Over flow
One. Serial port interrupt(-�'+3 R+f v3.� I�C �8P.� 2#H1 u')
+ +
Mechatronics Spring 1387
G.R. Vossoughi, Sharif Univ. of Tech. 30
�2#H1 �.H1 �� +> E'+z �� u' 5b �$ �� �.hh,D$ -�/^ B�.� "$ (enabled) �'-�/^+8� (disable) %.() .l(! 9'�v3.� +hhhhK`8c� Interrupt Enable) IE (�% bH�1 a�%; 0A8H w�hhh<)� "$ %.C.��8
78, +K`8c� IEY5-1� 78, *�+, �' -�/^ 9K:�3 -�/^+8� +>�� u' 2#H1 �.hhh,D$ *�hhhh> 1 �C�, "$ +:; 78,)78,ZKn> (7mc -�hhh/^ *��3 �'-�/^+8� *��3 2#H1 28FJ *�hhhh> "$ ��@, %1� :
Enable Timer 0 interruptA9HET0IE.1
Enable external 0 interruptA8HEX0IE.0
Enable external 1 interruptAAHEX1IE.2
Enable Timer 1 interruptABHET1IE.3
Enable serial port interruptACHESIE.4
Enable Timer 2 Interrupt (8052)ADHET2IE.5
UndefinedAEH-IE.6Global enable/disableAFHEAIE.7
DESCRIPTION(1=ENABLE, 0=DISABLE)
BIT ADDRESSSYMBOLBIT
���2hh@8U�<); "�/, �1+, b'�hhhH1 73� 9@($ 7'.P1� �� ��C�, ��%�.h:+, *+hhh�]�, �F�� �% �VhhP Y�>�.`31+f1+@8$ B�@$� 7'.P1� 2#H1 *��, �>%.c1 1% +W� 2hh@'.T�, %��% �.G, 2#H1 B�$D(> �.H., �h)�).8�, Y
a�3� +, B�.K, Y7'.P1��8�+� *�+, �� "S�: B%� ��S ISR �>*(Interrupt service routines) 2z.,+$ �C lU�H .2J %��`8$ Z>�+^ �� B�@$� 9'� 98��(> �> 2#H1 B%+J *��, 7'.P1� +W� w�<)� w�p�m, ISR Yu'2#H1
*+hhh�]�, 7'.P1� �, �.H., %�).8�, Y�)�.K, �.h`31+f1+@8$ Y ��KH.$ ISR 2z.,+$ 2#H1 2, �,78P1� 98'�f +��>� �� 2K:�3 2, 1"'.pk3�f X#H1 2, %��%+�, +�]�, 7'.P1� �, .
�.^ -1�c E z +,Y-�t$ *�+, YE'+z �� 2#H1 B%+J -�/^ 7mc Timer 0 78, Z> �'�,ET0 78, Z> 1EA B�$D(>Set �).C :
SETB ET0SETB EA
*�> 2#H1 Y+[)%�.$ *�> 2#H1 B%+J -�/^ �% 2J %.() 2c.� �'�, ���(_ +p'%)-�'+3 R+f 2#H1 ��)�$ (v3.� 2J %.n) I%�% +88�� Y%+8W "$ ��+H I%�#K3� %�.$ -�'+3 R+f 2$�)+,.
Mechatronics Spring 1387
G.R. Vossoughi, Sharif Univ. of Tech. 31
Poriority for Timer 0 interrupt0B9HPT0IP.1Poriority for external 0 interrupt0B8HPX0IP.0
Poriority for external 1 interrupt0BAHPX1IP.2Poriority for Timer 1 interrupt0BBHPT1IP.3Poriority for Serial port intrrupt0BCHPSIP.4Poriority for Timer 2 interrupt : (8052)0BDHPT2IP.5UndefinedIP.6UndefinedIP.7
DESCRIPTION(1=HIGHER LEVEL, 0=LOWER LEVEL)BIT ADDRESSSYMBOLBIT
��,�.^ -1�c 2, 2c.� *�+, -�t$ w�hh<)� �, B�$+^ “CLR PT0” 1“SETB PX0” 7'.P1� �8P.� 2#H1 v3.� I�CTimer 0 +hhh,�+,“+hh#S ”17'.P1� vhh3.� I�C �8P.� 2#H1 -�hh�p83 "c��hh: INT 0
+,�+,”1“�h>�.: %., .�8�+� 9'�, 17'.hP1� 2#H1 INT 0 7'.hhP1� �� +�]�, 2hhh#H1 Timer 0 �hh>�.: %.,.-� )�,reset +P+K�J1+@8$ B%+J 8051 7'.P1� 2#H1 28FJ +,�+, �hhh>“0”Z8[�� �>�.: �C.
�+hhP+K�J1+@8$ �%8051 7'.P1� �G3 1% v?^ �AJ (Two priority levels) *�hhhh> 2#H1 *�+, 73� 988 � l,�H jFKk$ .�� u' +> *��, 7'.P1� 7mc5u' +h> 2, �.,+$ *�> 78, Y2#H1 �8P.� b �$
+K`8c� �% �> 2#H1 ��(Interrupt priority register) 2, B�.� "$ ��0�'1%�% +88�� .
�R�.S �% 1H�.1%2#H1 B�`@' 7'.P1� �, �.G, B�$D(> Y+P+K�J1+@8$ �8�+� 7mc �� "S�: Poll B%+hJ 2#H1 7'�!� �hh> "$ �'�() .�8�+�Polling �� 7`��� !INT0 YTimer 0 YINT1 YTimer 1 Y
Serial port)1Timer 2 *�+hh, 8052 (�8�+� �VP Y�.,D$ 988/� I���J �8�+� "W�83� 2#H1 2, *�> jFKk$ )2J*���% ��K`> B�`@' 7'.P1� (v3.�+P+K�J1+@8$ �C�, "$ .
�78/_1 2#H1 28FJ �>78, v3.� Y*�>(Flag) 2z.,+$ .�% +?K`$+K`8c� *�h> TCON 1SCON)2JB�(> �8P.� b,��$ I���J 2#H1 ��K`> (l,�H",�hhhh8K3% "$ ��C�h, .R�.S �% u' B%., -�/^+8� 2#H1
2#H1 Y��: 2z.,+$ "() �8P.� %.C 78, �$� Y)�'Flag (R�.�, 2hhz.,+$ w+) *��D^� l,�H "�8,��, 1"3�+, 73�)2/c�+$ +'� -1�c 2, %.C (
T2CON.7 (8052)TF2Timer 2T2CON.6 (8052)EXF2Timer 2
SCON.0RISerial portSCON.1T1Serial port TCON.5TF0Timer 0TCON.7TF1Timer 1TCON.3IE1External 1TCON.1IE0External 0
REGISTER NSME AND BIT POSITONFLAGINTERRUPT
Ext.Int
Timer overflows
OR
Mechatronics Spring 1387
G.R. Vossoughi, Sharif Univ. of Tech. 32
�, �1+, u'2#H1+P+K�J1+@8$)28FJ ��$.(! 1�>�.`31+f1+@8$ (2$�)+, �+c� -�y �% 2K:�3 �>� �� 2, 10��%+f ISR "$ +'� ly�+$ E z +,%��%+f :
jP�(�+c� -�y �% B�$+^ 2$�)+, �� %%+W "$ l8(@� "FS� .
�(Program Counter *1� +,stack I+8:� %%+W "$ )7nW��, a�%;ef*�+c� �� ISR (
¬(2#H1 ��ORB�C Flag %.n8$ lS�y �> 2#H1 w�(�.78/_1)�'Status (2#H1 �>(Interrupt status Register) B�(> 2Jflag *�hhh> -1�c �% I�C +J� 2T#S l H ��C�, "$ "Fhhh:�% �.G, I+8:� "$ �)%+W...
�8P.� bhh �$ ���/,I���J +, 2#H1 98(> %%+W "$ 988/� a�3� .
%(2#H1 98'�f �, B�`@' 7'.P1� �, *�>+�2J.F, "$�).C.
I(a�%; (Interrupt service routine) ISR b �$ a�hh3� +,I���J�8P.� �% 2hh#H1 h:�% lProgram Counter ��+hH I%�% "$ %.C .+hh> �� u' 5�8P.� b �$ I�hh��J *���% 2hhh#H1 a�%; "�kn$ *�+, ISR
"$ ��C�,
2#H1 0��%+f I.T)
Interrupt Vector
��.G)�(> 2J l H 2T#S �% I��C� Y�C ��ly�+$ 2F(c u' 0��%+f Y2#H1 2h@K`); ���?$ Program
Counter 2#H1 �.H1 2[TP �%+,*1� stack ��+H I%�% �8P.� b �$ a�3� +, 1 YI�C I���J 2h#H1 a�%; �'�c)a�%; B�h(>Interrupt Service Routine (�%Program Counter ��+H I%�% "$%.C .2,a�%; �.,D$ *�>Interrupt Vectors 2K#W "$ %.C .
�-1�ca�%; *�> 2, �.,+$ Interrupt Vectors +P+K�J1+@8$ *�+,805x �+n, 73� +'� :
1(2$�)+, ISR %.C "$ �+c� .�(B�'�f �% ISR B�$+^ *�+c� �, 1(return from interrupt)RETI)+^ 2,�n$ B�$ RET B�h'�f �%
Subroutine (28P1� ���?$Program Counter *1� ��stack 28P1� ���?$ 1 I�C ",�'��,Interrupt status register ",�'��, D8)(restore) %.C "$ .2<8K) �%J"hhhFS� 2$�)+, 2, -+K�)�%
2#H1 �.H1 lT$ (%.C "$ I�)�%+W��,.