Microcontroller Lab Manual
Microcontroller Lab Manual
Microcontroller Lab Manual
A. PROGRAMMING IN 8051
1. DATA TRANSFER
Write an assembly language program to transfer n =10 bytes of data from location
20h to location 50h (without overlap).
$ INCLUDE (REG51.INC)
ORG 0000H
MOV R7,#0AH ; initialize counter by 10 (dec)
MOV R0,#20H ; get initial source location
MOV DPTR,#50H ; get initial destination location
BACK: MOV A,@R0 ; get first content in accumulator
MOVX @DPTR,A ; move acc value to external location
INC R0 ; increment source location
INC DPTR ; increment destination location
DJNZ R7, BACK ; decrement R7, if not zero then jump to back
HERE: SJMP HERE ; endless loop
END
RESULT:
AFTER EXECUTION: 10 locations X:50h are filled up with data from 20h.
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.
$ INCLUDE (REG51.INC)
ORG 0000H
RESULT:
BEFORE EXECUTION: 5 locations at X:0020h & X:0050h are filled up with data.
Algorithm
1. Initialize registers to hold count data (array size) & also the source & destination
addresses.
2. Get data from source location into accumulator and save in a register.
3. Get data from the destination location into accumulator.
4. Exchange the data at the two memory locations.
5. Decrement the count register and repeat from step 2 to 4 till count is zero.
$ INCLUDE (REG51.INC)
ORG 0000H
MOV R1,A
INC DPTR
MOVX @DPTR,A
MOV A,R2
MOVX @DPTR,A
INC DPTR
RESULT:
Algorithm
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.
$ INCLUDE (REG51.INC)
ORG 0000H
MOVX A,@DPTR
MOV R1,A
MOVX A,@DPTR
MOV R1,A
RESULT:
BEFORE EXECUTION:
Algorithm
2. ARITHMETIC INSTRUCTIONS
$ INCLUDE (REG51.INC)
ORG 0000H
MOV R0,#22H
CLR C ; make the carry bit to 0
MOV A,#3CH ; load the low byte into A
ADD A,#0DFH ; add the low byte, now A=1B, cy=1
MOVX @R0,A ; load low byte of sum in R0 ie., at 22h
DEC R0 ; address is decremented to 21h
MOV A,#0BAH ; load the high byte into A
ADDC A,#04H ; add the high bytes with carry i.e, 04+0BA+1=BF
MOVX @R0,A ; at 21h the value BF is moved
CLR A ; clear the accumulator value
RLC A ; to obtain the carry bit value from the accumulator DEC
R0 ; address is decremented to 20h
MOVX @R0,A ; the carry bit value is loaded to 20h
HERE: SJMP HERE ; endless loop
END
AFTER EXECUTION:
$ INCLUDE (REG51.INC)
ORG 0000H
MOV R0,#22H
CLR C ; make the carry bit to 0
MOV A,#58H ; load the low byte into A
SUBB A,#53H ; sub the low byte, now A=05, cy=0
MOV @R0,A ; load low byte of diff in R0 ie., at 22h
DEC R0 ; address is decremented to 21h
MOV A,#22H ; load the high byte into A
SUBB A,#48H ; sub the high bytes with carry 22-48-0=DA
MOV @R0,A ; at 21h the value DA is moved
CLR A ; clear the accumulator value
RLC A ; to obtain the carry bit value from the accumulator
DEC R0 ; address is decremented to 20h
MOV @R0,A ; the carry bit value is loaded to 20h
HERE: SJMP HERE ; endless loop
END
AFTER EXECUTION:
2258 – 4853 = 01 DA 05
$ INCLUDE (REG51.INC)
ORG 0000H
MOV R6,#62H ; load the higher byte of first value into R6
MOV R7,#30H ; load the lower byte of first value into R7
MOV R4,#43H ; load the higher byte of second value into R4
MOV R5,#2EH ; load the lower byte of second value into R5
; multiply R5 by R7
MOV A,R5 ; move the R5 into the accumulator
MOV B,R7 ; move R7 into B
MUL AB ; multiply the two values
MOV R2,B ; move B (high-byte) into R2
MOV R3,A ; move A (low-byte) into R3
; multiply R5 by R6
MOV A,R5 ; move R5 back into the accumulator
MOV B,R6 ; move R6 into B
MUL AB ; multiply the two values
ADD A,R2 ; add the low-byte into the value already in R2
MOV R2,A ; move the resulting value back into R2
MOV A,B ; move the high-byte into the accumulator
ADDC A,#00H ; add zero (plus the carry, if any)
MOV R1,A ; move the resulting answer into R1
MOV A,#00H ; load the accumulator with zero
ADDC A,#00H ; add zero (plus the carry, if any)
MOV R0,A ; move the resulting answer to R0
; multiply R4 by R7
MOV A,R4 ; move R4 into the accumulator
MOV B,R7 ; move R7 into B
MUL AB ; multiply the two values
ADD A,R2 ; add the low-byte into the value already in R2
AFTER EXECUTION:
$ INCLUDE (REG51.INC)
ORG 0000H
MOV R0,#49H ; lower byte of dividend
MOV R1,#00H ; higher byte of dividend
MOV R2,#10H ; lower byte of divisor
MOV R3,#00H ; higher byte of divisor
CLR C ; clear carry initially
MOV R4,#00H ; clear R4 working variable initially
MOV R5,#00H ; clear R5 working variable initially
MOV B,#00H ; clear B since B will count the number of left-shifted bits
DIV1: INC B ; increment counter for each left shift
MOV A,R2 ; move the current divisor low byte into the accumulator
RLC A ; shift low-byte left, rotate through carry to
; apply highest bit to high-byte
MOV R2,A ; save the updated divisor low-byte
MOV A,R3 ; move the current divisor high byte into the accumulator
RLC A ; shift high-byte left high, rotating in carry from low-byte
MOV R3,A ; save the updated divisor high-byte
JNC DIV1 ; repeat until carry flag is set from high-byte
; shift right the divisor
DIV2: MOV A,R3 ; move high-byte of divisor into accumulator
RRC A ; rotate high-byte of divisor right and into carry
MOV R3,A ; save updated value of high-byte of divisor
MOV A,R2 ; move low-byte of divisor into accumulator
RRC A ; rotate low-byte of divisor right, with carry from high-byte
MOV R2,A ; save updated value of low-byte of divisor
CLR C ; clear carry, not needed
MOV 07H,R1 ; make a safe copy of the dividend high-byte
MOV 06H,R0 ; make a safe copy of the dividend low-byte
MOV A,R0 ; move low-byte of dividend into accumulator
AFTER EXECUTION:
0049 h = dividend and 0010 h = divisor
Then, R0=09h, R1=00h, R2=04h and R3=00h
$ INCLUDE (REG51.INC)
ORG 0000H
MOV R6,#62H ; load the higher byte into R6
MOV R7,#30H ; load the lower byte into R7
MOV A,R6
MOV R4,A ; copy the contents from R6 to R4
MOV A,R7
MOV R5,A ; copy the contents from R7 to R5 then multiply R5 by R7
MOV A,R5 ; move the R5 into the accumulator
MOV B,R7 ; move R7 into B
MUL AB ; multiply the two values
MOV R2,B ; move B (high-byte) into R2
MOV R3,A ; move A (low-byte) into R3 then multiply R5 by R6
MOV A,R5 ; move R5 back into the accumulator
MOV B,R6 ; move R6 into B
MUL AB ; multiply the two values
ADD A,R2 ; add the low-byte into the value already in R2
MOV R2,A ; move the resulting value back into R2
MOV A,B ; move the high-byte into the accumulator
ADDC A,#00H ; add zero (plus the carry, if any)
MOV R1,A ; move the resulting answer into R1
MOV A,#00H ; load the accumulator with zero
ADDC A,#00H ; add zero (plus the carry, if any)
MOV R0,A ; move the resulting answer to R0 then multiply R4 by R7
MOV A,R4 ; move R4 into the accumulator
MOV B,R7 ; move R7 into B
MUL AB ; multiply the two values
ADD A,R2 ; add the low-byte into the value already in R2
MOV R2,A ; move the resulting value back into R2
$ INCLUDE (REG51.INC)
ORG 0000H
MOV DPTR,#9000H ; source location is chosen to be 9000h
MOVX A,@DPTR ; the content at 9000h is moved to accumulator
MOV B,A ; accumulator content is moved to B
PUSH B ; push B register to stack
MUL AB ; multiply the content of A and B
POP B ; pop B register from stack
MUL AB ; multiply the content of A and B
MOV R0,A ; accumulator content is moved to R0
INC DPTR ; address is 9001h
MOV A,B ; B register content is moved to accumulator
MOVX @DPTR,A ; from accumulator to 9001h
INC DPTR ; address is 9002h
MOV A,R0 ; R0 register content is moved to accumulator
MOVX @DPTR,A ; from accumulator to 9002h
HERE: SJMP HERE ; endless loop
END
3. COUNTERS
$ INCLUDE (REG51.INC)
ORG 0000H
MOV A,#00H
BACK: ACALL DELAY
INC A
CJNE A,#FFH,LOOP
SJMP DOWN
LOOP: SJMP BACK
DOWN: ACALL DELAY
DEC A
CJNE A,#00H,DOWN
SJMP BACK
DELAY: MOV R1,#0FFH
DEC1: MOV R2,#0FFH
DEC2: MOV R3,#0FFH
DJNZ R3,$
DJNZ R2,DEC2
DJNZ R1,DEC1
RET
HERE: SJMP HERE ; endless loop
END
AFTER EXECUTION:
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)
$ INCLUDE (REG51.INC)
ORG 0000H
MOV A,#00
BACK: ACALL DELAY
ADD A,#01H
DA A
CJNE A,#25H,LOOP
SJMP DOWN
LOOP: SJMP BACK
DOWN: ACALL DELAY
ADD A,#99H
DA A
CJNE A,#00H,DOWN
SJMP BACK
DELAY: MOV R1,#0FFH
DEC1: MOV R2,#0FFH
DEC2: MOV R3,#0FFH
DJNZ R3,$
DJNZ R2,DEC2
DJNZ R1,DEC1
RET
HERE: SJMP HERE ; endless loop
END
AFTER EXECUTION:
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(add 99h for down counter)
4. Decimal adjust accumulator for the BCD up/down counter.
RESULT:
BEFORE EXECUTION: X:08000h = 45 & X:8001 = 35
Algorithm:
1. Store the elements of the array from the address 4000h & Move the first number
in r0 and the second number in register A respectively
2. 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
3. If the carry bit is set then Set MSB of 2F(7FH), else LSB of data RAM 2F (bit
address 78H).
$ INCLUDE (REG51.INC)
ORG 0000H
MOV R0,#20H
MOV A,@R0 ; on chip data ram-donot use movx
MOV R1,A
INC R0
MOV A,@R0 ; A -num1
INC R0 ; R0 points to num2
CJNE R1,#0,CKOR
ANL A,@R0
SJMP LAST
CKOR: CJNE R1,#01,CKXOR
ORL A,@R0
SJMP LAST
CKXOR: CJNE R1,#02,OTHER
XRL A,@R0
SJMP LAST
OTHER: CLR A
LAST: INC R0
MOV @R0,A ; store result
HERE: SJMP HERE ; endless loop
END
RESULT:
BEFORE EXECUTION: D:020H =00, 21=0f, 22 = 12
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.
$ INCLUDE (REG51.INC)
ORG 0000H
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
ASCII: MOV R4,A ; Store A
CLR C
SUBB A,#0AH ; Check if digit >=0A
MOV A,R4
JNC SKIP
SJMP SKIP1
SKIP: ADD A,#07H ; Add 07 if >09
SKIP1: ADD A,#30H ; Else add only 30h for 0-9
RET
BEFORE EXECUTION:
AFTER EXECUTION:
Algorithm :
6. CODE CONVERSION
$ INCLUDE (REG51.INC)
ORG 0000H
MOV DPTR,#40H ; 2-digit decimal number to be converted is given at 40h
MOVX A,@DPTR
ANL A,#0F0H ; obtain upper decimal digit
SWAP A ; bring to the units place
MOV B,#0AH
MUL AB ; MULTIPLY tens digit with #0A-toget tens in hex
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 ; endless loop
END
RESULT:
Algorithm
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).
$ INCLUDE (REG51.INC)
ORG 0000H
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 ; endless loop
END
RESULT:
AFTER EXECUTION: 9001 to 9003 – unpacked BCD number (decimal)- 5,5,2 (i.e.,
255 stored Lower digit first)
Algorithm
$ INCLUDE (REG51.INC)
ORG 0000H
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
MOV R1,#52H
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
DEC R1
MOV @R1,A ; Store the upper digit's ASCII code
HERE: SJMP HERE ; endless loop
END
RESULTS:
BEFORE EXECUTION:
AFTER EXECUTION:
Algorithm :
2. Get the lower nibble(BCD digit) & ADD (or ORL) with 30h
4. Get the higher nibble(tens BCD digit) & ADD (or ORL) with 30h
$ INCLUDE (REG51.INC)
ORG 0000H
MOV R1,#50H
CLR C
SUBB A,#41H
MOV A,@R1
JC SKIP
CLR C
SUBB A,#07H
SKIP: CLR C
SUBB A,#30H
INC R1
RESULTS:
BEFORE EXECUTION:
AFTER EXECUTION:
Note: For this program the input data should be only in the range 30h-39h & 41h to 46h.
Algorithm :
2. If character is greater than 41h,(for A-F), then subtract 07h & 30h
$ INCLUDE (REG51.INC)
ORG 0000H
MOV R0,#20H
MOV R1,#30H
MOV R2,#5
RESULTS:
BEFORE EXECUTION:
AFTER EXECUTION:
$ INCLUDE (REG51.INC)
ORG 0000H
MOV R0,#20H
MOV R1,#30H
MOV R2,#5
REPEAT: MOV A,@R0
XRL A,#30H
MOV @R1,A
INC R0
INC R1
DJNZ R2,REPEAT
HERE: SJMP HERE ; endless loop
END
RESULTS:
BEFORE EXECUTION:
AFTER EXECUTION:
$ INCLUDE (REG51.INC)
ORG 0H ; Reset Vector
SJMP 30H
ORG 0BH ; TF0 vector
SJMP ISR
ORG 30H
MOV A,#00
MOV R0,#0
MOV R1,#0
MOV TMOD,#02H ; 00000010-Run timer0 in mode 2
MOV TH0,#118 ; Set up timer 0 to overflow in 0.05msec
MOV IE,#82H ; %10000010 – Enable timer0 interrupt
SETB TCON.4 ; Start the timer0
DLY: SJMP DLY
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
HERE:SJMP HERE ; endless loop
END
Algorithm:
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.
$ INCLUDE (REG51.INC)
ORG 0H
SJMP 30H
ORG 30H
MOV TMOD,#20H ; timer 1; mode 2
MOV TH1,#-3 ; -3=FD loaded into TH1 for 9600 baud, 11.0592MHz.
MOV SCON,#50H ; 8-bit, 1 stop bit, REN enabled
SETB TR1 ; Start timer 1
AGAIN:MOV A,#‘y‘ ; transfer ―y‖
ACALL TRANS
MOV A,#‘E‘ ; transfer ―E‖
ACALL TRANS
NEXT:SJMP NEXT
TRANS: MOV SBUF,A ; load SBUF
DLY:JNB TI,DLY ; Wait for last bit to transfer
CLR TI ; get ready for next byte
RET
HERE: SJMP HERE ; endless loop
END
RESULT: yE is printed on the serial window each time the program is executed.
To get 9600, 28800/3 is obtained by loading timer1 with -3 (i.e., FF – 3 = FD) for further
clock division. For 2400 baud rate, 28800/12 => -12 = F4 in TH1.
Algorithm:
B. INTERFACING
8 CRO
0 Dual
5 P0 DAC Xout
1 P1
μC
(1.1) PROGRAM FOR SQUARE WAVE GENERATION
#include "c:\ride\inc\51\reg51.h"
sbit Amp = P3^3; /* Port line to change amplitude */
sbit Fre = P3^2; /* Port line to change frequency */
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 */
{
if(fre > 1000) /* if frequency exceeds 1000 reset to default */
fre = 100;
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.
#include "c:\ride\inc\51\reg51.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;
}
}
}
#include "c:\ride\inc\51\reg51.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;
}
}
}
#include "c:\ride\inc\51\reg51.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.
• 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
Stepper
Motor Stepper
P0
Motor
FRC 26pin Interface
Card
Cable
#include "c:\ride\inc\51\reg51.h"
for(;x>0;x--);
return; }
Main ( )
P0=0x00;
Val = 0x11;
for (i=0;i<4;i++)
P0 = Val;
delay (500);
}}
• 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.
#include "c:\ride\inc\51\reg51.h"
sbit row0=P1^0;
sbit row1=P1^1;
sbit row2=P1^2;
sbit row3=P1^3;
sbit col0=P2^0;
sbit col1=P2^1;
sbit col2=P2^2;
sbit col3=P2^3;
void get_key();
void scan();
void main()
P1=0x00;
P2=0xff;
while(1)
delayms(100);
get_key();
if(flag)
P0=lut[r][c];
P2=0x0f;
{ row3=0;}
for(r=0;r<4;r++) if(flag!=0)
{if(r=0) break;}}}}
{ void scan()
row0=0; {temp=P2;
row2=1; if(temp!=0x0f)
row3=1; {flag=1;
{row0=1; C=0;
row2=1; C=1;
row1=1; C=3;
row2=0; }}
else if(!r==3) {
{ unsigned char j;
row0=1; for(j=0;j<k*2;k++)
row1=1; }
4. DC INTERFACE
PS
PS
8051µC
DC Motor
DC
P0 Interface Motor
Card
FRC 26pin
Cable
P3.2(inr)
P3.3(dec)
Program for DC motor
#include "c:\ride\inc\51\reg51.h"
main()
while (1)
if (!inr)
while (!inr);
if(i>10)
if(!dcr)
while(!dcr);
if(i<0xf0)
P0=i;
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
#include "c:\ride\inc\51\reg51.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;
6. ELEVATOR CONTROL
PS
PS
8051µC
Elevator
interface
P0
FRC 26pin
Cable
#include "c:\ride\inc\51\reg51.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 */
{ delay(25000);
}}
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(25000);
} }
CurFlr = ReqFlr; /* Update Current floor */
P0 = FClr[CurFlr]; /* Clear the indicator */
}}
void delay(unsigned int x)
{
for(;x>0;x--); }