Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;Julie Hui
  2. ;2/8/11
  3. ;6.115 Leeb
  4. ;Lab 1 Ex.6
  5.  
  6. ;supposed to:
  7. ;makes a simple calculator using the keypad
  8. ;adds/subtracts two 3-digit numbers and displays the sum
  9.  
  10. ;instead does this:
  11. ;takes and displayes two 3-digit number from keypad
  12. ;gets and displays +/- on screen
  13. ;gives out 099 as answer for anything...
  14.  
  15.  
  16. org 00h                             ;power up and reset vector
  17.     ljmp start                      ;when the micro wakes up, jump to the beginning
  18.                                     ;of the main body or loop in the program, called "start"
  19. org 100h                            ;Start the serial port by calling subroutine "init"
  20. start: 
  21.     lcall init                      ;Start the serial port by calling subroutine "init".
  22.     clr P3.2                        ;set Output enable on low
  23.     loop:
  24.         lcall storenums             ;store the ASCII code for each key into R5
  25.         lcall createnums            ;creates the two numbers input from the keypad
  26.         lcall addorsub              ;waits for +/- and runs getsum or getsub
  27.         lcall displaysum            ;displays sum on monitor
  28.     sjmp loop
  29.    
  30. init:
  31. ;set up serial port with a 11.0592 MHz crystal,
  32. ;use timer 1 for 9600 baud serial communications   
  33.     mov tmod, #00100000b            ;set timer 1 for auto reload-mode 2
  34.     mov tcon, #01000000b            ;run counter 1
  35.     mov th1, #0fdh                  ;set 9600 baud with xtal=11.059mhz
  36.     mov scon, #01010000b            ;set serial control reg for 8 bit data and mode 1
  37.     ret
  38.  
  39. getnum:
  40. ;this routine "gets" or receives a character from the PC, transmitted over
  41. ;the serial port. RI is the same as SCON.0 - the assembler recognizes
  42. ;either shorthand. The 7-bit ASCII code is returned in the accumulator.
  43. ;STORES THE ASCII CODE FROM KEYPAD INTO ACC
  44.     jnb P3.3, getnum                ;wait till character received
  45.     pop DPH                         ;pop getnum address out of stack
  46.     pop DPL
  47.     mov a, P1                       ;move byte from P1 into acc
  48.     anl a, #0fh                     ;mask off 5th, 6th, 7th, 8th bit
  49.     loop1:                          ;only get digit once
  50.         jb P3.3,loop1
  51.     push DPL                            ;pop getnum address back to stack
  52.     push DPH
  53.     ret
  54.    
  55. getkey:
  56. ;gets the correct value of the key and stores it in ACC
  57.     pop DPH                         ;pop getkey address out of stack
  58.     pop DPL
  59.     push DPL                        ;pop getnum address back to stack
  60.     push DPH
  61.     inc a                           ;increase a by 1 byte
  62.     inc a                           ;increase a by 1 byte
  63.     movc a,@a+pc                    ;gets correct keypad ASCII value for each key
  64.     mov R5,a                        ;STORES CORRECT ASCII KEY VALUE INTO R5
  65.     ret
  66.     db 31h, 32h, 33h, 2Bh, 34h, 35h, 36h, 2Dh, 37h, 38h, 39h, 43h, 2Ah, 30h, 23h, 44h
  67.    
  68. sndnum:
  69. ;this routine "sends" or transmits a number to the PC, using the serial
  70. ;port. The character to be sent is stored in the accumulator. SCON.1 and
  71. ;TI are the same as far as the assembler is concerned
  72. ;saves the ASCII code for the key in a and sbuf
  73.     pop DPH                         ;pop sndnum address out of stack
  74.     pop DPL
  75.     clr scon.1                      ;clear the ti complete flag
  76.     mov sbuf,R5                     ;move a number from R5 to the sbuf, ASCII KEY VALUE STORED IN SBUF AND R5
  77.     txloop:
  78.         jnb scon.1, txloop          ;wait till chr is sent
  79.     push DPL                            ;pop sndnum address back to stack
  80.     push DPH
  81.     ret
  82.  
  83. sndchr1:
  84. ;this routine "sends" or transmits a character to the PC, using the serial
  85. ;port. The character to be sent is stored in the accumulator. SCON.1 and
  86. ;TI are the same as far as the assembler is concerned
  87.     clr scon.1                  ;clear the ti complete flag.
  88. txloop1:
  89.     jnb scon.1, txloop1         ;wait till chr is sent
  90.     ret
  91.    
  92. crlf:
  93. ;carraige return and linefeed
  94.     mov sbuf, #00h
  95.     mov sbuf, #0Ah              ;moves the ASCII value 10 into sbuf causing a linefeed operation
  96.     lcall sndchr1
  97.     mov sbuf, #0Dh
  98.     lcall sndchr1
  99.     ret
  100.  
  101. storenums:
  102. ;moves each digit typed on the keyboard to the stack
  103.     pop ACC                         ;pops storenums address out of stack using ACC
  104.     mov R4,A                        ;stores part of adress in R4
  105.     clr A
  106.     pop ACC
  107.     mov R3, A                       ;stores part of address in R3
  108.     clr A
  109.    
  110.     mov R2,#03h                     ;moves the value of 3 into R2
  111.     loopsn1:                        ;gets first 3 digit number from keypad then exits
  112.         lcall getnum                ;gets digit from keyboard
  113.         lcall getkey                ;gets correct AsCII code for key
  114.         lcall sndnum                ;send digit back to monitor
  115.         clr A
  116.         mov A,sbuf                  ;moves correct ASCII key code in sbuf to ACC
  117.         mov R1,#30h                 ;moves 48 into R1
  118.         subb A,R1                   ;subtracts 48 from the ASCII code to get the real digit value and stores sum in A
  119.         mov R1,#00h                 ;clear R1
  120.         push ACC                    ;push real value of input digit into stack                 
  121.         djnz R2,loopsn1             ;reduced value of R2 by 1. when R2 is 0, exits loop
  122.        
  123.     lcall crlf
  124.  
  125.        
  126.     mov R2,#00h                     ;clear R2                  
  127.     mov R2,#03h                     ;move the value 2 into R2
  128.     loopsn2:                        ;gets second 3 digit number from keypad then exits
  129.         lcall getnum                ;gets digit from keyboard
  130.         lcall getkey                ;gets correct AsCII code for key
  131.         lcall sndnum                ;send digit back to monitor
  132.         clr A                       ;clears A
  133.         mov A,sbuf                  ;moves key ASCII value from sbut into A        
  134.         mov R1,#30h                 ;moves 48 into R1
  135.         subb A,R1                   ;subtracts 48 from the ASCII code of digit to get real digit value
  136.         mov R1,#00h                 ;clear R1
  137.         push ACC                    ;push key real value of input digit into stack
  138.         djnz R2,loopsn2             ;reduces R2 value by 1. When R2=0, exits loop
  139.    
  140.     lcall crlf
  141.        
  142.     mov A,R3                        ;pops subroutine addresses back into stack
  143.     push ACC
  144.     mov A,R4
  145.     push ACC
  146.     ret
  147.    
  148. createnums:
  149. ;create the two 3-digit numbers and store each in a byte in the memory                 
  150.     ;second number entered
  151.     pop DPH
  152.     pop DPL
  153.    
  154.     clr ACC
  155.     pop ACC
  156.     mov R2,#00h
  157.     mov R2,A            ;gets second 1x-digit from stack and store in R2
  158.    
  159.     pop ACC             ;gets second 10xdigit and store in ACC                 
  160.     mov B,#0Ah          ;moves value of 10 into B                          
  161.     mul AB              ;multiply second 10x digit by 10 and stores in A
  162.     add A,R2            ;add second 10 digit to second 1 digit and store in A
  163.     mov R2,#00h         ;clear R2
  164.     mov R2,A            ;move second 10x+1x digit
  165.     mov B,#00h          ;clears B
  166.     clr A               ;clears A
  167.      
  168.     pop ACC             ;pops second 100x value into ACC           
  169.     mov B,#64h          ;moves value 100 into B
  170.     mul AB              ;multiplies second 100x value by 100 and stores it in ACC
  171.     add A,R2            ;add 100x value to 10x+1x
  172.     mov R2,#00h         ;clears R2                         
  173.     mov B,#00h          ;clears B
  174.     mov R2,A            ;STORE SECOND NUMBER IN R2
  175.     clr A               ;clear A   
  176.  
  177.     ;first number entered
  178.     pop ACC             ;store first 1x value in ACC
  179.     mov R3, #00h        ;clears R3
  180.     mov R3,A            ;store first 1x value in R3 by moving it from ACC
  181.    
  182.     pop ACC             ;store first 10x value in ACC              
  183.     mov B,#0Ah          ;moves value of 10 into B          
  184.     mul AB              ;multiply second 10x digit by 10 and stores in A
  185.     add A,R3            ;add second 10 digit to second 1 digit and store in A
  186.     mov R3,#00h         ;clears R3
  187.     mov R3,A            ;moves added value to R3
  188.     mov B,#00h          ;clear B
  189.     clr A               ;clear A
  190.      
  191.     pop ACC             ;pop first 100x value into ACC             
  192.     mov B,#64h          ;move value 100 into B
  193.     mul AB              ;multiply first 100x value by 100 and store in A
  194.     add A,R3            ;add first 100x value to first 10x+1x and store sum in A
  195.     mov R3,#00h         ;clear R3
  196.     mov R3,A            ;STORE FIRST NUMBER IN R3              
  197.     mov B,#00h          ;clear B
  198.     clr A               ;clear A
  199.  
  200.     push DPL            ;pushes createnums addresses back into stack
  201.     push DPH
  202.     ret
  203.    
  204. addorsub:
  205. ;adds two numbers if user inputs +, subtracts if input is -
  206.     lcall getnum                    ;gets +(A) or -(B) from keypad
  207.     lcall getkey                    ;gets the correct ASCII value key pressed
  208.     lcall sndnum                    ;sends + or - to screen
  209.    
  210.     mov R6,#00h
  211.     mov R6,sbuf
  212.  
  213.     lcall crlf
  214.    
  215.     cjne R6,#2Bh, getsub                    ;if P1.1 bit is set, run getsum
  216.     lcall getsum                    ;if P1.2 bit is set, run getsub
  217.     ret
  218.  
  219.    
  220. getsum:
  221. ;gets the sum of the two numbers entered and stores the answer in A
  222.     clr A                   ;clear ACC
  223.     mov B,#00h              ;clear B
  224.     mov A,R2                ;move second number into A
  225.     mov B,R3                ;move first number into B
  226.     add A,B                 ;add first and second number and store result in ACC
  227.     ret
  228.    
  229. getsub:
  230. ;subtracts the first number from the second number and stores the answer in A
  231.     clr A                   ;clear ACC
  232.     mov B,#00h              ;clear B
  233.     mov A,R3                ;move first number into ACC
  234.     mov B,R2                ;move second number into B
  235.     subb A,B                ;subtract second number from first number
  236.     ret
  237.    
  238.    
  239. displaysum:
  240.     mov B,#64h                  ;move #100 into B
  241.     div AB                      ;divide answer by 100 and store 100x value into A
  242.     mov R2,A                    ;move 100x value in R2
  243.     clr A                       ;clear A
  244.     mov A,B                     ;move remainder of past division, 10x+1x value into A
  245.     mov B,#0Ah                  ;move #10 into B
  246.     div AB                      ;divide 10x+1x value by #10 to store 10x value into A
  247.     mov R3,A                    ;store 10x value into R3
  248.     clr A                       ;clear A
  249.     mov A,B                     ;move 1x value into A
  250.     mov R4,A                    ;store 1x value in R4
  251.    
  252.     mov R1,#30h                 ;move #48 into R1 because
  253.     mov A,R2                    ;move 100x value into A
  254.     add A,R1                    ;add 100x to 48 to get ASCII code for that value and store sum in ACC
  255.     clr scon.1                  ;clear the ti complete flag.
  256.     mov sbuf,a                  ;move ASCII code for 100x value to sbuf
  257.     txloop6:
  258.         jnb scon.1, txloop6     ;wait till digit is sent
  259.        
  260.     mov R1,#30h                 ;move #48 into R1
  261.     mov A,R3                    ;move 10x value into ACC
  262.     add A,R1                    ;add 48 to 10x value to get ASCII code for 10x value and store sum in ACC
  263.     clr scon.1                  ;clear the ti complete flag.
  264.     mov sbuf,a                  ;move 10x ASCII code into sbuf
  265.     txloop7:
  266.         jnb scon.1, txloop7     ;wait till digit is sent
  267.    
  268.     mov R1,#30h                 ;move #48 into R1
  269.     mov A,R4                    ;move 1x value into A
  270.     add A,R1                    ;add 48 to 1x value to get ASCII code for 1x value and store sum in ACC
  271.     clr scon.1                  ;clear the ti complete flag.
  272.     mov sbuf,a                  ;move 1x value into sbuf
  273.     txloop8:
  274.         jnb scon.1, txloop8     ;wait till chr is sent
  275.    
  276.     lcall crlf
  277.     ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement