在线时间3 小时
UID3269450
ST金币0
蝴蝶豆0
注册时间2016-5-8
新手上路
- 最后登录
- 2020-5-18
|
发表于 2016-5-8 01:21:26
|
显示全部楼层
a0a.1 0b0c
******************************************************************************/
void I2C_SlaveInit(void)
{
I2C_DeInit();
/*
* 将I2C配置为:
* 波特率 = 100K
* 从地址0x40
* 使能接收和发送
* 使能接收中断
*/
I2C_Init(100000, I2C_SLAVE_ADDRES, I2C_DUTYCYCLE_2, I2C_ACK_CURR, I2C_ADDMODE_7BIT, 16);
I2C_ITConfig((I2C_IT_TypeDef)(I2C_IT_ERR | I2C_IT_EVT | I2C_IT_BUF), ENABLE);
I2C_Cmd(ENABLE);
}
INTERRUPT_HANDLER(I2C_IRQHandler, 19)
{
/* In order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction.
*/
/* Read SR2 register to get I2C error */
if ((I2C->SR2) != 0)
{
/* Clears SR2 register */
// i2c.Event=I2C->SR2;
I2C->SR2 = 0;
/* Set LED2 */
// STM_EVAL_LEDOn(LED2);
}
i2c.Event = I2C_GetLastEvent();
switch (i2c.Event)
{
/******* Slave transmitter ******/
/* check on EV1 */
case I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED:
I2C_SendData(i2c.Slave_Buffer_Tx[i2c.Tx_Idx]);
i2c.Tx_Idx++;
break;
/* check on EV3 */
case I2C_EVENT_SLAVE_BYTE_TRANSMITTING:
break;
/******* Slave receiver **********/
/* check on EV1*/
case I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED:
i2c.Rx_Add_MATCHED=1;
break;
/* Check on EV2*/
case I2C_EVENT_SLAVE_BYTE_RECEIVED:
if(i2c.Rx_Add_MATCHED==1)
{
i2c.Rx_Add_MATCHED=0;
i2c.Tx_Idx=I2C_ReceiveData(); //确定修改或者读寄存器地址
}
else
{
i2c.Slave_Buffer_Tx[i2c.Tx_Idx] = I2C_ReceiveData();
i2c.Tx_Idx++;
}
break;
/* Check on EV4 */
case (I2C_EVENT_SLAVE_STOP_DETECTED):
/* write to CR2 to clear STOPF flag */
I2C->CR2 |= I2C_CR2_ACK;
break;
default:
break;
}
}
/**********************************************************/
发现进入中断卡死;从示波器看到 主机给103从机发送数据后没有ACK应答返回导致通信停止。。。。。。。。。 |
|