Sie sind auf Seite 1von 68

1

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.

Instruction Cycle of 8051

128 bytes of Internal RAM Structure (lower address space)

Internal RAM Structure


The lower 32 bytes are divided into 4 separate banks. Each register bank has 8 registers of one byte
each. A register bank is selected depending upon two bank select bits in the PSW register. Next
16bytes are bit addressable. In total, 128bits (16X8) are available in bitaddressable area. Each bit
can be accessed and modified by suitable instructions. The bit addresses are from 00H (LSB of the
first byte in 20H) to 7FH (MSB of the last byte in 2FH). Remaining 80bytes of RAM are available
for general purpose.

Internal Data Memory and Special Function Register (SFR) Map

Internal Data Memory Map


The special function registers (SFRs) are mapped in the upper 128 bytes of internal data memory
address. Hence there is an address overlap between the upper 128 bytes of data RAM and SFRs.
Please note that the upper 128 bytes of data RAM are present only in the 8052 family. The
lower128 bytes of RAM (00H - 7FH) can be accessed both by direct or indirect addressing while

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

ASSEMBLY LANGUAGE PROGRAMMING


AND SIMULATION OF 8051 IN KEIL IDE

5
Date: 27-01-2015

8-BIT ADDITION AND SUBTRACTION

1.A
27-01-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.

Create a New Project in Keil Software


Select the Controller as Atmel AT89C51
Create a New Script and type the program
Save the program as .asm file type
Add the saved file to the Source Group folder in the Target Folder under

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

ADDITION OF 8 BIT NUMBERS

START

LOAD ACCUMULATOR WITH ADDEND1

ADD ADDEND2 WITH ACCMULATOR

STORE THE RESULT IN EXTERNAL MEMORY


LOCATION

STOP

PROGRAM FOR 8 BIT ADDITIONS

ORG 0000H

; Starting Location= 0000H

MOV A, #25H

; Move value 25H into Accumulator

ADD A, 09H

; Add value 09H to Accumulator

MOV DPTR, #2050H ; Move the external address 2050 to the Data Pointer
MOVX @DPTR, A
Pointer
AGAIN: SJMP AGAIN

; Move the value of Accumulator into address of Data


; Stay here

OUTPUT
Address location 2050H=34
SUBTRACTION OF 8 BIT NUMBERS

7
PROGRAM FOR 8 BIT SUBTRACTIONS
START
ORG 0000H

; Set Starting location 0000H


CLEAR
; ClearTHE
CarryCARRY FLAG

CLR C
MOV A, #30

; Move value 30H into Accumulator

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

STORE THE RESULT IN EXTERNAL MEMORY


LOCATION

Address Location 2500H=21


STOP

RESULT
Thus the program for 8 bit Addition and Subtraction operations are performed and output
verified successfully using 8051.

16-BIT ADDITIONS AND SUBTRACTION

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.

Create a New Project in Keil Software


Select the Controller as Atmel AT89C51
Create a New Script and type the program
Save the program as .asm file type
Add the saved file to the Source Group folder in the Target Folder under

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

ADDITION OF 16 BIT NUMBERS

START

LOAD LSB OF ADDEND1 INTO ACCUMULATOR


LOAD LSB OF ADDEND2 INTO REGISTER B

ADD THE CONTENTS OF ACCUMULATOR AND REGISTER B

STORE THE RESULT IN EXTERNAL MEMORY


INCREMENT THE EXTERNAL MEMORY ADDRESS LOCATION

LOAD MSB OF ADDEND1 INTO ACCUMULATOR


LOAD MSB OF ADDEND2 INTO REGISTER B

ADD THE CONTENTS OF ACCUMULATOR AND REGISTER B ALONGWITH CARRY IF ANY


STORE THE RESULT IN EXTERNAL MEMORY

STOP

10
PROGRAM FOR 16 BIT ADDITIONS
ORG 0000H

; Set starting address=0000H

CLR C

; Clear carry

MOV R0, #00H

; Move the value 00H into Register R0

MOV A, #48H

; Move the value 48H into Accumulator

ADD A, #84H

; add the value 84H to Accumulator

MOV DPTR, #8200H ; Move the external address 8200 to the Data Pointer
MOVX @DPTR, A

; Move the value of Accumulator into address of Data Pointer

MOV A, #82H

; Move the value of 82H into Accumulator

ADDC A, #85H

; Add the value 85H with carry to Accumulator

JNC LOOP

; Jump if Not Carry to the loop LOOP i.e. CF=0

INC R0

; Increment the value of register R0 by one

LOOP: INC DPTR


MOVX @DPTR, A

; Increment the Data pointer address by one


; Move the value of Accumulator into address of Data Pointer

INC DPTR

; Increment the Data pointer address by one

MOV A, R0

; Move the content of register R0 into Accumulator

MOVX @DPTR, A

; Move the value of Accumulator into address of Data Pointer

SJMP $

; Stay here

END

; End the asm source file

OUTPUT
Address location 8200=32

Address location 8201=68

Address location 8202=01

Final Result =8248+8584=016832

SUBTRACTION OF 16 BIT NUMBERS

11

START

LOAD LSB OF MINUEND INTO ACCUMULATOR


LOAD LSB OF SUBTRAHEND INTO REGISTER B

SUBTRACT THE CONTENT OF REGISTER B FROM THE CONTENT OF ACCUMULATOR

STORE THE RESULT IN EXTERNAL MEMORY


INCREMENT THE EXTERNAL MEMORY ADDRESS LOCATION

LOAD MSB OF MINUEND INTO ACCUMULATOR


LOAD MSB OF SUBTRAHEND INTO REGISTER B

SUBTRACT THE CONTENT OF REGISTER B FROM THE CONTENT OF ACCUMULATOR ALONG WITH BORROW I
STORE THE RESULT IN EXTERNAL MEMORY

STOP

PROGRAM FOR 16 BIT SUBTRACTIONS

12
ORG 00000H

; Set starting address =0000H

CLR C

; Clear Carry

MOV R0, #00H

; Move the value 00H into Register R0

MOV A, #40H ; Move the value of 40H into Accumulator


SUBB A, #10H

; Subtract the value 10H from Accumulator with borrow

MOV DPTR, #8200H

; Move the external address 8200 to the Data Pointer

MOVX @DPTR, A

; Move the value of Accumulator into address of Data Pointer

MOV A, #20H

; Move the value of 20H into Accumulator

SUBB A, #60H

; Subtract the value 60H from Accumulator with borrow

JNC LOOP

; Jump if Not Carry to the loop LOOP i.e. CF=0

INC R0

; Increment the value of register R0 by one

(8200)

LOOP: INC DPTR

; Increment the Data pointer address by one

MOVX @DPTR, A

; Move the value of Accumulator into address of Data Pointer

MOV A, R0

; Move the content of register R0 into Accumulator

INC DPTR

; Increment the Data pointer address by one

MOVX @DPTR, A

; Move the value of Accumulator into address of Data Pointer

SJMP $

; Stay here

END

; End the asm source file

(8201)

(8202)

OUTPUT
Address Location 8200=70

Address location 8201=39

Address location 8202=01

(Borrow)
RESULT
Thus the program for 16 bit Addition and Subtraction operations are performed and output
verified successfully using 8051.
1.C

16-BIT MULTIPLICATIONS AND DIVISION

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.

Create a New Project in Keil Software


Select the Controller as Atmel AT89C51
Create a New Script and type the program
Save the program as .asm file type
Add the saved file to the Source Group folder in the Target Folder under

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

LOAD THE MULTIPLICANT IN R6, R7 AND MULTIPLIER


IN R4, R5

MULTIPLY R7 WITH R5, STORE RESULT IN R3AND R2


.LOAD A, B REG WITH R6 AND R5 MULTIPLY EACH
OTHER AND RESULT ADD WITH R2 AND CARRY

MULTIPLY R7 WITH R4, STORE RESULT IN R1AND


R0 .LOAD A, B REG WITH R6 AND R4 MULTIPLY EACH
OTHER AND RESULT ADD WITH R0 AND CARRY
16 BIT MULTIPLICATIONS RESULT WILL BE STORED IN
R0, R1, R2, and R3 RESPECTIVELY

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

; Starting Address of the Code


;To change the execution to main label

ORG 0X0040

; Starting address of subroutine

MOV R4, #0X20

; to Copy the upper byte to R4

MOV R5, #0X30

; to Copy the lower byte to R5

MOV R6, #0X20

; to Copy the upper byte to R6

MOV R7, #0X40

; to Copy the lower byte to R7

LCALL MUL16

; Call subroutine

SJMP $

; Stay here

MOV A, R5

; Copy lower byte of multiplier to accumulator

MOV B, R7

; Copy lower byte of multiplicand to B register

MUL AB

; Multiply A and B Register

MOV R3, A

; Copy the Result to R3 Register

MOV R2, B

; to Copy lower byte of result to R2 Register

MOV A, R5

; Copy Lower byte of multiplier to accumulator

MOV B, R6

; Copy upper byte of multiplier to B register

MUL AB

; Multiply A and B register

ADD A, R2

; Add upper byte of R5*R7 result to Accumulator

MOV R2, A

; to copy the result in R2 Register

MOV A, B

; to copy upper byte of R5*R6 result to

Accumulator
ADDC A, #00H

; Add carry of previous result to upper byte of

MOV R1, A

; Result copy to R1 register

MOV A, #00H

; Move 00H to accumulator

ADDC A, #00H

; Add carry to accumulator

MOV R0, A

; Copy accumulator value to R0 register

new result

MOV A, R4

; Copy upper byte of multiplier to accumulator

MOV B, R7

; Copy lower byte of multiplicand to B register

16
MUL AB

; Multiply A and B Register

ADD A, R2

; Add R2 register to accumulator

MOV R2, A

; Copy the Result to R2 Register

MOV A, B

; copy upper byte of R4*R7 result to accumulator

ADDC A, R1

; Add R1 register to accumulator with carry

MOV R1, A

; copy result to R1 register

MOV A, #00H

; move 00H to accumulator

ADDC A, R0

; add R0 register to accumulator with carry

MOV R0, A

; copy result to R0 register

MOV A, R4

; Copy upper byte of multiplier to accumulator

MOV B, R6

; Copy upper byte of multiplicand to B register

MUL AB

; Multiply A and B Register

ADD A, R1

; Add R1 register to accumulator

MOV R1, A

; copy result to R1 register

MOV A, B

; copy upper byte of R4*R6 to accumulator

ADDC A, R0

; add R0 to accumulator with carry

MOV R0, A

; Copy accumulator value to R0 register

END

; End asm source code

OUTPUT
MULTIPLICAND (2040) R6=20, R7=40

MULTIPLIER (2030) R4=20, R4=30

Final Answer will Store in (R0R1R2R3) R0=04, R1=14, R2=12, R3=00


2040*2030=04141200

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

CHECK CARRY FLAG=0?


NO

SOR VALUE R3, R2 TO ACCUMULATOR AND DO RIGHT SHIFT OPERATION

TOD

SUBTRACT R2 FROM R0 AND R3 FROM R1 UNTILL CARRY FLAG IS SET

YES

CHECK CARRY NO
FLAG=0?
NO

RESULT IS ZERO COPY THE DIVIDEND TO R0 AND R1 REGISTER, LOWER

BYTE AND UPPERBYTE

LOAD R4, R5 TO ACCUMULATOR AND DO LEFT SHIFT OPERATION

YES
DECREMENT
NO B
CHECK
B=0?
NO
STORE THE RESULT IN R3 AND R2

STOP

18

PROGRAM FOR 16 BIT DIVISIONS


ORG

DIV1

0X0000

; Starting address of code

CLR C

; Clear carry initially

MOV R4, #00H

; Clear R4 working variable initially

MOV R5, #00H

; Clear R5 working variable initially

MOV B, #00H

; Clear B since B will count the number of left-shifted bits

INC B

; Increment counter for each left shift

MOV A, R2

; Move the current divisor low byte into the accumulator

RLC A

; Shift low-byte left, rotate through carry to apply highest


bit to high Byte

DIV 2

MOV R2, A

; Save the updated divisor low-byte

MOV A, R3

; Move the current divisor high byte into the accumulator

RLC A

; Shift high-byte left high, rotating in carry from low-byte

MOV R3, A

; Save the updated divisor high-byte

JNC DIV1

; Repeat until carry flag is set from high-byte

MOV A, R3

; Move high-byte of divisor into accumulator

RRC A

; Rotate high-byte of divisor right and into carry

MOV R3, A

; Save updated value of high-byte of divisor

MOV A, R2

; low-byte of divisor into accumulator

RRC A

; Rotate low-byte of divisor right, with carry from high byte

MOV R2, A

; Save updated value of low-byte of divisor

CLR C

; Clear carry, we don't need it anymore

MOV 07H, R1

; Make a safe copy of Dividend low byte

MOV 06H, R0

; Make a safe copy of Dividend High byte

MOV A, R0

; move the contents in the register R0 into accumulator

SUBB A, R2

; Dividend - shifted divisor = result bit (no factor, only 0or1)

MOV R0, A

; Save updated dividend

MOV A, R1

; Move high-byte of dividend into accumulator

SUBB A, R3

; Subtract high-byte of divisor (all together 16-bit sub)

MOV R1, A

; Save updated high-byte back in high-byte of divisor

JNC DIV3

; if carry flag is NOT set, result is 1

MOV R1, 07H

; Otherwise result is 0, save copy of divisor to undo

MOV R0, 06H

; Otherwise result is 0, save copy of divisor to undo sub

subtraction

19
DIV3

CPL C

; Invert carry, so it can be directly copied into result

MOV A, R4

; Move the content of register R4 into the accumulator

RLC A

; Shift carry flag into temporary result

MOV R4, A

; Move the accumulator contents into register R4

MOV A, R5

; Move the contents of the register R5 into the accumulator

RLC A

; Rotate accumulator through carry

MOV R5, A

; copy accumulator value to R5

DJNZ B, DIV2

; now count backwards and repeat until "B" is zero

MOV R3, 05H

; Move result to R3/R2

MOV R2, 04H

; Move result to R3/R2

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

Low power crystal

Mid range crystal

High range crystal

RC oscillator (low cost).

5. Programmable timers and on-chip ADC.


6. Up to 12 independent interrupt sources.
7. Powerful output pin control (25 mA (max.) current sourcing capability per pin.)
8. EPROM/OTP/ROM/Flash memory option.
9. I/O port expansion capability.
10. Free assembler and simulator support from Microchip at www.microchip.com
PIC Memory Organisation:

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.

Program Memory map


The program memory map of PIC16C74A is shown in Figure.
On reset, the program counter is cleared and the program starts at 00H. Here a
'goto' instruction is required that takes the processor to the mainline program.
When a peripheral interrupt, that is enabled, is received, the processor goes to
004H. A suitable branching to the interrupt service routine (ISR) is written at
004H.
Data memory (Register Files):
Data Memory is also known as Register File. Register File consists of two components.
1. General purpose register file (same as RAM).
2. Special purpose register file (similar to SFR in 8051).

22

Data Memory map


The special purpose register file consists of input/output ports and control registers. Addressing from
00H to FFH requires 8 bits of address. However, the instructions that use direct addressing modes in
PIC to address these register files use 7 bits of instruction only. Therefore the register bank select
(RP0) bit in the STATUS register is used to select one of the register banks.
In indirect addressing FSR register is used as a pointer to anywhere from 00H to FFH in the data
memory.
Ex. No.:2
ASSEMBLY LANGUAGE PROGRAMMING

AND SIMULATION OF PIC 16FXXX IN


MPLAB IDE
Date: 10-02-2015
2.A
10.02.2015

8-BIT ADDITIONS AND SUBTRACTION

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

ADDITION OF 8 BIT NUMBERS

25

PROGRAM FOR 8START


BIT ADDITIONS

SUM

SET
FORfor
SUM
10HRAM ;LOC
Ram10H
loc 10H
sum

EQU
ORG

0H

; Start at address 0

MOV ADDEND 1 TO WREG AND ADD 34H TO WREG

MOVLW 25H

; WREG=25

ADDLW

; Add 34H to WREG

0X46

STORE THE RESULT IN RAM MEMORY


SUM ; Save the SUM in loc 10H
LOCATION 10H

MOVWF
HERE

GOTO
END

OUTPUT
WREG = 25
W = W+46=25+46
W=6B

HERE ; Stay here forever


; STOP
End the asm source file

26
SUBTRACTION OF 8 BIT NUMBERS

START

INITIALISE THE RAM LOCATION

LOAD WREG WITH MINUEND 1

SUBTRACT MINUEND 2 FROM MYREG

STORE THE RESULT IN MYREG MEMORY


LOCATION

STOP

27

PROGRAM FOR 8 BIT SUBTRACTIONS

MYREG

EQU

0X20

; Ram location 20H for SUM

ORG

0H

; Set Starting Address

MOVLW

0X4C

; Load WREG=4CH

MOVWF

MYREG

; MYREG= 4CH

MOVLW

0X6E

; WREG=6EH

SUBWF

MYREG, W

; WREG= MYREG-WREG

BNN

NEXT

; if N=0(C=1), jump to NEXT target

NEGF

0XFE8

; Take 2s complement of WREG

NEXT

MOVWF

MYREG

; Save the result in 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

16-BIT ADDITIONS AND SUBTRACTION

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

ADDITION OF 16 BIT NUMBERS

START

STORE ADDEND 1 IN RAM LOCATION 6 AND 7

LOAD LOWER BYTE OF ADDEND2 TO WREG

ADD LOWERBYTE OF ADDEND2 WITH RAM


LOCATION 6 AND STORE RESULT IN FILE REG F

LOAD UPPER BYTE OF ADDEND2 TO WREG AND ADD


WITH RAM LOCATION 7 RESULT STORE IN FILE REG F

STOP

30

PROGRAM FOR 16 BIT ADDITIONS

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

6= (8D) ; File register location= (8D)

; Location

7= (3B) ; File register location= (3B)

ORG

0H

; Set starting Location

MOVLW

0XE7

; Load the low byte now (WREG=E7H)

ADDWF

0X6, F

; F= W+F=E7+8D=74 and CY=1

MOVLW

0X3C

; Load the high byte (WREG=3CH)

ADDWFC

0X7, F

; F=W+F+carry =3C+3B+1=78H

HERE

; Stay here

GOTO
END

; End asm source file

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

SUBTRACTION OF 16 BIT NUMBERS

START

STORE MINUEND 1 IN RAM LOCATION 6 AND 7

LOAD LOWER BYTE OF MINUEND 1 TO WREG

SUBTRACT LOWERBYTE OF MINUEND1 WITH RAM


LOCATION 6 AND STORE RESULT IN FILE REG F

LOAD UPPER BYTE OF MINUEND1 TO WREG AND


SUBTRACT WITH RAM LOCATION 7 RESULT STORE IN
FILE REG F

STOP

32

PROGRAM FOR 16 BIT SUBTRACTIONS

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)

; File register location= (62)

; Loc 7

= (27)

; File register location= (27)

ORG

0H

MOVLW

0X96

; Load the low byte now (WREG=96H

SUBWF

0X6, F

; F= W-F=62H+96H=CCH, C= Borrow=0, N=1

MOVLW

0X12

; Load the high byte (WREG=12H)

SUBWFB

0X7, F

; F=F-W-b, Subtract byte with borrow

GOTO
END

HERE

; Set starting Location

; 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

8 BIT MULTIPLICATIONS AND DIVISION

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

SET ASIDE FILE REG AN STORE MULTIPLICANT IN


FILE REG

LOAD WREG WITH MULTIPLICANT

MULTIPLY WREG WITH MULTIPLIER AND RESULT


WILL STORE IN PRODH AND PRODL

STOP

35

PROGRAM FOR 8- BIT MULTIPLICATION

MUL

EQU D25

; Store the multiplicand in MUL label (25In decimal)

ORG

; Set starting Location

0H

MOVLW MUL ; WREG=25

HERE

MULLW 0X20

; 25H*20H =4A0H

GOTO HERE

; Stay here

END

; End the asm source file

OUTPUT
25D*20H =19H*20H
PRODH=01H and PRODL=F4H

36
8-BIT DIVISION

START

SET ASIDE FILE REG AND CLEAR THE QUOTIENT

LOAD WREG WITH NUMERATOR AND


DENOMINATOR
SUBTRACT DENOMINATOR FROM NUMERATOR AND
INCREMENT F REG FOR EACH SUBTRACTION

ADD DENOMENATOR BACK TO GET REMAINDER

STOP

37

PROGRAM FOR 8-BIT DIVISION

ORG

0H

; Set starting Location

NUM

EQU

0X19

; Set aside file Register

MYQ

EQU

0X20

MYNMB

EQU

D95

; to Load Decimal value 95

MYDEN

EQU

D10

; to Load Decimal value 10

CLRF

MYQ

; Quotient=0

B1

MOVLW MYNMB

; WREG=95

MOVWF

; Numerator=95

NUM

MOVLW MYDEN

; WREG= Denominator=10

INCF

MYQ, F

; Increment Quotient for every 10 subtraction

SUBWF

NUM, F

; Subtract 10 (F=F-W)

BC

B1

; keep doing it until C=0

DECF

MYQ, F

; Once too many

ADDWF

NUM, F

; Add 10 back to get remainder

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

INTERFACING LCD WITH 8051

Ex. No.:3
Date: 10-03-2015
Aim:

To interface LCD with 8051 microcontroller using Assembly Language.


Apparatus Required:
1.
2.
3.
4.

Personal Computer
Keil Vision Software
8051 Trainer kit
8051 Micro Controller

Procedure:
1.
2.
3.
4.
5.

Create a New Project in Keil Software


Select the Controller as Atmel AT89C51
Create a New Script and type the program
Save the program as .asm file type
Add the saved file to the Source Group folder in the Target Folder under

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

INTERFACING LCD WITH 8051

START

LOAD THE VALUE 38H AS COMMAND TO ACC,


L
CALL COMMAND SUBROUTINE

LOAD THE VALUE 0EH AS COMMAND TO ACC,


CALL COMMAND SUBROUTINE

LOAD THE VALUE 01H AS COMMAND TO ACC, CALL COMMAND SUBROUTINE

LOAD THE VALUE 06H AS COMMAND TO ACC, CALL COMMAND SUBROUTINE

LOAD THE VALUE 84H AS COMMAND TO ACC, CALL COMMAND SUBROUTINE

LOAD THE LETTER NHAS DATA TO ACC, CALL DATA SUBROUTINE

LOAD THE LETTER OH AS DATA TO ACC, CALL DATA SUBROUTINE

STAY HERE

40
DATA
START

LOAD THE VALUE FROM


ACC TO PORT A

CLEAR R/W AND SET RS

SET THE BIT EN

CLEAR THE BIT EN

RETURNN

COMMAND
START

LOAD THE VALUE FROM


ACC TO PORT A

CLEAR RS, R/W

SET THE BIT EN

CLEAR THE BIT EN

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

;INIT. LCD 2 LINES, 5X7 MATRIX

ACALL COMNWRT ;call command subroutine


ACALL DELAY

;give LCD some time

MOV A,#0EH

;display on, cursor on

ACALL COMNWRT

;call command subroutine

ACALL DELAY

;give LCD some time

MOV A,#01H

;clear LCD

ACALL COMNWRT ;call command subroutine


ACALL DELAY

;give LCD some time

MOV A,#06H

;shift cursor right

ACALL COMNWRT

;call command subroutine

ACALL DELAY

;give LCD some time

MOV A,#84H

;cursor at line 1, pos. 4

42
ACALL COMNWRT

;call command subroutine

ACALL DELAY

;give LCD some time

MOV A,#'N'

;display letter N

ACALL DATAWRT

;call display subroutine

ACALL DELAY

;give LCD some time

MOV A,#'O'

;display letter O

ACALL DATAWRT

;call display subroutine

ACALL DELAY

;give LCD some time

AGAIN: SJMP AGAIN

;stay here

COMNWRT:

;send command to LCD

MOV P0,A

;copy reg A to port 0

CLR P3.7

;RS=0 for command

CLR P3.6

;R/W=0 for write

SETB P3.5

;E=1 for high pulse

ACALL DELAY

;give LCD some time

CLR P3.5

;E=0 for H-to-L pulse

RET
DATAWRT:

;write data to LCD

MOV P0,A

;copy reg A to port 0

SETB P3.7

;RS=1 for data

43
CLR P3.6

;R/W=0 for write

SETB P3.5

;E=1 for high pulse

ACALL DELAY

;give LCD some time

CLR P3.5

;E=0 for H-to-L pulse

RET
DELAY: MOV R3,#50

;50 or higher for fast CPUs

HERE2: MOV R4,#255

;R4 = 255

HERE: DJNZ R4,HERE

;stay until R4 becomes 0

DJNZ R3,HERE2
RET
END

RESULT
Thus the LCD is interfaced with AT89C51 and verified.

44

INTERFACING DAC WITH 8051


MICROCONTROLLER IN PROTEUS

Ex. No.:9
Date: 12-05-2015
Aim:

To interface DAC with 8051 microcontroller in PROTEUS.


Apparatus Required:
1. Personal Computer.
2. Keil uVision 5.
3. Proteus 8 professional.
Algorithm:
STEP 1: Start the program.
STEP 2: Open keil software.
STEP 3: Open the keil editor window.
a. create a label named AGAIN.
b. Move the table address to DPTR.
c. Move the count value 13 to R2.
Step 4: Create the Loop named BACK.
a. Clear Accumulator.
b. Move the DPTR and accumulator address into accumulator.
c. Assign Port2 to A.
d. Increment DPTR.
Step 5: Execute the Loop named BACK, until the count reaches 13.
Step 6: Jump to loop named AGAIN.
a.save the program.
b.start debugging the program.
c.open logic analyzer and setup the corresponding port.
d.run the program and verify the output.
Step 7: end the program.

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:

LOGICAL ANALYSER OUTPUT IN KEIL:

Result:
Thus the DAC was interfaced with 8051 microcontroller using PROTEUS and the output
curves were verified.

48
Ex. No.:10

INTERFACING OF STEPPER MOTOR


WITH 8051 MICROCONTROLLER IN PROTEUS

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.

FULL STEP SEQUENCE:


STEP

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

HALF STEP SEQUENCE:


STEP

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

STUDY OF POSITION CONTROL OF A DC


SERVO MOTOR USING 8051 & PIC

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

One popular servo model, Futabas S3004, rotates counterclockwise


with increasing pulse widths, while another servo, the Blue Bird BMS-380,
rotates clockwise with increasing pulse widths. This difference is
significant to note. If your servos were included as part of Robodysseys
Mouse kit then you probably has the Futaba S3004. Robodyssey does sell
Blue Bird servos as well, but I assume that most readers own the Futaba
servo, so I will focus my discussion on the S3004. Rest assured, the
following discussion is valid for all servo models you simply must keep in
mind which servo you are using and program it accordingly. For a detailed
comparison of a number of popular servo models, see Appendix E: Servo
Comparisons and Physical Limitations.
INTERFACING SERVO MOTOR WITH 8051 USING KEIL C
A servo motor uses servo mechanism, which is a closed loop mechanism
that uses position feedback to control the precise angular position of the
shaft. Stepper motor, which is an open loop system, can also be used for
precise angular control. But Servo Motors are preferred in angular motion
applications such as robotic arm. Moreover controlling of servo motors are
very simple, easy and needs no extra hardware like stepper motor.
Usually hobby circuit servo motors have three wires. Two of them are red
and black which is used to give power to the motor and the third wire is
used to provide control signal for angular position. It uses Pulse Width
Modulated (PWM) waves as control signals. The angle of rotation is
determined by the width of the pulse at the control pin. The servo motor
used here is having angle of rotation from 0 to 180 degrees. We can
control the exact angular position by varying the pulse between 1ms to
2ms. Before using this in your application, please refer the datasheet of
your servo for angle and pulse width information

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_();
}
}

INTERFACING SERVO MOTOR WITH PIC MICROCONTROLLER


Servo can be easily be controlled using microcontrollers using Pulse
Width Modulated (PWM) signals on the control wire. Here we are using a
servo whose angular rotation is limited to 0 180. We can control the
exact angular position by using a pulse, whose width varying from
1 millisecond to 2 millisecond on the control wire. The actual behaviour of
a particular motor depends upon its manufacturer; please refer the
datasheet of the particular motor for that.

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:

void servoRotate0() //0 Degree


{
unsigned int i;
for(i=0;i<50;i++)
{
PORTB.F0 = 1;
Delay_us(800);
PORTB.F0 = 0;
Delay_us(19200);

60
}
}

void servoRotate90() //90 Degree


{
unsigned int i;
for(i=0;i<50;i++)
{
PORTB.F0 = 1;
Delay_us(1500);
PORTB.F0 = 0;
Delay_us(18500);
}
}
void servoRotate180() //180 Degree
{
unsigned int i;
for(i=0;i<50;i++)
{
PORTB.F0 = 1;
Delay_us(2200);
PORTB.F0 = 0;
Delay_us(17800);

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

Data Transfer Instructions:

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

Program Branching Instructions:

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

Clear bit b of register f

bsf f, b

Set bit b of register f

clrw

Clear working register W

clrf f

Clear f

movlw k

Move literal 'k' to W

movwf f

Move W to f

movf f, F(W)

Move f to F or W

swapf f, F(W)

Swap nibbles of f, putting result in F or W

andlw k

And literal value into W

andwf f, F(W)

And W with F and put the result in W or F

andwf f, F(W)

And W with F and put the result in W or F

iorlw k

inclusive-OR literal value into W

iorwf f, F(W)

inclusive-OR W with f and put the result in F or W

67

xorlw k

Exclusive-OR literal value into W

xorwf f, F(W)

Exclusive-OR W with f and put the result in F or W

addlw k

Add the literal value to W and store the result in W

addwf f, F(W)

Add W to f and store the result in F or W

sublw k

Subtract the literal value from W and store the result in W

subwf f, F(W)

Subtract f from W and store the result in F or W

rlf f, F(W)

Copy f into F or W; rotate F or W left through the carry bit

rrf f, F(W)

Copy f into F or W; rotate F or W right through the carry


bit

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)

Decrement f and copy the result to F or W; skip the next


instruction if the result is zero

1/2

incfcz f, F(W)

Increment f and copy the result to F or W; skip the next


instruction if the result is zero

1/2

goto label

Go to the instruction with the label "label"

68

call label

Go to the subroutine "label", push the Program Counter in


the stack

retrun

Return from the subroutine, POP the Program Counter


from the stack

retlw k

Retrun from the subroutine, POP the Program Counter


from the stack; put k in W

retie

Return from Interrupt Service Routine and re-enable


interrupt

clrwdt

Clear Watch Dog Timer

sleep

Go into sleep/ stand by mode

Das könnte Ihnen auch gefallen