Download as pdf or txt
Download as pdf or txt
You are on page 1of 44

“MICROCONTROLLERS LAB”

Applications of 8051
Microcontroller
Course Code BUE358A Semester III

SYLLABUS

I. PROGRAMMING
1. Data Transfer - Block move, Exchange, Sorting, Finding largest
element in an array
2. Arithmetic Instructions - Addition/subtraction, multiplication
and division, square, Cube – (16 bits Arithmetic operations – bit
addressable)
3. Counters
4. Boolean & Logical Instructions (Bit manipulations)
5. Conditional CALL & RETURN
6. Code conversion: BCD – ASCII; ASCII – Decimal; Decimal -
ASCII; HEX - Decimal and Decimal -HEX
7. Programs to generate delay, Programs using serial port and on-
Chip timer / counter
8. Write C programs to interface 8051 chip to Interfacing modules
to develop single chip solutions
9. Elevator interface to 8051
10. Program to blink LED
Demonstration Experiment
11. Stepper and DC motor control interface to 8051
“MICROCONTROLLERS LAB”

Creating and compiling a μ Vision5 project

1. Double Click on the  Vision5 icon on the desktop.


2. Close any previous projects that were opened using – Project->Close.
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.

5. Set the Target options using -> Project – Options for Target opens the Vision2
Options for Target – Target configuration dialog. Set the Xtal frequency as 11.0592
Mhz, and also the Options for Target – Debug – use either Simulator / Keil Monitor-
51 driver.

If Keil Monitor- 51 driver is used click on Settings -> COM Port settings select the
COM Port to which the board is connected and select the baud rate as 19200 or 9600
(recommended). Enable Serial Interrupt option if the user application is not using on-
chip UART, to stop program execution.
6. Build the project; using Project -> Build Project.  Vision translates all the user
application and links. Any errors in the code are indicated by – “Target not created” in the
Build window, along with the error line. Debug the errors. After an error free build, goto
Debug mode
“MICROCONTROLLERS LAB”

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


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.
“MICROCONTROLLERS LAB”

1. DATA TRANSFER INSTRUCTIONS

1) a. Write an assembly language program to transfer n =10 bytes of data from location
8035h to location 8041h (without overlap).

ORG 0000H
SJMP 30H
ORG 30H
MOV DPH,#80H
MOV R0,#35H //source address
MOV R1,#41H //destination address
MOV R3,#0AH //count
BACK: MOV DPL, r0
MOVX A,@dptr
MOV DPL, R1
MOVX @dptr,A
INC R0
INC R1
DJNZ R3, BACK
HERE: SJMP HERE
END
RESULT:

Before Execution: 10 locations X:8035h are filled up with data.

After Execution: 10 locations X:8041h are filled up with data from 8035h.

Algorithm:

1. Initialize registers to hold count data & also the source & destination addresses.
2. Get data from source location into accumulator and transfer to the destination
location.
3. Decrement the count register and repeat step 2 till count is zero.

Note: For data transfer with overlap start transferring data from the last location of
source array to the last location of the destination array.
“MICROCONTROLLERS LAB”

1) b. ASSEMBLY LANGUAGE PROGRAM TO EXCHANGE A BLOCK OF DATA.


Write an assembly language program to exchange n = 5 bytes of data at location
0027h and at location 0041h.

ORG 00H
SJMP 30H
ORG 30H
MOV R0,#27H //source address
MOV R1,#41H //destination address
MOV R3,#05H //count
BACK: MOVX A,@r0
MOV r2,a
MOVX a,@r1
MOVX @r0,a
MOV a, r2
MOVX @r1,a
INC R0
INC R1
DJNZ R3, BACK
HERE: SJMP HERE
END

RESULT :

Before Execution: 5 locations at X:0027h & X:0041h are filled up with data

Algorithm :

1.Initialize registers to hold count data (array size) & also the source & destination
addresses.
2.Get data from source location into accumulator and save in a register.
3.Get data from the destination location into accumulator.
4.Exchange the data at the two memory locations.
5.Decrement the count register and repeat from step 2 to 4 till count is zero
“MICROCONTROLLERS LAB”

1) c. ASSEMBLY LANGUAGE PROGRAM TO SORT NUMBERS.


//BUBBLE SORT PROGRAM
Write an assembly language program to sort an array of n= 6 bytes of data in
ascending order stored from location 8035h.(use bubble sort algorithm)

ORG 0000H
SJMP 30H
ORG 30H
MOV R0,#05 //count n-1 -ARRAY SIZE-n- Pass Counter
L1: MOV dptr, #9000h //array stored from address 9000h
MOV A,R0 //initialize exchange counter
MOV R1,A
L2: MOVX a, @dptr //GET NUMBER FROM ARRAY
MOV B, A //& STORE IN B
INC dptr
MOVX a, @dptr //next number in the array
CLR C //reset borrow flag
MOV R2, A //STORE IN R2
SUBB A, B //2nd - 1st no.—no compare instruction in 8051
JC NOEXCHG // JNC - FOR ASCENDING ORDER
MOV A,B //EXHANGE THE 2 NOES IN THE ARRAY
MOVX @dptr,a
DEC DPL //DEC dptr-INSTRUCTION NOT PTRESENT
MOV a,R2
MOVX @dptr,a
INC DPTR
NOEXCHG: DJNZ R1,L2 //decrement compare counter
DJNZ R0,L1 //decrement pass counter
here: SJMP here
END

Algorithm :

1.Store the elements of the array from the address 9000h


2.Initialize a pass counter with array size-1 count (for number of passes)
3.Load compare counter with pass counter contents & initialize DPTR to point to thestart
address of the array (here 9000h).
4.Store the current and the next array elements pointed by DPTR in registers B and r2respectively.
5.Subtract the next element from the current element.
“MICROCONTROLLERS LAB”

6.If the carry flag is set (for ascending order) then exchange the 2 numbers in the array.
7.Decrement the compare counter and repeat through step 4 until the counter becomes0.
8.Decrement the pass counter and repeat through step 3 until the counter becomes 0.
\

RESULT :

Before Execution:Unsorted Array at 9000h

After Execution: Sorted Array (Descending order) at 9000h

1) d. Write an assembly language program to find the largest element in a given string
of n = 6 bytes at location 4000h. Store the largest element at location 4062h.

ORG 0000H
SJMP 30H ORG
30H
MOV R3,#6 //length of the array
MOV DPTR,#4000H //starting address of the array
MOVX A,@DPTR
MOV r1,a
NEXTBYTE: INC DPTR
MOVX A,@DPTR
CLR C //reset borrow flag
MOV R2,A //next number in the array
SUBB A,R1 //OTHER Num - PREVIOUS LARGEST no.
JC skip // JNC for smallest element
MOV A,r2 //UPDATE larger number in r1
MOV R1,A
skip:DJNZ R3,NEXTBYTE
MOV DPL, #62H //LOCATION OF THE RESULT-4062H
MOV A,R1 //LARGEST NUMBER
MOVX @DPTR,A //STORE AT #4062H
OVER: SJMP OVER
END
“MICROCONTROLLERS LAB”

Algorithm :

1.Store the elements of the array from the address 4000h


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 (r1 is assigned to hold the largest number).
5.Increment DPTR.
6.Subtract the number pointed by DPTR from the contents of r1 (to compare whetherthe
next array element is larger than the one in r1).
7.If the element pointed by DPTR is larger then load the larger number into r1.
8.Decrement the counter and repeat steps through 5 until the counter becomes 0.
9.Store the largest number in r1 in address 4062h

RESULT :

Before Execution:

After Execution: Location 4062 has the largest element.


“MICROCONTROLLERS LAB”

2. ARITHMETIC INSTRUCTIONS :

2) a. ASSEMBLY LANGUAGE PROGRAM ILLUSTRATING ADDITION,


SUBTRACTION, MULTIPLICATION AND DIVISION .
Write an ALP to perform the following:
If x=0-perform w + v; else if x=1-perform w-v; else if x=2-perform w*v; elseif x=3-
perform w/v, where w & v are eight bit numbers.

ORG 0000H SJMP


30H ORG 30H
MOV R0, #40H
MOVX A,@R0
MOV R1, A //R1 HAS CONDITION XINC
R0
MOVX A,@R0
MOV B, A //B HAS 1ST NUMBER-vINC
R0
MOVX A,@R0 //A HAS 2ND NUMBER-w
CJNE R1,#00,CKSUB
ADD A,B //PERFORM ADDITION
MOV B,#00 //B HAS CARRYJNC
SKIP
MOV B,#01H
SKIP:SJMP LAST
CKSUB: CJNE R1,#01,CKMUL
CLR C //RESET BORROW FLAG
SUBB A,B
MOV B,#00 //B INDICATES BORROWJNC
SKIP1
MOV B,#0FFH //FF INDICATES NEGATIVE NUMBERSKIP1:SJMP LAST
CKMUL: CJNE R1,#02,CKDIV
MUL AB //16 bit product in AB with A having lower byte
SJMP LAST
CKDIV: CJNE R1,#03,OTHER
DIV AB //Quotient in A & remainder in B
SJMP LAST
OTHER:MOV A,#00
MOV B,#00
LAST: INC R0
MOVX @R0,AINC
R0
MOV A,B MOVX
@R0,A
HERE:SJMP HEREEND

Algorithm:
1.Store the condition x in r1.
2.Load the first and second numbers to A and B registers respectively
3.Compare the contents of r1 and perform the operations add, sub, etc accordingly.
4.Store the result present in A and B registers to the appropriate memory locations.
“MICROCONTROLLERS LAB”

RESULT: Before Execution:

ADD SUB

After Execution:

ADD After Execution: SUB

Before Execution:

MUL After Execution: MUL


“MICROCONTROLLERS LAB”

2)b. ASSEMBLY PROGRAM ILLUSTRATING SQUARE AND CUBE OPERATIONS.


//cube is an example of 16-bit arithmetic operation
//depending on flag condition, square or cube is performed
// Flag is a bit in the bit addressable RAM, say 1st bit of location 20h is used, then bit
address is 01
2) An eight bit number X is stored in external memory location 9000h. Write an ALP to
compute (i) the square of the number X if LSB of data RAM 20h (bit address 01H) is set
(ii) the cube of the number X if LSB of data RAM 20h (bit address 01H) is reset.

store your result at locations 9001, 9002, 9003h.

ORG 0000H
SJMP 30H ORG
30H
MOV DPTR,#9000H
MOVX A,@DPTR //GET NUMBER-X
MOV R0,A //STORE IN R0
MOV B,A
MUL AB //SQUARE IT-X^2
CLR C //FOR STORING RESULT
JB 01,LAST //IF BIT 01 IS SET THEN END, ELSE DO CUBE
PUSH B //STORE UPPER PART OF SQUARE
MOV B,A //B-LOWER PART OF X^2
MOV A,R0 //A-X
MUL AB //X*LOWER X^2INC
DPTR
MOVX @DPTR,A //STORE PARTIAL RESULT
MOV A,B
MOV R2,A //UPPER PART OF X*LOWER X^2 IN R2POP B
//GET BACK UPPER PART OF SQUARE
MOV A,R0 //A-X
MUL AB //X*UPPER X^2
ADD A,R2 //ADD TO PARTIAL RESULT
LAST:INC DPTR
MOVX @DPTR,A
MOV A,B
ADDC A,#00 //ADD CARRY TO B(FOR SQUARE RESULT, C=0)INC
DPTR
MOVX @DPTR,A
HERE:SJMP HERE
END

RESULT:

CUBE OF 56H IS 9B498 WHICH IS STORED AS 98, B4, 09 (LOWER BYTE FIRST)
“MICROCONTROLLERS LAB”

To get square make the D1 bit of data memory 20h high, say FF,02,06,etc. The bit addressis 01.
Similarly bit address 78h correspond to D0 bit 0f data ram location 2Fh.

Algorithm:

1. Store the eight bit number x in A, r0 & B registers.


2. Multiply A and B registers to obtain the square (say SQH:SQL) of the number x.Check if bit
01 is set. If set go to end (storing the result), else do the cubeoperations.
3. The high part of the square result (SQH) is stored on the stack.
4. Multiply the low part of the square result (SQL) with x (partial cube result).
5. Store the low part of the above result at 9001h & the high part in R2.
6. Retrieve the high part of the square result (SQH) stored on the stack & multiplywith
x.
7. Add the low part of the above result (SQH*X) with R2 and store in 9002h.
8. Add the high part (SQH*X) with the resulting carry and store in 9003.
“MICROCONTROLLERS LAB”

3) COUNTERS

ASSEMBLY PROGRAM ILLUSTRATING HEX UP/DOWN COUNTERS.


//counter program - hex/binary counters
3)a. Write an ALP to implement (display) an eight bit up/down binary (hex) counters on
watch window.
Note: to run this program, after selecting DEBUG session in the main menu use View->
Watch& call Stack window, in the Watches select watch 1(or 2) andpress F2 and
enter a (for accumulator A)

ORG 0H SJMP
30H ORG 0H
MOV a,#00
BACK: ACALL DELAY
INC a //dec a for binary down counterJNZ
BACK
HERE:SJMP HERE
DELAY: MOV r1,#0FFH
DECR1:MOV r2,#0FFH DECR:
MOV r3,#OFFH DJNZ r3,$
DJNZ r2,DECR
DJNZ r1,DECR1RET
END

RESULT:
Accumulator A is incremented in binary from 00, 01,02…09,0A,
0B,…,0F,10,11,…FF

Algorithm:
1. Move 00 to A register
2. Call the delay subroutine for 1 second, in delay program move FFH to registers r1,r2
and r3, loop and decrement until 0.
3. Increment A register(decremant for down counter)
“MICROCONTROLLERS LAB”

3)b. ASSEMBLY PROGRAM ILLUSTRATING BCD UP/DOWN COUNTERS.


//counter program – BCD up/down counters
Write an ALP to implement (display) an eight bit up/down BCD counters on watchwindow.

ORG 0H SJMP
30H ORG 30H
MOV a,#00
BACK:ACALL DELAY
ADD a,#99H //ADD 01 for BCD up counter
DA A //for bcd counter
JNZ BACK
HERE:SJMP HERE
DELAY:MOV r1,#0FFH
DECR1:MOV r2,#0FFH
DECR:MOV r3, #0FFH
DJNZ r3,$ DJNZ r2,
DECR
DJNZ r1, DECR1RET
END

Algorithm:

1.Move 00 to A register
2.Call the delay subroutine for 1 second (in delay program move FFH to registersr1, r2 and r3, loop
and decrement until 0).
3.Increment A register(add 99h for down counter)
4.Decimal adjust accumulator for the BCD up/down counter.

RESULT: Accumulator A is incremented in BCD from 00, 01, 02…09, 10, 11,…99.
“MICROCONTROLLERS LAB”

4. PROGRAM ILLUSTRATING BIT MANIPULATIONS

4) a. 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

ORG 0000H
SJMP 30H ORG
30H
MOV DPTR,#8000H
MOVX A,@DPTR MOV
R0,A
INC DPTR MOVX
A,@DPTRCLR C
SUBB A,R0JZ
EQUAL JNC
BIG SETB 78H
SJMP END1
BIG:SETB 7FH
SJMP END1
EQUAL:CLR 77H
CLR 7FH
END1:SJMP END1
END

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 areequal),
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 (bitaddress
78H).

RESULT:

1) Before Execution: X:08000h = 45 & X:8001 = 35


After Executuion: D:02FH =01

2) Before Execution: X:08000h = 25 & X:8001 = 35


After Executuion: D:02FH =80

3) Before Execution: X:08000h = 45 & X:8001 = 45


After Executuion: D:02FH =00
“MICROCONTROLLERS LAB”

4) b. LOGICAL INSTRUCTIONS:
4)b. ASSEMBLY PROGRAM ILLUSTRATING LOGICAL INSTRUCTIONS
(BYTELEVEL)
3 eight bit numbers X, NUM1 & NUM2 are stored in internal data RAM locations 20h,21h
& 22H respectively. Write an ALP to compute the following.
IF X=0; THEN NUM1 (AND) NUM2, IF X=1; THEN NUM1 (OR) NUM2,
IF X=2; THEN NUM1 (XOR) NUM2, ELSE RES =00, RES IS 23H LOCATION

ORG 0000H
SJMP 30HORG
30H
MOV A, 20h //do not use #, as data ram 20h is to be accessed
MOV R1,A //X IN R1
MOV A,21H //A -NUM1
CJNE R1,#0,CKOR
ANL A, 22H
SJMP END1
CKOR:CJNE R1,#01,CKXORORL
A, 22H
SJMP END1 CKXOR:CJNE
R1,#02,OTHER
XRL A, 22H
SJMP END1
OTHER: CLR A
END1: MOV 23H,A //STORE RESULT
HERE: SJMP HERE
END

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:

1)Before Execution: D:020H =00, 21=0f, 22 = 12


After Execution D:023H = 02
2) Before Execution: D:020H =01, 21=0f, 22 = 12
After Execution D:023H = 1F
3) Before Execution: D:020H =02, 21=0f, 22 = 12
After Execution D:023H = 1D
4)Before Execution: D:020H =34, 21=0f, 22 = 12
After Execution D:023H = 0
“MICROCONTROLLERS LAB”

20h = 01 => OR of MSBs = 0 (hence 00 in 23h location)

20h = 01 =>complement of MSB of 21h location. Hence 21h is changed to A1 and 23h
location has 80h
Before Execution After Execution

Algorithm:

1. Move the condition X (from 20h location) into R0 register.


2. If X=0; then move LSB bit of 21h to carry flag and ‘AND’ Carry flag with LSBbit of
22h. Goto step5
3. If X=1; then move MSB bit of 21h to carry flag and ‘OR’ Carry flag with MSBbit of
22h. Goto step5
4. If X=0; then complement MSB bit of 21h and move it to carry flag. Goto step5
5. Store Carry flag at MSB bit of 23h location.
“MICROCONTROLLERS LAB”

6. CONDITIONAL CALL AND RETURN

OBJECTIVES:
TO DEMONSTRATE CONDITIONAL BIT JUMP, CONDITIONAL BYTE JUMP,
UNCONDITIONAL JUMP, CALL and RETURN instructions
This program keeps checking P1.0 for low signal. Once a low signal arrives, program checks
for AAh at PORT 0. If the data is AAh a counter will start.

ORG 00H
/*INITIALIZING THE COUNTER AND PORTS*/
MAIN: MOV P0, #0FFH
MOV P1, #0FFH
MOV P2, #00H
MOV R0, #00H
/*WAITING FOR THE ARRIVAL OF LOW SIGNAL AT PORT 1.0*/
WAIT: JB P1.0, WAIT // CONDITIONAL BIT JUMP
ACALL VERIFY_DATA // CONDITIONAL CALL
SJMP WAIT // UNCONDITIONAL JUMP
//CHECKING THE DATA AT P0 FOR 0AAh
VERIFY_DATA:MOV A, P0
CJNE A, #0AAH, RETURN // CONDITIONAL BYTE JUMP
INC R0
MOV P2, R0
ACALL DELAY // UNCONDITIONAL CALL
RETURN:RET // RETURN
/*JUST A DELAY SUBROUTINE */
DELAY:NOP
MOV R2,#25
H0: MOV R3,#255
HI: MOV R4,#255
DJNZ R4,$ // CONITIONAL BYTE JUMPS
DJNZ R3,HI
DJNZ R2,H0
RET // RETURN
END
“MICROCONTROLLERS LAB”
“MICROCONTROLLERS LAB”

6. CONVERSION PROGRAMS
6 a)Write an ALP to implement BCD to ASCII conversion

ORG 0000H
SJMP 30h
ORG 30h
MOV R1,#50H
MOV A,@R1 //get BCD data byte from RAM location 50h
MOV R2,A //Store in R2
ANL A,#0FH //Get the lower nibble
ORL A,#30H //Add/or with 30h i.e., 0-9 converted to 30-39h
INC R1
MOV @R1,A //Store the lower digit's ASCII code
MOV A,R2 //Get back the number
SWAP A //Swap nibbles in A
ANL A,#0FH //Get the upper BCD digit
ORL A,#30H //Convert to ASCII
INC R1
MOV @R1,A //Store the upper digit's ASCII code
here: sjmp here
END

RESULT:

The BCD code 28 at D:0050h is converted to 2 ASCII codes-38h 32h

Algorithm :

//Converts the BCD byte in A into two ASCII characters.


1.Move the BCD data to be converted to accumulator.
2.Get the lower nibble(BCD digit) & ADD (or ORL) with 30h
3.Store the converted ASCII value
4.Get the higher nibble(tens BCD digit) & ADD (or ORL) with 30h
5.Store the converted ASCII value
“MICROCONTROLLERS LAB”

6)b. ASCII – DECIMAL

ORG 0
MOV R5, #"4" // LOADING R5 WITH ASCII EQUIVALENT OF 4 (IE, 34)
MOV R6, #"7" // LOADING R6 WITH ASCII EQUIVALENT OF 7 (IE, 37)
MOV A, R5
ANL A, #0FH // MASK LOWER NIBBLE
MOV R5, A
MOV A, R6
ANL A, #0FH // MASK UPPER NIBBLE
MOV R6, A
MOV A, R5
RL A
RL A
RL A
RL A
ORL A, R6 // PACKING
MOV P1,A
SJMP $
END
“MICROCONTROLLERS LAB”

6 c) DECIMAL – ASCII
(Conversion of Decimal number 0-9 to corresponding ASCII equivalent)

ORG 00H
MAIN: MOV R0,#RAM_ADDR
MOV R1,#ASC_RSULT
MOV R2,#0AH
BACK: MOV A,@R0 //stored decimal values
ORL A,#30H //are fetched ,they are ‘OR’ed with
MOV @R1,A //30 and results are placed at memory
INC R0 //pointed by R1 pointer
INC R1
DJNZ R2,BACK
MOV R1,#ASC_RSULT
MOV R2,#0AH //results are verified on
NEXT: MOV A,@R1 //PORT 1
MOV P1,A
INC R1
DJNZ R2,NEXT
SJMP $
END

6 d)Write an ALP to implement hex to decimal conversion

ORG 0000H
SJMP 30h
ORG 30h
MOV DPTR,#9000H
MOVX A,@DPTR //Get hex number
MOV B,#10
DIV AB //divide by 10 (0AH)
INC DPTR
XCH A,B
MOVX @DPTR,A //Store the remainder (in B) In units place
XCH A,B
MOV B,#10 //Divide the quotient in A by 10
DIV AB
INC DPTR
XCH A,B
MOVX @DPTR,A //Store the remainder (in B) In tens place
XCH A,B
INC DPTR
MOVX @DPTR,A //Store the quotient (in A) in hundreds place
HERE:SJMP HERE
End
“MICROCONTROLLERS LAB”

RESULT:

9000H – FF (HEX NUMBER)


9001 to 9003 – unpacked BCD number (decimal)- 5,5,2 (i.e., 255 stored Lower digit first)

Algorithm

1.Move the hex data to be converted to accumulator.


2.Move 10 to B register and divide with A reg to convert to ascii value
3.Store the converted LSB value in r7
4.Repeat the step 2 to obtain the converted MSB value
5.Store the same in r6

6 e)Write an ALP to implement decimal to hex conversion

ORG 0000H
SJMP 30h
ORG 30h
MOV DPTR,#40H //2-digit decimal number to be converted is given in data
memory 40h
MOVX A, @DPTR
ANL A, #0F0H //obtain upper decimal digit
SWAP A //bring to the units place
MOV B,#0AH //MULTIPLY tens digit with #0A-toget tens in hex
MUL AB
MOV r1,a //temporarily store the converted tens value
MOVX A,@DPTR //get the decimal number again
ANL A,#0FH //obtain the units digit
ADD A,R1 //add to the converted tens value
INC DPTR //increment data address
MOVX @DPTR,A //converted hexadecimal number in next location
HERE:SJMP HERE
END

RESULT:
before execution- X:0040H = 45 (Decimal/BCD)
After Execution: X:0041h = 2D (hex value)

Algorithm
1. Move the decimal data to be converted from external memory 40h to accumulator.
2. AND A reg with 0f0h and obtain the upper MSB of the decimal digit and swap the
LSB and MSB of accumulator to bring the same to units place.
3. Move 0ah to B register and multiply with A reg to convert to hex value, store the
converted tens value in r1
4. Get the LSB of the decimal number and add to the converted tens value
5. point to the next memory location and store the result (hexadecimal).
“MICROCONTROLLERS LAB”

7 .SERIAL DATA TRANSMISSION

Program illustrating serial ascii data transmission (data-yE)


Note-to use result of this program, after selecting DEBUG session in the main menu use
View-> serial window #1. On running & halting the program, the data is seen in the serial
window.
7) a. Conduct an experiment to configure 8051 microcontroller to transmit characters (yE)
to a PC using the serial port and display on the serial window.

ORG 0H
SJMP 30H
ORG 30H
MOV TMOD,#20H //timer 1; mode 2
MOV TH1,#-3 //-3=FD loaded into TH1 for 9600 baud, 11.0592MHz.
MOV SCON,#50H //8-bit, 1 stop bit, REN enabled
SETB TR1 //Start timer 1
AGAIN:MOV A,#’y’ //transfer “y”
ACALL TRANS
MOV a,#’E’ //transfer “E”
ACALL TRANS
AGAIN1:SJMP AGAIN1
TRANS: MOV SBUF,a //load SBUF
HERE:JNB TI,HERE //Wait for last bit to transfer
CLR TI //get ready for next byte
RET
END

RESULT: yE is printed on the serial window each time the program is executed.

Theory: In serial transmission as opposed to parallel transmission, one bit at a time is


transmitted. In serial asynchronous transmission, the data consists of a Start bit (high),
followed by 8 bits of data to be transmitted and finally the stop bit. The byte character to be
transmitted is written into the SBUF register. It transmits the start bit. The 8-bit character is
transferred one bit at a time. The stop bit is transferred. After the transmission, the TI flag
= 1 indicating the completion of transmission. Hence in the subroutine wait until TI is set.
Later clear the TI flag and continue with transmission of the next byte by writing into the
SBUF register. (The program can also be written in interrupt mode). The speed of the serial
transmission is set by the baud rate which is done with the help of timer 1. (Refer Ayala).
Timer1 must be programmed in mode 2 (that is, 8-bit, auto reload).
Baud rate Calculation: Crystal freq/ (12*32) = (11.0592MHz)/(12*32) = 28800.
Serial communication circuitry divides the machine cycle frequency(11.0592MHz)/(12) by
32 before it is being used by the timer to set the baud rate.
To get 9600, 28800/3 is obtained by loading timer1 with -3 (i.e., FF – 3 = FD) for further
clock division. For 2400 baud rate, 28800/12 => -12 = F4 in TH1.
Algorithm:
1.Initialize timer 1 to operate in mode 2 by loading TMOD register.
2.load TH1 with -3 to obtain 9600 baud.
3.Initialize the asynchronous serial communication transmission (SCON) register.
4.Start timer1 to generate the baud rate clock.
5.Transmit the characters “y” & “E” by writing into the SBUF register and waitingfor the
TI flag.
“MICROCONTROLLERS LAB”

7)b. TIMER DELAY PROGRAM


Program illustrating timer delay
Generate a 1second delay continuously using the on chip timer in interrupt mode.

ORG 0H //Reset Vector


SJMP 30H
ORG 0BH //TF0 vector
SJMP ISR
ORG 30H
MOV a,#00
MOV R0,#0
MOV R1,#0
MOV TMOD,#02H //00000010-Run timer0 in mode 2
MOV TH0,#118 //Set up timer 0 to overflow in 0.05msec
MOV IE,#82H //%10000010 – Enable timer0 interrupt
SETB TCON.4 //Start the timer0
HERE:SJMP HERE
ISR: CLR TCON.4 //Disable timer0
INC r1 //r1*r2 = 100*200 = 20000 * 0.05msec = 1sec
CJNE r1,#100,SKIP
MOV r1,#00
INC r0
CJNE r0,#200,SKIP
MOV r0,#00H
INC a
SKIP: SETB TCON.4 //Enable Timer
RETI //Return from interrupt subroutine
END

RESULT:

Accumulator A is incremented in binary from 00, 01,02…09,0A, 0B, …, 0F,10,


11, …FF every 1 second (for 33MHz clock setting & every 3 seconds for 11.0598MHz)

Algorithm:

1. Set up timer0 in mode 2 operation


2. Load TH1 with 118 to generate an interrupt every 0.05msec.
3. Reset registers a, r1 & r0.
4. Repeat step 4 continuously
5. On interrupt; ISR at 000B loaction goes to step 6
6. disable timer0
7. Update r1 & r0
8. Check if 20000 interrupts (=1 sec) over. Yes –increment accumulator a.
9. enable timer & return from ISR.
“MICROCONTROLLERS LAB”

Timerdelay = 12*(257-delay)/frequency
Timerdelay=0.05msec
Delay=256-((timerdelay * frequency)/12) =256-(0.05*10 -3 * 33*106)/12
=256-137.5 =118.5 //loaded in TH0
To get 1sec delay
1/0.05msec = 200*100 in the ISR
(assuming 33 MHZ crystal frequency. For 11 MHz, the calculations change).
“MICROCONTROLLERS LAB”

Steps FOR EXECUTING THE hardware PROGRAM:

STEP 1: Target setup.


Right click on “Target 1” and select “Option for Target, Target 1”.
Choose “Target” and change XTAL frequency as 11.0592.
Choose “Device” and then choose “ATMEL- AT89C51”
Choose “Output” and tick “Create Hex file” and then click “OK”.
Choose “Debug” and then choose “Keil monitor -51 Driver”.
“MICROCONTROLLERS LAB”

STEP 2: Make all the hardware connection required


“MICROCONTROLLERS LAB”

9) ELEVATOR INTERFACE TO 8051

OBJECTIVE
THIS PROGRAM IS CAPABLE OF DEMONSTRATING THE BASIC WORKING OF
ELEVATOR . INPUT IS VIA A MATRIX KEYPAD AND OUTPUT IS DISPLAYED ON A
2X16
LCD.
Hardware:
Microcontroller 89s8252
Crystal Freq 11.0592

I/O Port configuration


Port 0 output; LCD Data
Port 1 NA; Open
Port 2 input; Matrix Keypad
Port 3 output; LCD Control
Keypad:
123
456
789
0 ; 0 ----> up/down

#include <REG52.H>

sfr ldata = 0x80; //Variables declaration for LCD


sbit rs = P3^7;
sbit rw = P3^6;
sbit en = P3^5;
sbit busy = P0^7;
sbit carry = PSW^7;
//--------------Funtion declaration
void convert_display(unsigned char); //Hex to Binary converter
void MSDelay (unsigned int); //Delay
void lcdcmd (unsigned char value);
void lcddata (unsigned char value);
void lcdready ();
void down ();
void up ();
unsigned char getkey ();
unsigned char i,j,countl,countr, key=0, Data1='0',
Data2='0', Funct, Ans ;
//Serially recieved data
//-----------------The message
unsigned char idata msg[9] = {"Floor No:"};
unsigned char idata keypad[4][4]= { '1','2','3','X',
'4','5','6','X',
'7','8','9','X',
'X','0','X','X',};

void main()
{
lcdcmd(0x38);
“MICROCONTROLLERS LAB”

lcdcmd(0x0e);
lcdcmd(0x01);
lcdcmd(0x06);
lcdcmd(0x80);
for (j=0;j<9;j++)
{
lcddata(msg[j]); //Get data from lookup
//table
}
lcdcmd(0x8A);
lcddata('0');
lcddata('0');
while(1)
{
do
{
getkey (); //get data from matrix key pad
MSDelay(10);
Data1 = key;
key = key & 0xF0;
}
while (key!=0x30);
carry =0;
if (Data1<Data2) down();
else up();
}
}
void down()
{
for(i=(Data2-Data1);i>0;i--)
{
for (j=3;j>0;j--)
{
lcdcmd(0x8E);
lcddata('v');
lcddata('v');
MSDelay(30);
lcdcmd(0x8E);
lcddata(' ');
lcddata(' ');
MSDelay(30);
}
Data2--;
lcdcmd(0x8b);
lcddata(Data2);
}
}
void up()
{
for(i=(Data1-Data2);i>0;i--)
{
for (j=3;j>0;j--)
“MICROCONTROLLERS LAB”

{
lcdcmd(0x8E);
lcddata('^');
lcddata('^');
MSDelay(30);
lcdcmd(0x8E);
lcddata(' ');
lcddata(' ');
MSDelay(30);
}
Data2++;
lcdcmd(0x8b);
lcddata(Data2);
}
}
void MSDelay (unsigned int value) // Delay routine
{
unsigned int x,y;
for (x=0;x<900;x++)
for (y=0;y<value;y++);
}
void lcdcmd (unsigned char value)
{
lcdready();
ldata = value;
rs = 0;
rw = 0;
en = 1;
MSDelay(1);
en = 0;
return;
}
void lcddata (unsigned char value)
{
lcdready();
ldata = value;
rs = 1;
rw = 0;
en = 1;
MSDelay(1);
en = 0;
return;
}
void lcdready ()
{
busy = 1;
rs = 0;
rw = 1;
while (busy == 1)
{
en = 0;
MSDelay(1);
“MICROCONTROLLERS LAB”

en = 1;
}
return;
}
unsigned char getkey () // Routine to read matrix
keyboard
{
unsigned char colloc, rowloc;
TMOD = 0x20;
TH1 = -3;
SCON = 0x50;
TR1 = 1;
P2 = 0xff;
do {
P2 = 0x0f; //Wait untill all keys are released
colloc = P2;
colloc &= 0x0f;
} while (colloc != 0x0f);

do {
do
{
MSDelay (1);
colloc = P2;
colloc &= 0x0f;
} while (colloc == 0x0f);
//Check whether any key is pressed
MSDelay (1);
colloc = P2;
colloc &= 0x0f;
//Confirm whether any ket is pressed after delay
} while (colloc == 0x0f);//to aviod spikes
while(1)
{
P2 = 0xfE; //get the row presses
colloc = P2;
colloc &= 0xf0;
if (colloc != 0xf0)
{
rowloc = 0;
break;
}
P2 = 0xfd;
colloc = P2;
colloc &= 0xf0;
if (colloc != 0xf0)
{
rowloc = 1;
break;
}
P2 = 0xfb;
colloc = P2;
“MICROCONTROLLERS LAB”

colloc &= 0xf0;


if (colloc != 0xf0)
{
rowloc = 2;
break;
}
P2 = 0xf7;
colloc = P2;
colloc &= 0xf0;
rowloc = 3;
break;
} //get the coloum presses
if (colloc == 0xe0) key = (keypad[rowloc][0]);
else if (colloc == 0xd0) key = (keypad[rowloc][1]);
else if (colloc == 0xb0) key = (keypad[rowloc][2]);
else key = (keypad[rowloc][3]); return(key);
}
“MICROCONTROLLERS LAB”

10) PROGRAM TO BLINK LED

Write an ALP to blink an LED

#include<reg52.h> // special function register declarations


// for the intended 8051 derivative

sbit LED = P2^0; // Defining LED pin

void Delay(void); // Function prototype declaration

void main (void)


{
while(1) // infinite loop
{
LED = 0; // LED ON
Delay();
LED = 1; // LED OFF
Delay();
}
}

void Delay(void)
{
int j;
int i;
for(i=0;i<10;i++)
{
for(j=0;j<10000;j++)
{
}
}
}
“MICROCONTROLLERS LAB”

OUTPUT :

The output will be intensity variations in on-boards leds. Or if the variation is not
seen easily, one can program another led connected to any GPIO pin to visualize
the impact of potentiometer.
“MICROCONTROLLERS LAB”

11) STEPPER MOTOR CONTROL INTERFACE TO 8051

11.1 STEPPER MOTOR CONTROL

OBJECTIVE
THIS PROGRAM DEMONSTRATES A STEPPER MOTOR CONTROL USING AN H-
BRIDGE CONTROLLER. DIRECTION AND SPEED ARE SELECTED WITH HYPER
TERMINAL.

Hardware:
Microcontroller 89s8252
Crystal Freq 11.0592

I/O Port configuration


Port 0 output DC motor

#include <REG52.H>
void MSDelay (unsigned int);
void Send (unsigned char);
unsigned int rdata=1; //for clockwise 0 and anticlockwise 1
void main()
{
if(rdata==0)
{
while (1) //keep doing the function until next command
is received
{
P0 =0x66; //Clockwise Rotation
MSDelay (100); //to change speed change delay
P0 =0xCC;
MSDelay (100);
P0 =0x99;
MSDelay (100);
P0 =0x33;
MSDelay (100);
}
}
else
{
“MICROCONTROLLERS LAB”

while (1)
{
P0 =0x33; //AntiClockwise Rotation
MSDelay (100);
P0 =0x99;
MSDelay (100);
P0 =0xCC;
MSDelay (100);
P0 =0x66;
MSDelay (100);
}
}
}
void MSDelay (unsigned int value) // Delay rouitne
{
unsigned int x,y;
for (x=0;x<600;x++)
for (y=0;y<value;y++);
}
“MICROCONTROLLERS LAB”

VISVESVARAYA TECHNOLOGICAL UNIVERSITY,BELAGAVI


POST GRADUATION CENTRE ,KALABURAGI
BACHELOR OF TECHNOLOGY (B.Tech)
IN
ELECTRONICS AND COMPUTER ENGINEERING

A
Report
MICROCONTROLLER
LABORATORY ( BUE304 )

Submitted as a part of Academic Requirement for Third


Semester By

USN :

Department of Electronics and computer Engineering,


Post graduation Centre ,Visvesvaraya Technological
University,
KALABURAGI

2023 - 2024
“MICROCONTROLLERS LAB”

VISVESVARAYA TECHNOLOGICAL UNIVERSITY,


BELAGAVI CPGS KALABURAGI

U. S.N: DATE: / / 2024

CERTIFICATE

This is to certify that Mr./Ms.…………………………………………..


Studying in the Department of ECE IIIrd - Semester has successfully
completed all the Programs in……………………………with subject
as prescribed by the Visvesvaraya Technological University, Belagavi
CPGS Kalaburagi For the academic year 2023- 2024.

Faculty Incharge Programme Coordinator

Examiners :

1. Internal Examiner 2. Internal examiner


“MICROCONTROLLERS LAB”
“MICROCONTROLLERS LAB”
“MICROCONTROLLERS LAB”
“MICROCONTROLLERS LAB”
“MICROCONTROLLERS LAB”

You might also like