首页 > 解决方案 > 将一个引脚配置为输入而另一个引脚配置为输出时 AVR 端口上的奇怪行为

问题描述

在一个基本项目中,当我将输入引脚(引脚 0)配置为 in 并配置上拉电阻时,我使用 ATmega32 上的 portD 的一个引脚作为输入与开关连接,另一个引脚作为输出与 LED 连接然后将输出引脚(1)配置为输出,然后烧写代码我从 LED 获得的亮度非常低,尽管我尝试在一行上配置 DDRD,但代码运行良好,对此问题有任何建议

错误的代码

void main(){
DDRD&=~(0x01);
PORTD|=0x01;
DDRD|=0x02;

while(1)
{

    if((PIND & (1<<0)) == 0 )
    {
        PORTD|=0x02;
    }
    else
    {
        PORTD&=~(0x02);
    }

}
return;
}

按预期工作的代码(运行良好),

void main(){
DDRD=0b00000010;
PORTD|=0x01;
while(1)
{

    if((PIND & (1<<0)) == 0 )
    {
        PORTD|=0x02;
    }
    else
    {
        PORTD&=~(0x02);
    }

 }
return;
}

任何人都知道这种行为的原因

标签: cavr

解决方案


您打开内部引脚上拉。阅读 DS 了解更多详情


推荐阅读