Micro Controller Practical File...
Micro Controller Practical File...
-2
Aim:-Write a program to add two numbers laying at two memory location
and display the result.
Theory:-
1. Consider that a byte of data is present at memory location 30H and a second
byte of data is present at memory location 35 H.
2. We have two add the byte at bath memory location. Using ADD instruction we
will add the contents of both memory locations.
3. Result will be stored at memory location 30H.
Program:-
MOV 30H,#0A H
MOV 35,#04 H
MOV A,#00 H
ADD A,30H
ADD A,35 H
MOV 30H,A
END
Experiment No: 3
Aim:-Multiply two 8 bit numbers stored in external memory location 3000H
& 3001 H. store the result in memory location 3020 & 3021H.
Theory:-
1. Consider that a byte is present at the memory location 3000 H and second bytes
present at memory location 3001 H.
2. We have to multiply the bytes the present at the above tow memory location.
3.We will multiply the numbers using MUL instruction, as MUL instruction
operates only on the A and B register we will load the tow numbers from memory
location 3000 H and 3001H to the and the B registers, the result of multiplication is
stored in the A and B register
Program:-
Theory:-We have a number given. Initially, we check the LSB of that number by
ANDing the number with 01 H.if result of ANDing is 01 H then it is ODD
otherwise it is EVEN. Store Register A =00 if number is EVEN otherwise register
A=01 H if the number is ODD
Program:-
MOV A,#09H
ANL A,#01H
JNZ NEXT
MOV A,00H
SJUMP K1
NEXT: MOV A,#01H
K1: END
Experiment No. 5
Aim:-Study of interrupt structure of 8051/8031 microcontroller.
Once the I/O device is serviced the microcontroller resumes operation from the
point wherever it has stopped. The external asynchronous input applied to the
microcontroller is termed as interrupt. An interrupt may be generated by internal
chip operation or they be provided by external source. An interrupt causes the
microcontroller to enter an interrupt handling routine. The interrupt handling
routine is located at predetermined absolute address in the program memory.
We have 4 interrupts:
1) Timer (0)
2) Timer(1)
3) External hardware interrupt INT0 AND INT1
4) Serial communication interrupt R1 andT1
These are the internal interrupts:
Timer (0)
Timer (1)
Serial communication interrupts R1 andT1
These are the external interrupts:
INT0
INT1
Timer Flag Interrupt: when the timer/counter overflow the corresponding timer
flag TF) and TF1 is set to 1. The flag is cleared to 0 when interrupt generate
program call to the timer subroutine in the memory.
Serial Port Interrupt: the serial port interrupt is generated because of two bits RI
and TI,. These bits are logically ORed to provide a single interrupt to the
processor. The TI bit in the SCON register is set when a data byte is transmitted
and RI bit in the SCON register is set when a data byte is received. The serial port
interrupt RI & TI are not cleared like the timer interrupt generate a program call.
So the program which deals the serial communication must reset or clear the RI &
TI bit to zero to enable next data communication operation.
External Interrupt: the two interrupts that are generated by external circuits are
INT0 and INT1. The inputs on the pins of these interrupt set to the interrupt flag
IE0 and IE1 to the TCON register. These interrupts may be edge triggered or level
triggered.
The Interrupt Priority Register:-The register IP bits determine whether the
interrupt is to have a high or low priority
- - - PS PT1 PX1 PTO PX0
1. Priority bit =1 assign high priority
2. Priority bit =0 assign low priority
Symbol Position function
- IE.7 Reserved
- IE.6 Reserved
- IE.5 Reserve
PS IE.4 Serial Port Interrupt priority bit
PT1 IE .3 Timer 1 Interrupt priority bit
PX1 IE.2 External interrupt 1 Interrupt priority
bit
PT0 IE.1 Timer 0 Interrupt priority bit
PXO IE.0 External interrupt 0 Interrupt priority
bit
Program:-
ORG 0000 H
LJMP MANI
ORG 0003H
SETB PI.3
MOV R0,#200
DJNZ R0,L1
RETI
ORG 0030 H
MAIN:MOV IE,# 81 h
L2: SJUMP L2
END
Experiment No 7(a)
Aim:-Write a program to sort the number in ascending order
Theory: Consider that a block of n word is present now we have to arrange these n
numbers in ascending order, let n=9 for example. we will use DPTR as a pointer to
point a block of n number. Initialize in the first iteration we compare the first
number worth the second number if first number is Smaller than second number
don’t interchange the contents. If the first number is garter than second number
now swap their contents. Now at the end of this iteration first two elements are
stored in ascending order. In the next iteration we will compare the first number
with the third number. if the first number is smaller than the third don’t interchange
contents otherwise swap the contents at the end of this iteration first three elements
are stored in ascending order. Go on comparing till all the contents are stored in
ascending order.
Program:-
MOV R0,# 09H
START: MOV DPTR,#3000H
MOV R1,#09H
BACK: MOV R2,DPL
MOVX,A@DPTR
MOV R3,A
INC DPTR
MOV B,A
MOVA,R3
CJNE A,B,NE
AJUMP SKIP
NE : JC SKIP
MOV DPL,R2
MOV A,B
MOVX @DPTR,A
INC DPTR
MOV A,R3
MOVX @DPTR,A
SKIP :DJNZ R1,BACK
DJNZ R0,START
END
EXPERIMENT NO 7(b)
Theory: Consider that a block of n word is present now we have to arrange these
n numbers in descending order, let n=9 for example. we will use dptr as a pointer
to point a block of n number. Initialize in the first iteration we compare the first
number worth the second number if first number is greater than second number
don’t interchange the contents. If the first number is smaller than second number
now swap their contents. Now at the end of this iteration first two elements are
stored in descending order. In the next iteration we will compare the first number
with the third number. if the first number is greater than the third don’t interchange
contents otherwise swap the contents at the end of this iteration first three elements
are stored in descending order. Go on comparing till all the contents are stored in
descending order.
Program:-
MOV R0, # 09H
START: MOV DPTR, #3000H
MOV R1, #09H
BACK: MOV R2, DPL
MOVX A, @DPTR
MOV R3,A
INC DPTR
MOVX A, @DPTR
MOV B, A
MOVA, R3
CJNE A, B,NE
AJUMP SKIP
NE: JC SKIP
MOV DPL, R2
MOV A, B
MOVX @DPTR, A
INC DPTR
MOV A, R3
MOVX @DPTR, A
SKIP : DJNZ R1,BACK
DJNZ R0, START
END
EXPERIMENT NO 8
Aim:-Write a program to find factorial of a number.
Theory: To compute the factorial of the number means to multiply the number
says n with (n-1) (n-2) (n-3). In our program we will initialize the A and B register
with 1. We will load the number whose factorial is to be found in the A and
register R0. Store the numbers in register B in register R1. We will multiply the
number in A with the number in B. result of multiplication is stored in A register
and B register. Increment R1 check that R1 less than R0.if yes, continue the
process till factorial is computed.
Program:- MOV RO, #05H
MOV A, # 01H
MOV B, #01H
MOV R1, B
MOV A, R3
MUL AB
MOV R3, A
MOV B, R1
MOV A, B
CJNE B, #06 H, UP
MOV A , R3
MOV B, R2
END
Experiment No: 9
Aim:-To find the factorial of a number.
THEORY:-to compute the factorial of the no. , means to multiply the number says
n with (n-1)(n-2)(n-3)….1in our program we will initialize the A and B register
with 1.we will load the number whose factorial is be found in the a register and
R0.to store the number in register B inR1 we multiply the number in A with
number B. the result of multiplication stored in a register increment R1 check that
R1 is less than R0. if yes continue the process till factorial is computed.Store the
result.
PROGRAM:
MOV R0, # 05 H
MOV A, #01 H
MOV B, # 01 H
MOV R3, #01 H
MOV R1, B
MOV A, R3
MUL AB
MOV R3, A
MOV R2, B
INC R1
MOV A, B
CJNE B, #06 H, UP
MOV A, R3
MOV B, R2
END
Experiment No 10
Aim:-Program to control a stepper motor in direction, speed and number of
steps.
Theory:-
A) Main program
Step 8; if state is 1 and if stop key is pressed change state to 0. Call subroutine for
clockwise motion.
Step 9: if state is 2 and if stop key is pressed change state to 0. Call subroutine for
anticlockwise motion.
Register values
D7 D6 D5 D4 D3 D2 D1 D0
1 0 0 1` 0 0 0 0
9 0
D7 D6 D5 D4 D3 D2 D1 D0
0 0 0 1 0 0 0 0
2 0
TMOD=0*20
3)Serial control register
D7 D6 D5 D4 D3 D2 D1 D0
0 1 0 1 0 0 0 0
5 0
SCON =0*50
4) Timer 1 register are to be initialized to 0*FD so that a bode rate of 9600 can be
achieved because th1 = 0*FD
PROGRAM:
ORG 0000H
LJMP START
ORG 0023H
JNB RI, NXT
CLR RI
MOV A, SBUF
CJNE A,#’C’,A1
MOV RI,#01H
SJMP NXT
A1: CJNE A, #’A’, A2
MOV R1, #02H
SJMP NXT
A2: CJNE A, #’t’, A3
MOV R1, #03H
SJMP NXT
A3: CJNE A, #’s’,A4
MOV R2, A
MOV A, R3
ADD A, #50
MOV R3, A
MOV A, R2
JNC A5
INC R4
A5: SJMP NXT
A4: CJNE A,#’f’,NXT
MOV R2, A
MOV A, R3
CLR C
SUBB A, #50
MOV R3, A
MOV A, R2
JNC NXT
DEC R4
NXT: JNB T1, NXT1
CLR T1
NXT1: RETI
ORG 0100 H
CLOCKWISE: MOV R0, #30 H
STEP: MOV P1,@R0
MOV 06, R3
MOV 07, R4
AGAI N 1: MOV 04, R7
AGAIN: DJNZ R4, AGAIN
DJNZ R3, AGAIN 1
MOV 03, R6
MOV 04, R7
INC R0
CJNE R0, #34, STEP
RET
ORG 0200H
ANTICLOCKWISE
MOV R0, #40H
STEP 1: MOV P1,@R0
MOV 06, R3
MOV 07, R4
AGAIN 3: MOV 04, R7
AGAIN 2: DJNZ R4, AGAIN 2
DJNZ R3, AGAIN 3
MOV 03, R6
MOV 04, R7
INC R0
CJNE R0, #44 H, STEP 1
RET
ORG 1000H
START: MOV R3, #100
MOV R4, #50
MOV R0, #30
MOV @R0, #09 H
INC R0
MOV @R0, #0A H
INC R0
MOV @R0, #06 H
INC R0
MOV @R0, #05 H
MOV R0, #40 H
MOV @R0, #05 H
INC R0
MOV @R0, #06 H
INC R0
MOV @R0, #0A H
INC R0
MOV @R0, #09 H
MOV SCON, #50 H
MOV TH1, #0FD H
MOV IE, #90H
MOV TMOD, #20 H
SETB TR1
MOV R5, #00H
HERE: CJNE R5, #00 H, B1
CJNE R1, #01 H, B3
MOV R5, #01
B3: CJNE R1, #02 H, B1
MOV R5, #02 H
SJMP HERE
B1: CJNE R5, #01H, B2
CJNE R1, #03 H, B4
MOV R5, #00 H
SJMP HERE
B4: LCALL CLOCKWISE
SJMP HERE
B2: CJNE R5, #02H, HERE
CJNE R1, #03 H, B5
MOV R5, #00 H
SJMP HERE
B5: LCALL CLOCKWISE
SJMP HERE
END
Experiment no 11
Aim:-Interfacing of DC motor
Apparatus:-Keil software, Microcontroller kit and DC motor
Theory:-
Main Program:-
Step 1: Enable global and external interrupt 0 & 1.
Step 2: Make pin INT0 & INT1 programmed to be input.
Step 3: Enable serial and global interrupts.
Step 4: If clockwise motion is required as said by the switch, make pin 1A of the
L293 to be set and 2A pin cleared. Wait for a delay according to the speed
required. Clear both the output i.e. 1A & 2A of the remaining time, so as to have
some on time and some off time and hence to achieve PWM control of the motor
speed.
Step 5: If anticlockwise motion is required as said by the switch, make pin 2A of
the L293 to be set and 1A pin cleared. Wait for a delay according to the speed
required. Clear both the output i.e. 1A & 2A of the remaining time, so as to have
some on time and some off time and hence to achieve PWM control of the motor
speed.
Step 6: Go to step 4.
B) ISR of INT 0.
Step 1: Wait for an interrupt pulse to complete as there is a switch.
Step 2: Clear interrupt flag.
Step 3: Increase the delay to slow down the motor
C)ISR of INT1
Step 1: Wait for the interrupt pulse to complete as there is a switch.
Step 2: Clear interrupt flag.
Step 3: Increase the delay to slow down the motor.
Register Values
1) Interrupt Enable (IE) Register.
D7 D6 D5 D4 D3 D2 D1 D0
1 0 0 0` 0 1 0 1
8 5
IE=0x85
PROGRAM:
ORG 0000H
LJUMP START
ORG 0003H
LJUMP UP
ORG 0013H
LJUMP DOWN
ORG 0100H
MOV A,R7
ADD A,#20 H
MOV R7,A
CLR IEO
RETI
ORG 0200H
DOWN:
JNB P3.3,DOWN
MOV A,R7
CLR C
SUBB A,#20 H
MOV R7,A
CLR IE1
RET I
ORG 1000 H
START:
MOV DR7,#128
MOV IE #85 H
MOV P3,0C H
MOV P1.5,C
CPL C
MOV P1.6,C
MOV R4,R7
AGNAIN 1:
DNJZ R4,AGAIN 1
CLR P1.5
CLR P1.6
MOV R4,R7
MOV A,#0FF H
SUBB A,R4
MOV R4,A
SJUMP HERE
END