Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Nuvoton N76E003上的I2C实现_C_8051 - Fatal编程技术网

Nuvoton N76E003上的I2C实现

Nuvoton N76E003上的I2C实现,c,8051,C,8051,我正在尝试将基于I2C的GPIO扩展器与nuvoton N76E003微控制器连接起来。我在Keil C51开发 参考给定的示例和一些在线参考,我编写了一个代码来控制MCP23017的功能。但是,我在I2C总线上没有得到任何响应。我将我的代码与此一起附加。我对代码进行了注释,以便更容易理解 我将非常高兴你所有的积极评论和批评。 谢谢 萨特 检查您的从属地址。应该是: I2DAT=((MCP23017)(A2@Sarthdave,如果这解决了您的问题,请接受回答您好,谢谢您的回答。但是,请让我知道

我正在尝试将基于I2C的GPIO扩展器与nuvoton N76E003微控制器连接起来。我在Keil C51开发

参考给定的示例和一些在线参考,我编写了一个代码来控制MCP23017的功能。但是,我在I2C总线上没有得到任何响应。我将我的代码与此一起附加。我对代码进行了注释,以便更容易理解

我将非常高兴你所有的积极评论和批评。 谢谢 萨特


检查您的从属地址。应该是:


I2DAT=((MCP23017)(A2@Sarthdave,如果这解决了您的问题,请接受回答您好,谢谢您的回答。但是,请让我知道实际地址是什么,我很困惑。数据表上说,所有A1、A2和A3都将连接到地上,地址应该是0x20,后面是R/W位。如果您能找到地址,请告诉我地址好吗从数据表中删除?再次感谢。@Sarthdave,仔细检查数据表第9页。“硬件地址启用(HAEN)位仅启用/禁用MCP23S17上的硬件寻址。地址引脚(A2、A1和A0)必须是外部偏置的,无论HAEN位值如何。如果启用(HAEN=1),设备的硬件地址与地址引脚匹配。如果禁用(HAEN=0),设备的硬件地址为A2=A1=A0=0。“因此,如果HAEN=0,I2DAT=((MCP23017|SLA |(A2@Sarthdave,对不起,第20页,不是第9页谢谢你的及时回复。我的硬件地址引脚外部连接到地上。我没有对HAEN位做任何事情,正如你已经从代码中看到的那样。我的设备地址是(0x20)吗
#include "N76E003.h"
#include "Common.h"
#include "Delay.h"
#include "SFR_Macro.h"
#include "Function_define.h"

#define regular_I2C_pins              0
#define alternate_I2C_pins            1


#define regular_I2C_GPIOs()           do{P13_OpenDrain_Mode; P14_OpenDrain_Mode; clr_I2CPX;}while(0)

#define alternative_I2C_GPIOs()       do{P02_OpenDrain_Mode; P16_OpenDrain_Mode; set_I2CPX;}while(0)

#define I2C_GPIO_Init(mode)           do{if(mode != 0){alternative_I2C_GPIOs();}else{regular_I2C_GPIOs();}}while(0)

#define I2C_CLOCK               0x27
#define MCP23017_SLA            0x20
#define MCP23017_WR             0
#define MCP23017_RD             1
#define ERROR_CODE              0x78

void Init_I2C(void)
{
    I2C_GPIO_Init(regular_I2C_pins);
    I2CLK = I2C_CLOCK; //Set I2C clock rate
    set_I2CEN;         //Enable I2C hardware                          
}

void I2C_Error(void)
{
     while (1);    
}
void I2C_Process(void)
{
    /* Step1 */
    set_STA;                          /* Send Start bit to I2C device */
    clr_SI;
    while (!SI);                      //Check SI set or not
    if (I2STAT != 0x08)               //Check status value after every step
        I2C_Error();

    /* Step2 */
    clr_STA;                                    //STA=0
    I2DAT = (MCP23017_SLA | MCP23017_WR);
    clr_SI;
    while (!SI);                                //Check SI set or not
    if (I2STAT != 0x18)              
        I2C_Error();

    /* Step3 */
    I2DAT = 0x00;                               //address high for I2C EEPROM
    clr_SI;
    while (!SI);                                //Check SI set or not
    if (I2STAT != 0x28)              
        I2C_Error();

    /* Step4 */
    I2DAT = 0x00;                               //address low for I2C EEPROM
    clr_SI;
    while (!SI);                                //Check SI set or not
    if (I2STAT != 0x28)              
        I2C_Error();

    /* Step5 */
    I2DAT = (0x00);                             // Write to IODIRA register to declare port pins as output
    clr_SI;
    while (!SI);
    if (I2STAT != 0x18)
        I2C_Error();

    I2DAT = (0x00);                             // Declare the pins as outputs
    clr_SI;
    while (!SI);
    if (I2STAT != 0x18)
        I2C_Error();

     I2DAT = (0x00);                             // Write to IODIRB register to declare port pins as output
    clr_SI;
    while (!SI);
    if (I2STAT != 0x18)
        I2C_Error();

    I2DAT = (0x00);                             // Declare the pins as outputs
    clr_SI;
    while (!SI);
    if (I2STAT != 0x18)
        I2C_Error();

    I2DAT = (0x12);                             // Address port A
    clr_SI;
    while (!SI);
    if (I2STAT != 0x18)
        I2C_Error();

    I2DAT = (0xFF);                             // Make port A pins high
    clr_SI;
    while (!SI);
    if (I2STAT != 0x18)
        I2C_Error();

    I2DAT = (0x13);                             // Address port B
    clr_SI;
    while (!SI);
    if (I2STAT != 0x18)
        I2C_Error();

    I2DAT = (0xFF);                             // Make port B pins high
    clr_SI;
    while (!SI);
    if (I2STAT != 0x18)
        I2C_Error();

    /* Step6 */
    do{
        set_STO;
        clr_SI;

        set_STA;                   //Check if no ACK is returned by MCP23017
        clr_SI;
        while (!SI);              //Check SI set or not
        if (I2STAT != 0x08)       //Check status value after every step
            I2C_Error();

        clr_STA;
        I2DAT = (MCP23017_SLA | MCP23017_WR);
        clr_SI;
        while (!SI);                            //Check SI set or not
    }while (I2STAT != 0x18);

    /* Step7 */
    set_STO;
    clr_SI;
    while (STO);                                /* Check STOP signal */
}

void main(void)
{
    //Set_All_GPIO_Quasi_Mode;  
    Init_I2C();                                 //initial I2C circuit
    I2C_Process();                         
    while (1);
}
I2DAT = (((MCP23017_SLA | (A2 << 2) | (A1 << 1) | (A0 << 0)) << 1) | MCP23017_WR);