2012-06-17 04:09 PM
I want to send 6 Bytes by I2C to slave, but when I send address slave and wait for ACK the is no reaction. I used 10k pull-up resistor.
CLOCK_SPEED = 100000=100kHzBUFFERSIZE = 6/******************** main.c **********************/#include ''stm8l15x.h''
#include ''main.h''
/* -------------------------------- I2C ------------------------------------- */
uint8_t i = 0;
__IO uint8_t Tx_Idx = 0;
__IO uint8_t NumOfBytes = BUFFERSIZE;
extern __IO uint8_t TxBuffer[BUFFERSIZE];
static void I2C_Config(void);
void main(void)
{
I2C_Config();
/* Infinite loop */
while (1)
{}
}
static void I2C_Config(void)
{
/* TXBuffer initialization */
TxBuffer[0] = 0x00;
TxBuffer[1] = 0x81;
TxBuffer[2] = 0x08;
TxBuffer[3] = 0x50;
TxBuffer[4] = 0x01;
TxBuffer[5] = 0x15;
/* GPIOD configuration */
//GPIO_Init(GPIOC, GPIO_Pin_0|GPIO_Pin_1, GPIO_Mode_Out_OD_HiZ_Fast);
/* system_clock / 1 */
CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1);
/* I2C clock Enable*/
CLK_PeripheralClockConfig(CLK_Peripheral_I2C1, ENABLE);
/* Initialize I2C peripheral */
I2C_Init(I2C1, I2C_SPEED, 0xA0, I2C_Mode_I2C, I2C_DutyCycle_2, I2C_Ack_Enable, I2C_AcknowledgedAddress_7bit);
/* Enable Buffer and Event Interrupt*/
I2C_ITConfig(I2C1, (I2C_IT_TypeDef)(I2C_IT_EVT | I2C_IT_BUF) , ENABLE);
enableInterrupts();
I2C_Cmd(I2C1,ENABLE);
/* Send START condition */
I2C_GenerateSTART(I2C1, ENABLE);
while (NumOfBytes);
//while (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
}
/*************** stm8l151x_it.c *****************************/NTERRUPT_HANDLER(I2C1_SPI2_IRQHandler, 29)
{
switch (I2C_GetLastEvent(I2C1))
{
/* EV5 */
case I2C_EVENT_MASTER_MODE_SELECT :
/* Send slave Address for write */
I2C_Send7bitAddress(I2C1, SLAVE_ADDRESS, I2C_Direction_Transmitter);
break;
/* EV6 */
case I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED:
if (NumOfBytes != 0)
{
/* Send the first Data */
I2C_SendData(I2C1, TxBuffer[Tx_Idx++]);
/* Decrement number of bytes */
NumOfBytes--;
}
if (NumOfBytes == 0)
{
I2C_ITConfig(I2C1, I2C_IT_BUF, DISABLE);
}
break;
/* EV8 */
case I2C_EVENT_MASTER_BYTE_TRANSMITTING:
/* Transmit Data */
I2C_SendData(I2C1, TxBuffer[Tx_Idx++]);
/* Decrement number of bytes */
NumOfBytes--;
if (NumOfBytes == 0)
{
I2C_ITConfig(I2C1, I2C_IT_BUF, DISABLE);
}
break;
/* EV8_2 */
case I2C_EVENT_MASTER_BYTE_TRANSMITTED:
/* Send STOP condition */
I2C_GenerateSTOP(I2C1, ENABLE);
I2C_ITConfig(I2C1, I2C_IT_EVT, DISABLE);
break;
default:
break;
}
}
The last action, which I observed was in INTERRUPT_HANDLER (I2C_Send7bitAddress(I2C1, SLAVE_ADDRESS, I2C_Direction_Transmitter);)
2012-06-18 05:32 AM
I propose to check the I2C bus while you send the address.
If the device is addressed, it will send an ACK, i.e. pull down SDA while the 9.th SCL cycle. If you don't see this, either the address is not correct, or there is some hardware issue.2012-06-19 08:01 AM
Read the errata sheet on the device. There are some I2C issues and workarounds.
2013-03-24 12:05 PM
I have got the same problem, did you find the solution?
I would be helpfulregards