为体育场实现记分牌,以显示主场和参观计数器。每个团队的计数器始终从 0 到 9 递增,最初两个计数器都为零(本地 0 - 0 访问),为了增加每个团队的计数器值,将为每个团队单独使用一个按钮。
连接中的引脚分配如下:
引脚输入/输出说明 RA0输入GOL_LOCAL,按钮增加主场分数 RA1 输入GOAL_VISITA,增加访问标记按钮 RB0 输出 LOCAL_A,本地标记二进制码的数字 A RB1 输出 LOCAL_B,本地标记二进制码的数字 B RB2 输出 LOCAL_C,本地标记二进制码的数字 C RB3 输出 LOCAL_D,本地标记二进制码的数字 D RB4 输出 VISIT_A,访问标记的二进制码的数字 A RB5 输出 VISIT_B,访问标记的二进制代码的数字 B RB6 输出 VISIT_C,访问标记的二进制码的数字 C RB7 输出 VISIT_D,访问标记的二进制码的数字 D
要使用二进制代码和 7447 解码器在显示屏上显示数字,您必须 请使用下表作为参考。
十进制 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
例如,如果您想显示标记 LOCAL 2 – VISIT 1,则必须通过端口 B 发送以下 LOCAL 和 VISIT 的 BCD 组合
访问_D | 访问_C | 访问_B | 访问_A | LOCAL_D | LOCAL_C | LOCAL_B | LOCAL_A |
---|---|---|---|---|---|---|---|
0 | 0 | 0 | 1 | 0 | 0 | 1 | 0 |
为了实现这一目标,您可以考虑单独使用 2 个计数器,一个用于主场进球计数,另一个用于客场进球计数。
本地计数器
位7 | 位6 | 位5 | 位4 | 位3 | 位2 | 位1 | 位0 |
---|---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
参观柜台
位7 | 位6 | 位5 | 位4 | 位3 | 位2 | 位1 | 位0 |
---|---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
获取显示 LOCAL 2 所需的 BCD 组合 - VISIT 1 我们需要将 Visit_Counter 向左移动 4 个位置,这将为 Visit_Counter 提供新的值。
参观柜台
位7 | 位6 | 位5 | 位4 | 位3 | 位2 | 位1 | 位0 |
---|---|---|---|---|---|---|---|
0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
最后我们可以将变量Local_counter和Visit_counter的值进行或运算,得到想要的结果,必须通过端口B发送。
访问_D | 访问_C | 访问_B | 访问_A | LOCAL_D | LOCAL_C | LOCAL_B | LOCAL_A |
---|---|---|---|---|---|---|---|
0 | 0 | 0 | 1 | 0 | 0 | 1 | 0 |
这是我所能做到的。
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
我在proteus模拟器中尝试了这段代码,但它没有完全生成我需要的内容。我的代码中缺少什么?
准备出发。我对您的代码进行了一些修复,以使其符合要求。正如您将在代码中看到的,我使用了 2 个名为
scoreOfLocalTeam
和 scoreOfVisitorTeam
的变量,而不是以前的单个 Counter
变量。这样每个团队的得分都会保存自己的变量。
虽然代码是不言自明的,但它的工作原理如下:
RA0
和RA1
相连的任何按钮时,相应团队将增加1分。PORTB
对应的半字节。 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:该代码未经过测试,因为它是特定于微控制器的。所以我想听听您的反馈是否有效。