如何在C中将端口设置为按钮的输入?

Sha*_*vid 1 c embedded microcontroller input pic

我正在使用MikroC来尝试编程我的PIC16f62微控制器.我已经设法使我的输出工作(我可以打开LED等)但我似乎无法让输入工作.

这是我目前的代码:

void main() {
    TRISB.RB0 = 0; //set Port RB0 as output
    PORTB.RB0 = 1; //set Port RB0 to high (turn on LED)
    TRISA = 1; //Set PORTA as inputs 

    for(;;){  //endless loop
            if(PORTA.RA0 == 1){  //if push button is pressed
                         PORTB.RB0 = !PORTB.RB0;  \\toggle LED
            }
    }
}
Run Code Online (Sandbox Code Playgroud)

我不知道问题是我没有正确配置PORT还是我正在检查按钮是否被错误按下.

任何帮助表示赞赏.谢谢.

raj*_*115 7

此更改可能对您有所帮助.

for(;;){  //endless loop
        if(PORTA.RA0 == 1){  //if push button is pressed
                     PORTB.RB0 = !PORTB.RB0;  \\toggle LED
          while(PORTA.RA0 == 1);
       /*wait till button released as press of a buttons take time  and processor is too fast */
        }
Run Code Online (Sandbox Code Playgroud)