Beruflich Dokumente
Kultur Dokumente
MICROCONTROLLER 8051
INTRODUCTION
A micro controller is an integrated circuit or a chip with a processor and other support
devices like program memory, data memory, I/O ports, serial communication interface etc
integrated together. Unlike a microprocessor, a microcontroller does not require any external
interfacing of support devices.
Some of the microcontrollers of 8051 family are given as follows:
DEVIC
E
8031
8032
8051
8052
8751
8752
AT89C5
1
AT89C5
2
ON-CHIP
DATA
MEMOR
Y
(bytes)
128
256
128
256
128
256
128
256
ON-CHIP
16-BIT
PROGRAM TIMER/COUNTE
MEMORY R
(bytes)
None
none
4k ROM
8k ROM
4k EPROM
8k EPROM
4k
Flash
Memory
8k
Flash
memory
FULL
DUPLE
X I/O
2
2
2
3
2
3
2
NO.
OF
VECTORE
D
INTERUPT
S
5
6
5
6
5
6
5
1
1
1
1
1
1
1
MEMORIES
The 8051 has three very general types of memory. To effectively program the 8051 it is
necessary to have a basic understanding of these memory types. . They are: On-Chip
Memory, External Code Memory, and External RAM.
External Code Memory is code (or program) memory that resides off-chip. This is often in
the form of an external EPROM.
External RAM is RAM memory that resides off-chip. This is often in the form of standard
static RAM or flash RAM.
On-Chip Memory refers to any memory (Code, RAM, or other) that physically exists on the
microcontroller itself.
Register Banks
The 8051 uses 8 "R" registers which are used in many of its instructions. These "R" registers
are numbered from 0 through 7 (R0, R1, R2, R3, R4, R5, R6, and R7). These registers are
generally used to assist in manipulating values and moving data from one memory location to
another.
2
For example, to add the value of R4 to the Accumulator, we would execute the following
instruction: ADD A,R4
Thus if the Accumulator (A) contained the value 6 and R4 contained the value 3, the
Accumulator would contain the value 9 after this instruction was executed.
The 8051 has four distinct register banks. When the 8051 is first booted up, register bank 0
(addresses 00h through 07h) is used by default. However, your program may instruct the
8051 to use one of the alternate register banks; i.e., register banks 1, 2, or 3. The concept of
register banks adds a great level of flexibility to the 8051, especially when dealing with
interrupts . However, always remember that the register banks really reside in the first 32
bytes of Internal RAM.
Special Function Register (SFR):
Special Function Registers (SFRs) are areas of memory that control specific functionality of
the 8051 processor. For example, four SFRs permit access to the 8051s 32 input/output lines.
Another SFR allows a program to read or write to the 8051s serial port. Other SFRs allow
the user to set the serial baud rate, control and access timers, and configure the 8051s
interrupt system. When programming, SFRs have the illusion of being Internal Memory.
For example, if you want to write the value "1" to Internal RAM location 50 hex you would
execute the instruction: MOV 50h,#01h. Similarly, if you want to write the value "1" to the
8051s serial port you would write this value to the SBUF SFR, which has an SFR address of
99 Hex. Thus, to write the value "1" to the serial port you would execute the instruction:
MOV 99h,#01h.
The 8051 is a flexible microcontroller with a relatively large number of modes of operations.
Your program may inspect and/or change the operating mode of the 8051 by manipulating the
values of the 8051's Special Function Registers (SFRs). SFRs are accessed as if they were
normal Internal RAM. The only difference is that Internal RAM is from address 00h through
7Fh whereas SFR registers exist in the address range of 80h through FFh.
8051 Clock and Instruction Cycle
In 8051, one instruction cycle consists of twelve (12) clock cycles. Instruction cycle is sometimes
called as Machine cycle by some authors.
4
the upper 128 bytes of RAM (80H - FFH) are accessed by indirect addressing.The SFRs (80H FFH) are accessed by direct addressing only.
Addressing modes in 8051
8051 has about 111 instructions. These can be grouped into the following categories:
Immediate Addressing
Direct Addressing
Indirect Addressing
External Direct
Code Indirect
MOV A,#20h
MOV A,30h
MOV A,@R0
MOVX A,@DPTR
MOVC A,@A+DPTR
8051 Instructions
8051 has about 111 instructions. These can be grouped into the following categories
i.
ii.
iii.
iv.
v.
Arithmetic Instructions
Logical Instructions
Data Transfer instructions
Boolean Variable Instructions
Program Branching Instructions
Ex. No.:1
5
Date: 27-01-2015
1.A
27-01-2015
Aim:
Personal Computer
Keil Vision Software
8051 Trainer kit
8051 Micro Controller
Procedure:
1.
2.
3.
4.
5.
Project window
6. Click on Build target option under Project menu
7. Verify the absence of Errors
8. Click on Start/Stop Debug Session under Debug menu
9. Execute the program by clicking Run under Debug menu
10. View and Verify the output from Project Status Window
START
STOP
ORG 0000H
MOV A, #25H
ADD A, 09H
MOV DPTR, #2050H ; Move the external address 2050 to the Data Pointer
MOVX @DPTR, A
Pointer
AGAIN: SJMP AGAIN
OUTPUT
Address location 2050H=34
SUBTRACTION OF 8 BIT NUMBERS
7
PROGRAM FOR 8 BIT SUBTRACTIONS
START
ORG 0000H
CLR C
MOV A, #30
SUBB A, 09H
; Subtract the value of 09H from Accumulator
LOAD ACCUMULATOR WITH MINUEND
MOV DPTR, # 2500H ; Move the external address 2500 to the Data Pointer
MOVX @DPTR, A ; Move the value of Accumulator into address of Data
Pointer SUBTRACT MINUEND 2 FROM ACCMULATOR
AGAIN: SJMP AGAIN
OUTPUT
; Stay here
RESULT
Thus the program for 8 bit Addition and Subtraction operations are performed and output
verified successfully using 8051.
1.B
03-02-2015
Aim:
To perform the basic arithmetic operations of Addition and Subtraction using
Assembly Language in 8051
Apparatus Required:
1.
2.
3.
4.
Personal Computer
Keil Vision Software
8051 Trainer kit
8051 Micro Controller
Procedure:
1.
2.
3.
4.
5.
Project window
6. Click on Build target option under Project menu
7. Verify the absence of Errors
8. Click on Start/Stop Debug Session under Debug menu
9. Execute the program by clicking Run under Debug menu
10. View and Verify the output from Project Status Window
START
STOP
10
PROGRAM FOR 16 BIT ADDITIONS
ORG 0000H
CLR C
; Clear carry
MOV A, #48H
ADD A, #84H
MOV DPTR, #8200H ; Move the external address 8200 to the Data Pointer
MOVX @DPTR, A
MOV A, #82H
ADDC A, #85H
JNC LOOP
INC R0
INC DPTR
MOV A, R0
MOVX @DPTR, A
SJMP $
; Stay here
END
OUTPUT
Address location 8200=32
11
START
SUBTRACT THE CONTENT OF REGISTER B FROM THE CONTENT OF ACCUMULATOR ALONG WITH BORROW I
STORE THE RESULT IN EXTERNAL MEMORY
STOP
12
ORG 00000H
CLR C
; Clear Carry
MOVX @DPTR, A
MOV A, #20H
SUBB A, #60H
JNC LOOP
INC R0
(8200)
MOVX @DPTR, A
MOV A, R0
INC DPTR
MOVX @DPTR, A
SJMP $
; Stay here
END
(8201)
(8202)
OUTPUT
Address Location 8200=70
(Borrow)
RESULT
Thus the program for 16 bit Addition and Subtraction operations are performed and output
verified successfully using 8051.
1.C
13
03.02.2015
Aim:
To perform the basic arithmetic operations of Multiplication and Division using
Assembly Language in 8051
Apparatus Required:
1.
2.
3.
4.
Personal Computer
Keil Vision Software
8051 Trainer kit
8051 Micro Controller
Procedure:
1.
2.
3.
4.
5.
Project window
6. Click on Build target option under Project menu
7. Verify the absence of Errors
8. Click on Start/Stop Debug Session under Debug menu
9. Execute the program by clicking Run under Debug menu
10. View and Verify the output from Project Status Window
14
16 BIT MULTIPLICATIONS
START
STOP
15
PROGRAM FOR TWO 16 BIT MULTIPLICATION
First number stored in R6 and R7 and 2nd number stored in R4 and R5, Result in R0, R1,
R2, R3 Respectively
R6 R7*
R4 R5
---------------R0 R1 R2 R3
MAIN:
MUL16:
ORG 0X0000
LJMP MAIN
ORG 0X0040
LCALL MUL16
; Call subroutine
SJMP $
; Stay here
MOV A, R5
MOV B, R7
MUL AB
MOV R3, A
MOV R2, B
MOV A, R5
MOV B, R6
MUL AB
ADD A, R2
MOV R2, A
MOV A, B
Accumulator
ADDC A, #00H
MOV R1, A
MOV A, #00H
ADDC A, #00H
MOV R0, A
new result
MOV A, R4
MOV B, R7
16
MUL AB
ADD A, R2
MOV R2, A
MOV A, B
ADDC A, R1
MOV R1, A
MOV A, #00H
ADDC A, R0
MOV R0, A
MOV A, R4
MOV B, R6
MUL AB
ADD A, R1
MOV R1, A
MOV A, B
ADDC A, R0
MOV R0, A
END
OUTPUT
MULTIPLICAND (2040) R6=20, R7=40
17
16 BIT DIVISIONS
START
SET R4, R5 AS ZERO INITIALLY TO USE AS A WORKING VARIABLE AND SET REGISTER B AS A COUNTER
UE TO R2, R3 LOWER BYTE AND UPPER BYTE RESPECTIVELY AND DO LEFT SHIFT OPERATION TO CHECK DIVISOR IS GREA
YES
TOD
YES
CHECK CARRY NO
FLAG=0?
NO
YES
DECREMENT
NO B
CHECK
B=0?
NO
STORE THE RESULT IN R3 AND R2
STOP
18
DIV1
0X0000
CLR C
MOV B, #00H
INC B
MOV A, R2
RLC A
DIV 2
MOV R2, A
MOV A, R3
RLC A
MOV R3, A
JNC DIV1
MOV A, R3
RRC A
MOV R3, A
MOV A, R2
RRC A
MOV R2, A
CLR C
MOV 07H, R1
MOV 06H, R0
MOV A, R0
SUBB A, R2
MOV R0, A
MOV A, R1
SUBB A, R3
MOV R1, A
JNC DIV3
subtraction
19
DIV3
CPL C
MOV A, R4
RLC A
MOV R4, A
MOV A, R5
RLC A
MOV R5, A
DJNZ B, DIV2
RET
END
OUTPUT
Ex: 179 / 8 =?
R1/R0=0000000010110011 (Binary Equivalent of 72H (179d))
R3/R2 =0000000000001000 (Binary Equivalent of 8H (8d))
R1/R0 =0000000000000011 (Remainder=3H (3d))
R5/R4 =0000000000010110 (Quotient=16H (22d))
RESULT
Thus the program for 16 bit Multiplication and Division operation are performed and output
verified successfully using 8051.
20
PIC MICROCONTROLLER
PIC stands for Peripheral Interface Controller given by Microchip Technology to identify its
single-chip microcontrollers. These devices have been very successful in 8-bit
microcontrollers. The main reason is that Microchip Technology has continuously upgraded
the device architecture and added needed peripherals to the microcontroller to suit customers'
requirements. The development tools such as assembler and simulator are freely available on
the internet at www.microchip.com .
Popularity of the PIC microcontrollers is due to the following factors.
1. Speed: Harvard Architecture, RISC architecture, 1 instruction cycle = 4 clock cycles.
2. Instruction set simplicity: The instruction set consists of just 35 instructions
(as opposed to 111 instructions for 8051).
3. Power-on-reset and brown-out reset. Brown-out-reset means when the
power supply goes below a specified voltage (say 4V), it causes PIC to reset;
hence
malfunction
is
avoided.
A watch dog timer (user programmable) resets the processor if the
software/program ever malfunctions and deviates from its normal operation.
4. PIC microcontroller has four optional clock sources.
o
21
PIC microcontroller has 13 bits of program memory address. Hence it can address
up to 8k of program memory. The program counter is 13-bit. PIC 16C6X or
16C7X program memory is 2k or 4k. While addressing 2k of program memory,
only 11- bits are required. Hence two most significant bits of the program counter
are ignored. Similarly, while addressing 4k of memory, 12 bits are required. Hence
the MSB of the program counter is ignored.
22
Aim:
To perform the basic arithmetic operations of 8 bit Addition and Subtraction using
Assembly Language in PIC.
Apparatus Required:
1. Personal Computer
2. PIC Trainer kit
3. MPLAB software
23
Procedure:
1. Create a New project in MPLAB software.
2. Select the Controller as PIC16f877A
3. Open a new script and type the program.
4. Save the program as .asm file.
5. Add this file to the Source Group folder in the Target folder.
6. Build a target location for the program by clicking Build Target option in
Project tab.
7. Now start executing the program by clicking Start Debug Session option in
Debug tab.
8. Check for the errors and warning and finally run the Program.
9. The output can be viewed from the Project status window
24
25
SUM
SET
FORfor
SUM
10HRAM ;LOC
Ram10H
loc 10H
sum
EQU
ORG
0H
; Start at address 0
MOVLW 25H
; WREG=25
ADDLW
0X46
MOVWF
HERE
GOTO
END
OUTPUT
WREG = 25
W = W+46=25+46
W=6B
26
SUBTRACTION OF 8 BIT NUMBERS
START
STOP
27
MYREG
EQU
0X20
ORG
0H
MOVLW
0X4C
; Load WREG=4CH
MOVWF
MYREG
; MYREG= 4CH
MOVLW
0X6E
; WREG=6EH
SUBWF
MYREG, W
; WREG= MYREG-WREG
BNN
NEXT
NEGF
0XFE8
NEXT
MOVWF
MYREG
END
OUTPUT
4C
01001100
01001100
-6E
01101110 2S comp =
10010010
-22
11011110
MYREG = 22H
RESULT
Thus the program for 8 bit Addition and Subtraction operation are performed and output
verified successfully using PIC microcontroller
28
2.B
03.03.2015
Aim:
To perform the basic arithmetic operations of 16 bit Addition and Subtraction using
Assembly Language in PIC.
Apparatus Required:
1. Personal Computer
2. PIC Trainer kit
3. MPLAB Software
Procedure:
1. Create a New project in MPLAB software.
2. Select the Controller as PIC16f877A
3. Open a new script and type the program.
4. Save the program as .asm file.
5. Add this file to the Source Group folder in the Target folder.
6. Build a target location for the program by clicking Build Target option in
Project tab.
7. Now start executing the program by clicking Start Debug Session option in
Debug tab.
8. Check for the errors and warning and finally run the Program.
9. The output can be viewed from the Project status window
29
START
STOP
30
The numbers are 3CE7H and 3B8DH.Assume that file Register loc 6= (8D) and loc 7=
(3B).place the sum in the file register location 6 and 7; Assume location 6 should have Lower
Byte.
HERE
; Location
; Location
ORG
0H
MOVLW
0XE7
ADDWF
0X6, F
MOVLW
0X3C
ADDWFC
0X7, F
; F=W+F+carry =3C+3B+1=78H
HERE
; Stay here
GOTO
END
OUTPUT
After Execution of ADDWF Ram loc 0X6 will be F=W+F=E7+8D=74
After Execution of ADDWFC Ram loc 0X7 will be F=W+F+Carry=3C+3B+1=78H
Final Result=3CE7H+3B8DH=7874H
31
START
STOP
32
The numbers are 2762H-1296H.Assume file register location 6= (62) and Location 7=
(27).Place the difference in file register location6 and 7; loc 6 should have the lower byte.
HERE
; Loc 6
= (62)
; Loc 7
= (27)
ORG
0H
MOVLW
0X96
SUBWF
0X6, F
MOVLW
0X12
SUBWFB
0X7, F
GOTO
END
HERE
; Stay here
; End asm source file
OUTPUT
After the SUBWF, Loc 6 has =62H-96H=CCH and carry flag set to zero (Indicating a borrow
N=1).
When SUBWFB is executed the file Loc 7 has = 27H-12H-1= 14H
Therefore we have 2762H-1296H =14CCH
RESULT
Thus the program for 16 bit Addition and Subtraction operation are performed and output
verified successfully using PIC microcontroller.
33
2.C
03.03.2015
Aim:
To perform the basic arithmetic operations of 8 bit Multiplication and Division using
Assembly Language in PIC.
Apparatus Required:
1. Personal Computer
2. PIC Trainer kit
3. MPLAB Software
Procedure:
1. Create a New project in MPLAB software.
2. Select the Controller as PIC16f877A
3. Open a new script and type the program.
4. Save the program as .asm file.
5. Add this file to the Source Group folder in the Target folder.
6. Build a target location for the program by clicking Build Target option in
Project tab.
7. Now start executing the program by clicking Start Debug Session option in
Debug tab.
8. Check for the errors and warning and finally run the Program.
9. The output can be viewed from the Project status window
34
8 BIT MULTIPLICATIONS
START
STOP
35
MUL
EQU D25
ORG
0H
HERE
MULLW 0X20
; 25H*20H =4A0H
GOTO HERE
; Stay here
END
OUTPUT
25D*20H =19H*20H
PRODH=01H and PRODL=F4H
36
8-BIT DIVISION
START
STOP
37
ORG
0H
NUM
EQU
0X19
MYQ
EQU
0X20
MYNMB
EQU
D95
MYDEN
EQU
D10
CLRF
MYQ
; Quotient=0
B1
MOVLW MYNMB
; WREG=95
MOVWF
; Numerator=95
NUM
MOVLW MYDEN
; WREG= Denominator=10
INCF
MYQ, F
SUBWF
NUM, F
; Subtract 10 (F=F-W)
BC
B1
DECF
MYQ, F
ADDWF
NUM, F
END
OUTPUT
F= F W (keep doing until C=0)
Quotient = 9H
Remainder =5H
RESULT
Thus the program for 8 bit Multiplication and Division operation are performed and output
verified successfully using PIC microcontroller.
38
Ex. No.:3
Date: 10-03-2015
Aim:
Personal Computer
Keil Vision Software
8051 Trainer kit
8051 Micro Controller
Procedure:
1.
2.
3.
4.
5.
Project window
6. Click on Build target option under Project menu
7. Verify the absence of Errors
8. Click on Start/Stop Debug Session under Debug menu
9. Execute the program by clicking Run under Debug menu
10. View and Verify the output from Project Status Window
39
START
STAY HERE
40
DATA
START
RETURNN
COMMAND
START
RETURNNN
41
PROGRAM
;calls a time delay before sending next data/command
;P0.0-P0.7 are connected to LCD data pins D0-D7
;P3.7 is connected to RS pin of LCD
;P3.6 is connected to R/W pin of LCD
;P3.5 is connected to E pin of LCD
ORG
00h
MOV A,#38H
MOV A,#0EH
ACALL COMNWRT
ACALL DELAY
MOV A,#01H
;clear LCD
MOV A,#06H
ACALL COMNWRT
ACALL DELAY
MOV A,#84H
42
ACALL COMNWRT
ACALL DELAY
MOV A,#'N'
;display letter N
ACALL DATAWRT
ACALL DELAY
MOV A,#'O'
;display letter O
ACALL DATAWRT
ACALL DELAY
;stay here
COMNWRT:
MOV P0,A
CLR P3.7
CLR P3.6
SETB P3.5
ACALL DELAY
CLR P3.5
RET
DATAWRT:
MOV P0,A
SETB P3.7
43
CLR P3.6
SETB P3.5
ACALL DELAY
CLR P3.5
RET
DELAY: MOV R3,#50
;R4 = 255
DJNZ R3,HERE2
RET
END
RESULT
Thus the LCD is interfaced with AT89C51 and verified.
44
Ex. No.:9
Date: 12-05-2015
Aim:
45
Program:
CLR C
AGAIN: MOV DPTR,#TABLE
MOV R2, 37
BACK: CLR A
MOVC A,@A+DPTR
MOV P2,A
INC DPTR
DJNZ R2,BACK
SJMP AGAIN
ORG 300H
TABLE: DB 128,192,238,255,238,192,210,226,238,248,254,255,254,248,238,226,210,105
DB 192,171,150,128,105,84,64,45,29,17,7,2,0,2,7,17,29,45,64,84
END
46
CIRCUIT DIAGRAM:
C1
U1
X1
19
XTAL1
CRYSTAL
18
C2 33p
XTAL2
R1
P0.0/AD0
P0.1/AD1
P0.2/AD2
P0.3/AD3
P0.4/AD4
P0.5/AD5
P0.6/AD6
P0.7/AD7
RST
vcc
10k
C3
P2.0/A8
P2.1/A9
P2.2/A10
P2.3/A11
P2.4/A12
P2.5/A13
P2.6/A14
P2.7/A15
29
PSEN
30
ALE
31
EA
1uF
1
2
3
4
5
6
7
8
P1.0
P1.1
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7
P3.0/RXD
P3.1/TXD
P3.2/INT0
P3.3/INT1
P3.4/T0
P3.5/T1
P3.6/WR
P3.7/RD
39
38
37
36
35
34
33
32
21
22
23
24
25
26
27
28
10
11
12
13
14
15
16
17
AT89C51
R2
3.5k
U2
5
6
7
8
9
10
11
12
A1
A2
A3
A4
A5
A6
A7
A8
DAC0808
vee
Vref(+)
VREF+
VREFIOUT
14
15
4
R3
10k
16
COMP
3
VEE
RV1
A
C4
100n
50%
33p
B
C
5k
47
OUTPUT:
Result:
Thus the DAC was interfaced with 8051 microcontroller using PROTEUS and the output
curves were verified.
48
Ex. No.:10
Date: 12-05-2015
Aim:
To interface the unipolar stepper motor with 8051 in PROTEUS, using ULN2003 and check
for full stepping and half stepping sequence.
Apparatus Requires:
1. Personal Computer.2. Keil uVision 5.
3. Proteus 8 professional.
Procedure:
1. Open Keil software.
2. Type the given program ( Full stepping) in keil editor window, selecting crystal
frequency as 11.0592 and generate the hex file.
3. Open PROTEUS software and draw the circuit as given. Connect the port1 (assigned
as output) to ULN2003 input port (1,2,3,4) and ULN2003 output port(16,15,14,13) to
the motor terminals.
Connect ULN2003 COM port to +5v.
Dump the created hex file for full stepping in 8051.
Simulate and check for 90degree rotation ( step angle = 360/4 = 90)
Repeat the same for Half stepping and for 45degree rotation.
4.
5.
6.
7.
A/
B/
ROTATION
0
1
2
3
1
0
0
1
1
1
0
0
0
1
1
0
0
0
1
1
(degree)
0
90
180
270
A/
B/
ROTATION
(degree)
0
49
1
45
90
135
180
225
270
315
PROGRAM:
FULLSTEP SEQUENCE
org 0H
stepper equ P1
main:
mov stepper, #0CH
acall delay
mov stepper, #06H
acall delay
mov stepper, #03H
acall delay
mov stepper, #09H
acall delay
sjmp main
delay:
mov r7,#4
wait2:
mov r6,#0FFH
wait1:
mov r5,#0FFH
wait:
djnz r5,wait
djnz r6,wait1
djnz r7,wait2
ret
end
HALFSTEP SEQUENCE
org 0H
stepper equ P1
main:
50
mov stepper, #08H
acall delay
mov stepper, #0CH
acall delay
mov stepper, #04H
acall delay
mov stepper, #06H
acall delay
mov stepper, #02H
acall delay
mov stepper, #03H
acall delay
mov stepper, #01H
acall delay
mov stepper, #09H
acall delay
sjmp main
delay:
mov r7,#4
wait2:
mov r6,#0FFH
wait1:
mov r5,#0FFH
wait:
djnz r5,wait
djnz r6,wait1
djnz r7,wait2
ret
end
CIRCUIT SIMULATION:
51
+5v
U1
19
18
29
30
31
1
2
3
4
5
6
7
8
XTAL1
XTAL2
RST
PSEN
ALE
EA
P1.0
P1.1
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7
39
38
37
36
35
34
33
32
P0.0/AD0
P0.1/AD1
P0.2/AD2
P0.3/AD3
P0.4/AD4
P0.5/AD5
P0.6/AD6
P0.7/AD7
21
22
23
24
25
26
27
28
P2.0/A8
P2.1/A9
P2.2/A10
P2.3/A11
P2.4/A12
P2.5/A13
P2.6/A14
P2.7/A15
P3.0/RXD
P3.1/TXD
P3.2/INT0
P3.3/INT1
P3.4/T0
P3.5/T1
P3.6/WR
P3.7/RD
10
11
12
13
14
15
16
17
(AC)
+5v
AT89C51
U3
1
2
3
4
5
6
7
1B
2B
3B
4B
5B
6B
7B
COM
1C
2C
3C
4C
5C
6C
7C
9
16
15
14
13
12
11
10
+88.8
ULN2003A
Result:
Thus the stepper motor was interfaced with 8051 using PROTEUS and Half stepping & Full
stepping sequences were verified.
52
Ex. No.:11
Date: 12-05-2015
AIM
To control the position of a DC servo motor by using 8051 and PIC.
SERVO MOTOR
A servomotor is a rotary actuator that allows for precise control of
angular position, velocity and acceleration. It consists of a suitable motor
coupled to a sensor for position feedback. It also requires a relatively
sophisticated controller, often a dedicated module designed specifically
for use with servomotors.
Servomotors are not a specific class of motor although the
term servomotor is often used to refer to a motor suitable for use in a
closed-loop control system.
MECHANISM
As the name suggests, a servomotor is a servomechanism. More
specifically, it is a closed-loop servomechanism that uses position
feedback to control its motion and final position. The input to its control is
some signal, either analogue or digital, representing the position
commanded for the output shaft.
The motor is paired with some type of encoder to provide position and
speed feedback. In the simplest case, only the position is measured. The
measured position of the output is compared to the command position,
the external input to the controller. If the output position differs from that
required, an error signal is generated which then causes the motor to
rotate in either direction, as needed to bring the output shaft to the
appropriate position. As the positions approach, the error signal reduces to
zero and the motor stops.
The very simplest servomotors use position-only sensing via
a potentiometer and bang-bang control of their motor; the motor always
rotates at full speed (or is stopped). This type of servomotor is not widely
used in industrial motion control, but it forms the basis of the simple and
cheap servos used for radio-controlled models.
53
More sophisticated servomotors measure both the position and also the
speed of the output shaft. They may also control the speed of their motor,
rather than always running at full speed. Both of these enhancements,
usually in combination with a PID control algorithm, allow the servomotor
to be brought to its commanded position more quickly and more precisely,
with less overshooting.
SERVO OPERATION
The pulses are sent from the microcontroller to a control board
within the servo itself. The servos control board interprets the pulses and
rotates its shaft either clockwise or counter clockwise based on the pulse
widths. Servos will not respond to just any pulse width; rather, they are
limited to a well-defined range of pulse widths. Most servos have a
minimum pulse width limit around 0.0010s (1.0ms) and a maximum limit
around 0.0020s (2.0ms), although the actual minimum and maximum
pulse widths will vary slightly between the various servo brands. Sending
a pulse whose pulse width is outside this range may damage the servos
control board. Repeatedly sending these bad pulses will almost certainly
destroy the servo.
Pulse widths = positions
Servos interpret pulse widths as positions. Each position along
the arc traced out by the rotating shaft has a corresponding pulse width.
When we send a pulse to the servo, the control board calculates which
way the shaft should rotate in order to reach the corresponding position.
There are two ways that servo manufacturers can wire their servos:
positions increasing clockwise and positions increasing counter
clockwise. The positions and corresponding pulse widths for Futaba and
Blue Bird brand servos are shown in Figures 1.1 and 1.2 All servos have a
centre position, sometimes called the neutral position. Both servos
below are in the centre position as indicated by the dark pointers.
Observe that the Futaba centre position corresponds to 1.5ms and the
Blue Birds centre is at the 1.8ms-position.
(NOTE: In our lab experiment we are using FUTABA s3003 DC servo motor)
54
1.
jk
1.2
55
.
CIRCUIT DIAGRAM
8MHz crystal is used to provide the required clock for 8051 microcontroller
and 22pF capacitors are used to stabilize the operation of crystal. 10K
resistor and 10F capacitor is used to provide the required Power ON
56
Reset (POR) to the microcontroller. Control of Servo Motor is connected to
first pin of Port 2.
KEIL C CODE
#include<reg52.h>
#include<stdio.h>
#include <intrins.h>
sbit motor_pin = P2^0;
void Delay(unsigned int);
void Delay_servo(unsigned int);
void main()
{
motor_pin = 0;
do
{
//Turn to 0 degree
motor_pin = 1;
Delay_servo(50);
motor_pin = 0;
Delay(1000);
//Turn to 90 degree
motor_pin=1;
Delay_servo(82);
motor_pin=0;
Delay(1000);
//Turn to 180 degree
motor_pin=1;
Delay_servo(110);
motor_pin=0;
Delay(1000);
}while(1);
}
void Delay(unsigned int ms)
{
unsigned long int us = ms*1000;
while(us--)
{
_nop_();
}
}
57
void Delay_servo(unsigned int us)
{
while(us--)
{
_nop_();
}
}
58
VDD and VSS of the pic microcontroller is not shown in the circuit diagram.
VDD should be connected to +5V and VSS to GND.
Control wire of the Servo is connected to RB0 of the PIC Microcontroller.
Delay_ms() is used to make delay in milliseconds during the program
execution while Delay_us() is used to make delay in microsecond during
the program execution.
59
MIKRO C PROGRAMMING:
60
}
}
61
}
}
void main()
{
TRISB = 0; // PORTB as Ouput Port
do
{
servoRotate0(); //0 Degree
Delay_ms(2000);
servoRotate90(); //90 Degree
Delay_ms(2000);
servoRotate180(); //180 Degree
}while(1);
}
62
Result::
Thus the servo motor was interfaced with 8051 and pic16F87A
microcontrollers and the position controls of it were studied.
63
APPENDIX
8051 INSTRUCTIONS
Arithmetic Instructions:
Mnemonics
Description
ADD A, Rn
A
A + Rn
ADD A, direct A
A + (direct)
ADD A, @Ri A
A + @Ri
ADD A, #data A
A + data
ADDC A, Rn A
A + Rn + C
ADDC A, direct A
A + (direct) + C
ADDC A, @Ri A
A + @Ri + C
ADDC A, #data A
A + data + C
DA A
Decimal adjust accumulator
DIV AB
Divide A by B
A
quotient
B
remainder
DEC A
A
A -1
DEC Rn
Rn
Rn - 1
DEC direct
(direct)
(direct) - 1
DEC @Ri
@Ri
@Ri - 1
INC A
A
A+1
INC Rn
Rn
Rn + 1
INC direct
(direct)
(direct) + 1
INC @Ri
@Ri
@Ri +1
INC DPTR
DPTR
DPTR +1
MUL AB
Multiply A by B
A
low byte (A*B)
B
high byte (A* B)
1
2
1
2
1
2
1
2
1
Instruction
Cycles
1
1
1
1
1
1
1
1
1
1
1
2
1
1
1
2
1
1
1
1
1
1
1
1
1
1
2
SUBB A, Rn
SUBB A, direct
SUBB A, @Ri
SUBB A, #data
1
2
1
2
1
1
1
1
A
A
A
A
A - Rn - C
A - (direct) - C
A - @Ri - C
A - data - C
Bytes
64
Mnemonics
Description
MOV A, Rn
A
Rn
MOV A, direct
A
(direct)
MOV A, @Ri
A
@Ri
MOV A, #data
A
data
MOV Rn, A
Rn
A
MOV Rn, direct Rn
(direct)
MOV Rn, #data
Rn
data
MOV direct, A
(direct)
A
MOV direct, Rn (direct)
Rn
MOV direct1,
(direct1)
(direct2)
direct2
MOV direct, @Ri (direct)
@Ri
MOV direct, #data (direct)
#data
MOV @Ri, A
@Ri
A
MOV @Ri, direct @Ri
(direct)
MOV @Ri, #data @Ri
data
MOV DPTR,
DPTR
data16
#data16
MOVC A,
A
Code byte pointed by A + DPTR
@A+DPTR
MOVC A, @A+PC A
Code byte pointed by A + PC
MOVC A, @Ri
A
Code byte pointed by Ri 8-bit address)
MOVX A,
A
External data pointed by DPTR
@DPTR
MOVX @Ri, A
@Ri
A (External data - 8bit address)
MOVX @DPTR, @DPTR
A(External data - 16bit address)
A
PUSH direct
(SP)
(direct)
POP direct
(direct)
(SP)
XCH Rn
Exchange A with Rn
XCH direct
Exchange A with direct byte
Bytes Instruction
Cycles
1
1
2
1
1
1
2
1
1
1
2
2
2
1
2
1
2
2
3
2
2
3
1
2
2
3
2
2
1
2
1
2
1
1
1
2
2
2
1
1
2
2
2
2
1
2
2
2
1
1
65
Mnemonics
Description
ACALL addr11
AJMP addr11
CJNE A, direct, rel
PC + 2
(SP) ; addr 11 PC
Addr11
PC
Compare with A, jump (PC + rel) if not
equal
CJNE A, #data, rel
Compare with A, jump (PC + rel) if not
equal
CJNE Rn, #data, rel Compare with Rn, jump (PC + rel) if not
equal
CJNE @Ri, #data, rel Compare with @Ri A, jump (PC + rel) if
not equal
DJNZ Rn, rel
Decrement Rn, jump if not zero
DJNZ direct, rel
Decrement (direct), jump if not zero
JC rel
Jump (PC + rel) if C bit = 1
JNC rel
Jump (PC + rel) if C bit = 0
JB bit, rel
Jump (PC + rel) if bit = 1
JNB bit, rel
Jump (PC + rel) if bit = 0
JBC bit, rel
Jump (PC + rel) if bit = 1
JMP @A+DPTR
A+DPTR
PC
JZ rel
If A=0, jump to PC + rel
JNZ rel
If A 0 , jump to PC + rel
LCALL addr16
PC + 3
(SP), addr16
PC
LJMP addr 16
Addr16
PC
NOP
No operation
RET
(SP)
PC
SJMP rel
JMP @A+DPTR
JZ rel
JNZ rel
NOP
PC + 2 + rel
PC
A+DPTR
PC
If A = 0. jump PC+ rel
If A 0, jump PC + rel
No operation
Bytes Instruction
Cycles
2
2
2
2
3
2
3
2
3
2
2
3
3
3
1
2
2
3
3
1
1
2
2
2
2
2
2
2
2
2
2
2
2
1
2
2
1
2
2
1
2
2
2
2
1
66
PIC INSTRUCTIONS
Mnemonics
Description
Instruction
Cycles
bcf f, b
bsf f, b
clrw
clrf f
Clear f
movlw k
movwf f
Move W to f
movf f, F(W)
Move f to F or W
swapf f, F(W)
andlw k
andwf f, F(W)
andwf f, F(W)
iorlw k
iorwf f, F(W)
67
xorlw k
xorwf f, F(W)
addlw k
addwf f, F(W)
sublw k
subwf f, F(W)
rlf f, F(W)
rrf f, F(W)
btfsc f, b
Test 'b' bit of the register f and skip the next instruction if
bit is clear
1/2
btfss f, b
Test 'b' bit of the register f and skip the next instruction if
bit is set
1/2
decfsz f, F(W)
1/2
incfcz f, F(W)
1/2
goto label
68
call label
retrun
retlw k
retie
clrwdt
sleep