cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f0 discovery I2C not stable

setartarney
Associate II
Posted on August 05, 2014 at 15:04

hello, I try to communicate with an eeprom and I was able to initialise I2C1, the eeprom acknoledges when I send the adress command and I am able to send data to the I2C but the data are not allways the same, some times they are right and some times, wrong and the when I use the stop command, the eeprom acknoledges the adressing command but I am not able to send any data.

there is my full code:

/* Includes */
#include <
stddef.h
>
#include ''stm32f0xx_rcc.h''
#include ''stm32f0xx_gpio.h''
#include ''stm32f0xx_i2c.h''
void set_I2C(void);
int i;
/* Private typedef */
/* Private define */
/* Private macro */
/* Private variables */
/* Private function prototypes */
/* Private functions */
/* Global variables */
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);
//Configure pins: SCL and SDA ------------------
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
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);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_1);
I2C_DeInit(I2C1);
I2C_Cmd(I2C1, DISABLE);
I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
I2C_InitStructure.I2C_DigitalFilter = 0xff;
I2C_InitStructure.I2C_OwnAddress1 = 0x00;
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_Ack = I2C_Ack_Disable;
I2C_InitStructure.I2C_Timing = 0xA0120227;
//I2C_InitStructure.I2C_Timing = 0x20310A0D;
//I2C_InitStructure.I2C_Timing =0xB0420F13;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_Init(I2C1, &I2C_InitStructure);
I2C_Cmd(I2C1, ENABLE);
//
I2C_ITConfig(I2C1, I2C_FLAG_TXE, ENABLE);
//I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; // I2C mode
//I2C_InitStructure.I2C_OwnAddress1 = 0x00; // own address, not relevant in master mode
// I2C_InitStructure.I2C_Ack = I2C_Ack_Disable; // disable acknowledge when reading (can be changed later on)
//I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; // set address length to 7 bit addresses
//I2C_Init(I2C1, &I2C_InitStructure); // init I2C1
// enable I2C1
//I2C_Cmd(I2C1, ENABLE);
}
int main(void)
{
set_I2C();
I2C_SlaveAddressConfig(I2C1, 0b10100000);
I2C_GenerateSTART(I2C1, ENABLE);
I2C_NumberOfBytesConfig(I2C1,1);
I2C_SendData(I2C1,0x00);
I2C_NumberOfBytesConfig(I2C1,2);
I2C_SendData(I2C1,0x00);
I2C_GenerateSTOP(I2C1, ENABLE);
}

#i2c #eeprom #stm32f0
0 REPLIES 0