-3
\$\begingroup\$
;    
    LIST    P=16F877A
    INCLUDE P16F877.INC
    radix   dec
    __CONFIG _CP_OFF &_WDT_OFF & _BODEN_ON & _PWRTE_ON & _XT_OSC & _WRT_ENABLE_OFF & _LVP_OFF & _DEBUG_OFF & _CPD_OFF

; Static global variables
DATSEC1 udata
LUT res 16      ; 16 byte look up value     
    
; Here is the number variable    
number      EQU     0x70

    ; Reset vector
    org     0x00
    BSF     STATUS, RP0     ; Select Bank1
    CLRF    TRISA       ; PortA --> Output
    CLRF    TRISD
   
     MOVLW 0xFF
    MOVWF TRISB    ;PortB -->Input

    BCF     STATUS, RP0     ; Select Bank0

    ; Clear PORTD & Select DIS4
    CLRF    PORTD       ; PORTD = 0
    CLRF    PORTA
   
    ; Deselect all SSDs
   ; BSF        PORTA, 4        ; Select DIS4
    BSF     PORTA, 5        ; Select DIS4

 
    CLRF    number      ; number = 0

   
    
Start
     movf number,W
  call GetCode
  MOVWF PORTD
btfsc PORTB,3
GOTO button3
call Delay
btfsc PORTB,4
GOTO button4
call Delay
  
  
  btfsc PORTB,5
  goto button5
button3
   BTFSC PORTB,3
   GOTO button3
   INCF number,F
   CALL Delay
   goto Start
button4
   BTFSC PORTB,4
   GOTO button4
   DECFSZ number,F
   CALL Delay
   GOTO Start
button5
   BTFSC PORTB,5
   GOTO button5
   CLRF number
   CALL Delay
GOTO Start
    
    

   
   
;------------------------------------------------------------------------------
; Get the number's 7-segment bit code
;------------------------------------------------------------------------------
GetCode
    ADDWF   PCL, F      ; Jump to the correct number. PCL is the program counter register
    RETLW   B'00111111'     ; 0
    RETLW   B'00000110'     ; 1
    RETLW   B'01011011'     ; 2
    RETLW   B'01001111'     ; 3
    RETLW   B'01100110'     ; 4
    RETLW   B'01101101'     ; 5
    RETLW   B'01111101'     ; 6
    RETLW   B'00000111'     ; 7
    RETLW   B'01111111'     ; 8
    RETLW   B'01101111'     ; 9    

    
;------------------------------------------------------------------------------
; This function just wastes some time:
; Simply loops for 4x256x256 iterations of the loops
;------------------------------------------------------------------------------
count1 EQU 0x7d
count2 EQU 0x7e
count3 EQU 0x7f
Delay
    MOVLW 0x04
    MOVWF count3     ; w->[count3]

DelayLoop1
    MOVLW 0xff
    MOVWF count2     ; w->[count2]

DelayLoop2
    MOVWF count1     ; w->[count1]

DelayLoop3
    DECFSZ count1, f ; Decrement count1 and put the result in count1
    GOTO DelayLoop3

    DECFSZ count2, f ; Decrement count2 and put the result in count2
    GOTO DelayLoop2

    DECFSZ count3, f ; Decrement count3 and put the result in count3
    GOTO DelayLoop1

    RETURN    
        
    END

This is the complete code problems :only button3 works i cannot check or use other buttons ı use PicSimlab

\$\endgroup\$
6
  • 1
    \$\begingroup\$ Would you like to describe these issues, or should we just make some wild guesses? \$\endgroup\$ – brhans Jan 4 at 3:53
  • \$\begingroup\$ buttons works individual but i cant figure it out how works together The first button (the button connected to PORTB3) will be used to increment the counter. The second button (the button connected to PORTB4) will be used to decrement the counter. The third button (the button connected to PORTB5) will be used to reset the counter. \$\endgroup\$ – Çağrı Doğan Jan 4 at 4:58
  • \$\begingroup\$ This is not the complete program. What code is at button3, button4, and button5? \$\endgroup\$ – Bruce Abbott Jan 4 at 7:26
  • \$\begingroup\$ @BruceAbbott edited \$\endgroup\$ – Çağrı Doğan Jan 4 at 9:32
  • 2
    \$\begingroup\$ Well, and your question is what? Please edit your question and provide some context, like what you expect, and what you got instead, what you already tried, why you have this infinite loop, and so on. \$\endgroup\$ – the busybee Jan 4 at 11:40
1
\$\begingroup\$

Before the label button3 you forgot to insert goto Start (and CALL delay if you want it).

Explanation:

You are checking all 3 buttons in sequence. But if the first btfsc PORTB,5 skips because no button is pressed, the program continues with the instructions after button3.

In pseudo high level (simplified C) code:

Start:
    PORTD = GetCode(number)
    if (PORTB.3 == 1) {
        goto button3
    }
    Delay();
    if (PORTB.4 == 1) {
        goto button4
    }
    Delay()
    if (PORTB.5 == 1) {
        goto button5
    }
    // Here it falls through. Insert:
    // Delay()
    // goto Start

button3:
    while (PORTB.3 == 1) {
    }
    number++
    Delay()
    goto Start

button4:
    while (PORTB.4 == 1) {
    }
    number--
    Delay()
    goto Start

button5:
    while (PORTB.5 == 1) {
    }
    number = 0
    Delay()
    goto Start

Note 1: Why do you delay after each single check? Wouldn't it be better to delay just once in the loop?

Note 2: You did not tell us, so I read from the code that a pressed button will give a "1" on the port bit.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.