需要帮助配置端口以输入 8051
连接如下红外传感器电路,根据飞利浦微控制器 8051 端口 2_0 引脚的闭路或开路输出线产生 0 或 5v。问题是当我这样做时,电路值被端口 2_0 led 上的当前值覆盖这是我的代码(在keil c中)我想我没有将P 2_0配置为正确的输入
void MSDelay(unsigned int);
sbit led=P1^0;
void main()
{
unsigned int var;
P2=0xFF;
TMOD=0x20;
TH1=0xFD;
SCON =0x50;
TR1=1;
while(1)
{
var=P2^0;
if(var==0)
{
led=1;
SBUF='0';
while(TI==0);
TI=0;
MSDelay(250);
}
else
{
led=0;
SBUF='9';
while(TI==0);
TI=0;
MSDelay(100);
}
}
}
编辑:我遇到了一个问题,因为我使用的8086处理器有故障。建议任何尝试此操作的人在编程时获得一些备件。
The connection is as follows An infrared sensor circuit which yields 0 or 5v depending on closed or open circuit output line to port 2_0 pin of microcontroller 8051 philips.Problem is when i do this the circuit value are overridden by the current value on port 2_0 led always goes on.Here is my code(in keil c) i guess i have not configured P 2_0 as input properly
void MSDelay(unsigned int);
sbit led=P1^0;
void main()
{
unsigned int var;
P2=0xFF;
TMOD=0x20;
TH1=0xFD;
SCON =0x50;
TR1=1;
while(1)
{
var=P2^0;
if(var==0)
{
led=1;
SBUF='0';
while(TI==0);
TI=0;
MSDelay(250);
}
else
{
led=0;
SBUF='9';
while(TI==0);
TI=0;
MSDelay(100);
}
}
}
EDIT : I was facing a problem since the 8086 processor i was using had a fault in it. Would recommend anyone trying this to get a few spares when programming.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
jschmier 有一个很好的观点。此外,端口可能配置不正确,或者电路中是否有某些东西导致 LED 快速关闭和打开,因此看起来它一直处于打开状态。
jschmier has a good point. Also the port may not be configured correctly, or is there something in the circuit that is causing the led to toggle off and on very quickly so it looks like it is on all the time.
您通常使用 sbit P2_0 的数据类型用于定义特殊功能寄存器 (SFR) 内的位。
来自C51:从输入端口读取(已修改)
需要注意的是,sbit 变量不能在函数内部声明。它们必须在函数体之外声明。
另一种选择可能是读取 P2 的所有 8 个引脚,然后屏蔽掉不需要的位。
而不是将 P2 或 P2_0 引脚读入 unsigned int< /strong>(16 位),您可以使用 char(8 位)或单个 位以节省内存。
或者
另一种选择可能是使 char 位寻址。
您可以在Keil Cx51 编译器用户指南中找到其他有用信息和相关链接。
注意:我的 8051 经验大部分是在组装中。上面的 C 示例可能不是 100% 正确。
You typically use the sbit data type for P2_0 to define a bit within a special function register (SFR).
From C51: READING FROM AN INPUT PORT (modified)
It is important to note that sbit variables may not be declared inside a function. They must be declared outside of the function body.
Another option may be to read all 8 pins of P2 and then mask off the unwanted bits.
Rather than read P2 or the P2_0 pin into an unsigned int (16 bits), you could use a char (8 bits) or single bit to save on memory.
or
Another option may be to make the char bit-addressable.
You can find additional useful information in the Keil Cx51 Compiler User's Guide and related links.
Note: Most of my 8051 experience is in assembly. The C examples above may not be 100% correct.
非常感谢...我的编码工作正常
并且我学习如何定义输入端口并读取数据
Thank you so much... my coding works
And I learn how to define input port and read the data