读取PIC18F的输入引脚

时间:2017-12-10 17:34:17

标签: pic18 microc

我正在将一个开关连接到PIC,我想读取该开关。我使用的是PIC18F4580。如果输入引脚为低电平,则它将打开连接到另一个引脚的LED,配置为输出。但是,LED始终保持开启状态,并且开关按钮无效。这是我的代码:

void main() 
{
    IRCF2_bit = 1;    //Internal 8MHz Oscisllator Configuration
    IRCF1_bit = 1;
    IRCF0_bit = 0;
    INTSRC_bit = 1;
    PLLEN_bit = 0;
    TRISD0_bit = 1;    //Switch connected to D0 and pin configured as input
    TRISD1_bit = 0;     //LED connected to D1 and pin configured as output 
    PORTD.F1=0;        //Turn off LED

    while(1) 
    {

        if (PORTD.F0==0) 
        {     
            //If Switch is pressed
            delay_ms(100);          //switch debounce

            if (PORTD.F0==0) 
            {
                PORTD.F1=1;           //Turn on LED
            }
            else 
            {
                PORTD.F1=0;                 //Turn off LED
            }
        }
  }

}

我对做什么一无所知。我使用了上拉电阻作为开关按钮,所有硬件应该是正确的。任何帮助深表感谢。

2 个答案:

答案 0 :(得分:1)

使用LAT(LATD)代替PORT(PORTD)来更改输出

请参阅: Difference between PORT and LATCH on PIC 18F

答案 1 :(得分:1)

该程序永远不会达到声明

        PORTD.F1=0;                 //Turn off LED

尝试类似:

   while(1) 
   {
       if (PORTD.F0==0) 
       {     
           //If Switch is pressed
           delay_ms(100);          //switch debounce
           PORTD.F1=1;           //Turn on LED
        }
        else
        {
            PORTD.F1=0;                 //Turn off LED
        }
    }