1

我正在使用 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
            }
    }
}

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

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

4

2 回答 2

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 */
        }
于 2012-08-21T04:57:08.910 回答
2

您可能正在正确读取端口引脚,但是因为当您检测到按压时您正在打开和关闭 LED,您的眼睛看不到结果。

例如,1Mhz 的时钟频率将使开/关切换大约每秒 150,000 次(1,000,000 个周期/每个循环约 3 个 ASM 指令/2 个循环打开然后关闭)。

I would suggest taking the approach of having the LED match the state of the input pin.

for(;;)
{
  if(PORTA.RA0 == 1) //if button is pressed
  {
    PORTB.RB0 = 1;   //turn on LED
  }
  else
  {
    PORTB.RB0 = 0;   //turn off LED
  }
}

This technique is similar to what Rajesh suggested, but provides a bit more direct feedback on whether the input pin is set or not.

If that doesn't work, then something with your setup of the TRISA is not correct. You may want to try this:

TRISA.RB0 = 1;
于 2012-08-21T16:37:32.153 回答