Lab Programs - Part A and Part B Microcontroller
Lab Programs - Part A and Part B Microcontroller
Program Comments
No
Experiment no. 1: Data Transfer Programming
1. Program to Exchange the contents of two Registers
MOV R0,#30H
MOV R1,#40H
MOV A,R0
XCH A,R1
MOV R0,A
SJMP $
2. Program To Transfer A Block Of Data Bytes Storing From Memory Location
30h To Another Block Starting From Memory Location 40h
MOV R0,#30H
MOV R1,#40H
MOV R2,#COUNT ; Give the number of Bytes that you want to transfer
LOOP: MOV A,@R0
MOV@R1,A
INC R0
INC R1
DJNZ R2,LOOP
SJMP $
3. Program to interchange the block of data bytes present in internal memory
MOV R0,#30H
MOV R1,#40H
MOV R2,#COUNT ; Give the number of Bytes that you want to transfer
LOOP:MOV A,@R0 ; Data from R0 memory loc. to A
XCH A,@R1 ;Interchange A and R1 , now R1 Data is there with A
MOV @R0,A ; Transfer it to R0 Memory Location
INC R0
INC R1
DJNZ R2,LOOP
LOOP1:SJMP LOOP1
4. Program to transfer a blocks of data to another block considering overlapping
blocks in internal memory or (Program to copy blocks of data in N memory
locations to N+ith memory locations with overlapping(Right to Left & Vice
Versa) in internal memory)
Mov r0,#30h
Mov r1,#35h
Mov r2,#06h
Mov a,r0
Cjne a,01,next ;compare A & r1, if not equal jump to next
Sjmp stop
Next : jc bottom_trf ; if carry first address is lesser than overlap address
;so overlap is from Right to Left else Left to Right overlap
add a,r2
dec a
mov r1,a
back1 : mov a,@r0
mov @r1,a
dec r0
dec r1
djnz r2,back1
sjmp $
Output:
Before execution:
30: 1 2 3 4 5 6
35: 0 0 0 0 0 0
After execution: 30: 1 2 3 4 5 1 2 3 4 5 6
OR
exad equ 9000h
exad1 equ 9010h
inad equ 30h
cnt equ 06h
mov dptr,#exad
MOV R0,#inad
MOV R2,#cnt
back: movx a,@dptr
mov @r0,a
inc r0
inc dptr
djnz r2,back
MOV dptr,#exad1
MOV R0,#inad
mov r2,#cnt
back1:movx a,@dptr
xch a,@r0
movx @dptr,a
inc r0
inc dptr
djnz r2,back1
mov r2,#cnt
mov r0,#inad
MOV dptr,#exad
loop:MOV A,@R0
movx @dptr,A
inc r0
inc dptr
djnz r2,loop
end
10. Program to find the largest number in an array
MOV R0,#40H
MOV R1, #(N-1)
MOV A,@R0
LOOP:INC R0
MOV 50H,@R0
CJNE A, 50H, NEXT
SJMP NEXT1
NEXT: JNC NEXT1
MOV A, @R0
NEXT1:DJNZ R1,LOOP
MOV 50H,A
LOOP1: SJMP LOOP1
11. Program to find the largest number in an array @ External Memory
MOV DPTR,#9000H
MOV R2, #6
MOV R0,#40H
MOV R1, #(5-1)
L1:MOVX A,@DPTR
MOV @R0,A
INC R0
INC DPTR
DJNZ R2,L1
MOV R0,#40H
MOV A,@R0
LOOP:INC R0
MOV 50H,@R0
CJNE A, 50H, NEXT
SJMP NEXT1
NEXT: JNC NEXT1
MOV A, @R0
NEXT1:DJNZ R1,LOOP
MOV 50H,A
MOV DPTR,#9010H
MOVX @DPTR,A
LOOP1: SJMP LOOP1
MOV DPTR,#9000H
MOV R1, #(5-1)
MOVX A,@DPTR
LOOP:MOV 50H,A
INC DPTR
MOVX A,@DPTR
CJNE A,50H, L1
SJMP NEXT1
L1:JNC NEXT1
MOV A,50H
SJMP NEXT1
NEXT1:DJNZ R1,LOOP
MOV DPTR,#9010H
MOVX @DPTR,A
LOOP1: SJMP LOOP1
12. Program to find the Smallest number in an array @ Internal Memory
MOV R0,#44H
MOV R1, #(N-1)
MOV A,@R0
LOOP:INC R0
MOV 50H,@R0
CJNE A, 50H, NEXT
SJMP NEXT1
NEXT: JC NEXT1
MOV A, @R0
NEXT1:DJNZ R1,LOOP
MOV 50H,A
LOOP1: SJMP LOOP1
13. Program to find the Smallest number in an array located @ External Memory
MOV DPTR,#9000H
MOV R1, #(5-1)
MOVX A,@DPTR
LOOP:MOV 50H,A
INC DPTR
MOVX A,@DPTR
CJNE A,50H, L1
SJMP NEXT1
L1:JC NEXT1
MOV A,50H
SJMP NEXT1
NEXT1:DJNZ R1,LOOP
MOV DPTR,#9010H
MOVX @DPTR,A
LOOP1: SJMP LOOP1
14. Program to arrange the given set of numbers in ascending (Increasing Order-
JC) order
18. Program to add two 8bit number present in internal memory location and store
the result in next consecutive memory location
MOV R1,#00H
MOV R0,#30H
MOV A,@R0
INC R0
ADD A,@R0
JNC NEXT
INC R1
NEXT: INC R0
MOV @R0,A
INC RO
MOV A,R1
MOV @RO,A
SJMP $
19. Program to Subtract two 8bit number present in internal memory location and
store the result in next consecutive memory location
CLR C
MOV R1,#00H
MOV R0,#30H
MOV A,@R0
INC R0
SUBB A,@R0
JNC NEXT
INC R1
NEXT: INC R0
MOV @R0,A
INC RO
MOV A,R1
MOV @RO,A
SJMP $
20. Program to add two 16 bit numbers
Clr c //clear the carry flag
mov r0,#30h
mov r1,#40h
mov a,@r0
add a,@r1
mov 50h,a
Inc r0
Inc r1
mov a,@r0
addc a,@r1 //add contents of A and address in r1 with carry
mov 51h,a
end
Output:
Before execution:
30: 1234
40:7453
After execution:
50: 87 86
21. Program to subtract two 16 bit numbers
Clr c
mov r0,#30h
mov r1,#40h
mov a,@r0
subb a,@r1
mov 50h,a
Inc r0
Inc r1
mov a,@r0
subb a,@r1
mov 51h,a
end
Output:
Before execution:30: 8234
40:7453
After execution:50: D0 0C
22. Program to perform 8bit binary multiplication
MOV R0,#30H
MOV A,@R0
INC R0
MOV 0F0H,@R0
MUL AB
INC R0
MOV@R0,A
INC R0
MOV @R0,0F0H
LOOP:SJMP LOOP
23. Program to perform 8bit binary DIVISION
MOV R0,#30H
MOV A,@R0
INC R0
MOV 0F0H,@R0
DIV AB
INC R0
MOV@R0,A
INC R0
MOV @R0,0F0H
LOOP:SJMP LOOP
24. Program to find square of a given 8 bit number
MOV R,#30H
MOV A,@R1
MOV 0F0H, A
MUL AB
MOV 50H, A
MOV 51H, 0F0H
SJMP $
25. Cube of a Number
MOV R1,#30H
MOV A,@R1
MOV 0F0H,A
MUL AB
MOV R3,0F0H
MOV 0F0H, @R1
MUL AB
MOV 50H,A
MOV R5,0F0H
MOV 0F0H,@R1
MOV A,R3
MUL AB
ADD A,R5
MOV 51H, A
MOV 52H, 0F0H
SJMP $
OUTPUT:
D: x30H: 25
50h: 2556 01
26. Program to Perform 16 bit multiplication
MOV DPTR,#9000H
MOV R4,#00
MOV R0,#30H
MOV R1,#40H
MOV A,@R0
MOV 0F0H,@R1
MUL AB
MOVX @DPTR, A
MOV R2,0F0H
INC R1
MOV A,@RO
MOV 0F0H,@R1
MUL AB
ADD A,R2
MOV R2,A
MOV A,0F0H
ADDC A,#00H
MOV R3,A
INC RO
DEC R1
MOV A,@R0
MOV 0F0H,@R1
MUL AB
ADD A,R2
INC DPTR
MOVX @DPTR,A
MOV A,0F0H
ADDC A,R3
MOV R3,A
JNC NEXT
INC R4
NEXT: INC R1
MOV A,@R0
MOV 0F0H,@R1
MUL AB
ADD A,R3
INC DPTR
MOVX @DPTR,A
MOV A,0F0H
ADDC A,R4
INC DPTR
MOVX @DPTR,A
LOOP:SJMP LOOP
27. Program to Perform Square of a 16 bit Number
MOV DPTR,#9003H
MOV R1,#00
MOV R0,#30H
MOV A,@R0
MOV R4,A
INC R0
MOV A,@R0
MOV R5,A
DEC R0
MOV A,@R0
MOV B,R4
MUL AB
MOVX @DPTR,A
MOV R2,0F0H
MOV A,@R0
MOV B,R5
MUL AB
ADD A,R2
MOV R2,A
MOV A,0F0H
ADDC A,#00H
MOV R3,A
INC R0
MOV A,@R0
MOV B,R4
MUL AB
ADD A,R2
DEC DPL
MOVX @DPTR,A
MOV A,0F0H
ADDC A,R3
MOV R3,A
JNC NEXT1
INC R1
NEXT1:MOV A,@R0
MOV 0F0H,R5
MUL AB
ADD A,R3
DEC DPL
MOVX @DPTR,A
MOV A,0F0H
ADDC A,R1
DEC DPL
MOVX @DPTR,A
LOOP:SJMP LOOP
Experiment no. 3: Boolean and Logical Instructions programming
28. Program to check whether given data in memory is even or odd if even pass FFH
– R1 register , else 00H
mov r0,#30h
mov a,@r0
jnb acc.0,even
mov r1,#00h
even:mov r1,#0ffh
end
29. Program to check whether given bytes of data in internal memory locations is
even or odd , separate even and odd numbers in different internal memory
locations
cnt1 equ 30h
mov r0,#cnt1
mov r1,#40h
cnt equ 08h
mov r2,#cnt
l1:mov a,@r0
jb acc.0,od
inc r0
djnz r2,l1
sjmp arng
od:mov a,@r1
xch a,@r0
mov @r1,a
inc r1
inc r0
djnz r2,l1
arng:MOV R2, #(cnt-1) ; Descending order pgm to arrange even elements
LOOP1: MOV R0,#cnt1
MOV R3,#(cnt-1)
LOOP2: MOV A,@R0
INC R0
MOV 50H,@R0
CJNE A, 50H, NEXT
SJMP NEXT1
NEXT: JNC NEXT1
MOV @R0, A
DEC R0
MOV @R0,50H
INC R0
NEXT1: DJNZ R3, LOOP2
DJNZ R2, LOOP1
end
30. Program to check whether given bytes of data in External memory locations is
even or odd , separate even and odd numbers in different external memory
locations
MOV DPTR,#9000H
AD equ 30h
mov r0,#AD
mov r1,#40h
cnt equ 08h
mov r2,#cnt
l1:MOVX A,@DPTR
jb acc.0,od
inc dptr
djnz r2,l1
SJMP TRNSODD
od:xch a,@r0
MOVX @DPTR,A
INC DPTR
INC R0
DJNZ R2,l1
TRNSODD:MOV A,R0
anl a,#0fh
MOV R3,A
MOV R0,#AD
MOV DPTR,#9010H
L2:MOV A,@R0
MOVX @DPTR,A
INC DPTR
INC R0
DJNZ R3,L2
MOV R0,#30H
MOV A,@R0
MOV R2,A
INC R0
MOV A,@R0
MOV R3,A
MOV A,R2
ANL A,R3 //logically AND the contents
INC R0
MOV @R0,A
MOV A,R2
ORL A,R3 //logically OR the contents
INC R0
MOV @R0,A
MOV A,R2
XRL A,R3 //logically XOR the contents
INC R0
MOV @R0,A
SJMP $
33. Write an ALP to compare two eight bit numbers NUM1 and NUM2 stored in
external memory locations 8000h and 8001h respectively. Reflect your result as:
If NUM1<NUM2, SET LSB of data RAM location 2FH (bit address 78H). If
NUM1>NUM2, SET MSB of location 2FH (bit address 7FH). If NUM1 = NUM2,
then set both LSB & MSB of bit addressable memory location 2FH.
mov dptr,#8001h
movx a,@dptr
mov r0,a
dec dpl
movx a,@dptr
clr c
subb a,r0 ;( Compare instrun with a and r0 is not there , So Subb is used ,
;Compare instruction allowed are cjne a, direct ,
; cjne a,#data, cjne Rn,#data, cjne @Ri,#data )
jz equal
jnc greater
setb 78h
sjmp end1
greater: setb 7Fh
sjmp end1
equal: setb 78h
setb 7fh
end1:sjmp end1
34. Write an assembly language program to count number of ones and zeros in a
eight bit number
mov r0,#30h
mov r1,#00h // to count number of 0s
mov r2,#00h // to count number of 1s
mov r7,#08h // counter for 8-bits
mov a,@r0 //Bring data to count number of 1s and 0s
again: rlc a
jc next
inc r1
sjmp here
next: inc r2
here: djnz r7,again
end
Experiment no. 4: Code Conversion Programs:
35. Decimal to Hexadecimal Conversion ( Last 8 Bit Value in Decimal is 99 so given
data should be less than or equal to 99 )
;ORG 0000H
;SJMP30h
;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#0Ato
get 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
END
OUTPUT:
OUTPUT: D 30H: FF
D40H:02 55
37. BCD TO ASCII
ORG 0000H
SJMP30h
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,#0F H //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
OUTPUT:
ORG 0000H
SJMP30h
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
ADD A,#07H //Add 07 if >09
SKIP:ADD A,#30H //Else add only 30h for 0-9
RET
END
OUTPUT:
ORG 0000H
SJMP30h
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
SUBB A,#07H
SKIP:SUBB A,#30H
INC R1
MOV @R1,A //Store the hex code
here: sjmp here
END
OUTPUT:
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
OUTPUT: D50H: 34 34 33
MOV R0,#30H
MOV A,@R0
SUBB A,#30H
INC R0
MOV @R0,A
END
OUTPUT: D50H : 34 04
up:mov a,#00
back: acall delay ; (back: MOV 30h,a for counting in Internal memory )
inc a
jnz back
sjmp up ;( terminate the program here THIS PGM WILL EXECUTE
; only once)
delay: mov r1,#055h
decr1: mov r2,#066h
decr: mov r3,#0FFh
djnz r3,$
djnz r2,decr
djnz r1,decr1
ret
end
44. Write an assembly language program to implement (display) an eight bit decimal
UP counter on watch window( ONCE/ CONTINUOUSLY)
(To understand this pgm remove delay pgm and comment acall delay and press F11
, you will understand step wise)
mov dptr,#9000h
up:mov a,#00h
back: movx @dptr,a
acall delay
inc a
jnz back
sjmp up ;( terminate the program here THIS PGM WILL EXECUTE
; only once)
delay: mov r1,#55h
loop1: mov r2,#066h
loop2: mov r3,#0ffh
loop3: djnz r3,loop3
djnz r2,loop2
djnz r1,loop1
ret
end
mov dptr,#9000h
down:mov a,#0ffh
back: movx @dptr,a
acall delay
dec a
jnz back
sjmp down ;( terminate the program here THIS PGM WILL EXECUTE
; only once)
mov dptr,#9000h
up:mov a,#00h
back: movx @dptr,a
acall delay
add a,#01h
da a
jnz back
sjmp up;( terminate the program here THIS PGM WILL EXECUTE
; only once)
delay: mov r1,#55h
loop1: mov r2,#66h
loop2: mov r3,#0ffh
loop3: djnz r3,loop3
djnz r2,loop2
djnz r1,loop1
ret
end
mov dptr,#9000h
down:mov a,#99h
back: movx @dptr,a
acall delay
add a,#99h
da a
jnz back
movx @dptr,a
sjmp down;( terminate the program here THIS PGM WILL EXECUTE
; only once)
delay: mov r1,#55h
loop1: mov r2,#66h
loop2: mov r3,#0ffh
loop3: djnz r3,loop3
djnz r2,loop2
djnz r1,loop1
ret
end
MOV DPTR,#9000H
up:MOV A, #00H
back:MOVX @DPTR, A
ACALL DELAY ;call delay
ADD A,#01H
DA A
CJNE A,#(N)H,back ;compare accumulator with 00H if not equal jump up
SJMP up ;( terminate the program here THIS PGM WILL EXECUTE
; only once)
DELAY: MOV R0,#099H ; load r0 by FFH
BACK1: MOV R1,#0aaH ; load r1 by FFH
BACK2: MOV R2,#0FFH ; load r2 by FFH
HERE: DJNZ R2,HERE ; decrement r2 if not equal to zero jump here
DJNZ R1, BACK2 ;decrement r1 if not equal to zero jump Back
DJNZ R0,BACK1 ;decrement r0 if not equal to zero jump Back1
RET
END
RESULT: )X:9000:n-1.......00....n-1.....00(Continuously)
MOV DPTR,#9000H
up:MOV A,#00H ;load the accumulator with 00H
back:MOVX @DPTR,A
LCALL DELAY ;call delay
ADD A,#01H ; increment accumulator by 1
DA A
CJNE A,#(N)H,back ;compare accumulator with 40h if not equal jump ;up
down:ADD A,#99H
DA A
MOVX @DPTR,A
LCALL DELAY
CJNE A,#00H, down
SJMP up;( terminate the program here THIS PGM WILL EXECUTE
; only once)
DELAY:MOV R0,#099H ; load r0 by FFH
BACK1:MOV R1,#0aaH ; load r1 by FFH
BACK2:MOV R2,#0FFH ; load r2 by FFH
HERE:DJNZ R2,HERE ; decrement r2 if not equal to zero jump here
DJNZ R1, BACK2 ;decrement r1 if not equal to zero jump Back
DJNZ R0,BACK1 ;decrement r0 if not equal to zero jump Back1
RET ;return to main
END
Part B
;f=11.06MHz
;CLR P2.3 ;Clear P2.3
;f=11.06 MHz, 5 ms
CLR P2.3 ;Clear P2.3
mov tmod,#01
loop:mov r0,14h
here:mov tl0,#6Bh
mov th0,#4Bh
acall delay
djnz r0,here
cpl P1.5
sjmp loop
delay:setb tr0
again:jnb tf0,again
clr tr0
clr tf0
ret
end
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.
for(c=0;c<5;c++)
{
lcdcmd(lcd_command[c]); //function send the first
command
}
for(d=0;d<15;d++)
{
lcddata(lcd_message[d]); //send data to line from lcd first
massage
MSDelay(30);
}
back_lite =1;
for(c=0;c<5;c++)
{
lcdcmd(lcd_command1[c]); //function send the command
for second line
}
for(d=0;d<15;d++)
{
lcddata(lcd_message1[d]); //send data to line from lcd
second massage
MSDelay(30);
}
while(1)
{
}
}
#include <reg52.H>
while(1)
{
KEY =0xfe; //ground row 0
colloc =KEY; //read coloumn
colloc &=0xf0; //mask unused bits
if(colloc !=0xf0) //coloumn detected
{
rowloc =0; //save row
break; //exit while loop
}
KEY =0xfd; //ground row 1
colloc =KEY; //read coloumn
colloc &=0xf0; //mask unused bits
if(colloc !=0xf0) //coloumn detected
{
rowloc =1; //save row
break; //exit while loop
}
KEY =0xfb; //ground row 2
colloc =KEY; //read coloumn
colloc &=0xf0; //mask unused bits
if(colloc !=0xf0) //coloumn detected
{
rowloc =2; //save row
break; //exit while loop
}
KEY =0XF7;
colloc =KEY; //read coloumn
colloc &=0xf0; //mask unused bits
rowloc =3; //save row
break; //exit while loop
}
//CHECK COLOUMN AND SEND DATA TO SERIAL PORT
if(colloc ==0xe0)
sevenseg(keypad[rowloc][0]);
else if(colloc ==0xd0)
sevenseg(keypad[rowloc][1]);
else if(colloc ==0xb0)
sevenseg(keypad[rowloc][2]);
else if(colloc ==0x70)
sevenseg(keypad[rowloc][3]);
}
}
#include <reg52.H>
#define KEY P2 //P2.0-P2.3=ROW AND P2.4-P2.7=COL
#define lcd_databus P1
sbit lcd_rs = (P0 ^ 4);
sbit lcd_rw = (P0 ^ 5);
sbit lcd_en = (P0 ^ 6);
void lcd_init();
void lcd_command(unsigned char command);
void lcd_data(unsigned char d);
//void display_data(unsigned char d);
lcd_init();
lcd_data('K');
msdelay(1);
lcd_data('E');
msdelay(1);
lcd_data('Y');
msdelay(1);
lcd_data(' ');
msdelay(1);
while(1)
{
KEY =0xfe; //ground row 0
colloc =KEY; //read coloumn
colloc &=0xf0; //mask unused bits
if(colloc !=0xf0) //coloumn detected
{
rowloc =0; //save row
break; //exit while loop
}
KEY =0xfd; //ground row 1
colloc =KEY; //read coloumn
colloc &=0xf0; //mask unused bits
if(colloc !=0xf0) //coloumn detected
{
rowloc =1; //save row
break; //exit while loop
}
KEY =0xfb; //ground row 2
colloc =KEY; //read coloumn
colloc &=0xf0; //mask unused bits
if(colloc !=0xf0) //coloumn detected
{
rowloc =2; //save row
break; //exit while loop
}
KEY =0XF7;
colloc =KEY; //read coloumn
colloc &=0xf0; //mask unused bits
rowloc =3; //save row
break; //exit while loop
}
if(colloc ==0xe0)
lcd (keypad[rowloc][0]);
else if(colloc ==0xd0)
lcd (keypad[rowloc][1]);
else if(colloc ==0xb0)
lcd (keypad[rowloc][2]);
else if(colloc ==0x70)
lcd (keypad[rowloc][3]);
}
}
void lcd_init()
{
// LCD Initialisation
lcd_rs = 1;
lcd_rw = 0;
lcd_databus = d;
lcd_en = 1;
i++; i--;
lcd_en = 0;
}
#include <reg51.H>
#define KEY P2 //P2.0-P2.3=ROW AND P2.4-P2.7=COL
void msdelay(unsigned int value);
void sertx (unsigned char);
while(1)
{
KEY =0xfe; //ground row 0
colloc =KEY; //read coloumn
colloc &=0xf0; //mask unused bits
if(colloc !=0xf0) //coloumn detected
{
rowloc =0; //save row
break; //exit while loop
}
KEY =0xfd; //ground row 1
colloc =KEY; //read coloumn
colloc &=0xf0; //mask unused bits
if(colloc !=0xf0) //coloumn detected
{
rowloc =1; //save row
break; //exit while loop
}
KEY =0xfb; //ground row 2
colloc =KEY; //read coloumn
colloc &=0xf0; //mask unused bits
if(colloc !=0xf0) //coloumn detected
{
rowloc =2; //save row
break; //exit while loop
}
KEY =0XF7;
colloc =KEY; //read coloumn
colloc &=0xf0; //mask unused bits
rowloc =3; //save row
break; //exit while loop
}
//CHECK COLOUMN AND SEND DATA TO SERIAL PORT
if(colloc ==0xe0)
sertx(keypad[rowloc][0]);
else if(colloc ==0xd0)
sertx(keypad[rowloc][1]);
else if(colloc ==0xb0)
sertx(keypad[rowloc][2]);
else if(colloc ==0x70)
sertx(keypad[rowloc][3]);
}
}
void sertx(unsigned char x)
{
SBUF =x;
while(TI==0);
TI=0;
}
void msdelay(unsigned int value)
{
unsigned int x,y;
for (x=0;x<1275;x++)
for (y=0;y<value;y++);
}
#include<reg51.h>
#define stm_databus P0 // define P0 to stm databus
sbit REV = P0^4; // single bits
sbit FRW = P0^5;
void port_initi(void); // prototyping
void stm_forward(unsigned int);
void stm_reverse(unsigned int);
void delay_ms(unsigned int value);
if(REV==0)
{
stm_reverse(500); // function call for reverse rotation
}
}
}
stm_databus =0XF5;
if(REV == 0)
{
stm_reverse(count); // for revese direction pulses sequence will
reverse
}
}
}
while(1)
{
stm_databus= 0x05;
delay_ms(count);
stm_databus = 0x09;
delay_ms(count);
stm_databus = 0x0a;
delay_ms(count);
stm_databus = 0x06;
delay_ms(count-5);
stm_databus =0XF5;
if(FRW ==0)
{
stm_forward(count);
}
}
}
Or
Stepper motor with speed and direction control
#include<reg51.h>
#define stm_databus P0 // define P0 to stm databus
sbit REV = P0^4; // single bits
sbit FRW = P0^5;
sbit DCR = P0^6;
sbit INR = P0^7;
if(REV==0)
{
stm_reverse(500); // function call for reverse rotation
}
}
}
}
if(INR==0) //increament speed check key press
{
count -= 1; //decrease counter which decrease
delay between two pulses
if(count > 450)
{
count -= 10;
}
if(count > 1000)
{
count -= 100;
}
if(count > 5000)
{
count -= 500;
}
if(count < 160)
{
count = 160;
}
}
}
}
while(1)
{
stm_databus= 0x05;
delay_ms(count);
stm_databus = 0x09;
delay_ms(count);
stm_databus = 0x0a;
delay_ms(count);
stm_databus = 0x06;
delay_ms(count-5);
stm_databus =0XF5;
if(FRW ==0)
{
stm_forward(count);
}
if(DCR ==0)
{
count += 1;
if(count>450)
{
count += 10;
}
if(count > 1000)
{
count += 100;
}
if(count > 5000)
{
count += 500;
}
}
if(INR ==0)
{
count -= 1;
if(count >450)
{
count -= 10;
}
if(count > 1000)
{
count -= 100;
}
if(count > 5000)
{
count -= 500;
}
if(count < 160)
{
count = 160;
}
}
}
}
#include<reg51.h>
sbit mtr_1 =P2^0; //single bit declaration
sbit mtr_2 =P2^1;
sbit pwm_control =P2^3;
sbit key2_REV = P2^4;
sbit key1_FRW= P2^5;
sbit speed_dec = P2^6;
sbit speed_inc = P2^7;
}
}
while(1);
}
Or
#include<reg51.h>
sbit mtr_1 =P2^0; //single bit declaration
sbit mtr_2 =P2^1;
sbit pwm_control =P2^3;
sbit key2_REV = P2^4;
sbit key1_FRW = P2^5;
sbit speed_dec = P2^6;
sbit speed_inc = P2^7;
void main(void) // main function
{
unsigned int count,value=500;
while(1)
{
if(key1_FRW == 0) // check key is pressed for forward direction
{
mtr_1 =0; // directional bits for forward
mtr_2 =1;
pwm_control=0;
for(count=0;count<= 500;count++);
}
while(1);
}