Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

binary calculator using PIC16F887 coding in machine language

Status
Not open for further replies.

kadavie

Newbie level 1
Joined
Dec 16, 2012
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,308
the calculator is supposed to take in a switch input by pressing the switch while rotating the leds on the pic then first it adds then it subtracts the code doesnt seem to be detecting my switch press, the code is as shown below:


#include <p16F887.inc>

__CONFIG _CONFIG1, _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT
__CONFIG _CONFIG2, _WRT_OFF & _BOR21V

ORG 0x00
GOTO START

cblock 0x20
COUNTER1 ; define a variable to hold a counter value
DELAY1
VALUE
endc

START:

CLRF COUNTER1 ;initialise value of variable
MOVLW B'00000100'
MOVWF DELAY1 ;initialise value of variable
CLRF VALUE ;initialise value of variable
BANKSEL ANSELH ;select bank3
CLRF ANSELH ; PortB pins are digitial (important as RB0 is switch)

BANKSEL TRISA ;select bank1
MOVLW 0xFF ; b'11111111'
MOVWF TRISA ; Make PortA all input
MOVLW 0x01 ; Bit one to work register
MOVWF TRISB ; Make RBO pin input (switch)
CLRF TRISD ; Make TRISD all output


BANKSEL PORTD ;select bank0
CLRF PORTD ;clear value in PORTD
BCF PIR1,0 ;Clears Flag.
MOVLW 0X31
MOVWF T1CON ;Enables Timer_1
CLRF TMR1H ; Clear timer1 counter
CLRF TMR1L ; Clear timer1 counter

INPUT1:
INCF COUNTER1 ; Increase counter value
MOVF COUNTER1,W ; move value in counter to work register
MOVWF PORTD ; move value in work register to PORTD register
BTFSS PORTB,0
GOTO NEXT_IN
CALL DELAY
MOVF PORTD,W
XORLW B'00000111'
BTFSS STATUS,Z
GOTO INPUT1
CLRF COUNTER1
GOTO INPUT1

NEXT_IN:
MOVLW 0Xff
MOVWF PORTD
CALL DELAY
MOVF PORTD,W
MOVWF VALUE
CLRF PORTD
CALL DELAY
INPUT2:
INCF COUNTER1 ; Increase counter value
MOVF COUNTER1,W ; move value in counter to work register
MOVWF PORTD ; move value in work register to PORTD register
BTFSS PORTB,0
GOTO ADDITION
CALL DELAY
MOVF PORTD,W
XORLW B'00000111'
BTFSS STATUS,Z
GOTO INPUT2
CLRF COUNTER1
GOTO INPUT2

ADDITION:
MOVF PORTD,W
ADDLW VALUE
MOVWF PORTD
CALL DELAY
CALL DELAY
CLRF PORTD
GOTO INPUT3

INPUT3:
INCF COUNTER1 ; Increase counter value
MOVF COUNTER1,W ; move value in counter to work register
MOVWF PORTD ; move value in work register to PORTD register
BTFSS PORTB,0
GOTO NEXT_IN_2
CALL DELAY
MOVF PORTD,W
MOVWF VALUE
XORLW B'00000111'
BTFSS STATUS,Z
GOTO INPUT3
CLRF COUNTER1
GOTO INPUT3

NEXT_IN_2:
MOVF PORTD,W
MOVWF VALUE
CLRF PORTD

INPUT4:
INCF COUNTER1 ; Increase counter value
MOVF COUNTER1,W ; move value in counter to work register
MOVWF PORTD ; move value in work register to PORTD register
BTFSS PORTB,0
GOTO SUBTRACTION
CALL DELAY
MOVF PORTD,W
XORLW B'00000111'
BTFSS STATUS,Z
GOTO INPUT4
CLRF COUNTER1
GOTO INPUT4

SUBTRACTION:
MOVF PORTD,W
ADDLW VALUE
MOVWF PORTD
CALL DELAY
CALL DELAY
CLRF PORTD
GOTO INPUT1

DELAY:

BTFSS PIR1,0 ;Skip if SET/1
GOTO DELAY
BCF PIR1,0
DECFSZ DELAY1
GOTO DELAY
MOVLW b'00000100'
MOVWF DELAY1
RETURN

END
 

Hi,

Ok, so you are showing someones code, with little notation as to how it works;
You need to show the circuit diagram as well.

btw it is in Microchip Assembly code, machine code is one level lower.
 
Last edited:

Hi,

The code does seem to work to some degree, changing PortD outputs as you press the switch.

Have you wired the switch and leds like this picture shows ?
 

Attachments

  • calc.jpg
    calc.jpg
    33.6 KB · Views: 85

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top