Professional Documents
Culture Documents
IVth SEM EC
MICROCONTROLLER
LABORATORY MANUAL
(06ESL47)
DEPARTMENT
OF
ELECTRONICS & COMMUNICATION ENGINEERING
2008
8051 MANUAL
RNSIT
MICROCONTROLLERS LAB
IVth SEM EC
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
RNSIT
MICROCONTROLLERS LAB
IVth SEM EC
Introduction
PROCESSOR used is Atmel AT89C51ED2 - micro controller that has 64Kbytes of onchip 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 are also available.
8051 MANUAL
RNSIT
MICROCONTROLLERS LAB
IVth SEM EC
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 Monitor51 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 onchip 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
RNSIT
MICROCONTROLLERS LAB
IVth SEM EC
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
8051 MANUAL
RNSIT
MICROCONTROLLERS LAB
IVth SEM EC
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.
8051 MANUAL
RNSIT
MICROCONTROLLERS LAB
IVth SEM EC
8051 MANUAL
RNSIT
MICROCONTROLLERS LAB
IVth SEM EC
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.
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 8035h.(use bubble sort algorithm)
ORG 0000H
SJMP 30H
ORG 30H
MOV R0,#05
L1: MOV dptr, #9000h
MOV A,R0
MOV R1,A
L2: MOVX a, @dptr
MOV B, A
INC dptr
MOVX a, @dptr
CLR C
MOV R2, A
SUBB A, B
JC NOEXCHG
MOV A,B
MOVX @dptr,a
DEC DPL
MOV a,R2
MOVX @dptr,a
INC DPTR
NOEXCHG: DJNZ R1,L2
DJNZ R0,L1
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).
8051 MANUAL
RNSIT
MICROCONTROLLERS LAB
IVth SEM EC
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
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
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.
8051 MANUAL
RNSIT
MICROCONTROLLERS LAB
IVth SEM EC
3.
4.
5.
6.
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=3perform 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 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
8051 MANUAL
RNSIT 10
MICROCONTROLLERS LAB
IVth SEM EC
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
RNSIT 11
MICROCONTROLLERS LAB
IVth SEM EC
(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
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.
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.
8051 MANUAL
RNSIT 12
MICROCONTROLLERS LAB
IVth SEM EC
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.
RNSIT 13
MICROCONTROLLERS LAB
IVth SEM EC
4. LOGICAL INSTRUCTIONS
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
8051 MANUAL
RNSIT 14
MICROCONTROLLERS LAB
IVth SEM EC
MOV A,@R0
//ON CHIP DATA RAM-DONOT USE MOVX
MOV R1,A
//X IN R1
INC R0
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
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 23H LOCATION
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
MOV C,0FH
//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
RESULT: 20h = 00 => AND OF LSBs=1 (hence 80 in 23h location)
8051 MANUAL
RNSIT 15
MICROCONTROLLERS LAB
IVth SEM EC
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 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.
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
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
8051 MANUAL
RNSIT 16
MICROCONTROLLERS LAB
IVth SEM EC
DJNZ r1,DECR1
RET
END
RESULT: Accumulator A is incremented in binary from 00, 01,0209,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)
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
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).
8051 MANUAL
RNSIT 17
MICROCONTROLLERS LAB
IVth SEM EC
RNSIT 18
MICROCONTROLLERS LAB
1.
2.
3.
4.
5.
IVth SEM EC
Algorithm:
1.
2.
3.
4.
5.
6.
7.
8.
8051 MANUAL
RNSIT 19
MICROCONTROLLERS LAB
IVth SEM EC
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).
8051 MANUAL
RNSIT 20
MICROCONTROLLERS LAB
IVth SEM EC
8. CONVERSION PROGRAMS
14) Write an ALP to implement decimal to hex conversion
ORG 0000H
SJMP 30h
ORG 30h
MOV DPTR,#40H
memory 40h
MOVX A, @DPTR
ANL A, #0F0H
SWAP A
MOV B,#0AH
MUL AB
MOV r1,a
MOVX A,@DPTR
ANL A,#0FH
ADD A,R1
INC DPTR
MOVX @DPTR,A
HERE:SJMP HERE
END
8051 MANUAL
RNSIT 21
MICROCONTROLLERS LAB
XCH A,B
MOVX @DPTR,A
XCH A,B
INC DPTR
MOVX @DPTR,A
HERE:SJMP HERE
End
IVth SEM EC
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
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
RESULT: The BCD code 28 at D:0050h is converted to 2 ASCII codes-38h 32h
Algorithm :
8051 MANUAL
RNSIT 22
MICROCONTROLLERS LAB
IVth SEM EC
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
8051 MANUAL
RNSIT 23
MICROCONTROLLERS LAB
IVth SEM EC
Note: For this program the input data should be only in the range 30h-39h & 41h to 46h.
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
3. Else (i.e., for 0-9) subtract only 30h
4. Store the converted hexadecimal number.
8051 MANUAL
RNSIT 24
MICROCONTROLLERS LAB
IVth SEM EC
INTERFACING PROGRAMS
Hardware Interfacing
1.Waveform Generation using Dual DAC
2.Stepper Motor interface.
3.4X4 hexadecimal Keyboard interface
4.DC motor interface
5.Calculator using Keyboard and Seven segment display.
6.Elevator control.
7.Temperature sensor.
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.
Keil is a versatile
8 software with a cross compiler that will convert the C program
CRO to
assembly language
and
thus
the
program
can
be
executed
on
the
desired
target (say
0
Dual
8051).
Xout
5 P0
DAC
8051 MANUAL
RNSIT 25
MICROCONTROLLERS LAB
IVth SEM EC
8051 MANUAL
RNSIT 26
MICROCONTROLLERS LAB
IVth SEM EC
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;
while(1)
{
for(i=0;i<0xff;i++){
P1 = i;
P0 = i;
}
for(i=0xfe;i>0x00;i--)
{P0 = i;
P1 = i;}
}
}
/* P0 as Output port */
/* Generate ON pulse */
8051 MANUAL
RNSIT 27
MICROCONTROLLERS LAB
IVth SEM EC
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]; }
}
}
2.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 360o 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.
8051 MANUAL
RNSIT 28
MICROCONTROLLERS LAB
IVth SEM EC
PS
PS
8051C
P0
FRC 26pin
Cable
Stepper
Motor
Interface
Card
Stepper
Motor
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 1s is returned. So scan next row.
Repeat the action until all rows are scanned.
8051 MANUAL
RNSIT 29
MICROCONTROLLERS LAB
IVth SEM EC
PS
PS
8051C
P0
FRC 26pin
Cable
4X4
Keyboard
Interface
Card
4X4 Hex
keypad
RNSIT 30
MICROCONTROLLERS LAB
IVth SEM EC
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();
}
}
4.DC Motor
Algorithm for DC motor interface
8051 MANUAL
RNSIT 31
MICROCONTROLLERS LAB
IVth SEM EC
PS
PS
8051C
P0
FRC 26pin
Cable
P3.2(inr)
P3.3(dec)
DC Motor
Interface
Card
DC
Motor
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.
8051 MANUAL
RNSIT 32
MICROCONTROLLERS LAB
PS
8051C
IVth SEM EC
PS
Keypad
P0
FRC 26pin
Cable
7 Seg
Display
RNSIT 33
MICROCONTROLLERS LAB
IVth SEM EC
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)
{
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 */
8051 MANUAL
RNSIT 34
MICROCONTROLLERS LAB
IVth SEM EC
{
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);
}}
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 LEDs from current floor to the requested.
If the requested floor lesserthan current moving down of the lift is indicated by
glowing of LEDs from current floor to the requested.
8051 MANUAL
RNSIT 35
MICROCONTROLLERS LAB
IVth SEM EC
PS
PS
8051C
P0
FRC 26pin
Cable
Elevator
interface
RNSIT 36
MICROCONTROLLERS LAB
delay(25000);
}
}
CurFlr = ReqFlr;
P0 = FClr[CurFlr];
}
}
void delay(unsigned int x)
{
for(;x>0;x--);
}
IVth SEM EC
7.Temperature Sensor
PS
PS
8051C
Temp
Sensor
Interface
P0,P2,P3
FRC 26pin
Cable
Heat
Source
The interface card has a DAC to convert the actual temperature to digital this is compared
with reference temperarture. Realay 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 intial 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.
8051 MANUAL
RNSIT 37
MICROCONTROLLERS LAB
P0=0x00;
while(1)
{
DacIp= 0xff;
do
{
DacIp++;
Dac_Data = DacIp;
delay();
}while(Cmp_Out);
if(DacIp > 0x20)
Rel_Con = 1;
IVth SEM EC
/*make P0 as output*/
/*DAC input Data counter*/
/*Increment the DAC input Data*/
/*Move the DAC data to DAC*/
/* Check comparator output for low */
/*Compare with the set value i.e.0x20*/
else
Rel_Con = 0;
}
8051 MANUAL
RNSIT 38