需要帮助配置端口以输入 8051

发布于 2024-08-31 12:46:05 字数 723 浏览 7 评论 0原文

连接如下红外传感器电路,根据飞利浦微控制器 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

逆蝶 2024-09-07 12:46:05

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.

指尖上的星空 2024-09-07 12:46:05

您通常使用 sbit P2_0 的数据类型用于定义特殊功能寄存器 (SFR) 内的位。

来自C51:从输入端口读取(已修改)

sfr P2 = 0xA0;
sbit P2_0 = P2^0;
...
P2_0 = 1;    /* set port for input */
var = P2_0;  /* read P2_0 into var */

需要注意的是,sbit 变量不能在函数内部声明。它们必须在函数体之外声明。


另一种选择可能是读取 P2 的所有 8 个引脚,然后屏蔽掉不需要的位。

char var;     /* define 8 bit variable */
P2 = 0xFF;    /* set P2 for input */
var = P2;     /* read P2 into var */
var &= 0x01;  /* mask off unwanted bits */

而不是将 P2 或 P2_0 引脚读入 unsigned int< /strong>(16 位),您可以使用 char(8 位)或单个 以节省内存。

char var;
...
var = P2;

或者

bit var;
...
var = P2_0;

另一种选择可能是使 char 位寻址

char bdata var;      /* bit-addressable char */
sbit var_0 = var^0;  /* bit 0 of var */
...
var = P2;       /* read P2 into var */
if(var_0 == 0)  /* test var_0 (bit 0 of var 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)

sfr P2 = 0xA0;
sbit P2_0 = P2^0;
...
P2_0 = 1;    /* set port for input */
var = P2_0;  /* read P2_0 into var */

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.

char var;     /* define 8 bit variable */
P2 = 0xFF;    /* set P2 for input */
var = P2;     /* read P2 into var */
var &= 0x01;  /* mask off 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.

char var;
...
var = P2;

or

bit var;
...
var = P2_0;

Another option may be to make the char bit-addressable.

char bdata var;      /* bit-addressable char */
sbit var_0 = var^0;  /* bit 0 of var */
...
var = P2;       /* read P2 into var */
if(var_0 == 0)  /* test var_0 (bit 0 of var char) */
{
    ...
}

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.

猛虎独行 2024-09-07 12:46:05

非常感谢...我的编码工作正常

并且我学习如何定义输入端口并读取数据

#include<reg51.h>     
#define opp P1
#define ipp P0
sbit op =P1^0;
sbit ip =P0^0;

main()
{

    unsigned int value;
    P0=0xFF;
    value=P0;
    value &=0x01;
    if(value==0)
    {
        P1=0x01;
    }
    else
    {
        P1=0x00;
    }  
}    

Thank you so much... my coding works

And I learn how to define input port and read the data

#include<reg51.h>     
#define opp P1
#define ipp P0
sbit op =P1^0;
sbit ip =P0^0;

main()
{

    unsigned int value;
    P0=0xFF;
    value=P0;
    value &=0x01;
    if(value==0)
    {
        P1=0x01;
    }
    else
    {
        P1=0x00;
    }  
}    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文