8051 MICROCONTROLLER PROGRAMS
ADDRESSING MODES:
START: MOV A,#40H ; IMMEDIATE
MOV R1,#50H
MOV R0,A ; REGISTER
MOV B,R1
MOV 40H,R0 ; DIRECT
MOV 50H,R1
MOV B,@R0 ; INDIRECT
MOV A,@R1
BASIC OPERATIONS:
ADD A,#0ABH ; ARITHMETIC
MUL AB
SUBB A,40H
DIV AB
;
SETB C ; BOOLEAN (bit level operations)
;
MOV R7,#60H ; DATA TRANSFER
ANL A,B ; LOGICAL (byte level operations)
JC START ; PROGRAM BRANCHING
END
PROGRAM 1:
STORE THE HIGHER NIBBLE OF R7 IN TO BOTH NIBBLES OF R6
Mov a, r7 ; get the content in acc
Anl a, #0F0h ; mask lower bit
Mov r6, a ; send it to r6
Swap a ; xchange upper and lower nibbles of acc
Orl a, r6 ; OR operation
Mov r6, a ; finally load content in r6
PROGRAM 2:
THE CRYSTAL FREQUENCY IS GIVEN AS 12 MHZ. MAKE A SUBROUTINE THAT WILL
GENERATE DELAY OF EXACT 1 MS. USE THIS DELAY TO GENERATE SQUARE WAVE OF 50 HZ
ON PIN P2.0
Mov r6, #0Ah ; load 10d in r6
Acall delay ; call 1 ms delay ×10 = 10 ms
Clr p2.0 ; send 0 to port pin
Mov r6, #0Ah ; load 10d in r6
Acall delay ; call 1 ms delay ×10 = 10 ms
Sjmp loop ; continuous loop
Delay: ; load count 250d
Lp2: Mov r7, #0FAh
Lp1: Nop ; 1 cycle
Nop ; 1+1=2 cycles
Djnz r7, lp1 ; 1+1+2 = 4 cycles
Djnz r6, lp2 ; 4×250 = 1000 cycles = 1000 µs = 1 ms
Ret
PROGRAM 3:
COUNT NUMBER OF INTERRUPTS ARRIVING ON EXTERNAL INTERRUPT PIN INT1.
STOP WHENCOUNTER OVERFLOWS AND DISABLE THE INTERRUPT. GIVE THE INDICATION ON
PINP0.0
Movr2, #00h ; initialize the counter
Movie, #84h ; enable external interrupt 1
Here: Sjmp here ; continuous loop
Org 0013h ; interrupt 1location
Incr2 ; increment the count
Cjner2, #00h, out ; check whether it overflows
Movie, #00h ; if yes then disable interrupt
Clr p0.0 ; and give indication
Out : reti ; otherwise keep counting
PROGRAM 4:
Array addition of HEX numbers
ORG 0000H
AJMP 0010H
;
ORG 0010H
INI: MOV 40H,#22H
MOV 41H,#44H
MOV 42H,#88H
;
MOV A,40H
ADD A,41H
ADD A,42H
MOV 50H,A
END
PROGRAM 5:
Write Assembly program to SET & RESET each flag bits one at a time.
ORG 0000H
AJMP 0010H
;
ORG 0010H
MOV A,#0F0H
ADD A,#21H ; CY bit is SET
CLR C ; CY bit is RESET
MOV A,#0FH
ADD A, #03H ; AC bit is SET
CLR AC ; AC bit is RESET
MOV A,#60H
ADD A,#21H ; OV bit is SET
CLR OV ; OV bit is RESET
MOV A,#70H ; P bit is SET
CLR P ; CLR P does not RESET P bit.
MOV A,#03H ; P bit is RESET.
;
END
PROGRAM 6:
Write a Assembly program to :
1 - Read the DATA from PORT-0 and Write it to PORT-2
2 - Read the DATA from PORT-1 and Write it to PORT-3
3 - Repeat steps 1 & 2 non stop.
ORG 0000H
AJMP 0010H
ORG 0010H
MOV SP,#30H
START: MOV P2,P0
MOV P3,P1
JMP START
END
PROGRAM 7:
KEYBOARD INTERFACING:
Circuit diagram of INTERFACING KEY BOARD TO 8051
Circuit diagram of INTERFACING KEY BOARD TO 8051.
Start of main program:
to check that whether any key is pressed
start: mov a,#00h
mov p1,a ;making all rows of port p1 zero
mov a,#0fh
mov p1,a ;making all rows of port p1 high
press: mov a,p2
jz press ;check until any key is pressed
after making sure that any key is pressed
mov a,#01h ;make one row high at a time
mov r4,a
mov r3,#00h ;initiating counter
next: mov a,r4
mov p1,a ;making one row high at a time
mov a,p2 ;taking input from port A
jnz colscan ;after getting the row jump to check
column
mov a,r4
rl a ;rotate left to check next row
mov r4,a
mov a,r3
add a,#08h ;increment counter by 08 count
mov r3,a
sjmp next ;jump to check next row
after identifying the row to check the colomn following steps are followed
colscan: mov r5,#00h
in: rrc a ;rotate right with carry until get the carry
jc out ;jump on getting carry
inc r3 ;increment one count
jmp in
out: mov a,r3
da a ;decimal adjust the contents of counter
before display
mov p2,a
jmp start ;repeat for check next key.