Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;Julie Hui
- ;2/8/11
- ;6.115 Leeb
- ;Lab 1 Ex.6
- ;supposed to:
- ;makes a simple calculator using the keypad
- ;adds/subtracts two 3-digit numbers and displays the sum
- ;instead does this:
- ;takes and displayes two 3-digit number from keypad
- ;gets and displays +/- on screen
- ;gives out 099 as answer for anything...
- org 00h ;power up and reset vector
- ljmp start ;when the micro wakes up, jump to the beginning
- ;of the main body or loop in the program, called "start"
- org 100h ;Start the serial port by calling subroutine "init"
- start:
- lcall init ;Start the serial port by calling subroutine "init".
- clr P3.2 ;set Output enable on low
- loop:
- lcall storenums ;store the ASCII code for each key into R5
- lcall createnums ;creates the two numbers input from the keypad
- lcall addorsub ;waits for +/- and runs getsum or getsub
- lcall displaysum ;displays sum on monitor
- sjmp loop
- init:
- ;set up serial port with a 11.0592 MHz crystal,
- ;use timer 1 for 9600 baud serial communications
- mov tmod, #00100000b ;set timer 1 for auto reload-mode 2
- mov tcon, #01000000b ;run counter 1
- mov th1, #0fdh ;set 9600 baud with xtal=11.059mhz
- mov scon, #01010000b ;set serial control reg for 8 bit data and mode 1
- ret
- getnum:
- ;this routine "gets" or receives a character from the PC, transmitted over
- ;the serial port. RI is the same as SCON.0 - the assembler recognizes
- ;either shorthand. The 7-bit ASCII code is returned in the accumulator.
- ;STORES THE ASCII CODE FROM KEYPAD INTO ACC
- jnb P3.3, getnum ;wait till character received
- pop DPH ;pop getnum address out of stack
- pop DPL
- mov a, P1 ;move byte from P1 into acc
- anl a, #0fh ;mask off 5th, 6th, 7th, 8th bit
- loop1: ;only get digit once
- jb P3.3,loop1
- push DPL ;pop getnum address back to stack
- push DPH
- ret
- getkey:
- ;gets the correct value of the key and stores it in ACC
- pop DPH ;pop getkey address out of stack
- pop DPL
- push DPL ;pop getnum address back to stack
- push DPH
- inc a ;increase a by 1 byte
- inc a ;increase a by 1 byte
- movc a,@a+pc ;gets correct keypad ASCII value for each key
- mov R5,a ;STORES CORRECT ASCII KEY VALUE INTO R5
- ret
- db 31h, 32h, 33h, 2Bh, 34h, 35h, 36h, 2Dh, 37h, 38h, 39h, 43h, 2Ah, 30h, 23h, 44h
- sndnum:
- ;this routine "sends" or transmits a number to the PC, using the serial
- ;port. The character to be sent is stored in the accumulator. SCON.1 and
- ;TI are the same as far as the assembler is concerned
- ;saves the ASCII code for the key in a and sbuf
- pop DPH ;pop sndnum address out of stack
- pop DPL
- clr scon.1 ;clear the ti complete flag
- mov sbuf,R5 ;move a number from R5 to the sbuf, ASCII KEY VALUE STORED IN SBUF AND R5
- txloop:
- jnb scon.1, txloop ;wait till chr is sent
- push DPL ;pop sndnum address back to stack
- push DPH
- ret
- sndchr1:
- ;this routine "sends" or transmits a character to the PC, using the serial
- ;port. The character to be sent is stored in the accumulator. SCON.1 and
- ;TI are the same as far as the assembler is concerned
- clr scon.1 ;clear the ti complete flag.
- txloop1:
- jnb scon.1, txloop1 ;wait till chr is sent
- ret
- crlf:
- ;carraige return and linefeed
- mov sbuf, #00h
- mov sbuf, #0Ah ;moves the ASCII value 10 into sbuf causing a linefeed operation
- lcall sndchr1
- mov sbuf, #0Dh
- lcall sndchr1
- ret
- storenums:
- ;moves each digit typed on the keyboard to the stack
- pop ACC ;pops storenums address out of stack using ACC
- mov R4,A ;stores part of adress in R4
- clr A
- pop ACC
- mov R3, A ;stores part of address in R3
- clr A
- mov R2,#03h ;moves the value of 3 into R2
- loopsn1: ;gets first 3 digit number from keypad then exits
- lcall getnum ;gets digit from keyboard
- lcall getkey ;gets correct AsCII code for key
- lcall sndnum ;send digit back to monitor
- clr A
- mov A,sbuf ;moves correct ASCII key code in sbuf to ACC
- mov R1,#30h ;moves 48 into R1
- subb A,R1 ;subtracts 48 from the ASCII code to get the real digit value and stores sum in A
- mov R1,#00h ;clear R1
- push ACC ;push real value of input digit into stack
- djnz R2,loopsn1 ;reduced value of R2 by 1. when R2 is 0, exits loop
- lcall crlf
- mov R2,#00h ;clear R2
- mov R2,#03h ;move the value 2 into R2
- loopsn2: ;gets second 3 digit number from keypad then exits
- lcall getnum ;gets digit from keyboard
- lcall getkey ;gets correct AsCII code for key
- lcall sndnum ;send digit back to monitor
- clr A ;clears A
- mov A,sbuf ;moves key ASCII value from sbut into A
- mov R1,#30h ;moves 48 into R1
- subb A,R1 ;subtracts 48 from the ASCII code of digit to get real digit value
- mov R1,#00h ;clear R1
- push ACC ;push key real value of input digit into stack
- djnz R2,loopsn2 ;reduces R2 value by 1. When R2=0, exits loop
- lcall crlf
- mov A,R3 ;pops subroutine addresses back into stack
- push ACC
- mov A,R4
- push ACC
- ret
- createnums:
- ;create the two 3-digit numbers and store each in a byte in the memory
- ;second number entered
- pop DPH
- pop DPL
- clr ACC
- pop ACC
- mov R2,#00h
- mov R2,A ;gets second 1x-digit from stack and store in R2
- pop ACC ;gets second 10xdigit and store in ACC
- mov B,#0Ah ;moves value of 10 into B
- mul AB ;multiply second 10x digit by 10 and stores in A
- add A,R2 ;add second 10 digit to second 1 digit and store in A
- mov R2,#00h ;clear R2
- mov R2,A ;move second 10x+1x digit
- mov B,#00h ;clears B
- clr A ;clears A
- pop ACC ;pops second 100x value into ACC
- mov B,#64h ;moves value 100 into B
- mul AB ;multiplies second 100x value by 100 and stores it in ACC
- add A,R2 ;add 100x value to 10x+1x
- mov R2,#00h ;clears R2
- mov B,#00h ;clears B
- mov R2,A ;STORE SECOND NUMBER IN R2
- clr A ;clear A
- ;first number entered
- pop ACC ;store first 1x value in ACC
- mov R3, #00h ;clears R3
- mov R3,A ;store first 1x value in R3 by moving it from ACC
- pop ACC ;store first 10x value in ACC
- mov B,#0Ah ;moves value of 10 into B
- mul AB ;multiply second 10x digit by 10 and stores in A
- add A,R3 ;add second 10 digit to second 1 digit and store in A
- mov R3,#00h ;clears R3
- mov R3,A ;moves added value to R3
- mov B,#00h ;clear B
- clr A ;clear A
- pop ACC ;pop first 100x value into ACC
- mov B,#64h ;move value 100 into B
- mul AB ;multiply first 100x value by 100 and store in A
- add A,R3 ;add first 100x value to first 10x+1x and store sum in A
- mov R3,#00h ;clear R3
- mov R3,A ;STORE FIRST NUMBER IN R3
- mov B,#00h ;clear B
- clr A ;clear A
- push DPL ;pushes createnums addresses back into stack
- push DPH
- ret
- addorsub:
- ;adds two numbers if user inputs +, subtracts if input is -
- lcall getnum ;gets +(A) or -(B) from keypad
- lcall getkey ;gets the correct ASCII value key pressed
- lcall sndnum ;sends + or - to screen
- mov R6,#00h
- mov R6,sbuf
- lcall crlf
- cjne R6,#2Bh, getsub ;if P1.1 bit is set, run getsum
- lcall getsum ;if P1.2 bit is set, run getsub
- ret
- getsum:
- ;gets the sum of the two numbers entered and stores the answer in A
- clr A ;clear ACC
- mov B,#00h ;clear B
- mov A,R2 ;move second number into A
- mov B,R3 ;move first number into B
- add A,B ;add first and second number and store result in ACC
- ret
- getsub:
- ;subtracts the first number from the second number and stores the answer in A
- clr A ;clear ACC
- mov B,#00h ;clear B
- mov A,R3 ;move first number into ACC
- mov B,R2 ;move second number into B
- subb A,B ;subtract second number from first number
- ret
- displaysum:
- mov B,#64h ;move #100 into B
- div AB ;divide answer by 100 and store 100x value into A
- mov R2,A ;move 100x value in R2
- clr A ;clear A
- mov A,B ;move remainder of past division, 10x+1x value into A
- mov B,#0Ah ;move #10 into B
- div AB ;divide 10x+1x value by #10 to store 10x value into A
- mov R3,A ;store 10x value into R3
- clr A ;clear A
- mov A,B ;move 1x value into A
- mov R4,A ;store 1x value in R4
- mov R1,#30h ;move #48 into R1 because
- mov A,R2 ;move 100x value into A
- add A,R1 ;add 100x to 48 to get ASCII code for that value and store sum in ACC
- clr scon.1 ;clear the ti complete flag.
- mov sbuf,a ;move ASCII code for 100x value to sbuf
- txloop6:
- jnb scon.1, txloop6 ;wait till digit is sent
- mov R1,#30h ;move #48 into R1
- mov A,R3 ;move 10x value into ACC
- add A,R1 ;add 48 to 10x value to get ASCII code for 10x value and store sum in ACC
- clr scon.1 ;clear the ti complete flag.
- mov sbuf,a ;move 10x ASCII code into sbuf
- txloop7:
- jnb scon.1, txloop7 ;wait till digit is sent
- mov R1,#30h ;move #48 into R1
- mov A,R4 ;move 1x value into A
- add A,R1 ;add 48 to 1x value to get ASCII code for 1x value and store sum in ACC
- clr scon.1 ;clear the ti complete flag.
- mov sbuf,a ;move 1x value into sbuf
- txloop8:
- jnb scon.1, txloop8 ;wait till chr is sent
- lcall crlf
- ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement