Micro Controller Lab Manual AMC Updated
Micro Controller Lab Manual AMC Updated
IV SEMESTER B.E.
LABORATORY MANUAL
Staff in charge:
Mr.ANANTHAKUMAR
Asst. Professor
BRANCH :
1
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
LIST OF EXPERIMENTS
8
Stepper Motor Interface
DC motor interface for
9
direction and speed control
a. Sine
b. Square
10
c. Triangular
d. Ramp generator
11 Elevator
2
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
MICROCONTROLLER LABORATORY-1
Experiments
Note: For the experiments 1 to 6, 8051 assembly programming is to be used.
1 Data transfer – Program for block data movement, sorting, exchanging, finding largest element
in an array.
2 Arithmetic instructions: Addition, subtraction, multiplication and division. Square and cube
operations for 16 bit numbers.
3 Counters
4 Boolean and logical instructions (bit manipulation).
5 Conditional call and return instructions.
6 Code conversion programs – BCD to ASCII, ASCII to BCD, ASCII to decimal, Decimal to
ASCII, Hexa decimal to and Decimal to Hexa.
7 Programs to generate delay, Programs using serial port and on-chip timer/counters.
Note: Single chip solution for interfacing 8051 is to be with C Programs for the following
experiments.
II. INTERFACING
3
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
At first sight, it is imposing number of odd signs that must be known by heart. However, it is not as
complicated as it looks like. Many instructions are considered to be “different”, even though they perform
the same operation, so there are only 111 truly different commands. For example: ADD A, R0, ADD A,
R1... ADD A, R7 are instructions that perform the same operation (addition of the accumulator and register).
Since there are 8 such registers, each instruction is counted separately. Taking into account that all
instructions perform only 53 operations (addition, subtraction, copy etc.) and most of them are rarely used
in practice, there are actually 20-30 abbreviations to be learned, which is acceptable.
Types of instructions
Depending on operation they perform, all instructions are divided in several groups:
Arithmetic Instructions
Branch Instructions
Data Transfer Instructions
Logic Instructions
Bit-oriented Instructions
The first part of each instruction, called MNEMONIC refers to the operation an instruction performs
(copy, addition, logic operation etc.). Mnemonics are abbreviations of the name of operation being executed.
For example:
4
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
Arithmetic instructions
Arithmetic instructions perform several basic operations such as addition, subtraction, division,
multiplication etc. After execution, the result is stored in the first operand. For example: ADD A,R1 - The
result of addition (A+R1) will be stored in the accumulator.
Arithmetic Instructions
Mnemonic Description Byte Cycle
ADD A,Rn Adds the register to the accumulator 1 1
ADD A,direct Adds the direct byte to the accumulator 2 2
ADD A,@Ri Adds the indirect RAM to the accumulator 1 2
ADD A,#data Adds the immediate data to the accumulator 2 2
ADDC A,Rn Adds the register to the accumulator with a carry flag 1 1
ADDC
Adds the direct byte to the accumulator with a carry flag 2 2
A,direct
ADDC
Adds the indirect RAM to the accumulator with a carry flag 1 2
A,@Ri
ADDC Adds the immediate data to the accumulator with a carry
2 2
A,#data flag
SUBB A,Rn Subtracts the register from the accumulator with a borrow 1 1
SUBB Subtracts the direct byte from the accumulator with a
2 2
A,direct borrow
Subtracts the indirect RAM from the accumulator with a
SUBB A,@Ri 1 2
borrow
SUBB Subtracts the immediate data from the accumulator with a
2 2
A,#data borrow
INC A Increments the accumulator by 1 1 1
INC Rn Increments the register by 1 1 2
INC Rx Increments the direct byte by 1 2 3
INC @Ri Increments the indirect RAM by 1 1 3
DEC A Decrements the accumulator by 1 1 1
DEC Rn Decrements the register by 1 1 1
DEC Rx Decrements the direct byte by 1 1 2
DEC @Ri Decrements the indirect RAM by 1 2 3
INC DPTR Increments the Data Pointer by 1 1 3
MUL AB Multiplies A and B 1 5
DIV AB Divides A by B 1 5
Decimal adjustment of the accumulator according to BCD
DA A 1 1
code
5
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
Unconditional jump instructions: upon their execution a jump to a new location from where the program
continues execution is executed.
Conditional jump instructions: a jump to a new program location is executed only if a specified
condition is met. Otherwise, the program normally proceeds with the next instruction.
Branch Instructions
Mnemonic Description Byte Cycle
ACALL
Absolute subroutine call 2 6
addr11
LCALL
Long subroutine call 3 6
addr16
RET Returns from subroutine 1 4
RETI Returns from interrupt subroutine 1 4
AJMP addr11 Absolute jump 2 3
LJMP addr16 Long jump 3 4
Short jump (from –128 to +127 locations relative to the
SJMP rel 2 3
following instruction)
JC rel Jump if carry flag is set. Short jump. 2 3
JNC rel Jump if carry flag is not set. Short jump. 2 3
JB bit,rel Jump if direct bit is set. Short jump. 3 4
JBC bit,rel Jump if direct bit is set and clears bit. Short jump. 3 4
JMP
Jump indirect relative to the DPTR 1 2
@A+DPTR
JZ rel Jump if the accumulator is zero. Short jump. 2 3
JNZ rel Jump if the accumulator is not zero. Short jump. 2 3
CJNE Compares direct byte to the accumulator and jumps if not
3 4
A,direct,rel equal. Short jump.
CJNE Compares immediate data to the accumulator and jumps if
3 4
A,#data,rel not equal. Short jump.
CJNE Compares immediate data to the register and jumps if not
3 4
Rn,#data,rel equal. Short jump.
CJNE Compares immediate data to indirect register and jumps if
3 4
@Ri,#data,rel not equal. Short jump.
DJNZ Rn,rel Decrements register and jump if not 0. Short jump. 2 3
DJNZ Rx,rel Decrements direct byte and jump if not 0. Short jump. 3 4
NOP No operation 1 1
6
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
7
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
Logic Instructions
Logic instructions perform logic operations upon corresponding bits of two registers. After execution, the
result is stored in the first operand.
Logic Instructions
Mnemonic Description Byte Cycle
ANL A,Rn AND register to accumulator 1 1
ANL A,direct AND direct byte to accumulator 2 2
ANL A,@Ri AND indirect RAM to accumulator 1 2
ANL A,#data AND immediate data to accumulator 2 2
ANL direct,A AND accumulator to direct byte 2 3
ANL
AND immediate data to direct register 3 4
direct,#data
ORL A,Rn OR register to accumulator 1 1
ORL A,direct OR direct byte to accumulator 2 2
ORL A,@Ri OR indirect RAM to accumulator 1 2
ORL direct,A OR accumulator to direct byte 2 3
ORL
OR immediate data to direct byte 3 4
direct,#data
XRL A,Rn Exclusive OR register to accumulator 1 1
XRL A,direct Exclusive OR direct byte to accumulator 2 2
XRL A,@Ri Exclusive OR indirect RAM to accumulator 1 2
XRL A,#data Exclusive OR immediate data to accumulator 2 2
XRL direct,A Exclusive OR accumulator to direct byte 2 3
XORL
Exclusive OR immediate data to direct byte 3 4
direct,#data
CLR A Clears the accumulator 1 1
CPL A Complements the accumulator (1=0, 0=1) 1 1
SWAP A Swaps nibbles within the accumulator 1 1
RL A Rotates bits in the accumulator left 1 1
RLC A Rotates bits in the accumulator left through carry 1 1
RR A Rotates bits in the accumulator right 1 1
RRC A Rotates bits in the accumulator right through carry 1 1
Bit-oriented Instructions
8
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
Similar to logic instructions, bit-oriented instructions perform logic operations. The difference is that these
are performed upon single bits.
Bit-oriented Instructions
Mnemonic Description Byte Cycle
CLR C Clears the carry flag 1 1
CLR bit Clears the direct bit 2 3
SETB C Sets the carry flag 1 1
SETB bit Sets the direct bit 2 3
CPL C Complements the carry flag 1 1
CPL bit Complements the direct bit 2 3
ANL C,bit AND direct bit to the carry flag 2 2
ANL C,/bit AND complements of direct bit to the carry flag 2 2
ORL C,bit OR direct bit to the carry flag 2 2
ORL C,/bit OR complements of direct bit to the carry flag 2 2
MOV C,bit Moves the direct bit to the carry flag 2 2
MOV bit,C Moves the carry flag to the direct bit 2 3
Here is a list of the operands and their meanings:
A- accumulator;
Rn - is one of working registers (R0-R7) in the currently active RAM memory bank;
Direct - is any 8-bit address register of RAM. It can be any general-purpose register or a SFR (I/O
port, control register etc.);
@Ri - is indirect internal or external RAM location addressed by register R0 or R1;
#data - is an 8-bit constant included in instruction (0-255);
#data16 - is a 16-bit constant included as bytes 2 and 3 in instruction (0-65535);
addr16 - is a 16-bit address. May be anywhere within 64KB of program memory;
addr11 - is an 11-bit address. May be within the same 2KB page of program memory as the first byte
of the following instruction;
rel - is the address of a close memory location (from -128 to +127 relative to the first byte of the
following instruction). On the basis of it, assembler computes the value to add or subtract
from the number currently stored in the program counter;
bit - is any bit-addressable I/O pin, control or status bit; and
C- is carry flag of the status register (register PSW).
Addressing Modes
Instructions tell the processor which operation to carry out. For example, MOV A, #5EH tells the
processor to move the data 5EH to the accumulator.
Instructions result in the processor performing some operation on some data (the data being #5EH in the
example above). The instruction above is an example of what's known as immediate addressing. The
reason for this is because the data immediately follows the instruction in code memory.
However, it is not always adequate to store the data in code memory itself. If we needed to read the
information from a keyboard, for example, we would need to use something other than immediate
addressing.
9
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
The first byte of an instruction is known as the opcode (operation code) because this is the byte that is
decoded by the processor - from this code the processor can work out what operation it must perform.
For a one-byte instruction there is only the opcode.
For a two-byte instruction the second byte is the operand. The operand can be data or an 8-bit address.
For a three-byte instruction the second and third bytes make up the operand. A two-byte operand is usually
a 16-bit address, as we shall see when we look at long addressing.
Immediate
Register
Direct
Indirect
Relative
Absolute
Long
Indexed
Immediate Addressing
If the operand is a constant then it can be stored in memory immediately after the opcode. Remember,
values in code memory (ROM) do not change once the system has been programmed and is in use in the
everyday world. Therefore, immediate addressing is only of use when the data to be read is a constant.
For example, if your program needed to perform some calculations based on the number of weeks in the
year, you could use immediate addressing to load the number 52 (34H) into a register and then perform
arithmetic operations upon this data.
MOV R0, #34
The above instruction is an example of immediate addressing. It moves the data 34H into R0. The
assembler must be able to tell the difference between an address and a piece of data. The has symbol (#)
is used for this purpose (whenever the assembler sees # before a number it knows this is immediate
addressing).
This is a two-byte instruction.
Register Addressing
Often we need to move data from a register into the accumulator so that we can perform arithmetic
operations upon it. For example, we may wish to move the contents of R5 into the accumulator.
MOV A, R5
10
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
This is an example of register addressing. It moves data from R5 (in the currently selected register bank)
into the accumulator.
ADD A, R6
The above is another example of register addressing. It adds the contents of R6 to the accumulator, storing
the result in the accumulator. Note that in both examples the destination comes first. This is true of all
instructions.
Direct Addressing
Direct addressing is used for accessing data in the on-chip RAM. Since there are 256 bytes of RAM (128
bytes general storage for the programmer and another 128 bytes for the SFRs). That means the addresses
go from 00H to FFH, any of which can be stored in an 8-bit location.
MOV A, 67
The above instruction moves the data in location 67H into the accumulator. Note the difference between
this and immediate addressing. Immediate addressing uses the data, which is immediately after the
instruction. With direct addressing, the operand is an address. The data to be operated upon is stored in
that address. The assembler realises this is an address and not data because there is no hash symbol before
it.
ADD A, 06
The above instruction adds the contents of location 06H to the accumulator and stores the result in the
accumulator. If the selected register bank is bank 0 then this instruction is the same as ADD A, R6.
Indirect Addressing
Register addressing and direct addressing both restrict the programmer to manipulation of data in fixed
addresses. The address the instruction reads from (MOV A, 30H) or writes to (MOV 30H, A) cannot be
altered while the program is running.
There are times when it is necessary to read and write to a number of contiguous memory locations. For
example, if you had an array of 8-bit numbers stored in memory, starting at address 30H, you may wish
to examine the contents of each number in the array (perhaps to find the smallest number). To do so, you
would need to read location 30H, then 31H, then 32H and so on.
This can be achieved using indirect addressing. R0 and R1 may be used as pointer registers. We can use
either one to store the current memory location and then use the indirect addressing instruction shown
below.
MOV A, @Ri
where Ri is either R0 or R1.
Now, we can read the contents of location 30H through indirect addressing:
MOVR0,#30H
MOV A, @R0
The first instruction is an example of immediate addressing whereby the data 30H is placed in R0. The
second instruction is indirect addressing. It moves the contents of location 30H into the accumulator.
If we now wish to get the data in location 31H we use the following:
INC R0
MOV A, @R0
11
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
Once we see how to write a loop in assembly language, we will be able to read the entire contents of the
array.
Relative Addressing
Relative addressing is used only with certain jump instructions. The system executes a jump by changing
the contents of the PC to the address of the next instruction to be executed. For example, if we wished to
jump to the instruction stored at location 4EH in code memory, the PC would be loaded with 4EH. Then,
during the next execution cycle the contents of the PC (4EH) are placed on the address bus and the
instruction at 4EH is retrieved.
A relative address (or offset) is an 8-bit signed value, which is added to the PC to form the address of the
next instruction to be executed.
With 8-bit signed numbers, the MSB is used to determine whether the number is positive or negative. If
the MSB is 0 then the number is positive, while if the MSB is 1 the number is negative.
The instruction below shows how to jump six locations ahead.
SJMP 06H
SJUMP is an unconditional jump and is a 2-byte instruction. The number following it is an offset address.
If this instruction were stored in code memory at locations 100H and 101H, as shown below:
100H80H
101H06H
The opcode for SJMP is 80H. The operand is the offset address. If this instruction were executed the PC
would get the value 108H. This is what happens:
The PC contains 100H, therefore the instruction 80H is read into the IR.
The instruction is decoded as the 2-byte SJMP instruction.
The PC is incremented so that the operand may be retrieved.
The operand is read from code memory and the PC is incremented again (because this is a 2-bye
instruction).
The operand (06H) is added to the PC (102H + 06H = 108H).
The next instruction (at 108H) is executed.
Once we deal with 2's compliment and how negative numbers are dealt with in the CPU, we will look at
a backward jump.
The S in SJMP stands for short. The range of signed 8-bit numbers is -127 to 128. Therefore, using SJMP
allows us to jump 127 locations forward or 128 locations backward. Hence the nameshort jump.
When writing assembly programs we do not need to calculate the offset when using SJMP. Instead, we
use labels. If we wished to jump to the instruction at 108H we would simply label the instruction with an
appropriate name, for example THERE. We would then write the assembly code SJMP THERE. The
assembler does the work of replacing the label with the correct offset.
Absolute Addressing
Absolute addressing is only used with the ACALL and AJMP instructions.
ACALL - subroutine call (2 byte instruction)
AJMP - unconditional jump (2 byte instruction)
These instructions allow you to move to any instruction within the same 2K of memory.
12
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
We will look at the AJMP instruction only (at a later date, when we begin dealing with subroutines we
will deal with the ACALL instruction).
Note that only the eleven least significant bits of the PC are altered. The five most significant bits remain
the same. This means the AJMP will only allow you to jump to a location in the same 2K page as the
instruction directly after the jump.
For example:
If the label THERE represents an instruction at address 0F46H and the instruction AJMP THERE is in
memory at locations 0900H and 0901H, the assembler will encode the instruction as
11100001 1st byte (A10 - A8 + opcode)
010001102nd byte (A7 - A0)
The underlined bits are the low-order 11 bits of the destination address, 0F46H = 0000111101000110B.
The upper five bits in the program counter will not change when this instruction executes. Note that both
the AJMP instruction and the destination are within the 2K page bounded by 0800H and 0FFFH, and
therefore have the upper five address bits in common.
SOFTWARE DESCRIPTION
PROCESSOR used is Atmel AT89C51ED2 - micro controller that has 64Kbytes of on chip program
memory. It is a version of 8051 with enhanced features.
AT 89C51ED2 operates at 11.0592 MHz.
PROCESSOR FEATURES
DATA MEMORY: 256 Bytes of RAM, 1792 Bytes of XRAM, 2K Bytes of EEPROM.
ON-CHIP PERIPHERALS
3 16-bit Timers/Counters, Watch Dog Timer, Programmable Counter Array (PCA) on Port1 i.e. PWM and
Capture & Compar
INTERRUPTS
Nine sources of interrupt (both external and internal).
13
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
Two External interrupts INT0 and INT1 are provided with push button switches; these can also be used as
general-purpose switches.
Creating project
3. Start Project – New Project, and select the CPU from the device database (Database-Atmel-
AT89C51ED2). (Select AT89C51ED2 or AT89C51RD2 as per the board).On clicking ‘OK’, the following
option is displayed. Choose Yes.
4. Create a source file (using File->New), type in the assembly or C program and save this (filename.asm/
filename.c) and add this source file to the project using either one of the following two methods.
(i) Project-Components,Environmentand Books->addfiles->browse to the required file -> OK “OR”
(ii) right click on the Source Group in the Project Window and the Add Files to Group option.
8. The program is run using the Debug-Run command & halted using Debug-Stop Running. Also the
(reset, run, halt) icons can be used. Additional icons are (step, step over, step into, run till cursor).
9. If it is an interface program the outputs can be seen on the LCD, CRO, motor, led status etc. If it is a
part A program, the appropriate memory window is opened using View ->memory window (for data RAM
& XRAM locations), Watch window (for timer program),serial window, etc.
Note: To access data RAM area type address as D:0020h.
Similarly to access the DPTR region (XRAM-present on chip in AT89C51ED2) say 9000h location type
in X:09000H.
3. Start Project – New Project, and select the CPU from the device database (Database-Philips-
89C51RD2+). On clicking ‘OK’, the following option is displayed. Choose Yes.
4. Create a source file (using File->New), type in the assembly or C program and save this (filename.asm/
filename.c) and add this source file to the project using either one of the following two methods.
7. Now user can enter into Debug mode with Debug- Start / Stop Debug session dialog or by clicking in
the icon.
8 .The program is run using the Debug-Run command & halted using Debug-Stop Running. Also the
(reset, run, halt) icons can be used. Additional icons are (step, step
over, step into, run till cursor).
15
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
Write an ALP to transfer 10 bytes of data from memory location 8035H to 8062H
Program algorithm:
1. Initialize register to hold source, destination addresses, data and count values.
2. Obtain data from source location into accumulator and from accumulator into the destination
location.
3. Decrement the count of register and repeat step 2 till the count value reads zero.
PROCEDURE:
16
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
Result:TRIAL I:
Source:
X:8035H 8036H 8037H 8038H 8039H
2 4 6 8 9
Destination:
X:8062H 8063H 8064H 8065H 8066H
2 4 6 8 9
TRIAL II:
Write an ALP to exchange 5 bytes of data from memory location 32H to 62H
Program algorithm:
1. Initialize register to hold source, destination addresses, data and count values.
2. Obtain data from source location into accumulator and save in a register.
3. Obtain data from destination location into accumulator.
4. Exchange the data at the memory location.
5. Decrement the count of register and repeat from step 2 to 4 till the count value reads zero.
17
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
PROCEDURE:
Source:
X:32H 33H 34H 35H 36H
2 4 6 8 9
Destination:
X:62H 63H 64H 65H 66H
4 7 8 9 E
After execution
Source:
X:32H 33H 34H 35H 36H
4 7 8 9 E
Destination:
X:62H 63H 64H 65H 66H
2 4 6 8 9
Write an ALP to sort 5 bytes of data in descending order stored at memory location 9000H
Program algorithm:
1. Store the elements of the array from the address 9000H
2. Initialize a pass counter with array size -1 count.
3. Load compare counter with pass counter contents and initialize DPTR to point to the start address
4. Store the current and the next array elements pointed by DPTR in reg B & R2
5. Subtract the next element from the current element.
6. If the carry flag is not set then exchange the 2 no’s in the array.
7. Decrement the compare counter and repeat through step 4 until the counter becomes zero.
8. Decrement the pass counter and repeat through step 3 until the counter becomes zero.
18
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
Assembly program:
PROCEDURE:
19
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
Write an ALP to find the largest number in an array of 6 bytes of data at location 5000H.Store the
largest element at location 5052H.
Program algorithm:
1. Store the elements of the array from the address 5000H
2. Store the length of the array in R3 and set it as counter.
3. DPTR is loaded with starting address of the array.
4. Store the first number of the array in R1 where R1 is assigned to hold the largest number.
5. Increment DPTR.
6. Subtract the number pointed by DPTR from the contents of R1 for comparison if the next
array element is larger than the one in R1.
7. If the element pointed by DPTR is larger then load the larger number intoR1.
8. Decrement the counter and repeat steps through 5 until the counter becomes zero.
9. Store the largest number in R1 in address 5052H
20
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
PROCEDURE:
Result:
After execution:
5052H
FA
PROGRAM ALGORITHM:
1. Store the condition x in R1.
2. Load the first & second numbers to A & B registers.
3. Compare the contents of R1 and perform the operations.
4. Store the result present in A & B reg. to the appropriate memory locations.
21
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
CJNE R1,#00,CKSUB
ADD A,B Add B to A
MOV B,#00 Move imm data to B
JNC SKIP Jump
MOV B,#01H Move imm data to B
SKIP: SJMP LAST Short jump
CKSUB: CJNE R1,#01,CKMUL
CLR C Clear carry
SUBB A,B Subtract borrow
MOV B,#00 Movimm data to B
JNC SKIP1
MOV B,#0FFH
SKIP1: SJMP LAST
CKMUL: CJNE R1,#02,CKDIV
MUL AB Multiply AB
SJMP LAST
CKDIV: CJNE R1,#03,OTHER
DIV AB Divide AB
SJMP LAST
OTHER: MOV A,#00
MOV B,#00 Move imm data to B
LAST: INC R0 Increment R0
MOVX @R0,A Move A to ext RAM
INC R0 Increment R0
MOV A,B Move B to A
MOVX @R0,A Move A to ext RAM
HERE: SJMP HERE
END
PROCEDURE:
After execution
0040H 0041H 0042H 0043H
00 10 40 50
22
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
After execution
0040H 0041H 0042H 0043H
01 10 40 30
After execution
0040H 0041H 0042H 0043H
02 02 22 44
After execution
0040H 0041H 0042H 0043H
03 02 22 11
23
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
MOVX @DPTR,A
MOV A,B
MOV R2,A
POP B
MOV A,R0
MUL AB
ADD A,R2
LAST: INC DPTR
MOVX @DPTR,A
MOV A,B
ADDC A,#00
INC DPTR
MOVX @DPTR,A
HERE: SJMP HERE
END
PROCEDURE:
9000H 9001H
02 08
Trial 2:
24
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
Program algorithm:
1. Move 00 to A register.
2. Call the delay subroutine for 1Sec. Move FFH to registers R1,R2 & R3.
3. Decrement the value of registers nested one within the other such that they create a
delay of 1Sec.
4. Increment value of A
5. Stop the counter after it reaches a maximum value of FFH
PROCEDURE:
RESULT:
25
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
Program algorithm:
1. Move 00 to A register.
2. Call the delay subroutine for 1Sec. Move FFH to registers R1,R2 & R3.
3. Decrement the value of registers nested one within the other such that they create a
delay of 1Sec.
4. Increment value of A
5. Stop the counter after it reaches a maximum value of FFH and rolls back to 00H.
PROCEDURE:
RESULT:
Accumulator A is incremented in BCD form 00, 01, 02……..99.
26
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
1) Two eight bit numbers NUM1 & NUM2 are stored in external memory locations 8000h & 80001h
respectively. Write an ALP to compare the 2 nos.
Reflect your result as: if NUMI<NUM2, SET LSB of data RAM 2F (bit address 78H)
IF NUM1>NUM2, SET MSB OF 2F(7FH). if NUM1 = NUM2-Clear both LSB & MSB of bit
addressable memory location 2Fh
Algorithm:
1. Store the elements of the array from the address 4000h
2. Move the first number in r0 and the second number in register A respectively
3. Clear carry flag and subtract the two numbers, if the carry flag is 0(if the nos are equal), Clear both LSB
& MSB of bit addressable memory location 2Fh
4. If the carry bit is set then Set MSB of 2F(7FH), else LSB of data RAM 2F (bit address 78H).
PROCEDURE:
1. Create the project and save it.
2. Select target device – Atmel 89C51ED2.
3. Create a new file/ develop the source code.
4. Save the created file with .asm extension.
5. Add the file to the project.
6. Build the target.
7. Debug to enable the output.
27
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
Algorithm:
1. Point to the data RAM register 20h and store the condition x.
2. Point to 21h and 22h and move the first number to A register.
3. Compare the contents of r1 and perform the operations accordingly.
4. The result will be stored in 23H register.
RESULT:
28
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
Algorithm:
1) Check for set bit of P1.0
2) If set, toggle port2 with data FFh & 00h with a delay.
3) If not set, toggle port2 with data D3h & 78h with delay.
ORG 00H
JB P1.0, LOOP1
LOOP2: MOV A,#0FFh
MOV P2,A
ACALL DELAY
MOV A,#00h
MOV P2,A
ACALL DELAY
SJMP LOOP2
LOOP1: MOV A,#78h
MOV P2,A
ACALL DELAY
MOV A,#0D3h
MOV P2,A
ACALL DELAY
SJMP LOOP1
DELAY: MOV R0,#0FFh
HERE1: DJNZ R1, HERE1
HERE2: DJNZ R0, HERE2
RET
END
RESULT:
29
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
6 CODE CONVERSIONS
Algorithm:
PROCEDURE:
1. Create the project and save it.
2. Select target device – Atmel 89C51ED2.
3. Create a new file/ develop the source code.
4. Save the created file with .asm extension.
5. Add the file to the project.
6. Build the target.
7. Debug to enable the output.
8. Load the BCD value at address 50H
9. The ASCII output is seen at 51H & 52H.
Label Mnemonics Comments
ORG 0000H Start from 0000h location
SJMP 30H Short jump to 30h address
ORG 30H Start from 30h location
MOV R1,#50H Move #50H to R1
MOV A,@R1 Move the value in R1 to A
MOV R2,A Move A to R2
ANL A,#0FH AND #0FH to A
ORL A,#30H ORL #30H to A
INC R1 Increment R1
MOV @R1,A Move A to indirect RAM
MOV A,R2 Move R2 to A
SWAP A Swap nibbles within A
ANL A,#0FH AND #0FH to A
ORL A,#30H ORL #30H to A
INC R1 Increment R1
MOV @R1,A Move A to indirect RAM
HERE: SJMP HERE Short jump
END End
RESULT:
TRIAL I: D: 050H
30
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
Algorithm:
ORG 00H
ORG 00H
MOVX A,@DPTR
CLR C Clear carry flag
SUBB A,#30H
MOV R7,A
END
RESULT:
Before execution x:9000h – 45
After execution
Project window
R7 – 0x15
A – 0x15
Algorithm:
1) Get the decimal number in the range 0-99 as an input.
2) Separate the higher and lower nibble of the two digit number.
3) Add 30h to the lower nibble & store the result.
4) Bring the higher nibble to the ones position, add 30h to it & display the result.
ORG 00H
LJMP 9000H
ORG 9000H
MOV DPTR,#9000H
MOVX A,@DPTR
ANL A,#0FH Mask the upper nibble
ADD A,#30H
MOV R6,A
MOVX A,@DPTR
ANL A,#0F0H Mask the lower nibble
SWAP A Bring it to the ones place
ADD A,#30H
MOV R7,A
END
31
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
RESULT:
Before execution x:9000h – 39
After execution
Project window
R6 – 0x39
R7 – 0x33
Algorithm:
PROCEDURE:
32
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
TRIAL I:
X:9000H
33
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
RESULT:
Trial 1: Trial 2:
Before execution
X: 040H : 45
After execution
X: 041H : 2D
34
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
Algorithm:
1) Load TMOD with 02h i.e timer0 mode2 8-bit auto reload.
2) Load TH0 with -3 to set the baud rate to 9600.
3) Start timer0 by setting TR0 to 1.
4) Monitor the value of TF0 flag with the use of the jump instruction.
5) Clear flag TF0 & compliment the 1st bit of the port1.
6) This makes the last bit of port1 to toggle.
MOV TMOD,#02h
MOV TH0,-3
SETB TR0
LOOP: JNB TF0,LOOP
CLR TF0
CPL P1.0
SJMP LOOP
END
RESULT:
35
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
Algorithm:
1) The TMOD register is loaded with the value 20h, indicating the use of timer1 in mode2 to set baud
rate.
2) TH1 is loaded with one of the value -3 to set the baud rate.
3) The SCON register is loaded with the value 50h, indicating serial mode1, where 8-bit data is framed
with start & stop bit.
4) TR1 is set to start timer1.
5) The character byte to be transferred serially is written into the SBUFF register.
6) The TI flag is monitored with the use of the jump instructions, to see if the character has been
transferred completely.
7) Clear TI flag to assure the transmition of the next byte.
8) To transfer the next byte, go to step5.
MOV TMOD,#20h
MOV TH1,#-3
MOV SCON,#50h
SETB TR1
MOV A.#’H’
ACALL TRANS
MOV A,#’I’
ACALL TRANS
AGAIN: SJMP AGAIN
TRANS: MOV SBUF,A
HERE: JNB TI,HERE
CLR TI
RET
END
RESULT:
UART#1
HI
36
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
Hardware Interfacing
Features of Embedded C:
Symbol Operation
& Bitwise AND
| Bitwise OR
~ Bitwise NOT
>> Shift Right
<< Shift Left
^ P0.0
37
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
Experiment No. 8
Write a C program to control the speed of rotation and direction of rotation of a Stepper Motor.
Stepper motor unlike DC motor rotates in steps. Stepper motor has 4 coils which forms the stator and a
central rotor. Rotation depends on excitation of stator coils.
Step Coil A Coil B Coil C Coil D
1 0 0 0 1
2 1 0 0 0
3 0 1 0 0
4 0 0 0 1
Anyone of these values forms the initial value. To get 360o revolution 200 steps are required. Step angle=
3600 /200 = 1.80. (difference between 2 teeth).
Algorithm:
1. Configure P0 as output.
2. Apply the initial excitation of 11 to motor coils through P0.
3. For clockwise motion -Rotate right once the excitation and repeat step 2.
4. For anticlockwise motion -Rotate left once the excitation and repeat step 2.
38
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
Program:
#include <REG51xD2.H>
void delay (unsigned int x) /* Delay Routine */
{
for(;x>0;x--);
return;
}
main ( )
{
unsigned char Val, i;
P0=0x00;
Val = 0x11;
for (i=0;i<4;i++)
{
P0 = Val;
Val = Val<<1; /* Val= Val>>1; for clockwise direction*/
delay (500);
}
}
39
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
Experiment No. 9
Algorithm:
Program:
#include <REG51xD2.H>
sbit inr= P3^2; /*speed increment switch*/
sbit dcr= P3^3 /*speed decrement switch*/
main()
{
unsigned char i=0x80;
P0 = 0x7f; /*Run the motor at half speed.*/
while (1)
{
if (!inr)
{
while (!inr);
if(i>10)
i=i-10; /*increase the DC motor speed*/
}
if(!dcr)
40
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
{
while(!dcr);
if(i<0xf0)
i=i+10; /*decrease the DC motor speed*/
}
P0=i;
}
}
41
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
Experiment No. 10
a. Square waveform
b .Triangular Waveform
c. Ramp waveform
d. Sine waveform
42
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
Algorithm:
1. Let initial, amplitude of the square wave be 2.5v (7F) and frequency count 100.
2. Output the values 00h (0ff) and 7fh (on) Values through P0.
3. If amplitude key is pressed then increase the voltage in steps of 0.15v (8).
4. If the frequency key is pressed increment the count in steps of 50. If the count exceeds 1000
reset it back to 100.
5. Every time amplitude and frequency changes output the value through P0 and note the
waveform on CRO.
Program:
#include <REG51xD2.H>
void delay(unsigned int x) /* delay routine */
{
for (;x>0;x--);
}
main()
{
unsigned char on = 0xFF,Off=0x00;
P0=on;
delay(100);
P0 = Off; /* clear port */
delay(100);
}
43
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
Algorithm:
Program:
#include <REG51xD2.H>
main()
{
unsigned char i=0;
while(1)
{
for(i=0;i<0xff;i++)
{ /* Generate ON pulse */
P1 = i;
}
for(i=0xfe;i>0x00;i--) /* Generate OFF pulse */
{
P1 = i;}
}
}
44
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
Algorithm:
Program:
#include <REG51xD2.H>
main ()
{
Unsigned char i=0;
P0 = 0x00; /* P0 as Output port */
while (1)
{
for (i=0;i<0xff;i++) /* Generate ON pulse */
{
P0 = i;
}
}
45
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
Algorithm:
Program:
#include <REG51xD2.H>
main()
{
static int a[13]={128,192,238,255,238,192,128,64,17,0,17,64,128};
unsigned char i=0;
P0 = 0x00; /* P0 as Output port */
while (1)
{
for(i=0;i<13;i++) /* Output different values */
{
P0 = a[i];
}
}
}
46
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
Experiment No. 11
Algorithm:
Program:
#include <REG51F.H>
void delay(unsigned int);
main()
{
unsigned char Flr[9] = {0xff,0x00,0x03,0xff,0x06,0xff,0xff,0xff,0x09};
unsigned char FClr[9] = {0xff,0x0E0,0x0D3,0xff,0x0B6,0xff,0xff,0xff,0x79};
unsigned char ReqFlr,CurFlr = 0x01,i,j;
P0 = 0x00;
P0 = 0x0f0;
while(1)
{
P1 = 0x0f;
ReqFlr = P1 | 0x0f0;
while(ReqFlr == 0x0ff)
ReqFlr = P1 | 0x0f0; /* Read Request Floor from P1 */
ReqFlr = ~ReqFlr;
if(CurFlr == ReqFlr) /* If Request floor is equal to Current Floor */
{
P0 = FClr[CurFlr]; /* Clear Floor Indicator */
continue;
} /* Go up to read again */
else if(CurFlr > ReqFlr) /* If Current floor is > request floor */
{
i = Flr[CurFlr] - Flr[ReqFlr]; /* Get the no of floors to travel */
j = Flr[CurFlr];
for(;i>0;i--) /*Move the indicator down */
{
P0 = 0x0f0 | j;
j--;
delay(25000);
}
}
47
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
48
Department of EEE/AMCEC/Bangalore
Microcontroller Lab Manual
49
Department of EEE/AMCEC/Bangalore