c - 将一个引脚配置为输入而另一个引脚配置为输出时 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;
}
任何人都知道这种行为的原因
解决方案
您打开内部引脚上拉。阅读 DS 了解更多详情
推荐阅读
- linux - 如何理解这段汇编代码的流程
- c - 有没有办法去除数字周围的黑色背景颜色?
- docker-machine - docker-machine 不创建虚拟机
- java - 从 ListView 适配器中删除重复项
- javascript - 如何将附加变量传递给 React.forwardRef
- sql - 如果参数在具有单个参数的函数中具有多个值,如何从 pl/sql 函数中获取数据
- regex - RegEx 用于删除分隔符前后的所有内容
- reinforcement-learning - 得分函数如何帮助策略梯度?
- android - 登录页面在 Xamarin Android 应用程序中找不到 SQLite 数据库
- node.js - Dialogflow v2 有简单的 HTTP API 吗?