cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with I2C on STM8L151

andy2
Associate II
Posted on November 04, 2014 at 15:20

Hello, I tried distilling the peripheral library sample on i2c to make it simpler for my own use. However, strangely I cannot get pass a the send address part, and can't figure out why. Can someone give me some direction? Attached is the code I used, and the line with problem is at

''while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) '' inside readRegistry function. Thanks.

#include ''stm8l15x.h''
void initI2C(void);
uint16_t readRegistry(uint8_t address, uint8_t reg);
#define HMC5883_ADDR (0x1E) 
#define I2C_SPEED 100000 /*!< I2C Speed */
#define I2C_SCL_PIN GPIO_Pin_1 /* PC.01 */
#define I2C_SCL_GPIO_PORT GPIOC /* GPIOC */
#define I2C_SDA_PIN GPIO_Pin_0 /* PC.00 */
#define I2C_SDA_GPIO_PORT GPIOC /* GPIOC */
void main(void)
{
CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1);
enableInterrupts();
initI2C();
readRegistry(HMC5883_ADDR, 4);
while
(1)
{
}
}
void initI2C(void)
{
GPIO_Init(GPIOC, GPIO_Pin_0, GPIO_Mode_Out_OD_HiZ_Fast);
GPIO_Init(GPIOC, GPIO_Pin_1, GPIO_Mode_Out_OD_HiZ_Fast);
CLK_PeripheralClockConfig(CLK_Peripheral_I2C1, ENABLE);
I2C_Init(I2C1, I2C_SPEED, 0xA0, I2C_Mode_I2C, I2C_DutyCycle_2, I2C_Ack_Enable, I2C_AcknowledgedAddress_7bit);
I2C_ITConfig(I2C1, I2C_IT_ERR, ENABLE); 
I2C_Cmd(I2C1, ENABLE);
}
uint16_t readRegistry(uint8_t address, uint8_t reg)
{
uint16_t RegValue=0;
I2C_AcknowledgeConfig(I2C1, DISABLE);
I2C_GenerateSTART(I2C1, ENABLE);
while
(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) 
/* EV5 */
{
}
I2C_Send7bitAddress(I2C1, address, I2C_Direction_Transmitter);
while
(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) 
/* EV6 <--- EVERYTHING STOPS HERE */
{
}
I2C_SendData(I2C1, reg);
while
(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) 
/* EV8 */
{
}
I2C_GenerateSTART(I2C1, ENABLE);
while
(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) 
/* EV5 */
{
}
I2C_Send7bitAddress(I2C1, address, I2C_Direction_Receiver);
while
(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) 
/* EV6 */
{
}
while
(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) 
/* EV7 */
{
}
RegValue = I2C_ReceiveData(I2C1);
I2C_AcknowledgeConfig(I2C1, DISABLE);
I2C_GenerateSTOP(I2C1, ENABLE);
while
(I2C_GetFlagStatus(I2C1, I2C_FLAG_RXNE) == RESET)
{}
return
RegValue;
}

2 REPLIES 2
missubeemail
Associate
Posted on November 06, 2014 at 06:01

I have same problem. I use STM8L Discovery control DS1307, when I send

 I2C_GenerateSTART

() function it's good but when I send

I2C_Send7bitAddress

() function, I2C1->DR = ---- it doesn't get value and I2C1->SR1 (ADDR) bit is not set. Because it doesn't get out

while
(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))

; //EV6. Can someone help me to solver problem ?

PS: i debug on IAR

selcukozb
Associate III
Posted on November 06, 2014 at 08:22

Hi winix,

It seems that you do not get ACK from your chip. If your address is really 0x1E, you must shift it 1 bit left, otherwise it will be transmitted as 0xF. If that does not work you can try to reduce the speed.

Good luck.