Binary 1’s and 0’s Counter:
org 0000h
mov r5,#08h
mov r0,#00h
mov r1,#00h
mov a,#89h
label2: rrc a
jc label1
inc r0
sjmp here
label1:inc r1
here: djnz r5,label2
end
Square Wave 1:
org 0000h
mov tmod,#10h
rep: mov r0,#28
cpl p2.4
back: mov th1,#00h
mov tl1,#00h
setb tr1
again: jnb tf1,again
clr tr1
clr tf1
djnz r0,back
sjmp rep
end
Square wave 2:
org 0000h
mov tmod,#01h
rep: mov tl0,#075h
mov th0,#0b8h
mov p0,#00h
acall delay
mov tl0,#8ah
mov th0,#0e4h
mov p0,#0ffh
acall delay
sjmp rep
org 0030h
delay: setb tr0
again: jnb tf0,again
clr tr0
clr tf0
ret
end
LCD Interfacing:
;Call a time delay before sending next data/command
; P1.0-P1.7=D0-D7, P2.0=RS, P2.1=R/W, P2.2=E
ORG 0000H
MOV DPTR,#MYCOM
C1: CLR A
MOVC A,@A+DPTR
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
JZ SEND_DAT
INC DPTR
SJMP C1
SEND_DAT: MOV DPTR,#MYDATA
D1: CLR A
MOVC A,@A+DPTR
ACALL DATAWRT ;call command subroutine
ACALL DELAY ;give LCD some time
INC DPTR
JZ AGAIN
SJMP D1
AGAIN: SJMP AGAIN ;stay here
COMNWRT: ;send command to LCD
MOV P1,A ;copy reg A to P1
CLR P2.0 ;RS=0 for command
CLR P2.1 ;R/W=0 for write
SETB P2.2 ;E=1 for high pulse
ACALL DELAY ;give LCD some time
CLR P2.2 ;E=0 for H-to-L pulse
RET
DATAWRT: ;write data to LCD
MOV P1,A ;copy reg A to port 1
SETB P2.0 ;RS=1 for data
CLR P2.1 ;R/W=0 for write
SETB P2.2 ;E=1 for high pulse
ACALL DELAY ;give LCD some time
CLR P2.2 ;E=0 for H-to-L pulse
RET
DELAY: MOV R3,#250 ;50 or higher for fast CPUs
HERE2: MOV R4,#255 ;R4 = 255
HERE: DJNZ R4,HERE ;stay until R4 becomes 0
DJNZ R3,HERE2
RET
ORG 300H
MYCOM: DB 38H,0EH,01,06,84H,0 ; commands and null
MYDATA: DB 'HELLO EXTC',0
END
Matrix Keypad:
#include<REGX51.H>
sbit KROW0 = P0^0;
sbit KROW1 = P0^1;
sbit KROW2 = P0^2;
sbit KROW3 = P0^3;
sbit KCOL0 = P0^4;
sbit KCOL1 = P0^5;
sbit KCOL2 = P0^6;
sbit KCOL3 = P0^7;
sfr LEDPORT = 0x90;
void main(void)
unsigned char key_status = 0,i;
KROW0 = 0; //ROWS - DECLARED AS OUTPUT PINS
KROW1 = 0;
KROW2 = 0;
KROW3 = 0;
KCOL0 = 1; //COLS - DECLARED AS INPUT PINS
KCOL1 = 1;
KCOL2 = 1;
KCOL3 = 1;
LEDPORT = 0x00; //LEDPORT IS DECLARED AS OUTPUT PORT
while(1)
for(i = 0; i<4; i++)
if( i == 0)
KROW0 = 0;
KROW1 = 1;
KROW2 = 1;
KROW3 = 1;
if(KCOL0 == 0)
while(KCOL0 == 0);
key_status = 1;
if(KCOL1 == 0)
while(KCOL1 == 0);
key_status = 2;
}
if(KCOL2 == 0)
while(KCOL2 == 0);
key_status = 3;
if(KCOL3 == 0)
while(KCOL3 == 0);
key_status = 4;
if( i == 1)
KROW0 = 1;
KROW1 = 0;
KROW2 = 1;
KROW3 = 1;
if(KCOL0 == 0)
while(KCOL0 == 0);
key_status = 5;
if(KCOL1 == 0)
while(KCOL1 == 0);
key_status = 6;
if(KCOL2 == 0)
while(KCOL2 == 0);
key_status = 7;
if(KCOL3 == 0)
while(KCOL3 == 0);
key_status = 8;
if( i == 2)
KROW0 = 1;
KROW1 = 1;
KROW2 = 0;
KROW3 = 1;
if(KCOL0 == 0)
while(KCOL0 == 0);
key_status = 9;
if(KCOL1 == 0)
{
while(KCOL1 == 0);
key_status = 10;
if(KCOL2 == 0)
while(KCOL2 == 0);
key_status = 11;
if(KCOL3 == 0)
while(KCOL3 == 0);
key_status = 12;
if( i == 3)
KROW0 = 1;
KROW1 = 1;
KROW2 = 1;
KROW3 = 0;
if(KCOL0 == 0)
while(KCOL0 == 0);
key_status = 13;
}
if(KCOL1 == 0)
while(KCOL1 == 0);
key_status = 14;
if(KCOL2 == 0)
while(KCOL2 == 0);
key_status = 15;
if(KCOL3 == 0)
while(KCOL3 == 0);
key_status = 16;
LEDPORT = ~key_status;
}
7 Segment Display:
#include <REGX51.H>
sbit digi0 = P2^0;
sbit digi1 = P2^1;
sbit digi2 = P2^2;
sbit digi3 = P2^3;
sfr DATAPORT = 0x80;
sfr KEYPORT = 0xa0;
sbit key_1 = P2^4;
sbit key_2 = P2^5;
sbit key_3 = P2^6;
sbit key_4 = P2^7;
unsigned char dis_round =1;
unsigned char return_key;
void delay(void);
void ldelay(void);
void dis_key(void);
void main(void)
unsigned char temp_key;
digi0 = 0;
digi1 = 0;
digi2 = 0;
digi3 = 0;
DATAPORT = 0x00;
key_1 = 1;
key_2 = 1;
key_3 = 1;
key_4 = 1;
while(1)
DATAPORT = 0xF5; //1111 0000
digi0 = 1;
digi1 = 0;
digi2 = 0;
digi3 = 0;
ldelay();
DATAPORT = 0x4A;
digi0 = 0;
digi1 = 1;
digi2 = 0;
digi3 = 0;
ldelay();
DATAPORT = 0x04; //0000 1000
digi0 = 0;
digi1 = 0;
digi2 = 1;
digi3 = 0;
ldelay();
DATAPORT = 0xF5; //0000 1000
digi0 = 0;
digi1 = 0;
digi2 = 0;
digi3 = 1;
ldelay();
void ldelay(void)
unsigned char i;
for(i=255; i>0; i--);
for(i=255; i>0; i--);
for(i=255; i>0; i--);
for(i=255; i>0; i--);
for(i=255; i>0; i--);