30
Experiment No.-2 Aim :-Write a program to add two numbers laying at two memory location and display the result. Apparatus :-Keil software, Microcontroller kit and Computer 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

Micro Controller Practical File

Embed Size (px)

Citation preview

Page 1: Micro Controller Practical File

Experiment No.-2

Aim:-Write a program to add two numbers laying at two memory location and display the result.

Apparatus:-Keil software, Microcontroller kit and Computer

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

Page 2: Micro Controller Practical File

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.

Apparatus:-Keil software, Microcontroller kit and Computer

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:-

MOV DPTR, #3000HMOVEX A,@DPTR

MOV B, AINC DPTRMUL. AB

MOV DPTR. #3020HMOVX @DPTR, A

INC DPTRMOV A, B

MOVX@DPTR,A

Page 3: Micro Controller Practical File

Experiment No. 4

Aim:-Write a program to check a number for being ODD or EVEN and show the result on the display.

Apparatus:- Keil software, Microcontroller kit and Computer

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,#09HANL A,#01HJNZ NEXT

MOV A,00HSJUMP K1

NEXT: MOV A,#01H K1: END

Page 4: Micro Controller Practical File

Experiment No. 5

Aim:-Study of interrupt structure of 8051/8031 microcontroller.

Theory :- Whenever the microcontroller is executing a program and if a user wants to service to an I/O Device then an external asynchronous input would inform the microcontroller that it should complete the execution of current instruction and then fetch a new routine that will service the new request ion I/O device.

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 INT14) 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

Page 5: Micro Controller Practical File

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

Page 6: Micro Controller Practical File

bit

The interrupt enable register :

EA - - ES ET1 EX1 ET0 EX0

Symbol Position function

EA IE.7 Disable all the interrupts. If EA=0, no interrupt will be acknowledged. If EA=1 each interrupt source is individually enabled or disabled by setting or clearing its enable bit.

- IE.6 Reserved

ET2 IE.5 Reserved for future expansions.

ES IE.4 Serial Port Interrupt enablebit

ET1 IE .3 Timer 1 Interrupt enable bit

EX1 IE.2 External interrupt 1 Interrupt enable bit

ET0 IE.1 Timer 0 Interrupt enable bit

EX0 IE.0 External interrupt 0 Interrupt enable bit

Page 7: Micro Controller Practical File

Experiment No. 6

Aim:-Program to show the use of INT 0 and INT 1

Theory: - The 8051 microcontroller has two external hardware interrupt INT 0 and INT 1.Upon the activation of these interrupt the 8051 microcontroller is interrupted. The microcontroller stops the current program execution and to the interrupt vector table to execute the interrupt service routine.execute interrupts vector location for INT 0 is 0003 h and location for INT 1 is 0013 H Both these interrupts can either be edge triggered or level triggered. We will write a program to assume that INT0 pin is connected to a switch that is normally high. Whenever the switch is low the LED should turn on. The LED’s connected to P1.3 pin of port 1 when external interrupt INT0 is activated.

Program:-

ORG 0000 HLJMP MANIORG 0003HSETB PI.3

MOV R0,#200DJNZ R0,L1

RETIORG 0030 H

MAIN:MOV IE,# 81 hL2: SJUMP L2

END

Page 8: Micro Controller Practical File

Experiment No 7(a)

Aim:-Write a program to sort the number in ascending order

Apparatus :- Keil software, Microcontroller kit and Computer

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,# 09HSTART: MOV DPTR,#3000H

MOV R1,#09HBACK: MOV R2,DPL

MOVX,A@DPTRMOV R3,AINC DPTRMOV B,AMOVA,R3

CJNE A,B,NEAJUMP SKIPNE : JC SKIPMOV DPL,R2

MOV A,BMOVX @DPTR,A

INC DPTRMOV A,R3

MOVX @DPTR,ASKIP :DJNZ R1,BACK

DJNZ R0,STARTEND

Page 9: Micro Controller Practical File

EXPERIMENT NO 7(b)

Aim:-Write a program to sort the number in descending order

Apparatus :- Keil software, Microcontroller kit and Computer

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, # 09HSTART: MOV DPTR, #3000H

MOV R1, #09HBACK: MOV R2, DPL

MOVX A, @DPTRMOV R3,AINC DPTR

MOVX A, @DPTRMOV B, AMOVA, R3

CJNE A, B,NEAJUMP SKIPNE: JC SKIP

MOV DPL, R2MOV A, B

MOVX @DPTR, AINC DPTRMOV A, R3

MOVX @DPTR, A

Page 10: Micro Controller Practical File

SKIP : DJNZ R1,BACKDJNZ R0, START

END

Page 11: Micro Controller Practical File

EXPERIMENT NO 8

Aim:-Write a program to find factorial of a number.

Apparatus :- Keil software, Microcontroller kit and Computer

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, #05HMOV A, # 01HMOV B, #01H

MOV R1, BMOV A, R3

MUL ABMOV R3, AMOV B, R1MOV A, B

CJNE B, #06 H, UPMOV A , R3MOV B, R2

END

Page 12: Micro Controller Practical File

Experiment No: 9

Aim:-To find the factorial of a number.

Apparatus:-Keil software, Microcontroller kit and Computer

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 HMOV A, #01 HMOV B, # 01 HMOV R3, #01 H

MOV R1, BMOV A, R3

MUL ABMOV R3, AMOV R2, B

INC R1MOV A, B

CJNE B, #06 H, UPMOV A, R3MOV B, R2

END

Page 13: Micro Controller Practical File

Experiment No 10

Aim:-Program to control a stepper motor in direction, speed and number of steps.

Apparatus:-Keil software, Microcontroller kit and Stepper motor

Theory:-

A) Main program

Step 1 : initialize serial port in port 1 and enable reception.

Step 2”: initialize TH1 for the required bide rate.

Step 3: enable serial and global interrupt.

Step 4: initialize timer 1 in mode 2.

Step 5: switch on the timer 1 run bit.

Step 6: implementation of state diagram, so initialize state is 0.

Step 7: if state is 0, check if key is 1 or 2 and change state accordingly to key pressed and transmitted from program counter.

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.

Step 10: go to step 7.

B) ISR of serial interrupt

Step 1: if interrupt is because of T1 then clear the T1 and return.Step 2: if interrupt is because of RI then coy the data into temporary register and change the value of variable to indicate the next state of the state diagram system. If the speed is to be increased or decreased, decrease or increase the delay respectively.

Page 14: Micro Controller Practical File

Step 3: clear RI flag.

C) Subroutine for clockwise rotation.

Step 1; give the data into the sequence of forward motion.

Step 2: give the delay after every data.

Register values

1) Interrupt enable registerTo enable global and serial interrupt

D7 D6 D5 D4 D3 D2 D1 D0

EA - - ES ET1 EX1 ET0 EX0

1 0 0 1` 0 0 0 0

9 0

2) timer mode register

D7 D6 D5 D4 D3 D2 D1 D0

Gate C/T M1 M0 Gate C/T M1 M0

0 0 0 1 0 0 0 0

2 0

TMOD=0*20

Page 15: Micro Controller Practical File

3)Serial control register

D7 D6 D5 D4 D3 D2 D1 D0

Sm0 SM1 SM2 REN TB8 RB8 TI RI

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 0000HLJMP STARTORG 0023HJNB RI, NXT

CLR RIMOV A, SBUF

CJNE A,#’C’,A1MOV RI,#01H

SJMP NXTA1: CJNE A, #’A’, A2

MOV R1, #02HSJMP NXT

Page 16: Micro Controller Practical File

A2: CJNE A, #’t’, A3MOV R1, #03H

SJMP NXTA3: CJNE A, #’s’,A4

MOV R2, AMOV A, R3ADD A, #50MOV R3, AMOV A, R2

JNC A5INC R4

A5: SJMP NXTA4: CJNE A,#’f’,NXT

MOV R2, AMOV A, R3

CLR CSUBB A, #50MOV R3, AMOV A, R2JNC NXTDEC R4

NXT: JNB T1, NXT1CLR T1

NXT1: RETIORG 0100 H

CLOCKWISE: MOV R0, #30 HSTEP: MOV P1,@R0

MOV 06, R3MOV 07, R4

AGAI N 1: MOV 04, R7AGAIN: DJNZ R4, AGAIN

DJNZ R3, AGAIN 1MOV 03, R6MOV 04, R7

INC R0

Page 17: Micro Controller Practical File

CJNE R0, #34, STEPRET

ORG 0200HANTICLOCKWISE

MOV R0, #40HSTEP 1: MOV P1,@R0

MOV 06, R3MOV 07, R4

AGAIN 3: MOV 04, R7AGAIN 2: DJNZ R4, AGAIN 2

DJNZ R3, AGAIN 3MOV 03, R6MOV 04, R7

INC R0CJNE R0, #44 H, STEP 1

RETORG 1000H

START: MOV R3, #100MOV R4, #50MOV R0, #30

MOV @R0, #09 HINC R0

MOV @R0, #0A HINC R0

MOV @R0, #06 HINC R0

MOV @R0, #05 HMOV R0, #40 H

MOV @R0, #05 HINC R0

MOV @R0, #06 HINC R0

MOV @R0, #0A HINC R0

MOV @R0, #09 H

Page 18: Micro Controller Practical File

MOV SCON, #50 HMOV TH1, #0FD H

MOV IE, #90HMOV TMOD, #20 H

SETB TR1MOV R5, #00H

HERE: CJNE R5, #00 H, B1CJNE R1, #01 H, B3

MOV R5, #01B3: CJNE R1, #02 H, B1

MOV R5, #02 HSJMP HERE

B1: CJNE R5, #01H, B2CJNE R1, #03 H, B4

MOV R5, #00 HSJMP HERE

B4: LCALL CLOCKWISESJMP HERE

B2: CJNE R5, #02H, HERECJNE R1, #03 H, B5

MOV R5, #00 HSJMP HERE

B5: LCALL CLOCKWISESJMP HERE

END

Page 19: Micro Controller Practical File

Experiment no 11

Aim:-Interfacing of DC motorApparatus:-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 motorC)ISR of INT1Step 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

Page 20: Micro Controller Practical File

1) Interrupt Enable (IE) Register.D7 D6 D5 D4 D3 D2 D1 D0

EA - - ES ET1 EX1 ET0 EX0

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

UP: JNB P3.2,UP

MOV A,R7

ADD A,#20 H

MOV R7,A

CLR IEO

RETI

Page 21: Micro Controller Practical File

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

HERE: MOV C,P1.0

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

Page 22: Micro Controller Practical File

MOV R4,R7

MOV A,#0FF H

SUBB A,R4

MOV R4,A

AGAIN 2: DJNZ R4, AGAIN2

SJUMP HERE

END

Page 23: Micro Controller Practical File
Page 24: Micro Controller Practical File