Button 我想使用两个或更多具有不同功能的按钮。如何操作?
我想让2个或更多按钮等待按下 例如,增加或减少7段显示中的值。 按钮1增加值,按钮2减少值。 对于下面的代码,我可以递减或递增,但不能同时执行这两种操作 对于一个按钮,我的操作方式如下: 过程2:;SW09和SW11的功能Button 我想使用两个或更多具有不同功能的按钮。如何操作?,button,assembly,pic,Button,Assembly,Pic,我想让2个或更多按钮等待按下 例如,增加或减少7段显示中的值。 按钮1增加值,按钮2减少值。 对于下面的代码,我可以递减或递增,但不能同时执行这两种操作 对于一个按钮,我的操作方式如下: 过程2:;SW09和SW11的功能 BTFSC PORTB,7 This line is to understand whether we pressed button or not. GOTO PROCESS2 We cant go belo
BTFSC PORTB,7 This line is to understand whether we pressed button or not.
GOTO PROCESS2 We cant go below until the button pressed
CALL UP ;Up increments the value which will be shown in the 7-segment-display.
BTFSS PORTB,7
GOTO $-1
那么如何对多个按钮执行此操作。算法是什么?它背后的逻辑是什么?我不知道是否必须使用汇编程序,但在C语言中这很简单 假设BUTTON1和BUTTON2在其他地方定义为输入引脚,并且它们具有上拉功能,您可以尝试:
void checkIncrement(void)
{
if (BUTTON1 == 0) {
while (BUTTON1 == 0) DelayMs(1); // software debounce
increment(); // call the increment function
}
}
void checkDecrement(void)
{
if (BUTTON2 == 0) {
while (BUTTON2 == 0) DelayMs(1);
decrement();
}
}
int main(void)
{
// your main loop
while (1)
{
checkIncrement();
checkDecrement();
// do something else if none of the buttons are pressed
}
}
如果需要汇编程序,您可以尝试编译并查看汇编程序列表,以了解它是如何完成的。我不熟悉PIC汇编,但我记得关于MCS51汇编,也许这与您的问题无关,但可以作为参考 我假设按钮工作为低电平(按下按钮时,按钮端口将被拉低,或者按下按钮时,按钮端口将接地)
button1 EQU P1.0 ; Port1 bit0 as button1 (input)
button2 EQU P1.1 ; Port1 bit1 as button2 (input)
led EQU P2 ; Port2 bit 0 until 8 as led (output)
ORG 00H
mov A,#7FH ; init led value 7FH = 0111 1111B
mov R0,A
main:
btn1sts:
jb button1, btn2sts ;if button1 not pressed then check button2 status
jnb button1, $ ;wait until button 1 is released
inc R0 ;A value only increases after the button1 is released.
sjmp process:
btn2sts:
jb button2, btn1sts ;if button2 not pressed then check button1 status
jnb button2, $ ;wait until button 2 is released
dec R0 ;A value only decreases after the button2 is released.
process:
mov A, R0
mov led, A ;show A value in P2 (led).
ljmp main: ;goto main