cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F0 discovery board with I2C

setartarney
Associate II
Posted on August 02, 2014 at 18:40

Hello, I want to communicate with an I2C eeprom but I don't know how to read/write bytes and how to sen the adress. I have an initialisation code that I found on the forum but I am stuck.

does anybody now how to communicate with that eeprom? that is my initialisation code:

void set_I2C(void){
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_1);
//Configure pins: SCL and SDA ------------------
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
I2C_DeInit(I2C1);
I2C_Cmd(I2C1, DISABLE);
I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
I2C_InitStructure.I2C_DigitalFilter = 0x00;
I2C_InitStructure.I2C_OwnAddress1 = 0x40;
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
//I2C_InitStructure.I2C_Timing = 0xA0120227;
//I2C_InitStructure.I2C_Timing = 0x20310A0D;
I2C_InitStructure.I2C_Timing =0xB0420F13; //100KHz (see page 529 - table 73 in the manual reference)
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_Cmd(I2C1, ENABLE);
I2C_Init(I2C1, &I2C_InitStructure);
I2C_ITConfig(I2C1, I2C_FLAG_TXE, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
I2C_DeInit(I2C2);
I2C_Cmd(I2C2, DISABLE);
I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Disable;
I2C_InitStructure.I2C_DigitalFilter = 0x00;
I2C_InitStructure.I2C_OwnAddress1 = 0x30;
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_Timing =0xB0420F13;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_Cmd(I2C2, ENABLE);
I2C_Init(I2C2, &I2C_InitStructure);
I2C_ITConfig(I2C2, I2C_AcknowledgedAddress_7bit, ENABLE);
}

#stm32f0 #stm32 #i2c
3 REPLIES 3
Posted on August 02, 2014 at 19:12

STM32F0xx_StdPeriph_Lib_V1.1.0\Project\STM32F0xx_StdPeriph_Examples\I2C\I2C_EEPROM\readme.txt

Most EEPROM come with manuals describing their address, and command functions.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
setartarney
Associate II
Posted on August 03, 2014 at 11:31

It finally works!!! You were right, I entered a small mistake in the adress of the eeprom so it didn't acknoledge but I was shure that it was the right adress. Now I2C_SendData works.

setartarney
Associate II
Posted on August 04, 2014 at 11:35

Now, I have a problem with I2C_NumberOfBytesConfig. The eeprom acknoledge the start command and the first data is transmitted but not the second data and when I use I2C_GenerateSTOP, the start command is transmitted but after that, no data is transmitted at all.

int main(void)
{
set_I2C();
while(1)
{
I2C_SlaveAddressConfig(I2C1, 0b10100000);
I2C_GenerateSTART(I2C1, ENABLE);
I2C_NumberOfBytesConfig(I2C1, 8);
I2C_SendData(I2C1,0b00000000);
I2C_NumberOfBytesConfig(I2C1, 8);
I2C_SendData(I2C1,0b00000000);
I2C_GenerateSTOP(I2C1, ENABLE);
while(1);
}
}