Sie sind auf Seite 1von 56

“MICROCONTROLLERS LAB”

VTU SYLLABUS

SUBJECT: MICROCONTROLLERS LAB EXAM HOURS: 3


(Common to EE, EC, IT, TC, BM and ML) EXAM MARKS: 50

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

II. INTERFACING

Write C programs to interface 8051 chip to Interfacing modules to develop single


chip solutions

8. Simple Calculator using 6 digit seven-segment display and Hex Keyboard interface to
8051
9. Alphanumeric LCD panel and Hex keypad input interface to 8051
10. External ADC and Temperature control interface to 8051
11. Generate different waveforms Sine, Square, Triangular, Ramp etc. using DAC
interface to 8051; change the frequency and amplitude
12. Stepper and DC motor control interface to 8051
13. Elevator interface to 8051

8051 MANUAL NCET 1


“MICROCONTROLLERS LAB”
INTRODUCTION

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
ON-CHIP MEMORY: CODE MEMORY: 64K Bytes of flash.
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 & Compare, SPI (Serial Peripheral Interface)
on Port1,Full duplex enhanced UART.

INTERRUPTS
Nine sources of interrupt (both external and internal).
Two External interrupts INT0 and INT1 are provided with push button switches; these can
also be used as general-purpose switches.

I/O (Port) Lines Four 10-pin connectors for all the 32 I/O lines.
P0, P1 and P2 Port lines are available on a 26-pin connector,

16X2 LCD & SERIAL I/O ports are available.

8051 MANUAL NCET 2


“MICROCONTROLLERS LAB”

Creating and compiling a μVision2 project

1. Double Click on the Vision3 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

8051 MANUAL NCET 3


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

8051 MANUAL NCET 4


“MICROCONTROLLERS LAB”

I.PROGRAMMING

1. DATA TRANSFER INSTRUCTIONS


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

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.

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.

8051 MANUAL NCET 5


“MICROCONTROLLERS LAB”

2) 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
Either using XCH command.
ORG 0000H
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
XCH a, r2
MOVX @r1,a
XCH a, r2
MOVX @r0,a
INC R0
INC R1
DJNZ R3, BACK
HERE: SJMP HERE
END

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.

8051 MANUAL NCET 6


“MICROCONTROLLERS LAB”

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

After Execution: The


data at X:8027h &
X:8041h are exchanged.

3) 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 9000h.(use bubble sort algorithm)

ORG 0000H
SJMP 30H
ORG 30H

8051 MANUAL NCET 7


“MICROCONTROLLERS LAB”
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 the
start address of the array (here 9000h).
4. Store the current and the next array elements pointed by DPTR in registers B and
r2 respectively.
5. Subtract the next element from the current element.
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
becomes 0.
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 at 9000h.

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

8051 MANUAL NCET 8


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

2. ARITHMETIC INSTRUCTIONS
ASSEMBLY LANGUAGE PROGRAM ILLUSTRATING ADDITION,
SUBTRACTION, MULTIPLICATION AND DIVISION.
5) 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

8051 MANUAL NCET 9


“MICROCONTROLLERS LAB”
MOVX A,@R0
MOV R1, A //R1 HAS CONDITION X
INC R0
MOVX A,@R0
MOV B, A //B HAS 1ST NUMBER-v
INC R0
MOVX A,@R0 //A HAS 2ND NUMBER-w
CJNE R1,#00,CKSUB
ADD A,B //PERFORM ADDITION
MOV B,#00 //B HAS CARRY
JNC 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 BORROW
JNC SKIP1
MOV B,#0FFH //FF INDICATES NEGATIVE NUMBER
SKIP1: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,A
INC R0
MOV A,B
MOVX @R0,A
HERE:SJMP HERE
END

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.

RESULT:

Before Execution: ADD SUB

8051 MANUAL NCET 10


“MICROCONTROLLERS LAB”

After Execution: ADD After Execution: SUB

Before Execution: MUL After Execution: MUL

8051 MANUAL NCET 11


“MICROCONTROLLERS LAB”
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
6) 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^2
INC DPTR
MOVX @DPTR,A //STORE PARTIAL RESULT
MOV A,B
MOV R2,A //UPPER PART OF X*LOWER X^2 IN R2
POP 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
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.
3. Check if bit 01 is set. If set go to end (storing the result), else do the cube
operations.
4. The high part of the square result (SQH) is stored on the stack.
5. Multiply the low part of the square result (SQL) with x (partial cube result).
6. Store the low part of the above result at 9001h & the high part in R2.
7. Retrieve the high part of the square result (SQH) stored on the stack & multiply
with x.
8. Add the low part of the above result (SQH*X) with R2 and store in 9002h.
9. Add the high part (SQH*X) with the resulting carry and store in 9003.

8051 MANUAL NCET 12


“MICROCONTROLLERS LAB”

RESULT:
CUBE OF 56H IS 9B498 WHICH IS STORED AS 98, B4, 09 (LOWER BYTE FIRST)

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

3. PROGRAM ILLUSTRATING BIT MANIPULATIONS

8051 MANUAL NCET 13


“MICROCONTROLLERS LAB”
7) Two eight bit numbers NUM1 & NUM2 are stored in external memory locations 8000h
& 8001h respectively. Write an ALP to compare the 2 nos.
Reflect your result as: if NUM1<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,@DPTR
CLR C
SUBB A,R0
JZ EQUAL
JC BIG
SETB 78H
SJMP END1
BIG:SETB 7FH
SJMP END1
EQUAL:CLR 78H
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 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).

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

4. LOGICAL INSTRUCTIONS

8051 MANUAL NCET 14


“MICROCONTROLLERS LAB”
8) ASSEMBLY PROGRAM ILLUSTRATING LOGICAL INSTRUCTIONS (BYTE
LEVEL)
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 30H
ORG 30H
MOV A, 20h //donot 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,CKXOR
ORL 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 = 00

The above program can also be written as shown below (using indirect addressing)
ORG 0000H
SJMP 30H
ORG 30H
mov r0,#20h
MOV A,@R0 //ON CHIP DATA RAM-DONOT USE MOVX
MOV R1,A //X IN R1

INC R0

8051 MANUAL NCET 15


“MICROCONTROLLERS LAB”
MOV A,@R0 //A -NUM1
INC R0 // R0 POINTS TO NUM2
CJNE R1,#0,CKOR
ANL A, @R0
SJMP END1
CKOR:CJNE R1,#01,CKXOR
ORL A, @R0
SJMP END1
CKXOR:CJNE R1,#02,OTHER
XRL A, @R0
SJMP END1
OTHER: CLR A
END1:INC R0
MOV @R0,A //STORE RESULT
HERE:SJMP HERE
END
Boolean variable instructions are also called as bit level logical instructions.

8051 MANUAL NCET 16


“MICROCONTROLLERS LAB”
9) 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 LSB OF NUM1 (AND) LSB OF NUM2,
IF X=1; THEN MSB OF NUM1 (OR) MSB OF NUM2,
IF X=2; THEN COMPLEMENT MSB OF NUM1
STORE THE BIT RESULT IN RES, WHERE RES IS MSB OF 23 H LOCATIONS

ORG 00H
SJMP 30h
ORG 30h
MOV R0,20H //R0-X
CJNE R0,#0,CK1
MOV C,08H //LSB OF NUM1 (21H) - BIT ADDRESS -08
ANL C,10H //LSB OF NUM2 (22H) - BIT ADDRESS -10
SJMP LAST
CK1:CJNE R0,#1,CK2
MOV C,0FH //MSB OF NUM1 (21H) - BIT ADDRESS -0F
ANL C,17H //MSB OF NUM2 (22H) - BIT ADDRESS -17
SJMP LAST
CK2:CJNE R0,#2,CK3
CPL 0FH or MOV C, OFH
MOV C, 0FH CPL C //MSB OF NUM1 (21H) - BIT ADDRESS -0F
SJMP LAST
CK3:CLR C
LAST:MOV 1FH,C //RES IS MSB OF 23H LOCATION -1FH
HERE:SJMP HERE
END

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 LSB bit
of 22h. Goto step5
3. If X=1; then move MSB bit of 21h to carry flag and ‘OR’ Carry flag with MSB bit
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.

RESULT: 20h = 00 => AND OF LSBs=1 (hence 80 in 23h location)

8051 MANUAL NCET 17


“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

5. COUNTERS
ASSEMBLY PROGRAM ILLUSTRATING HEX UP/DOWN COUNTERS.
//counter program - hex/binary counters
10) 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) and

8051 MANUAL NCET 18


“MICROCONTROLLERS LAB”
press 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 counter
JNZ BACK
HERE:SJMP HERE

DELAY: MOV r1,#0FFH


DECR1:MOV r2,#0FFH
DECR: MOV r3,#OFFH
DJNZ r3,$
DJNZ r2,DECR
DJNZ r1,DECR1
RET
END

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(decrement for down counter)

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


…,0F,10,11,…FF

ASSEMBLY
PROGRAM
ILLUSTRATING
BCD UP/DOWN
COUNTERS.
//counter program –
BCD up/down
counters
11) Write an ALP to implement (display) an eight bit up/down BCD counters on watch
window.
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

8051 MANUAL NCET 19


“MICROCONTROLLERS LAB”
JNZ BACK
HERE:SJMP HERE
DELAY:MOV r1,#0FFH
DECR1:MOV r2,#0FFH
DECR:MOV r3, #0FFH
DJNZ r3,$
DJNZ r2, DECR
DJNZ r1, DECR1
RET
END

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

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

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

8051 MANUAL NCET 20


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

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 waiting
for the TI flag.

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

7) TIMER DELAY PROGRAM


Program illustrating timer delay
13) 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

8051 MANUAL NCET 21


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

8051 MANUAL NCET 22


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

8. CONVERSION PROGRAMS

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

8051 MANUAL NCET 23


“MICROCONTROLLERS LAB”

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

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

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

8051 MANUAL NCET 24


“MICROCONTROLLERS LAB”

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

RESULT: 9000H – FF (HEX NUMBER)


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

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

Algorithm :

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


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

8051 MANUAL NCET 25


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

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

17) Write an ALP to implement hexadecimal to ASCII conversion


//This program also illustrates conditional branching (JNC), call and return instructions.
ORG 0000H
SJMP 30h
ORG 30h
MOV R1,#50H
MOV A,@R1 //get hexadecimal data byte from RAM location 50h
MOV R2,A //Store in R2
ANL A,#0FH //Get the lower nibble
ACALL ASCII //Convert to ASCII
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
ACALL ASCII
INC R1
MOV @R1,A //Store the upper digit's ASCII code
here: sjmp here

ASCII:MOV R4,A //Store a


CLR C
SUBB A,#0AH //Check if digit >=0A
MOV A,R4
JC SKIP

8051 MANUAL NCET 26


“MICROCONTROLLERS LAB”
ADD A,#07H //Add 07 if >09
SKIP:ADD A,#30H //Else add only 30h for 0-9
RET
END

Algorithm :
//Converts the hexadecimal byte in A into two ASCII characters.
1. Move the hexadecimal data to be converted to accumulator.
2. Get the lower nibble & call ASCII routine
3. Store the converted ASCII value
4. Get the higher nibble & call ASCII routine
5. Store the converted ASCII value
ASCII subroutine
1. If digit greater than 09,(for A-F) add 07h & 30h
2. Else (i.e., for 0-9) add only 30h
3. return

RESULT: The BCD code 2C at D:0050h is converted to 2 ASCII codes-43h(for 0B) &
32h (for 02) Another Example-BA

18) Write an ALP to implement ASCII to hexadecimal conversion


ORG 0000H
SJMP 30h
ORG 30h
MOV R1,#50H
MOV A,@R1 //get ASCII byte from RAM location 50h
CLR C
SUBB A,#41H
MOV A,@R1
JC SKIP
CLR C
SUBB A,#07H
SKIP:CLR C
SUBB A,#30H
INC R1
MOV @R1,A //Store the hex code
here: sjmp here
END

Algorithm :

//Converts the ASCII characters into hexadecimal number.


1. Move the ASCII character to be converted to accumulator.
2. If character is greater than 41h,(for A-F), then subtract 07h & 30h

8051 MANUAL NCET 27


“MICROCONTROLLERS LAB”
3. Else (i.e., for 0-9) subtract only 30h
4. Store the converted hexadecimal number.

RESULT: The ASCII code 45 at D:0050h is converted to hexadecimal -0E at 51h

Note: For this program the input data should be only in the range 30h-39h & 41h to 46h.

II.INTERFACING

Part A: Interfacing Procedure OR Initial Steps

1) Make Kit Connectors keeping power supply off


2) Create a new folder on desktop
3) Copy the LCD Library files into you folder
4) Goto the Keil software
5) Click Project on menu then click new project
6) Select Desktop in that select you folder (Created on Desktop)
7) Create one file in your folder then save
8) Click Atmel in this select “AT89C51LD2” & click ok then click no for adding
startup code
9) Goto file on menu click new file
10) Type the program then save
11) Give the filename as ‘file name C ‘ [Under your folder only]
12) Goto options ‘for target’
a) Click target, tick ‘use on chip ROM ‘

8051 MANUAL NCET 28


“MICROCONTROLLERS LAB”
b) Click output tick ‘Create Hex File’
c) Click Debug tick Use Keil Monitor
d) Tick Load application
e) Tick run to main
f) Click ‘Settings & Click Serial Interrupt’
13) Minimize the Keil Software
14) Open the folder what you are created on desktop
15) Check whether all files are there or not
16) Right click on each file then goto properties clear the all ticks () in the
properties
17) Close the folder open Keil Software
18) Click + Target on the left side of screen it gives source group
19) Right click on the source group1 Select Add files to group Source group1
20) Then select your folder .
a. Click ‘all the files’ click the file containing program & click add.
B.Click the LCD files & click add
c. Click the LCD library files & click add then click close

21) Click project on new Menu & select Rebuild all the files
22) Switch on the power supply for kit, initially LCD shows ESA MCB 51 V1.00
23) Click DEBUG & Select Start/Stop
24) Click Ok & wait until loading completes
25) Click DEBUG & Select RUN.

Hardware Interfacing

1. Calculator using Keyboard and Seven segment display


2. 4X4 hexadecimal Keypad interface
3. Temperature sensor.
4. Waveform Generation using Dual DAC
5. Stepper Motor & DC motor interface
6. Elevator control.

Features of Embedded C
• C is a simple programming language and so very easy to code.
• Embedded C has most features of C-language with more stress on certain bit
manipulative instructions.
• This feature makes it easy to write program for μC and μP.

8051 MANUAL NCET 29


“MICROCONTROLLERS LAB”
• Keil is a versatile software with a cross compiler that will convert the C program to
assembly language and thus the program can be executed on the desired target (say
8051).

Some of the bit manipulative instructions used are


Symbol Operation
& Bitwise AND
| Bitwise OR
~ Bitwise NOT
>> Shift Right
<< Shift Left
^ P0.0

8051 MANUAL NCET 30


“MICROCONTROLLERS LAB”

1. Calculator using Keyboard and 7-segment display

Algorithm

• Read the numbers n1 and n2 from keyboard and display them on seven segment.
• Read the operand from the keypad if key pressed is B (+), C (-), D (*),E(/) then
respective operation is performed.
• Result is displayed on 2 digit seven segment display.
• If any time the key pressed value returned as 10h then clear the LCD.

PS
PS

8051µC Keypad
P0
FRC 26pin
Cable
7 Seg
Display

Program for calculator

#include <REG51xD2.H>
void DispChar(unsigned char ch);
void ClrLED();
unsigned char getkey();
unsigned char getnum();
unsigned char getOp();
sbit Clk = P3^4; /* Clock line for 7 segment display */
sbit Dat = P0^0; /* Data line for 7 segment display */
main()
{
unsigned char tmp=0x0ff,n1=0,n2,Op,Res;
unsigned char NumTab[10]={ 0x0c0,0x0f9,0x0a4,0xb0,0x99,0x92,0x82,0x0f8,0x80,0x90 };
unsigned char OpTab[4] = { 0x88,0x0Bf,0xc8,0x0a1};
bit Neg=0;
ClrLED(); /* Clear 7 segment display */
while(1)
{
Neg = 0; /* Negative flag */
n1=getnum(); /* Get 1st number */
Op = getOp() - 0x0B; /* Get Opcode. 0x0b is keycode of '+'(see keyboard
schematics)*/
n2=getnum(); /* Get 2nd number */
while(getkey()!=0x13); /* wait for '=' key */
ClrLED();

8051 MANUAL NCET 31


“MICROCONTROLLERS LAB”
switch(Op) /* Perform corresponding operation */
{
case 0: Res = n1 + n2;
break;
case 1:
if(n2>n1) /* check for negativity */
{

Neg = 1;
Res = n2 - n1;
break; }
Res = n1 - n2;
break;
case 2: Res = n1 * n2;
break;
case 3: Res = n1 / n2;
break; }
DispChar(NumTab[Res%10]); /* Display number */
DispChar(NumTab[Res/10]);
if(Neg) /* if negative result display '-' */
DispChar(0x0Bf);
}}
void DispChar(unsigned char ch) /* Routine to display char on 7 segment */
{
unsigned char i,tmp;
P0=0x00;
for(i=0;i<8;i++) /* for all bits */
{
tmp = ch & 0x80;
if(tmp) /* write data depending on MSB */
Dat = 1;
else
Dat = 0;
Clk = 0; /* Give Clk Pulse for synchronization */
Clk = 1;
ch = ch << 1; /* Get next bit */
}
}
void ClrLED()
{
unsigned char i;
for(i=0;i<4;i++)
DispChar(0x0ff); /* 0xff for clear segment ( see 7 segment manual for more info) */
}
unsigned char getkey()
{ unsigned char i,j,indx,t;

P2 = 0x00; /* P2 as Output port */


P0 = 0x0ff;
indx = 0x00; /* Index for storing the first value of scanline */
while(1)

8051 MANUAL NCET 32


“MICROCONTROLLERS LAB”
{
for(i=1;i<=4;i<<=1) /* for 4 scanlines */
{
P2 = 0x0f & ~i; /* write data to scanline */
t = P0; /* Read readlines connected to P0*/
t = ~t;
if(t>0) /* If key press is true */
{
for(j=0;j<=7;j++) /* Check for 8 lines */
{ t >>=1;
if(t==0) /* if get pressed key*/
{
return(indx+j); /* Return index of the key pressed */
}
}
}
indx += 8; /* If no key pressed increment index */
} }}
unsigned char getnum() /* Method for getting number */
{
unsigned char tmp;
while(1)
{
tmp = getkey();
if(tmp < 0x0a || tmp==0x10) /* if pressed key is number, return */
return(tmp);
}}
unsigned char getOp() /* Method for getting Operator */
{
unsigned char tmp;
while(1)
{
tmp = getkey();
if((tmp > 0x0a && tmp <0x0f)|| tmp==0x10) /* if pressed key is a Operator, return */
return(tmp);
}}

8051 MANUAL NCET 33


“MICROCONTROLLERS LAB”
2. 4X4 HEX Keypad

Algorithm for Keyboard Interface

• Configure P1 as output port to scan the rows and P0 as input port to read the column
values.
• First select the last row by grounding that row. Scan the columns of entire row if a
key is pressed in that row then one of the column reads ‘0’.
• If now key is pressed in the selected row all 1’s is returned. So scan next row. Repeat
the action until all rows are scanned.

PS
PS

8051µC

P0 4X4
Keyboard 4X4 Hex
FRC 26pin
Interface keypad
Cable
Card

Program for 4X4 hex keypad.

#include < REG51xD2.H>


#include <intrins.h>
#include "lcd.h"
unsigned char rows,columns,result,abhi;
unsigned char temp = 0;
void delay()
{
unsigned int i;
for(i = 0; i <= 20000; i ++);
}
void Display()
{
if(result > 0x09)
{
result += 0x37;
WriteChar(result);
}
else
{
result += 0x30;
WriteChar(result);
}
}
void KeyScan()

8051 MANUAL NCET 34


“MICROCONTROLLERS LAB”
{
again: columns = 0x77;
rows = 0x04;
result = 0x0c;
next: P1 = columns;
columns >>=1;
if(CY)

columns = columns |0x08 ;


temp = P0;
temp = (temp & 0x0f);
if(temp != 0x0f)
{
rot: temp >>= 1;
if(!CY)
{
ClrLcd();
return;
}
else
{
result += 1;
goto rot;
}
}
else
{
result -= 0x04;
rows --;
if(rows == 0)
goto again;
else
{
goto next;
}}}
void main()
{
P0 = 0xff;
P1 = 0x00;
InitLcd();
WriteString ("KEY PRESSED=");
while(1)
{
KeyScan();
WriteString ("KEY PRESSED=");
Display();
}
}

8051 MANUAL NCET 35


“MICROCONTROLLERS LAB”
3. Temperature Sensor

PS
PS

8051µC
Temp Heat
P0,P2,P3 Sensor Source
FRC 26pin Interface
Cable

The interface card has a DAC to convert the actual temperature to digital this is compared
with reference temperature. Relay also a part of interface card will turn on and off to
indicate if the actual temperature is above or below reference.

Algorithm for Temperature sensor

1. Configure P0 and P1 as o/p, P3 as input port.


2. Set up a counter with initial value 0xff send it to DAC thro P0 after a delay check if
comparator o/p has gone low.
3. If low compare with set value if actual greaterthan set turn on the relay else turn off.

Program for temperature sensor

#include <REG51xD2.H>
sbit Cmp_Out = P3^4; /*Input Bit for Comparator output*/
sbit Rel_Con = P0^0; /*Relay controller Bit i.e Heater Power supply control*/
/*1- Supply OFF, 0-Supply ON*/
#define Dac_Data P1 /*DAC input Data PORT i.e. P1*/
void delay()
{ int l;
for (l=0;l<=0x8;l++);
}
main()
{ unsigned char DacIp;
void delay(void);
Dac_Data =0x00; /*Move 00h to Dac input*/
P0=0x00; /*make P0 as output*/
while(1)
{ DacIp= 0xff; /*DAC input Data counter*/
do
{DacIp++; /*Increment the DAC input Data*/
Dac_Data = DacIp; /*Move the DAC data to DAC*/
delay(); }while(Cmp_Out); /* Check comparator output for low */
if(DacIp > 0x20) /*Compare with the set value i.e.0x20*/
Rel_Con = 1;
else
Rel_Con = 0; }} /* Relay ON, Supply OFF */

8051 MANUAL NCET 36


“MICROCONTROLLERS LAB”

4. Dual DAC Interface to generate

A. Square waveform
B.Triangular Waveform
C.Ramp waveform
D.Sine waveform

8
0 Dual CRO
5 P0 Xout
DAC
1 P1

μC

4 a) Algorithm for Square wave generation

 Let initial, amplitude of the square wave be 2.5v(7F) and frequency count 100.
 Output the values 00h (0ff) and 7fh(on) Values through P0.
 If amplitude key is pressed then increase the voltage in steps of 0.15v(8).
 If the frequency key is pressed increment the count in steps of 50. If the count
exceeds 1000 reset it back to 100.
 Every time amplitude and frequency changes output the value thro P0 and note the
waveform on CRO.

Program for square wave

#include <REG51xD2.H>
sbit Amp = P3^3; /* Port line to change amplitude */
sbit Fre = P3^2; /* Port line to change frequency */
void delay(unsigned int x) /* delay routine */
{
for(;x>0;x--);
}
main()
{
unsigned char on = 0x7f,off=0x00;
unsigned int fre = 100;

while(1)
{
if(!Amp) /* if user choice is to change amplitude */
{
while(!Amp); /* wait for key release */
on+=0x08; /* Increase the amplitude */
}
if(!Fre) /* if user choice is to change frequency */

8051 MANUAL NCET 37


“MICROCONTROLLERS LAB”
{
if(fre > 1000) /* if frequency exceeds 1000 reset to default */
fre = 100;

while(!Fre); /* wait for key release */


fre += 50; } /* Increase the frequency */
P0=on; /* write amplitude to port */
P1=on;
delay(fre);
P0 = off; /* clear port */
P1 = off;
delay(fre);
}}

8051 MANUAL NCET 38


“MICROCONTROLLERS LAB”
4 b) Algorithm for Triangular wave generation

 Output the initial value 00 through P0.


 Increment it in steps of 1 until a count value of FFh (5V) is reached. Every time
repeat step 1.
 Decrement it in steps of 1 until a zero value is reached and repeat step 1.

Program for triangular wave:

#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 */
P1 = i;
P0 = i;
}
for(i=0xfe;i>0x00;i--) /* Generate OFF pulse */
{P0 = i;
P1 = i;}
}
}

8051 MANUAL NCET 39


“MICROCONTROLLERS LAB”
4c) Algorithm for Ramp wave generation

 Output the initial value 00 through P0.


 Increment it in steps of 1 until a count value of FFh(5V) is reached. Every time
repeat step 1.
 Repeat step 1 & 2 continuously.

Program for Ramp waveform

#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 */
{
P1 = i;
P0 = i; }
}
}

8051 MANUAL NCET 40


“MICROCONTROLLERS LAB”
4 d) Algorithm for Sine wave

 Compute different step values (θ = 20o,15o…) of sine using the equation


V= 2.5V +2.5Vsinθ. . Output the values thro P0.
 More the steps smoother will be sine wave.
 E.g.: θ = 0o
V= 2.5V +2.5Vsinθ = 2.5V
The value sent to DAC is 25.6X5V= 128.

Program for sine wave

#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];
}}}

8051 MANUAL NCET 41


“MICROCONTROLLERS LAB”
5. Stepper Motor & DC Motor Interface:-

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 3600 revolution 200 steps are
required.
Step angle= 360o /200 = 1.8o. (difference between 2 teeth).

Algorithm for Stepper Motor


• Configure P0 as output.
• Apply the initial excitation of 11 to motor coils through P0.
• For clockwise motion -Rotate right once the excitation and repeat step 2.
• For anticlockwise motion -Rotate left once the excitation and repeat step 2.

PS
PS

8051µC

P0 Stepper
Motor Stepper
FRC 26pin
Interface Motor
Cable
Card

Program for stepper motor interface

#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); }}

8051 MANUAL NCET 42


“MICROCONTROLLERS LAB”

DC Motor

Algorithm for DC motor interface

• Configure P0, P1 as output port and P3 as input port.


• Let initially the motor rotate with half speed count 7fh.
• If “INR” button is pressed reduce the count because the speed is inversely
proportional to count.
• If “DEC” button is pressed increase the count.

PS
PS
8051µC

P0 DC Motor
FRC 26pin Interface DC
Cable Card Motor
P3.2(inr)
P3.3(dec)

Program for DC motor

#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)
{
while(!dcr);
if(i<0xf0)
i=i+10; //decrease the DC motor speed
}
P0=i;
} }

8051 MANUAL NCET 43


“MICROCONTROLLERS LAB”
6. Elevator

Algorithm for elevator interface


• Read the floor request through input port P1.
• If the current floor and requested floor are the same no change light up the
corresponding LED through P0.
• If the requested floor greaterthan current moving up of the lift is indicated by
glowing of LED’s from current floor to the requested.
• If the requested floor lesserthan current moving down of the lift is indicated by
glowing of LED’s from current floor to the requested.

PS
PS

8051µC

P0 Elevator
FRC 26pin interface
Cable

8051 MANUAL NCET 44


“MICROCONTROLLERS LAB”
#include <REG51XD2.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(50000);
}
}
else /* If Current floor is < request floor */
{
i = Flr[ReqFlr] - Flr[CurFlr]; /* Get the no of floors to travel */
j = Flr[CurFlr];
for(;i>0;i--) /*Move the indicator Up */
{
P0 = 0x0f0 | j;
j++;
delay(50000);
}
}
CurFlr = ReqFlr; /* Update Current floor */
P0 = FClr[CurFlr]; /* Clear the indicator */
}
}
void delay(unsigned int x)
{
for(;x>0;x--);
}

8051 MANUAL NCET 45


“MICROCONTROLLERS LAB”

Experiment I
Aim: To Demonstrate the Stepper Motor interface to 8051
Write a program to control stepper motor direction & speed through software.

Apparatus Required: ESA MCB51, RS232 cable, Power Adapter ESA ADD 5(2),
ESA Interface Module [Stepper Motor Module] & 26 pin
FRC connector.

Theory: A Steeper motor is a widely used device that translates electrical pulses
into Mechanical Movement.
There are two types of stepper motor:
a) Permanent magnet
b) Variable reluctance
Operations: Figure (1) shows a stepper motor in its simplest form .It consists of two
stator windings A, B & a motor having two magnetic poles N & S when a +ve Voltage is
applied to stator winding A, a magnetic field Fa is generated as shown in fig (a) The rotor
positions itself such that its poles lock with corresponding stator poles. With the winding
‘A’ Excited as before winding ‘B’ is now switched on to a +ve Voltage as shown in figure
(b).This produces a magnetic field Fb in addition to Fa .The resulting magnetic filed F
makes an angle of 450 as shown in fig (b). The rotor consequently moves through 45 0 in

8051 MANUAL NCET 46


“MICROCONTROLLERS LAB”
anti-clock wise direction, again to cause locking of rotor poles with corresponding stator
poles.
While winding ‘B’ has +ve Voltage applied to it, winding ‘A’ is Switched Off
in figure(c). The rotor then moves through a further 450 in anti-clock wise direction to
align itself with stator filed Fb. With voltage +V on winding ‘B’ a voltage –V is applied
to winding ‘A’ as shown in fig (d) .Then the stator magnetic field has 2 components Fa, Fb
& their resultant F makes an angle of 1350 position.
In this way it can be seen that, as the pattern of excitation the state of windings
is changed, the motor moves successively through 450 steps through figures (e) to (h), &
completes one full revolution in anti clock wise direction .A practical stepper motor will
have 1.80 step angle 50 tooth on its rotor there are 8 main poles on the stator, each having
5 tooth in pole face. The step angle is given by,
A=360/(N*K) degrees
Where, N= number of rotor tooth
K= excitation sequence factor
For applications of stepper Motor refer “Stepper Motor Interface”
Illustration of Stepper motor operating principle

8051 MANUAL NCET 47


“MICROCONTROLLERS LAB”

Interfacing Circuit Diagram

Stepper Motor
PA0

PA1
8051 PA2

PA3

Com Com
COM +5v
COM +5V

Description:
The steeper motor interface uses 4 transistor pairs in a darlington
configuration. each Darlington pair is used to excite the particular winding of the motor
connected through 4 pin connector on the interface. The inputs to these transistors are

8051 MANUAL NCET 48


“MICROCONTROLLERS LAB”
from 8051 input/output lines of kit. Port A Lower nibble PA0, PA1, PA2, PA3, are the 4
lines brought out to the 26 pin FRC male connector (J1) on the interface module.

Connection: For installation refer 'Stepper Motor Interface & part A.

Expected Result:
When program is burn on interface module, motor rotates in anti-clock
wise .When interrupt button is pressed, direction changes.

Reference Questions:
1) Write a program to control stepper motor direction using port 0.
2) Demonstrate the stepper motor interface for 8051.
3) Write a program to control stepper motor direction so that it should start rotating
anti -clock wise direction.
4) Write a program to control speed of the stepper motor.

Experiment 2

Aim: To demonstrate the DC motor interface for 8051 .Write a program to control DC
motor Speed through softwares.

Apparatus Required: ESA MCB51, RS232 cable, Power Adapter ESA ADD-5 ESA
interface module [DC motor Module] & 26 pin FRC Connector.

Theory: DC motors are used in many applications like process control & automation in an
industry, robotics, consumer electronics etc.One can consider the use of DC motor where
ever there is need to control the motion of an object .Speed control of the motor is
important in the applications involving them .The DC motor used in this interface module
is a 12V, 4watt motor.

Operation: The Pulse Width Modulation technique is used to vary the speed of the DC
motor .The frequency of the Pulses is 120Hz. Keeping the frequency constant, the width of
the pulses is used to change the speed .When the pulse width is minimum, the speed is

8051 MANUAL NCET 49


“MICROCONTROLLERS LAB”
minimum & when the width is maximum, the speed is max (2400 rpm). The ramp &
pedestal technique is used to change the pulse width & there by the speed.

Connection: For installation refer part A & DC motor interface.

Expected Result:
When program is burn on interface module, motor starts rotating from lower
speed to higher speed. To change the speed of motor, interrupt button has to be used.

Reference Questions:
1. Write a program to control the speed of the DC motor
2. Demonstrate the DC motor interface for 8051
3. Write a program to display measured speed of DC motor
4. Write a program to control the speed of DC motor & it should run with half speed.

Experiment: 3

Aim: To Demonstrate DAC interface to 8051. Write a program to generate a square wave
with 2.5V (0x7f) Amplitude . To change the amplitude use P3.3 (INT1) on 8051.

Apparatus Required: ESA MCB51, RS232 cable, power adapter, ESA interface Module
[DAC module] & 26 pin FRC connector.

Theory: The Dual DAC interface can be used to generate different intersecting wave
forms using 8051 c. There are 2 8bit Digital to Analog converters provided based on
DAC. The digital inputs to these DACs are provided through the port A & port B of 8051
used as output ports. The analog outputs from the DACs are given to operational
amplifiers which act as current to voltage converters. Two 10K pots are provided for the
offset balancing of Op-Amps.

Connection: For installing refer Dual DAC interface manual & part A

8051 MANUAL NCET 50


“MICROCONTROLLERS LAB”
Expected result: When program is burn on interface module, Square wave with 2.5V
amplitude appears on the CRO.

Reference Questions:
1. Write a program to generate a square wave with 2.5V amplitude
2. Demonstrate the DAC interface to 8051 for getting square wave with 2.5V
amplitude
3. Write a program to generate a Square wave with 5V amplitude.

Experiment:4

AIM: To demonstrate DAC interface to 8051 .Generate a triangular wave with 5V


amplitude.

Apparatus Required: ESA MCB51, RS232 cable, power adapter ESA ADD-5, ESA
interface Module [DAC interface Module] & 26 pin FRC Connector & CRO.

Theory: The Dual DAC interface can be used to generate different wave forms using 8051
c. In this experiment DAC is used to generate triangular waveform. There are two 8 bit
Digital to Analog converters provided based on DAC. The Digital inputs to these DACs
are provided through the port A & port B of 8051 used as output ports. The analog outputs
from the DACs are given to Op-Amps which act as current to voltage converters. Two
10K pots are provided for the offset balancing of Op-Amps.

Connection: For connecting 8051 to Dual DAC interface module refers installing steps
from Dual DAC interface manuals & port A initiating Keil software.

8051 MANUAL NCET 51


“MICROCONTROLLERS LAB”
Expected result: When program is burn on interface module triangular wave with 5V
amplitude is displayed on the CRO.

Reference Questions:
1. Write a program to generate a triangular wave with 5V amplitude
2. Demonstrate the DAC interface to 8051 for getting triangular wave with 5V
amplitude
3. Write a program to generate a triangular wave with 10V amplitude.

Experiment :5

Aim: To demonstrate DAC interface to 8051 generate a ramp with 5v amplitude.

Apparatus Required: ESA MCB51, RS232, ESA ADD-5, ESA interface module [DAC
interface module]& 26-pin FRC connector & CRO.

Theory: The Dual DAC interface can be used to generate Ramp wave form there are two
8 bit Digital to Analog converters provided based on DAC. The digital inputs to these
DACs are provided through the port A & port B of 8051 used as output ports. The analog
outputs from the DACs are given to Op-Amps which act as current to voltage converters.
Two 10K pots are provided for the offset balancing of Op-Amps.

Connection: For connecting 8051 to Dual DAC interface module refer installing steps
from Dual DAC interface manual & part A initiating Keil software.

8051 MANUAL NCET 52


“MICROCONTROLLERS LAB”
Expected Result: When program is burn on interface module, Ramp wave with 5V
amplitude is displayed on the CRO.

Reference Questions:

1. Write a program to generate ramp with 5V amplitude.

2. Demonstrate the DAC interface to 8051 for getting ramp wave with 5V

amplitude

3. Write a program to generate ramp with 10V amplitude

8051 MANUAL NCET 53


“MICROCONTROLLERS LAB”

Experiment: 6
Aim: Write a program to demonstrate Elevator interface to 8051

Apparatus Required: ESA MCB51, RS232 cable, ESAADD-5 (Power Adapter), ESA
interface module [Elevator interface module] & 26 pin FRC Connector.

Theory: Elevator interface simulates the control & operation of an Elevator. Four floors
are assumed & for each floor a key & corresponding LED indicator are provided to serve
as request buttons & request status indicators .The elevator itself is represented by a
column of ten LED’s the motion of the elevator can be simulated by turning on successive
LED’s, one at a time the delay between turning Off one LED & turning on the next LED
can simulate the “Speed”of the Elevator. User can need the request status information
through one port, reset the request indicators through another port & control the elevator
through another port.

Operation: The interface is having 4 keys, which is marked as 0, 1 , 2 & 3 representing


the request buttons at the 4 floors. Pressing of key causes a corresponding flip -flops to be
set the outputs of the 4 flip flops can be read through port B also, the status of these
signals is reflected by a set of 4 LED’s, the flip flops can be reset through port A. A
column of 10 LED’s representing the Elevator can be controlled through port A.

Connection: For connecting 8051 to Elevator interface module refer Elevator interface
manual & part A initial steps of Keil software.

Expected Result: When program is burn on interface module, initially Other LED glows
& when buttons (0 , 1, 2, & 3) pressed that particular LED glows (0 or 1 or 2 or 3).

Reference Questions:
1. Write a program to demonstrate elevator interface to 8051
2. Write a program to perform elevator interface using 4 software’s & 4 LED’s
3. Using 3 different ports control the operation of elevator through software

8051 MANUAL NCET 54


“MICROCONTROLLERS LAB”
Experiment: 7
Aim: To demonstrate the calculator keypad interface to 8051. Write the program to
display the calculated result on the on board LCD pattern of 8051.

Apparatus required: ESA, MCB51, RS232 cable, ESA ADD-5 (power adapter) ESA
interface module [Calculator Keyboard Interface Module] & 26 pin FRC Connector.

Theory: Calculator keypad is used as an input device. A calculator keypad can be


interfaced to a  c using a dedicated controller (keyboard controller). The present interface
module provides a calculator style calculator keypad consisting of the keys 0 to 9, +,
- , X, = , %, C, CE & two space keys. These 20 keys are arranged in a 3x8 matrix (The 3rd
row has only 4 keys). The row lines can be driven through port C & the status of column
lines can be read through port A. This interface allows the user to study a number of
techniques generally used in calculator Key pad interfacing.

Operation: The 20 keys are arranged in a 3x8 matrix fashion .The row lines are driven by
PC0, PC1 & PC2. The column lines are read through port A. When no key is pressed, all
the return lines are low. The rows are driven high one after another in sequence when a
row is driven high. Pressing a key in that row causes the corresponding return line to be as
high. Then it scans for the column for which the key is depressed. The row and column
positions can then be used to encode the key as the scanning of the rows occurs at very
high speed compared to human reaction times.

Connection: For connection 8051 to calculator interface module refer calculator keypad
interface manual & part A initial steps of Keil software.

Expected result: When program is burn on interface module the module works as
calculator. It performs the following functions like Addition, Subtraction, Multiplication
etc.

Reference Questions:
1. Demonstrate the calculator keypad interface to 8051
2. Write a program to display calculated result on onboard LCD pattern 0f 8051
3. Write a program to display calculated result for 3x8 matrix fashioned calculator .

8051 MANUAL NCET 55


“MICROCONTROLLERS LAB”
Experiment: 8

Aim: To demonstrate the Keyboard Interface to 8051. Write the program to display the
pressed key’s key code on the on-board LCD pattern of 8051.

Apparatus Required: ESA MCB51, RS232 cable, ESA ADD-5, ESA interface module
[Keyboard Interface Module]& 26-pin FRC connector.

Theory: The keyboard interface module consists of 20 keys. These 20 keys are arranged
in a 3x8 matrix .The row lines can be driven through port C & the status of column lines
can be read through port A. This interface allows the user to study a number of techniques
generally used in keyboard interfacing.

Connection: For connecting 8051 to Keyboard interface refer keyboard interface manual
& part A initiating steps of Keil software.

Expected Result: When program is burn on interface module, the key pressed by the user
will be displayed on LCD.

Reference Questions:
1. Demonstrate the keyboard interface to 8051
2. Write a program to display the pressed Keys’s key code on the on-board LCD
pattern of 8051
3. Write a program for key board interface to 8051

8051 MANUAL NCET 56

Das könnte Ihnen auch gefallen