0

为什么这段代码不起作用?我想要的只是 portb 在我按下按钮时切换。

main
trisb=0
trisa=0xff
while true
if ra0<>0 then
portb = not portb
end if
wend .end
4

1 回答 1

0

我不确定那是什么;是伪代码吗?

无论如何,您需要触发从 RA0 == 0 到 RA0 == 1 的 CHANGE。正如所写,只要 RA0 == 1,那么您的 PORTB 每次循环都会切换。

这是 C 语言中的一个示例:

int main(void)
{
    unsigned char bButtonValue = 0;

    TRISA = 0xFF;
    TRISB = 0x00;

    while (1)
    {
        if (RA0 != bButtonValue) 
        {
            bButtonValue = RA0;
            if (bButtonValue == 1)
            {
                 PORTB= ~PORTB;
            }
         }
     }
}

请记住,对于一个真实的应用程序,您可能希望消除开关输入的抖动(确保在触发更改事件之前它对于几个样本是稳定的。

于 2012-05-09T16:16:07.763 回答