Microcontrollers Lab: Programming & Interfacing
Microcontrollers Lab: Programming & Interfacing
VTU SYLLABUS
MICROCONTROLLERS LAB
CYCLE 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
CYCLE II.A
Write programs to interface 8051 chip to Interfacing modules to develop single chip
solutions.
1. Interface a simple toggle switch to 8051 and write an ALP to generate an interrupt
which switches on an LED (i) continuously as long as switch is on and (ii) only once
for a small time when the switch is turned on.
2. Write a C program to (i) transmit and (ii) to receive a set of characters serially by
interfacing 8051 to at terminal.
CYCLE II.B
Objective: To write an ALP to transfer the block of data from source memory to destination memory
Algorithm
1. Start.
2. Set the counter value which is equal to number of data to be transferred.
3. Initialize source and destination memory locations.
4. Fetch the first data from source memory location to Accumulator.
5. Transfer the fetched data to destination memory location with the help of data pointer.
6. Decrement the counter value by 1 and increment the data pointer to fetch next data.
7. Repeat steps from 3 to 6 till counter value becomes zero.
8. End.
Program: To transfer 8 bytes of data from external memory location starting from 8100h to external
memory location starting from 8200h
ORG 0000H
MOV R1, #81H ; initialize the source memory location higher byte
MOV R2, #82H ; initialize the destination memory location higher byte
MOV R3, #00H ; initialize the destination & source location lower byte
BACK: MOV DPH, R1 ; get the source memory location address to DPTR
MOV DPL, R3
SJMP $
END
Outcome:
Before execution
After execution
Objective: To write an ALP to exchange the data between two external memory locations
Algorithm
1. Start.
2. Set the counter value which is equal to number of data to be exchanged.
3. Initialize two blocks of memory locations.
4. Fetch the first data from one memory location and save it in the intermediate register.
5. Fetch the first data from other memory location to accumulator
6. Exchange the date between accumulator and register
7. Transfer the data to corresponding memory location with the help of data pointer.
8. Decrement the counter value by 1 and increment the data pointer to fetch next data
9. Repeat steps from 4 to 8 till counter value becomes zero.
10. End
Program: To exchange 8 bytes of data between external memories location starting from 8100h
and external memory location starting from 8200h
ORG 0000H
MOV DPL, R3
SJMP $
END
Before execution
Address Data
Address Data
0x8200 0x32
0x8100 0x12
0x8201 0xFF
0x8101 0x24
0x8202 0xAD
0x8102 0x56
0x8203 0xDA
0x8103 0xFF
0x8204 0x88
0x8104 0xEE
0x8205 0x99
0x8105 0xAB
0x8206 0x56
0x8106 0x10
0x8207 0x55
0x8107 0x03
After execution
Algorithm
1. Start.
2. Set the counter value which is equal to number of data minus one.
3. Initialize memory location to provide the input and to view the output.
4. Fetch the first two data from memory location and compare them.
5. Check whether two numbers are equal, if they are equal then no need to compare continue
checking with the next data. If they are not equal then compare the two numbers.
6. If the first data is greater than second data then exchange the data between accumulator and
register so that largest number lies in accumulator.
7. Increment the data pointer to fetch next data to be compared with the previously stored largest
number in accumulator.
8. Repeat steps from 5 to 7 till counter becomes zero
9. After all comparison the largest number will be present in accumulator, transfer the number to
initialized memory location to view the result.
10. End
Program: To find the largest number in a given array of size 5 starting from 5100h external memory
location. The largest number has to be stored in 8100h external memory location.
ORG 0000H
DJNZ R1, BACK ; if A & B are equal, then decrement count, if count! =0
L2: DJNZ R1, BACK ; Decrement count, if count! =0, jump to label,‟ BACK‟
MOVX @DPTR, A ; move the largest data from accumulator to new memory
Location
SJMP $
END
Outcome:
Before execution
Address Data
Address Data
0x8100 0x00
0x5100 0x12
0x5101 0x24
0x5102 0x56
0x5103 0xFF
0x5104 0xEE
After execution
Algorithm
1. Start.
2. Set the counter value which is equal to number of data minus one.
3. Initialize memory location to provide the input and to view the output.
4. Fetch the first two data from memory location and compare them.
5. Check whether two numbers are equal, if they are equal then no need to compare continue
checking with the next data. If they are not equal then compare the two numbers.
6. If the first data is smaller than second data then exchange the data between accumulator and
register so that smallest number lies in accumulator.
7. Increment the data pointer to fetch next data to be compared with the previously stored
smallest number in accumulator.
8. Repeat steps from 5 to 7 till counter becomes zero
9. After all comparison the largest number will be present in accumulator, transfer the number to
initialized memory location to view the result.
10. End
Program: To find the smallest number in a given array of size 5 starting from 5100h external memory
location. The smallest number has to be stored in 8100h external memory location.
ORG 0000H
DJNZ R1, BACK ; if A & B are equal, then decrement count, if count! =0
NEXT: JC L2 ; If A & B are not equal, then check for CY= 1(A<B)
; and if so jump to label „L2‟
L2: DJNZ R1, BACK ; Decrement count, if count! = 0, jump to label,‟ BACK‟
MOVX @DPTR, A ; move the smallest data from accumulator to new memory
Location
SJMP $
END
Outcome:
Before execution
Address Data
Address Data
0x8100 0x00
0x5100 0x12
0x5101 0x24
0x5102 0x56
0x5103 0xFF
0x5104 0xEE
After execution
Objective: To write an ALP to arrange the data in given array in ascending order
Algorithm
1. Start.
2. Set the counter1 value for outer loop which is equal to number of data minus one.
3. Set the counter2 value for inner loop which is equal to number of data minus one.
4. Initialize memory location to provide the number of data to be arranged.
5. Point data pointer to initial memory location.
6. Fetch the data from memory location and compare it with next number.
7. If the first data is greater than second data then exchange the data between accumulator and
register so that largest number lies in accumulator.
8. Decrement counter 2 by 1 and increment data pointer by 1 to fetch the next data.
9. Repeat steps from 6 to 8 till counter 2 becomes zero.
10. Decrement counter1 by one, load the counter2 to initial value
11. Repeat step from 5 to 10 till counter1 becomes zero.
12. The numbers will be arranged in ascending order in the same memory location.
13. End
Program: The array of data which has to be arranged in the ascending order starts from 5100h
external memory location. The array contains 5 data‟s. Rearrange the data in the
ascending order
ORG 0000H
; label L2‟.
DJNZ R1, L1 ; decrement step count, if count! = 0 then jump to label „L1‟
SJMP $
END
Outcome:
Before execution:
Address Data
0x5100 0x1F
0x5101 0xD4
0x5102 0x56
0x5103 0Xff
0x5104 0x01
After execution:
Address Data
0x5100 0x01 SMALLEST
0x5101 0x1F
0x5102 0x56
0x5103 0xD4
0x5104 0XFF LARGEST
Objective: To write an ALP to arrange the data in given array in descending order
Algorithm
1. Start.
2. Set the counter1 value for outer loop which is equal to number of data minus one.
3. Set the counter2 value for inner loop which is equal to number of data minus one.
4. Initialize memory location to provide the number of data to be arranged.
5. Point data pointer to initial memory location.
6. Fetch the data from memory location and compare it with next number.
7. If the first data is smaller than second data then exchange the data between accumulator and
register so that smallest number lies in accumulator.
8. Decrement counter 2 by 1 and increment data pointer by 1 to fetch the next data.
9. Repeat steps from 6 to 8 till counter 2 become zero.
10. Decrement counter1 by one, load the counter2 to initial value
11. Repeat step from 5 to 10 till counter1 becomes zero.
12. The numbers will be arranged in ascending order in the same memory location.
13. End
Program: The array of data which has to be arranged in the descending order starts from 5100h
external memory location. The array contains 5 data‟s. Rearrange the data in the
ascending order
ORG 0000H
; label‟ L2‟.
DJNZ R1, L1 ; decrement step count, if count! =0 then jump to label „L1‟
SJMP $
END
Outcome:
Before execution:
Address Data
0x5100 0x1F
0x5101 0xD4
0x5102 0x56
0x5103 0xFF
0x5104 0x01
After execution:
Address Data
0x5100 0XFF LARGEST
0x5101 0xD4
0x5102 0x56
0x5103 0X1F
0x5104 0x01 SMALLEST
Algorithm
1. Start.
2. Initialize 2 memory location to provide 2 data to be added.
3. Initialize a memory location to view the output
4. Fetch the lower byte of first data and add it with lower byte of second data.
5. Transfer the result to the output memory location.
6. Fetch the higher byte of first data and add it with higher byte of second data with the carry
generated in the previous addition.
7. Transfer the result to the output memory location.
8. Clear the accumutor, add its content with carry generated.
9. Transfer the final carry generated to the output memory location
10. End
Program: To add two 16 bit numbers, first 16 bit number placed in 8100h and 8101h external
memory locations and second 16 bit number placed in 8200h and 8201h external memory
locations. The result has to be stored in 8300h, 8301h and 8302h external memory
locations.
ORG 0000H
MOVX A,@DPTR ; get the 1st LSB data from memory to accumulator
MOVX A,@DPTR ; get the 2nd LSB data from memory to accumulator
MOVX A,@DPTR ; get the 1st MSB data from memory to accumulator
MOVX A,@DPTR ; get the 2nd MSB data from memory to accumulator
SJMP $
END
Outcome:
Objective: To write an ALP to subtract one 16- bit number from another
Algorithm
1. Start.
2. Initialize 2 memory locations to provide 2 data to be subtracted.
3. Initialize a memory location to view the output.
4. Fetch the lower byte of second data and subtract it from lower byte of first data with borrow.
5. Transfer the result to the output memory location.
6. Fetch the higher byte of second data and subtract it from higher byte of first data with borrow.
7. Transfer the result to the output memory location.
8. Clear the accumulator, subtract its content from borrow.
9. Transfer the final borrow generated to the output memory location.
10. End
Program: To subtract one 16-bit number from another. Minuend is placed in 8100h and 8101h
external memory locations and Subtrahend is placed in 8200h and 8201h external memory
locations. The difference has to be stored in 8300h, 8301h and 8302h external memory
locations. The 8300h memory location should indicate the sign of the result.
ORG 0000H
MOVX A,@DPTR ; get the 1st LSB data from memory to accumulator
MOVX A,@DPTR ; get the 2nd LSB data from memory to accumulator
MOVX A,@DPTR ; get the 1st MSB data from memory to accumulator
MOVX A,@DPTR ; get the 2nd MSB data from memory to accumulator
SJMP $
END
Outcome:
Before execution
After execution
Before execution
Algorithm
1. Start.
2. Initialize 2 memory location to provide 8 bit multiplier and 16 bit multiplicand.
3. Initialize a memory location to view the output.
4. Fetch the lower byte of multiplicand and multiply it with multiplier.
5. Transfer the lower byte of result to the output memory location.
6. Save the higher byte of result in register.
7. Fetch the higher byte of multiplicand and multiply it with multiplier.
8. Add with carry the lower byte of result obtained with previously stored intermediate result.
9. Transfer the result to the output memory location.
10. Add the higher byte of result obtained with carry and transfer to the output memory location.
11. End
Program: To multiply an 8-bit number placed in external memory location 8100h and the 16 bit
number is placed in external memory locations 8200h and 8201h.The product will be
stored in external memory locations 8300h, 8301h and 8302h.
ORG 0000H
MOVX A,@DPTR
MOV DPTR, #8302H ; store the lower byte result in result+2 memory
MOVX @DPTR, A
MOVX A, @DPTR
MOVX @DPTR, A
DEC DPL
SJMP $
END
Outcome:
Before execution
After execution
Algorithm
1. Start.
2. Initialize 2 memory location, one to provide input and one to view the output.
3. Fetch the data from memory location and multiply the number with itself.
4. Transfer the result to the output memory location.
End
Program: To find square of given number, input is placed in external memory location 8100h, and
square is placed in the external memory 8101h and 8102h.
ORG 0000H
INC DPTR ; get the result+1 address to store the square result
INC DPTR
MOVX @DPTR, A ; store the upper byte of the result to memory location
SJMP $
END
Before execution
After execution
Algorithm
1. Start.
2. Initialize memory location to provide input and to view output.
3. Fetch the data and multiply the number with itself to find square of a number.
4. The lower and higher byte of result is again multiplied with the number to find a cube of a
number.
5. Transfer the result obtained to the output memory location
6. End
Program: To find cube of given number, the given number is placed in external memory location
8100h, and the cube is placed in the external memory 8200h, 8201h and 8202h
ORG 0000H
MOV R1, B ; copy the upper byte of the square result in the R1 register
MOVX @DPTR, A ; store the lower byte of cube output in result+2 memory
ADDC A, R2 ; add the input data to the partial result with the previous carry
MOVX @DPTR, A ; store the 2nd byte of cube output in result+1 memory
MOVX @DPTR, A store the 3rd byte of cube output in result memory
SJMP $
END
Outcome:
Before execution
After execution
Algorithm
1. Start.
2. Initialize memory location to provide dividend and divisor.
3. Initialize memory location to view the remainder and quotient.
4. Fetch the inputs, divide the dividend by the divisor.
5. Transfer the quotient and remainder obtained to the output memory location.
6. End
Program: To perform 8 bit / 8bit division. Dividend is placed in external memory location 8200h,
and divisor is placed in the external memory location 8100h, the result will be placed in
the memory locations 8300h (quotient) and 8301h (remainder).
ORG 0000H
SJMP $
END
Before execution
After execution
Address Data
Address Data Address Data 0x8300 0X03 Quotient
0x8100 0x13 0x8200 0x45 0x8301 0X0C Remainder
Algorithm
1. Start.
2. Initialize timer 0 in mode 1 configuration to generate delay.
3. Initialize port1 to view result.
4. Initial accumulator with value 00
5. Load the value from accumulator to port1.
6. Call delay subroutine.
7. Increment the accumulator.
8. Repeat step from 5 to 7 till accumulator value reaches 99h
9. Repeat step from 4 to 8 continuously.
10. End
Program: To display BCD up count (00 to 99) continuously in Port1. The delay between two counts
should be 1 second. Configure TMOD register in Timer0 Mode1 configuration.
ORG 0000H
MOV R0, #0EH ; get the count for repetition of timer register count (14 d)
BACK: MOV TL0, #00H ; set the initial count for “0.071 second x 14 = 1 second”
END
Outcome:
Algorithm
1. Start.
2. Initialize timer 0 in mode 1 configuration.
3. Initialize port1 to view result.
4. Initial accumulator with value 99
5. Load the value from accumulator to port1.
6. Call delay subroutine.
7. Add accumulator content with 99 to decrement the value by1..
8. Repeat step from 5 to 7 till accumulator value reaches 99h
9. Repeat step from 4 to 8 continuously.
10. End
Program: To display BCD down count (99 to 00) continuously in Port1. The delay between two
counts should be 1 second. Configure TMOD register in Timer0 Mode1 configuration.
ORG 0000H
MOV R0, #0EH ; get the count for repetition of timer register count (14 d)
BACK: MOV TL0, #00H ; set the initial count for “0.071 second x 14 = 1 second”
END
Outcome:
Objective: To write an ALP to find whether the given number is odd or even
Algorithm
1. Start.
2. Initialize memory location to provide input.
3. Initialize register to indicate whether the number is odd or even.
4. Fetch the data from memory location.
5. Rotate right the content of data with carry in order to check its LSB.
6. If carry is generated, means if LSB is one then the number is odd.
7. Indicate the number is odd by moving FF to the register.
8. If carry is not generated, means if LSB is zero then the number is even.
9. Indicate the number is even by moving 11 to the register.
10. End
Program: To check whether the given number placed in external memory location 8100h is odd or
even, If the given number is odd store FF h in R1 register else if even store 11h in R1
register.
ORG 0000H
MOV DPTR, #8100H ; get the input data from source memory location
MOVX A,@DPTR
SJMP LAST
LAST: SJMP $
END
Before execution
After execution
Before execution
After execution
Objective: To write an ALP to find whether the given number is Positive or Negative
Algorithm
1. Start.
2. Initialize memory location to provide input.
3. Initialize register to indicate whether the number is odd or even.
4. Fetch the data from memory location.
5. Rotate left the content of data with carry inorder to check its MSB.
6. If carry is generated, means if MSB is one then the number is Negative.
7. Indicate the number is Negative by moving FF to the register.
8. If carry is not generated, means if MSB is zero then the number is Positive.
9. Indicate the number is positive by moving 11 to the register.
10. End
Program: To check whether the given number placed in external memory location 8100h is Positive
or Negative. If the given number is Negative store FF h in R1 register else if Positive store
11h in R1 register.
ORG 0000H
MOV DPTR, #8100H ; get the input data from source memory location
MOVX A,@DPTR
SJMP LAST
LAST: SJMP $
END
Before execution
After execution
Before execution
After execution
Objective: To write an ALP to find number of logical ones and zeroes in the given number
Algorithm
1. Start.
2. Initialize memory location to provide input.
3. Set the counter value which is equal to number of bits in the data.
4. Initialize two registers to store number of one‟s and zero‟s value.
5. Fetch the data from memory location.
6. Rotate right the content of data with carry inorder to check the bit value.
7. If carry is generated, then increment the register which contains number of ones value.
8. If carry is not generated, then increment the register which contains number of zeros value
9. End
Program: To find the number of logical zeroes and ones in the given number placed in the external
memory location 8100h. The number of logical ones is indicated in the R2 register and the
number of logical zeroes is indicated in the register R3.
ORG 0000H
MOV DPTR, #8100H ; get the input data from source memory location
MOVX A,@DPTR
MOV R1, #08H ; keep the count in R1 to check 8 bits of input data
SJMP LAST
LAST: DJNZ R1, NEXTBIT ; if all the 8 bits are not checked, go back to label NEXTBIT
SJMP $
END
Before execution
After execution
Algorithm
1. Start.
2. Initialize timer 0 in mode 1 configuration to generate delay.
3. Initialize port1.
4. Initial accumulator with value 00
5. Load the value from accumulator to port1.
6. Call delay subroutine.
7. Compliment the content of accumulator.
8. Repeat step from 4 to 8 continuosly
9. Use logical analyzer to view the square wave output
10. End
Program: To generate the square wave in P1 with the 50% duty cycle and the time delay of 10 ms
using timer. Assume the crystal frequency of 11.0592 MHz Configure the timer in Timer0
mode1.
ORG 0000H
CPL A
MOV TL0, #000H ; set the initial value in timer register for 5ms
END
Outcome:
Observed the 50% duty cycle square wave on CRO generated on P1 and measured the time delay
of 10ms.
Objective: To write an ALP to convert BCD number to its equivalent ASCII number.
Algorithm
1. Start.
2. Initialize memory location to provide input.
3. Initialize memory location to view output.
4. Fetch the data, obtain its higher and lower nibble.
5. Add 30 separately to higher and lower nibble to obtain its ascii value
6. Transfer the output the initialized output memory location.
7. End
Program: To convert unpacked BCD number (00-99) placed in internal memory location 20h to its
equivalent ASCII number (30-39). The result as to be stored in internal memory location
40h and 41h.
ORG 0000H
SJMP $
END
Before execution
After execution
Algorithm
1. Start.
2. Initialize memory location to provide input.
3. Initialize memory location to view output.
4. Fetch the data, and divide the number by 10 in decimal.
5. Store the remainder in register.
6. Divide the quotient obtained by 10 in decimal.
7. Add the remainder obtained with the previously stored remainder.
8. Transfer the result to the initialized output memory location.
9. Transfer the quotient obtained to the initialized output memory location.
10. End
Program: To convert the hexadecimal number placed in the external memory location 8100h to
decimal number and store the result in the external memory location 8200h and 8201h.
ORG 0000H
MOV DPTR, #8100H ; get the input data (hex number) memory location
MOVX @DPTR, A ; store the tens and units (accumulator) place result
END
Outcome:
Before execution
After execution
Algorithm
1. Start.
2. Initialize memory location to provide input.
3. Initialize memory location to view output.
4. Fetch the data, and save its lower nibble in register.
5. Obtain the upper nibble of data and multiply with 0A.
6. Add the resultr obtained with the lower nibble of data.
7. Transfer the result to the initialized output memory location.
8. End
Program: To convert the decimal number placed in the external memory location 8100h to
hexadecimal number and store the result in the external memory location 8101h
ORG 0000H
MOV DPTR, #8100H ; get the input data (decimal number) memory location
ANL A, #0FH ; mask off the upper nibble of the input data
ADD A, R1 ; add multiplied data with input data‟s lower nibble value
MOVX @DPTR, A ; store the hex decimal value in the result memory location
SJMP $
END
Before execution
After execution
Objective: To write an ALP to generate square wave with the on time delay of 6 ms and off time
delay of 4 ms
Algorithm
1. Start.
2. Initialize timer 0 in mode 1 configuration to generate delay.
3. Initialize port1.
4. Load port 1 with 00h.
5. Call delay subroutine of 1msec twice to obtain 2ms OFF time.
6. Load port 1 with FFh.
7. Call delay subroutine of 1msec four times to obtain 4ms ON time.
8. Repeat step from 4 to 7 continuously
9. Use logical analyzer to view the square wave output
10. End
Program: To generate the square wave with the on time delay of 6ms and off time delay of 4 m sec.
Configure the timer in Timer0 mode1. Assume the crystal frequency of 11.0592 M Hz.
ORG 0000H
LCALL DELAY
LCALL DELAY ; Call 1ms delay subroutine four times to get 4ms
LCALL DELAY
LCALL DELAY
LCALL DELAY
MOV TL0, #0cdH ; set the initial value in timer register for 2ms
END
Outcome:
Observed the waveform with 6 msec ON time and 4 msec OFF time on CRO as generated on parallel
port 1
Hardware Programs
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” .
Hardware
µC board
PC Device
RS232 (Connect to com PCI bus
AT89C51 DAC/LCD/
MOTOR/ADC
Objective: Interface a simple toggle switch to 8051 and write an ALP to generate an interrupt
which switches on an LED
Program
Outcome:
Toggle switch is successfully interfaced with microcontroller by observing the status of LED.
Objective: To write an ALP to send your name serially using UART at the baud rate of 9600
Algorithm:
Step 3 : Initialize serial communication register to send the given data through serial buffer
Program: To send the letter „J‟ serially using the UART at the baud rate of 9600. Configure SCON
register in mode 1. Assume the crystal frequency of 11.0592MHz.
ORG 0000H
MOV SBUF, #'J' ; send the letter „J‟ through SBUF register
HERE: JNB TI, HERE ; wait until „J‟ character is sent (8bits are transferred)
SJMP $
END
OUTPUT:
Outcome:
Transmitted the letter „J‟ serially using the UART at the baud rate of 9600
Objective: To interface DAC to 8051 Microcontroller and to display different waveforms Square,
Triangular and Staircase waveforms on CRO
Algorithm
Program:
ORG 0000H
MOV P0,#00H
JMP REPEAT
CALL DELAY2SEC
MOV P1,#00H
CALL DELAY2SEC
RET
INC R7
CJNE R7,#0FFH,TRIWAVE1
MOV R7,#0FFH
DJNZ R7,TRIWAVE2
RET
CALL DELAY2SEC
MOV P1,#20H
CALL DELAY2SEC
MOV P1,#40H
CALL DELAY2SEC
MOV P1,#80H
RET
DJNZ R2,$
DJNZ R1,DEL1
DJNZ R0,DEL2
RET
DJNZ R2,$
DJNZ R1,DEL21
DJNZ R0,DEL22
RET
END
Objective: To interface stepper motor to 8051 Microcontroller and to make rotations in clockwise
and anticlockwise directions
Algorithm
A1 EQU P1.0
A2 EQU P1.1
A3 EQU P1.2
A4 EQU P1.3
ORG 00H
MOV TMOD,#00000001B
MAIN: CLR A1
ACALL DELAY
SETB A1
CLR A2
ACALL DELAY
SETB A2
CLR A3
ACALL DELAY
SETB A3
CLR A4
ACALL DELAY
SETB A4
SJMP MAIN
DELAY:MOV R6,#1D
BACK: MOV TH0,#00000000B
MOV TL0,#00000000B
SETB TR0
HERE2: JNB TF0, HERE2
CLR TR0
CLR TF0
DJNZ R6, BACK
RET
END
A1 EQU P1.0
A2 EQU P1.1
A3 EQU P1.2
A4 EQU P1.3
ORG 00H
MOV TMOD,#00000001B
MAIN: CLR A1
ACALL DELAY
SETB A4
CLR A2
ACALL DELAY
SETB A3
CLR A3
ACALL DELAY
SETB A3
CLR A4
ACALL DELAY
SETB A1
SJMP MAIN
DELAY:MOV R6,#1D
BACK: MOV TH0,#00000000B
MOV TL0,#00000000B
SETB TR0
HERE2: JNB TF0,HERE2
CLR TR0
CLR TF0
DJNZ R6,BACK
RET
END
Outcome:
Interfaced Stepper motor and rotated Stepper Motor in both clockwise and anti-clockwise directions.
Dept. of ECE, ATME College of Engineering Page 60
Microcontrollers Lab 18ECL47
Algorithm
LCD initialization
The steps that has to be done for initializing the LCD display is given below and these steps
are common for almost all applications.
The steps for sending data to the LCD module are given below. I have already said that the
LCD module has pins namely RS, R/W and E. It is the logic state of these pins that make the
module to determine whether a given data input is a command or data to be displayed.
Program
A1 EQU P1.0
A2 EQU P1.1
A3 EQU P1.2
A4 EQU P1.3
ORG 00H
MOV A,#38H // Use 2 lines and 5x7 matrix
ACALL CMND
ACALL CMND
ACALL CMND
ACALL CMND
ACALL CMND
ACALL CMND
MOV A,#49D
ACALL DISP
MOV A,#54D
ACALL DISP
MOV A,#88D
MOV A,#50D
ACALL DISP
MOV A,#32D
ACALL DISP
MOV A,#76D
ACALL DISP
MOV A,#67D
ACALL DISP
MOV A,#68D
ACALL DISP
ACALL CMND
MOV A,#67D
ACALL DISP
MOV A,#73D
ACALL DISP
MOV A,#82D
ACALL DISP
MOV A,#67D
ACALL DISP
MOV A,#85D
ACALL DISP
MOV A,#73D
ACALL DISP
MOV A,#84D
ACALL DISP
MOV A,#83D
ACALL DISP
ACALL DISP
MOV A,#79D
ACALL DISP
MOV A,#68D
ACALL DISP
MOV A,#65D
ACALL DISP
MOV A,#89D
ACALL DISP
CLR P3.5
CLR P3.4
SETB P3.3
CLR P3.3
ACALL DELY
RET
DISP:MOV P1,A
SETB P3.5
CLR P3.4
SETB P3.3
CLR P3.3
ACALL DELY
RET
CLR P3.5
MOV P1,#0FFh
SETB P3.3
MOV A,P1
JB ACC.7,DELY
CLR P3.3
CLR P3.4
RET
END
Outcome:
Interfaced 16 X 2 LCD to 8051 µC and observed the given message on the display.
Algorithm
Step 1: Initiate the circuit with ADC to convert a given analog input ,
Step 2: The circuit accepts the corresponding digital data and displays it on the LED array connected
at P0.
Program
MOV P1, #11111111B // initiates P1 as the input port
CLR P3.6 // high to low pulse to RD for reading the data from ADC
END
Outcome: