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

Ic2 programming interfacing DS895450

Hi,
This programing is to Start I2c Communication with the basic start up code followed by sending a 8 bit address together with a write bit. Next, it will send a string a serial port to show a string "nack" only if the SDA is not pulled low, which means not acknowledged.

Chip used: Dallas DS89C450 11.0592Mhz
Accelerometer: SEN 10955 2.25 Mhz

Problem faced: SDA could not be pulled low. Nack received.

Question: May i know is there any faults in this program?

#include <DS89C4XX.h>     //DS89C450 Microcontroller
#include <intrins.h>      //_nop_() function

sbit SDA=P1^0;
sbit SCL=P1^1;
//--------------------------Transmit----------------------------------
void Uart_Tx2(unsigned char c)
{
        while (TI_0==0);
        TI_0=0;
        SBUF0=c;
}

// ----------------------Transmit string ------------------------------------------------------
void sendstring (char *String)
{
        int i=0;        //set counter to 0
        while(String[i])
        {
                Uart_Tx2(String[i++]);
        }

}


//-------------------------------Delay -----------------------------------------
void I2CBitDly()   // wait approximately 4.7uS
{ // tune to xtal. This works at 11.0592MHz
unsigned int time_end = 9;
unsigned int index;
for (index = 0; index < time_end; index++);
return;
}


//-------------------------------------------Start--------------------------
void I2CSendStart(void)
{
SDA = 1;
I2CBitDly();
SCL = 1;
I2CBitDly();
SDA = 0;
I2CBitDly();
SCL = 0;
I2CBitDly();
}

//-------------------------------------------STOP-----------------------------
void I2CSendStop(void)
{
SDA = 0;
I2CBitDly();
SCL = 1;
I2CBitDly();
SDA = 1;
I2CBitDly();
}

//-----------------------------------------SEND data---------------------------
void I2CSendByte(unsigned char bt)
{
register unsigned char i;
for (i=0; i<8; i++)
{
if ((bt<<i)&0x80) SDA = 1; // Send each bit
else SDA = 0;

SCL = 1;
I2CBitDly();
SCL = 0;
I2CBitDly();

}
SDA = 1; // make P1.0 an input.
I2CBitDly();
SCL = 1;        // 9th clock high.
I2CBitDly();


if (SDA == 1) //
{
        sendstring("nack");             // SDA is not pulled low. nack is sent.

}
   SCL = 0;


I2CBitDly();
//SDA = 1; // end transmission
SCL = 1;
}



//---------------MAIN---------------------------------
void main ()
{

         SCON1  = 0x52;
         SCON0 = 0x52;
         TMOD  = 0x20;
         TCON  = 0x69;
         TH1   = 0xFD;  //Buad Rate: 9600 bps



         I2CSendStart();
         I2CSendByte(0x38);

   while(1)
   {

   };
}

Parents
  • A large percentage of embedded devices will probably see no performance loss from sw-implemented I2C. Lots of equipment needs an area for storage of some configuration. So it's enough to read out the configuration on boot, and to rewrite one or more configuration entries when the user enters the menu system to reconfigure the device.

    It's only data loggers or similar devices that is constantly reading/writing data that have to consider the percentage of time the device is locked up doing I2C communication.

    But the big question here is that since sw-implemented I2C is so very portable (it's just how to read and write the pins and how to create the delay that depends on the processor), there are lots of well-working code out there. So why get stuck with broken code, unless it is a school assignment to implement the algorithm?

Reply
  • A large percentage of embedded devices will probably see no performance loss from sw-implemented I2C. Lots of equipment needs an area for storage of some configuration. So it's enough to read out the configuration on boot, and to rewrite one or more configuration entries when the user enters the menu system to reconfigure the device.

    It's only data loggers or similar devices that is constantly reading/writing data that have to consider the percentage of time the device is locked up doing I2C communication.

    But the big question here is that since sw-implemented I2C is so very portable (it's just how to read and write the pins and how to create the delay that depends on the processor), there are lots of well-working code out there. So why get stuck with broken code, unless it is a school assignment to implement the algorithm?

Children
No data