cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to communicate to external peripheral device using I2C.

NPaba
Associate II

I am using STM8L Discovery board, and want to communicate with external memory and humidity sensor which requires I2C protocol for communication.

I am using STM8l15x standard peripheral library, but unable to communicate. I found the following problems while debug:

  1. If not connecting any pull-up resistor, not getting acknowledgment from slave device and getting stuck in while((I2C1->SR1 & I2C_SR1_ADDR) == 0) loop
  2. If connecting pull-up resistor on both SDA and SCL (1k and 4.7K tried), unable to generate start condition.

Please help me out how can I resolve it?

Thank you so much in advance.

13 REPLIES 13

Hello Cristian,

After removing SB17 and giving address 0xA0, it is working fine, but while writing I am able to write one byte only.

I modified the library as per 24AA512(giving 2 byte address - while library has one byte register addressing only).

I tried both the way, writing one byte at a time and multiple byte a time.

below code and file I used:

I2C_Init();

//one byte a time

for(i=0;i<strlen(SendBuf);i+=1)

{

I2C_WriteRegister(15+i,1,&SendBuf[i]);

}

//multiple byte

I2C_WriteRegister(15,strlen(SendBuf),SendBuf);

//reading back

for(i=0;i<strlen(SendBuf);i+=1)

{

I2C_ReadRegister(15+i,1,&ReceiveBuff[i]);

}

In ReceiveBuff I could see only byte updated for both cases

Cristian Gyorgy
Senior III

Hello Pabari!

I only had a short view on the code you posted, I think you are mistaking addresses. ​First of all, in master mode only (this is your case if you only have the EEPROM on the bus) you don't use the MCU's Own Address as it is not addressed as a slave.

The 0xA0 address is the slave's address, if you have more slaves on the same bus this is how you can access them, and if you always leftshift this address to add the R/W bit, than the 0x50 value was good.

​What you mention 2 byte addressing is actually the EEPROM's address where you want to write/read from.

Try to understand the EEPROM's datasheet, I will try to provide you some sample code.​

Hi Pabari!

I quickly checked the software you posted and I think you make a confusion between the I2C slave address and the EEPROM's data access word-address.

I just checked what software ST provides for this board/MCU, and there is a complete and documented driver software for all MCU peripherals in en.stsw-stm8016. Just download this standard peripheral library, and there you also have an example of EEPROM interface.

ST provides in this driver complete functions to access the EEPROM, just try to understand how to use them. Check the stm8_eval_i2c_ee.h/c files and also the example en.stsw-stm8016/STM8L15x-16x-05x-AL31-L_StdPeriph_Lib/main.c. You even have template files.

I never used such a library, as I write only assembler software, but you have everything you need in there.

S.Ma
Principal

yeah, everybody is confused as I2C address can be said to be 7 bits 0x50 = 101 0000 or 0xA0/A1 if you add the read/write bit to make it 8 bit.

In my case, when I boot, I sweep for all possible slave address and it gives me a list of detected slaves, faster than guessing how the datasheet presents it.