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
  • int OW_ReadScratch(char* scratchpadData,char TA1,char TA2,char ES)
    {
            char readTA1,readTA2,readES;
            char* readScratchPad;
            short i;
    
            readScratchPad = 0;
            OW_ResetAndSkipRom();
            OW_WriteByte(0xAA); //read scratch
            readTA1 = OW_ReadByte(); //read TA1
            if(readTA1 != TA1)
                    return 0;
            readTA2 = OW_ReadByte(); //read TA2
            if(readTA2 != TA2)
                    return 0;
            readES = OW_ReadByte(); //read ES byte
            if(readES != ES)
                    return 0;
            OW_ReadBytes(32, readScratchPad ); //read entire scratchpad
            for(i=0;i<32;i++)
            {// make sure it is what we think it is
                    if(readScratchPad[i] != scratchpadData[i])
                            return 0;
            }
    
            return 1;
    }
    
    void OW_CopyScratch(char TA1,char TA2,char ES)
    {
            OW_ResetAndSkipRom();
            OW_WriteByte(0x55);
            OW_WriteByte(TA1);
            OW_WriteByte(TA2);
            OW_WriteByte(ES);
    //      waitSec;
    }
    
    char* OW_ReadRom(char TA1,char TA2,int howmany)
    {
            char* readbytes;
    
            readbytes=0;
            OW_ResetAndSkipRom();
            OW_WriteByte(0xF0);
            OW_WriteByte(TA1);
            OW_WriteByte(TA2);
            OW_ReadBytes(howmany, readbytes); //read 'howmany' bytes of mem
    
            return readbytes;
    }
    
    //////******////////////
    //      Overdrive functions
    ///////////////////////
    
    /****OW_ResetAndSkipRom***/
    //Sends a standard speed One Wire
    //Reset and a OD skip rom command. Returns a
    //1 on successful PD pulse. Done at regular
    //speed, puts part in OD mode.
    /**************************/
    int OW_ResetAndODSkipRom()
    {
            PullLineLow(7200);       //pull low ~480us
            delay(900);              // wait ~60us for tpdh
            //read Presence detect
            if( ReadOne )   //line high, no presence detect low
                    return 0;                //means no part or error
    
            delay(2700);         //finish tpdl ~180us
            OW_WriteByte(0x3C);
    
            ODflag=1;
    
            return 1;
    }
    
    /****OW_ResetAndMatchRom***/
    //Sends a standard speed One Wire
    //Reset and a match rom command with
    //Rom Code. Fails if no PD is read.
    //Reset and PD done at reg speed,
    //puts part in OD mode, sends rom
    //code at OD speeds.
    /****************************/
    int OW_ResetAndODMatchRom(char* Rom)
    {
    
            return 0;
    }
    

Reply
  • int OW_ReadScratch(char* scratchpadData,char TA1,char TA2,char ES)
    {
            char readTA1,readTA2,readES;
            char* readScratchPad;
            short i;
    
            readScratchPad = 0;
            OW_ResetAndSkipRom();
            OW_WriteByte(0xAA); //read scratch
            readTA1 = OW_ReadByte(); //read TA1
            if(readTA1 != TA1)
                    return 0;
            readTA2 = OW_ReadByte(); //read TA2
            if(readTA2 != TA2)
                    return 0;
            readES = OW_ReadByte(); //read ES byte
            if(readES != ES)
                    return 0;
            OW_ReadBytes(32, readScratchPad ); //read entire scratchpad
            for(i=0;i<32;i++)
            {// make sure it is what we think it is
                    if(readScratchPad[i] != scratchpadData[i])
                            return 0;
            }
    
            return 1;
    }
    
    void OW_CopyScratch(char TA1,char TA2,char ES)
    {
            OW_ResetAndSkipRom();
            OW_WriteByte(0x55);
            OW_WriteByte(TA1);
            OW_WriteByte(TA2);
            OW_WriteByte(ES);
    //      waitSec;
    }
    
    char* OW_ReadRom(char TA1,char TA2,int howmany)
    {
            char* readbytes;
    
            readbytes=0;
            OW_ResetAndSkipRom();
            OW_WriteByte(0xF0);
            OW_WriteByte(TA1);
            OW_WriteByte(TA2);
            OW_ReadBytes(howmany, readbytes); //read 'howmany' bytes of mem
    
            return readbytes;
    }
    
    //////******////////////
    //      Overdrive functions
    ///////////////////////
    
    /****OW_ResetAndSkipRom***/
    //Sends a standard speed One Wire
    //Reset and a OD skip rom command. Returns a
    //1 on successful PD pulse. Done at regular
    //speed, puts part in OD mode.
    /**************************/
    int OW_ResetAndODSkipRom()
    {
            PullLineLow(7200);       //pull low ~480us
            delay(900);              // wait ~60us for tpdh
            //read Presence detect
            if( ReadOne )   //line high, no presence detect low
                    return 0;                //means no part or error
    
            delay(2700);         //finish tpdl ~180us
            OW_WriteByte(0x3C);
    
            ODflag=1;
    
            return 1;
    }
    
    /****OW_ResetAndMatchRom***/
    //Sends a standard speed One Wire
    //Reset and a match rom command with
    //Rom Code. Fails if no PD is read.
    //Reset and PD done at reg speed,
    //puts part in OD mode, sends rom
    //code at OD speeds.
    /****************************/
    int OW_ResetAndODMatchRom(char* Rom)
    {
    
            return 0;
    }
    

Children
  • I forgot to mention that this code uses a MCB2140 running at 60Mhz.

    Drew

  • //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?