cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F100RB will not communicate with EEPROM

tomwilliamcole
Associate
Posted on September 27, 2013 at 12:40

Hello guys , I'm trying to write then read to the AT24C256 EEPROM.(

http://www.atmel.com/Images/doc06pdf

)datasheet. After debugging the code gets stuck at the flag check. (!I2C_CheckEvent(I2C2,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) The I2C is outputting what i want on the oscilloscope and the address pins on the EEPROM and Write Protect have been tied to ground. If anyone could help that would be great. here is the code:


#include <
stm32f10x_i2c.h
>

#include <
stm32f10x_gpio.h
>

#include <
stm32f10x_rcc.h
>

#include ''stm32F10x.h''




#define I2C I2C2

#define I2C_EE_CLK RCC_APB1Periph_I2C2

#define I2C_GPIO GPIOB

#define I2C_GPIO_CLK RCC_APB2Periph_GPIOB

#define I2C_SCL GPIO_Pin_10

#define I2C_SDA GPIO_Pin_11






void Delay(__IO uint32_t nCount)

{

for(; nCount != 0; nCount--);

}


uint8_t I2C_read_ack(I2C_TypeDef* I2Cx){

// enable acknowledge of recieved data

I2C_AcknowledgeConfig(I2C2, ENABLE);

// wait until one byte has been received

while( !I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED) );

// read data from I2C data register and return data byte

uint8_t data = I2C_ReceiveData(I2C2);

return data;

}




int main(void)

{

RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2 , ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);




////////////////////////////////////////////////////////////////////////////////

//---------------------------I2C----------------------------------------------

//??????????????????????????initiation///////////////////////////////////////



//set-up pins

GPIO_InitTypeDef GPIO_InitStructure;

GPIO_InitStructure.GPIO_Pin = I2C_SCL | I2C_SDA; // sda =7 scl = 6

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_Init(I2C_GPIO , &GPIO_InitStructure);


I2C_Cmd(I2C2, ENABLE);

I2C_InitTypeDef I2C_InitStructure;


I2C_StructInit (& I2C_InitStructure);

I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;

I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;

I2C_InitStructure.I2C_OwnAddress1 = 0xAE;

I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;

I2C_InitStructure.I2C_AcknowledgedAddress =

I2C_AcknowledgedAddress_7bit;

I2C_InitStructure.I2C_ClockSpeed = 100000;

I2C_Init(I2C2 , &I2C_InitStructure);


//int i = 0;

uint8_t EEPROMaddress = (0b1010000)<<1;


while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY));

I2C_GenerateSTART(I2C2, ENABLE);

while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));

asm(''NOP'');

I2C_Send7bitAddress(I2C2, 0xA0, I2C_Direction_Receiver);

while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));



asm(''NOP'');


I2C_SendData(I2C2, 0x00);

while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

I2C_SendData(I2C2, 0x00);

while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

I2C_SendData(I2C2, 0x00);

while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));


I2C_GenerateSTOP(I2C2, ENABLE);

asm(''NOP'');



I2C_GenerateSTART(I2C2, ENABLE);

while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));

I2C_Send7bitAddress(I2C2, 0xA0, I2C_Direction_Transmitter);

while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

I2C_SendData(I2C2, 0x00);

while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

I2C_SendData(I2C2, 0x00);

while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

I2C_Send7bitAddress(I2C2, 0xA0, I2C_Direction_Receiver);

while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));

int testbyte = I2C_ReceiveData(I2C2);

I2C_GenerateSTOP(I2C2, ENABLE);


asm(''NOP'');



}

thanks for taking the time to read. #stm32 #i2c #discovery #ieeprom
0 REPLIES 0