【问题标题】:Why is my If statement not working properly?为什么我的 If 语句不能正常工作?
【发布时间】:2018-12-11 12:27:41
【问题描述】:

所以我正在构建一个简单的 MikroC 程序,如果 PORTA.B0 等于 1,则设置 PORTB.B0

void main() {
   PORTA.B0=1;
   while(1){
   if(PORTA.B0){
        PORTB.B0=1;
   }
   }
}

但是它没有给我任何东西 PortB.B0 不等于 1 只有 portA。

谁能帮忙?

【问题讨论】:

    标签: mikroc


    【解决方案1】:

    根据您的代码,您必须已将 portb.rb0 和 porta.ra0 位的方向分别初始化为输出和输入。此外,您还需要稍微延迟一下去弹跳。

    void main()
    {
    ANSEL=0x00;
    trisa.ra0=1; //for input
    trisb.rb0=0; //for output
    while(1)
    {
       if (porta.ra0)
       {
         portb.rb0=1;  // setting high
         delay_ms(300); // time to stabilize mechanical button
        }
    }
    
    }
    

    【讨论】:

      【解决方案2】:

      试试 if(PORTA.B0==1)。这应该可以。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-01-14
        • 1970-01-01
        • 1970-01-01
        • 2023-02-03
        • 1970-01-01
        • 2011-03-14
        • 1970-01-01
        • 2022-12-03
        相关资源
        最近更新 更多