binary calculator using PIC16F887 coding in machine language

Discussion in 'Embedded Systems and Microcontrollers' started by kadavie, Dec 16, 2012.

  1. kadavie

    Thread Starter New Member

    Dec 16, 2012
    1
    0
    my calculator is meant to take in two button inputs from the user after rotating leds and and add and subtract them, but it doesnt seem to be working properly can anyone help me fix it??

    Code ( (Unknown Language)):
    1.  
    2. #include <p16F887.inc>
    3.  
    4. __CONFIG _CONFIG1, _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT
    5. __CONFIG _CONFIG2, _WRT_OFF & _BOR21V
    6.  
    7.     ORG     0x00
    8.     GOTO    START
    9.  
    10.        cblock 0x20    
    11.                 COUNTER1         ; define a variable to hold a counter value
    12.                 DELAY1
    13.                 VALUE
    14.      endc          
    15.  
    16. START:
    17.                
    18.     CLRF        COUNTER1         ;initialise value of variable
    19.     MOVLW        B'00000100'        
    20.     MOVWF        DELAY1             ;initialise value of variable
    21.     CLRF        VALUE            ;initialise value of variable
    22.     BANKSEL     ANSELH           ;select bank3  
    23.     CLRF        ANSELH            ; PortB pins are digitial (important as RB0 is switch)
    24.  
    25.     BANKSEL        TRISA             ;select bank1
    26.     MOVLW         0xFF             ; b'11111111'
    27.     MOVWF         TRISA            ; Make PortA all input
    28.      MOVLW         0x01             ; Bit one to work register
    29.     MOVWF         TRISB            ; Make RBO pin input (switch)
    30.     CLRF        TRISD            ; Make TRISD all output
    31.  
    32.  
    33.     BANKSEL        PORTD             ;select bank0
    34.     CLRF        PORTD            ;clear value in PORTD
    35.     BCF             PIR1,0              ;Clears Flag.
    36.     MOVLW        0X31
    37.     MOVWF        T1CON              ;Enables Timer_1
    38.     CLRF        TMR1H            ; Clear timer1 counter
    39.     CLRF        TMR1L            ; Clear timer1 counter
    40.    
    41. INPUT1:
    42.     INCF          COUNTER1         ; Increase counter value
    43.     MOVF          COUNTER1,W       ; move value in counter to work register
    44.     MOVWF         PORTD            ; move value in work register to PORTD register
    45.     BTFSS        PORTB,0
    46.     GOTO        NEXT_IN
    47.     CALL        DELAY
    48.     MOVF        PORTD,W
    49.     XORLW        B'00000111'
    50.     BTFSS        STATUS,Z
    51.     GOTO         INPUT1
    52.     CLRF        COUNTER1
    53.     GOTO        INPUT1
    54.  
    55. NEXT_IN:
    56.     MOVF        PORTD,W
    57.     MOVWF        VALUE
    58.     CLRF        PORTD
    59.     CALL        DELAY
    60. INPUT2:
    61.     INCF          COUNTER1         ; Increase counter value
    62.     MOVF          COUNTER1,W       ; move value in counter to work register
    63.     MOVWF         PORTD            ; move value in work register to PORTD register
    64.     BTFSS        PORTB,0
    65.     GOTO        ADDITION
    66.     CALL        DELAY
    67.     MOVF        PORTD,W
    68.     XORLW        B'00000111'
    69.     BTFSS        STATUS,Z
    70.     GOTO         INPUT2
    71.     CLRF        COUNTER1
    72.     GOTO        INPUT2
    73.  
    74. ADDITION:
    75.     MOVF        PORTD,W
    76.     ADDLW        VALUE
    77.     MOVWF        PORTD
    78.     CALL        DELAY    
    79.     CALL        DELAY
    80.     CLRF        PORTD
    81.     GOTO        INPUT3
    82.  
    83. INPUT3:
    84.     INCF          COUNTER1         ; Increase counter value
    85.     MOVF          COUNTER1,W       ; move value in counter to work register
    86.     MOVWF         PORTD            ; move value in work register to PORTD register
    87.     BTFSS        PORTB,0
    88.     GOTO        NEXT_IN_2
    89.     CALL        DELAY
    90.     MOVF        PORTD,W
    91.     MOVWF        VALUE
    92.     XORLW        B'00000111'
    93.     BTFSS        STATUS,Z
    94.     GOTO         INPUT3
    95.     CLRF        COUNTER1
    96.     GOTO        INPUT3
    97.  
    98. NEXT_IN_2:
    99.     MOVF        PORTD,W
    100.     MOVWF        VALUE
    101.     CLRF        PORTD
    102.  
    103. INPUT4:
    104.     INCF          COUNTER1         ; Increase counter value
    105.     MOVF          COUNTER1,W       ; move value in counter to work register
    106.     MOVWF         PORTD            ; move value in work register to PORTD register
    107.     BTFSS        PORTB,0
    108.     GOTO        SUBTRACTION
    109.     CALL        DELAY
    110.     MOVF        PORTD,W
    111.     XORLW        B'00000111'
    112.     BTFSS        STATUS,Z
    113.     GOTO         INPUT4
    114.     CLRF        COUNTER1
    115.     GOTO        INPUT4
    116.  
    117. SUBTRACTION:
    118.     MOVF        PORTD,W
    119.     ADDLW        VALUE
    120.     MOVWF        PORTD
    121.     CALL        DELAY    
    122.     CALL        DELAY
    123.     CLRF        PORTD
    124.     GOTO        INPUT1
    125.  
    126. DELAY:
    127.  
    128.     BTFSS        PIR1,0             ;Skip if SET/1
    129.     GOTO        DELAY
    130.     BCF             PIR1,0
    131.     DECFSZ        DELAY1
    132.     GOTO         DELAY
    133.     MOVLW        b'00000100'
    134.     MOVWF        DELAY1
    135.     RETURN
    136.  
    137. END
    138.  
     
    Last edited by a moderator: Dec 16, 2012
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Take a look at the first post here http://forum.allaboutcircuits.com/showthread.php?t=44852 Take a look at these links
    Introduction to the MPLAB SIM Software Simulator
    Using Asynchronous Stimulus with the MPLAB Simulator
    Using Stimulus for Algorithm Verification with the MPLAB IDE Simulator
     
  3. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,012
    That is your job. Folks here will help but they won't do your job for you. You are going to at least narrow your problem down to a line or two. Learn to use your debugger and debug your code just like any engineer or hobbyist would do.
     
Loading...