Problem when implementing a scoreboard for a stadium, how can I fix this code

75 views Asked by At

implement a scoreboard for a stadium, such that it shows the HOME and VISIT counter. The counter of each team always goes ascending from 0 to 9, initially both counters are at zero (Local 0 - 0 Visit), to increase the value of the counter of each team a button will be used for each one independently.

The pin assignment in the connection is as follows:

PIN I/O Description
RA0 input GOL_LOCAL, button to increase the home score
RA1 Input GOAL_VISITA, button to increase the visit marker
RB0 Output LOCAL_A, Digit A of the local marker binary code
RB1 Output LOCAL_B, Digit B of the local marker binary code
RB2 Output LOCAL_C, Digit C of the local marker binary code
RB3 Output LOCAL_D, Digit D of the local marker binary code
RB4 Output VISIT_A, Digit A of the binary code of the visit marker
RB5 Output VISIT_B, Digit B of the binary code of the visit marker
RB6 Output VISIT_C, Digit C of the binary code of the visit marker
RB7 Output VISIT_D, Digit D of the binary code of the visit marker

To show a digit on the display using binary code and the 7447 decoder, you must Use the following table as a reference.

Decimal D C B A
   0    0 0 0 0
   1    0 0 0 1
   2    0 0 1 0
   3    0 0 1 1
   4    0 1 0 0
   5    0 1 0 1
   6    0 1 1 0
   7    0 1 1 1
   8    1 0 0 0
   9    1 0 0 1

SUGGESTED LOGIC FOR SENDING DATA TO THE 7-SEGMENT DISPLAYS:

For example, if you want to show the marker LOCAL 2 – VISIT 1, through port B you must send the following BCD combination for LOCAL and VISIT

VISIT_D VISIT_C VISIT_B VISIT_A LOCAL_D LOCAL_C LOCAL_B LOCAL_A
0 0 0 1 0 0 1 0

To achieve this you may consider using 2 counters separately, one for the HOME goal count and another for the Away goal count.

local_counter

Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
0 0 0 0 0 0 1 0

Visit_Counter

Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
0 0 0 0 0 0 0 1

To get the BCD combination needed to display LOCAL 2 – VISIT 1 we need to move the Visit_Counter 4 positions to the left, which would give usa new value for Visit_Counter.

Visit_Counter

Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
0 0 0 1 0 0 0 0

Finally we can perform the OR operation between the values of the variables Local_counter and Visit_counter, obtaining the desired result that must be send through port B.

VISIT_D VISIT_C VISIT_B VISIT_A LOCAL_D LOCAL_C LOCAL_B LOCAL_A
0 0 0 1 0 0 1 0

Schematic

This is as far as I could go on my part.

    LIST P=16F84A
INCLUDE <P16F84A.INC>
CBLOCK 0X0C
Counter
ENDC
        ORG 0
    bsf STATUS,RP0
    movlw 0x00
    movwf TRISB
    bsf TRISA,RA0
    bcf STATUS,RP0
    clrf Counter
    clrf PORTB

Loop
    btfsc PORTA,RA0
    goto $-.1
    btfss PORTA,RA0
    goto $-.1
    movf Counter,W
    call TABLE
    movwf PORTB
    incf Counter,F
    movlw .10
    subwf Counter,W
    btfsc STATUS,Z
    clrf Counter
    goto Loop

BOARD
addwf PCL,F
DT 1,2,3,4,5,6,7,8,9,0
    END

I tried this code in the proteus simulator but it doesn't generate what I need completely. What am I missing in my code?

1

There are 1 answers

0
Kozmotronik On

Ready to go. I've made some fixes in your code to make it comply the requirements. As you will see in the code, I used 2 variables named scoreOfLocalTeam and scoreOfVisitorTeam instead of the former single Counter variable. This way scores for each team is saved its own variable.

Although the code is self explanatory, here is how it works briefly:

  • When the any of the buttons connected to RA0 and RA1 is pressed, 1 score is added to the corresponding team.
  • After the value is added, it is compared against 10, if it is 10 then the value is reset to the 0.
  • Then the actual value is output to its corresponding nibble of PORTB according to the tables in your question.
  • Eventually, once the button press has been serviced, it then waits for button release.
  • As soon as the button released it goes back and waits for a new button press.
  • Watch the part where I marked with the ATTENTION word.
    LIST P=16F84A
INCLUDE <P16F84A.INC>
CBLOCK 0X0C
scoreOfLocalTeam
scoreOfVisitorTeam
ENDC
        ORG 0
    bsf     STATUS,RP0
    movlw   0x00
    movwf   TRISB
    ; You should make sure that both pins (RA<1:0>) are configured as inputs
    bsf     TRISA,RA0 ; BTN_LOCAL input button
    bsf     TRISA,RA1 ; BTN_VISIT input button
    bcf     STATUS,RP0
    clrf    scoreOfLocalTeam
    clrf    scoreOfVisitorTeam
    clrf    PORTB

Loop
    ; Check inputs: you should check the both inputs. But in your code,
    ; you check only for the LOCAL button
    btfss   PORTA,RA0
    goto    IncrementLocalScore ; Local button press detected, go and increment local count
    btfss   PORTA,RA1
    goto    IncrementVisitorScore ; Visit button press detected, go and increment visit count
    goto    Loop ; No button press detected, keep checking

IncrementLocalScore
    incf    scoreOfLocalTeam,F
    movlw   .10
    xorwf   scoreOfLocalTeam,W ; Does local count reach to 10?
    btfsc   STATUS,Z
    clrf    scoreOfLocalTeam ; Yes, reset the local count
    ; Update the local 7 segment digit
    movlw   0xF0 ; Clear the low nibble but don't touch the high nibble
    andwf   PORTB,F
    movf    scoreOfLocalTeam,W
    andlw   0xF ; Low nibble only (mask)
    iorwf   PORTB,F ; output the local count through PORTB
    btfss   PORTA,RA0 ; Wait for button release
    goto    $-1
    goto    Loop ; Keep checking the buttons

IncrementVisitorScore
    incf    scoreOfVisitorTeam,F
    movlw   .10
    xorwf   scoreOfVisitorTeam,W ; Does local count reach to 10?
    btfsc   STATUS,Z
    clrf    scoreOfVisitorTeam ; Yes, reset the local count
    ; Update the local 7 segment digit
    movlw   0xF ; Clear the high nibble but don't touch the low nibble
    andwf   PORTB,F
    ; Attention here!!!
    ; Since the visit display is controlled by the high nibble of PORTB,
    ; we need to swap the nibbles of the scoreOfVisitorTeam variable but without
    ; affecting its value. That's why the result of swap command will be
    ; saved in W register.
    swapf   scoreOfVisitorTeam,W ; IMPORTANT! Save the result to the W register
    andlw   0xF0 ; Hİgh nibble only (mask)
    iorwf   PORTB,F ; output the local count through PORTB
    btfss   PORTA,RA1 ; Wait for button release
    goto    $-1
    goto    Loop ; Keep checking the buttons

    END

PS: The code is not tested since it is microcontroller specific. So I'd like to hear from your feedback whether it worked.