This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

help :one wire master bridge ds2482 interface with lpc2148

hi aim implementing i-button interface to lpc2148 using ds2482 1-wire to i2c bridge. my problem is i am using hardware i2c controller in lpc2148.i am writing 3 routine
1 detect the i button
2 read i-button
3 write i-button each routine using i2c interrupt.i seen some example in that all i2c code are written in side the interrupt routine only.
im confused about how can i uses the interrupt routine for 3 main routine. and can i enable the i2c and initiate start and send slave addresses and receive the acknowledgment and come back from interrupt routine and do some work in main routine and again enter to i2c interrupt routine and continue the next i2c operation

Parents Reply Children
  • //one wire interface using ds2482 using LPC2148 hardware I2C
    
    //************** crystal=12Mhz  Pclk=12Mhz**************//
    #include<lpc214x.h>
    void i2c_INIT(void);
    void i2c_STOP(void);
    unsigned int i2c_data_read(void);
    void i2c_data_write(unsigned int);
    void SendI2CAddress_R(unsigned char);
    void SendI2CAddress_W(unsigned char);
    #define ds2482_address_write 0x30
    #define ds2482_address_read 0x31
    #define STA 0x20
    #define SIC 0x08
    #define SI 0x08
    #define STO 0x10
    #define STAC 0x20
    #define AA 0x04
    #define AAC 0x04
    #define I2CBLOCK_ENABLE 0x40 //enable the hardware I2C block
    unsigned int   state;
    //*****************MAIN_START************************//
    main()
    {
    
    unsigned  int value;
    i2c_INIT();
    while(1)
    {
    unsigned int temp=0x00;SendI2CAddress_W(ds2482_address_write);
    
    i2c_data_write(command);
    
    SendI2CAddress_R(ds2482_address_read);
    
    temp=i2c_data_read();
    
    
    
    
      while(1);
    
       }
     }
    //*****************I2C_INITIALIZATION***************************//
    void i2c_INIT(void)
    {
    I2C0CONCLR = 0xFF;
    PINSEL0 |= 0x50; // Set pinouts as scl and sda
    I2C0SCLL=0x64; //Set bit rate 12 Mhz/VPBDIV+SCLH+SCLL
    I2C0SCLH=0x64; //
    
    }
    
    //****************i2c_write*******************//
    void i2c_data_write(unsigned int data)
    I2C0DAT=data;
    while( ! (I2C0CONSET & SI));        //wait untill status available...
    
    state=I2C0STAT;
    
    if(state ==0x28 )
    {
    I2C0CONCLR |= SIC;
    }
    else
    if(state==0x30)// i2c stop condition.
     {
     I2C0CONCLR = SIC;
     I2C0CONSET = STO;
      }
    
    }
    //****************i2c_address_write*****************//
    void SendI2CAddress_W(unsigned char Addr_S)
    {
      I2C0CONCLR = 0xFF; // function would hang.
     I2C0CONSET = I2CBLOCK_ENABLE; // Hardware I2C bus //block enabled
    
     I2C0CONSET = STA ; // set STA - allow master to //acknowlege slave;
     while(I2C0STAT!=0x08); // Wait for start to be completed
    I2C0CONCLR =             STAC    ;//stop repeatrd //start condition
     I2C0DAT = Addr_S; // Charge slave Address
     I2C0CONCLR = SIC;
     while( ! ( I2C0CONSET & SI)) ; // wait till status //available
    state=I2C0STAT; // read Status.See standard error codes
    if(state ==  0x18)
    {
    state=I2C0STAT; // read Status.See standard error codes
    }
    else
    if(state !=0x18)
    {
    
    i2c_STOP();
    }
    }
    //****************i2c_address_read*****************//
    void SendI2CAddress_R(unsigned char Addr_S)
    {
      I2C0CONCLR = 0xFF; // function would hang.
     I2C0CONSET = I2CBLOCK_ENABLE; // Hardware I2C bus block enabled
    
     I2C0CONSET = STA | AA; // set STA - allow master to //acknowlege slave;
     while(I2C0STAT!=0x08); // Wait for start to be //completed
     I2C0CONCLR = STAC;//clear repeated start condition
     I2C0DAT = Addr_S; // Charge slave Address
     I2C0CONCLR = SIC;
    while( !(I2C0CONSET & SI)) ;// wait till  status //available
    state=I2C0STAT;// read Status. See standard error code
      if(state !=0x40)
    {
    
    i2c_STOP();
    }
    else
    if(state ==  0x40)
    {
    
    
      state=I2C0STAT; // read Status. See standard error codes
    
    }
    
    }
    
    //*******************I2C_READ_DATA*****************//
    unsigned int i2c_data_read(void)
    {
             unsigned int temp;
               I2C0CONCLR = SIC;
    while( ! ( I2C0CONSET & SI)) ; // wait till status available
    
    state=I2C0STAT;
    
    if(state == 0x50)
    {
    return  I2C0DAT;
    }
    else
    if(state== 0x58)
    {
    
    i2c_STOP();
    }
    
    }
    // ********************I2C_STOP**************************//
    void i2c_STOP(void)
    {
     while( ! ( I2C0CONSET & SI)) ; // wait till status available
     I2C0CONSET = STO;
    I2C0CONCLR = SIC;
    
    
    }
    
    }
    
    ******************************************
    


    1.my problem is that when i send the address to i2c slave im getting status code as 0x18 ie address+w is sented to save and ack is received by master.
    2. when i try to write data im getting status code as 0x30 insted of 0x28 i.e. data on I2C0DAT is transmitted and ack not recived.

  • What is the value of command in i2c_data_write(command)
    ? I think if you send an invalid command the ds2482 will nack.

    0xF0 is a device reset and clears the state machine of the DS2482, have you tried writing that as your first command?