主机的中断服务程序在官方样例基础上缩减:
#define SLAVE_ADDRESS 0x30
__IO uint8_t TxBuffer[32] = "Get it!\n";
__IO uint8_t NumOfBytes = 9;
__IO uint8_t Tx_Idx =0;
INTERRUPT_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;
}
}
|