Showing posts with label Calculator using 8051. Show all posts

Program for Calculator in assembly language



This is the assembly language program for Calculator. It can perform operation like addition, subtraction, multiplication, division for one digit. 
You are just required to know about LCD and keyboard interfacing.

/************************************************************************************/
//            PROGRAM TO  DEMONSTRATE  INTERFACING OF HEX DISPLAY WITH MCS51       
/************************************************************************************/


ORG 0000H


/************************************************************************************/
/*                                           I/O PIN INITIALISATIONS AND DECLARATIONS                                                                                                                   */
/************************************************************************************/

C1 EQU P2.0
C2 EQU P2.1
C3 EQU P2.2
C4 EQU P2.3
ROW1 EQU P2.4
ROW2 EQU P2.5
ROW3 EQU P2.6
ROW4 EQU P2.7
LCD EQU P0
RS EQU P1.0
RW EQU P1.1
EN EQU P1.2
MOV LCD,#80H
ACALL CMD
ACALL DELAY
MOV LCD,#08H
ACALL CMD
ACALL DELAY
ERROR: MOV LCD,#01H
ACALL CMD
ACALL DELAY
MOV LCD,#0EH
ACALL CMD
ACALL DELAY
MOV LCD,#80H
ACALL CMD
ACALL DELAY

MOV P2,#00H
MOV P0,#00H
MOV DPTR,#MSG
CLR A
MOV B,#00H
MOV R3,#00H

MOV R0,#04
SLR: ACALL KEYPAD
DJNZ R0,SLR
MOV A,R4
CJNE R7,#'=',ERROR
CJNE R5,#'+',CO1
SJMP ADDITION
CO1: CJNE R5,#'-',CO2
SJMP SUBST
CO2: CJNE R5,#'*',CO3
SJMP MULTI
CO3: CJNE R5,#'/',ERROR
SJMP DIVIDE

SUBST: SUBB A,R6
JC NEGATIVE
JZ ZERO
SACK: SJMP GOM
NEGATIVE: MOV LCD,#'-'
ACALL DISP
ACALL DELAY
MOV A,R6
SUBB A,R4
INC A
SJMP SACK
ZERO: MOV LCD,#'0'
ACALL DISP
ACALL DELAY
SJMP ALT

MULTI: MOV B,R6
MUL AB
MOV R6,A
SUBB A,#10
JC STAR3
MOV A,R6
MOV B,#10
DIV AB
MOV DPTR,#MSG
MOVC A,@A+DPTR
MOV LCD,A
ACALL DISP
ACALL DELAY
MOV A,B
SJMP GOM

STAR3: MOV A,R6
MOV DPTR,#MSG
MOVC A,@A+DPTR
MOV LCD,A
ACALL DISP
ACALL DELAY
SJMP ALT

DIVIDE: MOV B,R6
DIV AB
MOV DPTR,#MSG
MOVC A,@A+DPTR
MOV LCD,A
ACALL DISP
ACALL DELAY
MOV LCD,#' '
ACALL DISP
ACALL DELAY
MOV A,B
SJMP GOM

ADDITION: ADD A,R6
MOV R6,A
SUBB A,#10
JC STAR
JZ STAR2
MOV LCD,#'1'
ACALL DISP
ACALL DELAY
GOM: MOV DPTR,#MSG
MOVC A,@A+DPTR
MOV LCD,A
ACALL DISP
ACALL DELAY
SJMP ALT

STAR2: MOV LCD,#'1'
ACALL DISP
ACALL DELAY
MOV LCD,#'0'
ACALL DISP
ACALL DELAY
SJMP ALT

STAR: MOV A,R6
MOV DPTR,#MSG
MOVC A,@A+DPTR
MOV LCD,A
ACALL DISP
ACALL DELAY
ALT: SJMP ALT

/************************************************************************************/
/*                                           READING COLUMN VALUE                                                                                                                                                                                                          */
/************************************************************************************/


KEYPAD: ACALL DELAY           
       MOV P2, #0FH
                   ACALL DELAY
K1:             MOV A,P2
                   CJNE A,#0FH,KK1
                   SJMP K1
KK1:   JB C1,N1
                   MOV B,#00h
                   ACALL DELAY
N1:            JB C2,N2
                   MOV B,#01h
                   ACALL DELAY
N2:            JB C3,N3
                   MOV B,#02h
                   ACALL DELAY
N3:            JB C4,N4
                   MOV B,#03h
                   ACALL DELAY

/************************************************************************************/
/*                                           READING ROW VALUE                                                                                                                                                                                                                   */
/************************************************************************************/
                              
N4:   MOV P2, #0F0H
K11:  MOV A,P2
                  CJNE A,#0F0H,KK11
                  SJMP K11
KK11: ACALL DELAY
                  JB ROW1,NR1
                  MOV R3,#00h
                  ACALL DELAY
NR1:  JB ROW2,NR2
                  MOV R3,#04h
                  ACALL DELAY
NR2:  JB ROW3,NR3
                  MOV R3,#08h
                  ACALL DELAY
NR3:  JB ROW4,NR4
                  MOV R3,#0Ch
                  ACALL DELAY
NR4:  MOV A,R3

/************************************************************************************/
/*                           SENDING KEYVALUE TO PORT 0 AND SERAIL PORT                                                                                                                                            */
/************************************************************************************/
                 
                  ADD A,B
                  CJNE R0,#04,SL1
                  MOV R4,A
SL1:  CJNE R0,#02,SL2
                  MOV R6,A

                 
SL2:  MOVC A,@A+DPTR
      CJNE R0,#03,SL3
                  MOV R5,A
SL3:  CJNE R0,#01,SL4
      MOV R7,A
SL4:  MOV LCD,A
                  ACALL DISP
                  ACALL DELAY
                  CLR A
                  RET

/************************************************************************************/
/*                                           DELAY SUBROUTINE                                                                                                                                                                                                                       */
/************************************************************************************/


DELAY:  MOV R2,#5FH
a1:     MOV R1,#0FEH
a2:     DJNZ R1,a2
        DJNZ R2,a1
                    RET

CMD:     CLR RS
        CLR RW
                                SETB EN
                                NOP
                                CLR EN
                                RET
DISP:   SETB RS
        CLR RW
                                SETB EN
                                NOP
                                CLR EN
                                RET

/************************************************************************************/
/*                                           LOOKUP TABLE W.R.T 0-15 SWITCHES IN HEX KEYPAD                                                                                                     */
/************************************************************************************/

MSG: DB '0','1','2','3','4','5','6','7','8','9','+','-','*','/','=','F',0
      END


If you have any problem then comment!!!!!!!!!!!!!!

Simple calculator program in assembly



org 00h
dig_ctrl4 equ p1.0
dig_ctrl3 equ p1.1
dig_ctrl2 equ p1.2
dig_ctrl1 equ p1.3
mov dptr,#200h
mov r0,#00d
ljmp main
org 000Bh
mov tl0,#36h
mov th0,#0f6h
mov p2,#0ffh
clr dig_ctrl1
clr dig_ctrl2
clr dig_ctrl3
clr dig_ctrl4
inc r0
mov a,r0
mov b,#4
div ab
mov r0,b
cjne r0,#00,b1      ;check for remainder
mov dptr,#200h
mov a,r1
movc a,@a+dptr
mov p2,a
setb dig_ctrl1
sjmp last
b1: cjne r0,#01,b2
mov dptr,#200h
mov a,r2
movc a,@a+dptr
mov p2,a
setb dig_ctrl2
sjmp last
b2: cjne r0,#02,b3
mov dptr,#200h
mov a,r3
movc a,@a+dptr
mov p2,a
setb dig_ctrl3
sjmp last
b3: cjne r0,#03,last
mov dptr,#200h
mov a,r4
movc a,@a+dptr
mov p2,a
setb dig_ctrl4
last:
reti


main: mov tmod,#11h
mov tl0,#36h
mov th0,#0f6h
mov ie,#82h
setb tr0
again: mov r4,#00
l4: mov r3,#00
l3: mov r2,#00
l2: mov r1,#00
l1: acall delay
inc r1
cjne r1,#10,l1
inc r2
cjne r2,#10,l2
inc r3
cjne r3,#10,l3
inc r4
cjne r4,#10,l4
sjmp again

delay: mov r5,#20                            ;initialize counter for delay
rpt: mov tl1,#0fdh           ;initialising timer for delay
mov th1,#4bh
setb tr1
here: jnb tf1,here
clr tr1
clr tf1
djnz r5,rpt
ret

org 200h
db 40h,0f9h,24h,30h,19h,12h,02h,0f8h,00h,10h
end



If you are facing any problem in this program or any kind of doubt then comment please


Calculator project using 8051



It is an interesting project. You can implement it. its very simple to implement. The most important thing you should know that about the lcd and keyboard interfacing. I have just interfaced keyboard with port 2 and lcd with port 0.
It can add and display the result of one digit.


CLICK HERE TO SEE THE ASSEMBLY LANGUAGE PROGRAM FOR CALCULATOR

- Copyright © Microcontroller Projects -Metrominimalist- Powered by Blogger - Designed by Johanes Djogan -