Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Add I2C "device available" code ?

Status
Not open for further replies.

tubos

New Member
How can i add a way to check if an I2C device is active and can respond?
For example checking if the DS1631 sensor is seated in its socket or not?

Code is C18 for 18F26K22

Code:
unsigned char Init_DS1631(void)
{
	unsigned char Idata=0b10001101; // Config=12bit / 1shot
	OpenI2C2(MASTER, SLEW_OFF);     // Initialize I2C module
	SSP2ADD=0x13;                              // 100khz = 8Mhz / ( ( 0x13+1) * 4 )
	IdleI2C2(); 
	StartI2C2();                                     // Generate I2C2 start condition
	IdleI2C2(); 
	WriteI2C2(0x90);                            // Address
	IdleI2C2();
	WriteI2C2(0xAC);                           // Config
	IdleI2C2();
	WriteI2C2(Idata);                          // Write config data
	IdleI2C2();
	StopI2C2(); 	
					
}
 
Last edited:
Read the configuration register immediately after power up. It should equal 0b100011XX when the chip is in its socket.
 
I'm not sure. I'm not too familiar with I2C having had not much to do with it.

Nearly always though I will read a known-value register and make sure the data is correct to check if a hardware item is available.
 
Is there no other way that checks if the device responds with ack or Nack ?

Of course there is..... If you check the nack rather than assume its there you can return false...

Here is my write routine... If it fails on the address it returns false.

Code:
char E_Write(int addr, unsigned char* ch, char len)
	{
	I2C_Start();									// Send a start condition
	I2C_Write(WRITE);								// chip write address
	if(!I2C_nack()) return 0;						// wait for ack
	if(WRITE == 0xA0)
		{
		I2C_Write((unsigned char)( addr >> 8 & 0xff));// Not used if out if single byte address
		if(!I2C_nack()) return 0;					// wait for ack
		}
	if(!(WRITE==0x90))	
		{
		I2C_Write((unsigned char) addr & 0xff);		// low address (ADC not used)
		if(!I2C_nack()) return 0;					// wait for ack
		}
	if(len == 1)
		{
		I2C_Write(*ch);
		if(!I2C_nack()) return 0;
		}
	else if(len > 1)
		{					
		while(len--)
			{
			I2C_Write(*ch++);						// Write values to Eeprom
			if(!I2C_nack()) return 0;		
			}
		}
	else
		{
		while(*ch != 0)
			{
			I2C_Write(*ch++);						// Write values to Eeprom
			if(!I2C_nack()) return 0;
			}
		I2C_Write(0);								// Write values to Eeprom
		if(!I2C_nack()) return 0;
		}
	I2C_Stop();										// Send a stop.
	return 1;										// All went well
	}

The return is for good or bad... if the code returns 1 then the chip is there... otherwise there is a problem...
 
Thanks all, I finally got it working myself with C18 compiler.
Here is my code:

Code:
unsigned char Init_DS1631(unsigned char dev_address) // Returns 0xff on error , 0 on success
{
	unsigned char Idata=0b10001101; // Config=12bit / 1shot
	OpenI2C2(MASTER, SLEW_OFF);		// Initialize I2C module
	SSP2ADD=0x13;					// 100khz = 8Mhz / ( ( 0x13+1) * 4 )
	IdleI2C2(); 
	StartI2C2();					// Generate I2C2 start condition
	IdleI2C2(); 
	if(WriteI2C2(dev_address)!=0) 			// 
     {
		StopI2C2();					
		CloseI2C2();
		return 0xff;				// Error :no reaction from sensor
     }
	else 
	 {
		IdleI2C2();
		WriteI2C2(0xAC);			// Send config command
		IdleI2C2();
		WriteI2C2(Idata);			// Send our configuration
		IdleI2C2();
		StopI2C2(); 
		return 0;					// All went well	
	 }
}
 
Remember that after a write you will not receive an Ack. For that reason, when I write the address I keep retrying for 10mS before concluding that the chip isn't there.

Mike.
 
I think you mean that if the chip isnt there you will not receive an ack , because it wasnt clear to me.
An Ack should be returned by the sensor after a write.

I tried both removing the chip or disconnecting the communication lines
and it works as expected.

So it must be that the WriteI2C2 function takes care of that.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top