#define _I2C_DEBUG_ #ifdef _I2C_DEBUG_ u32 gAu32Tmp[100]; #endif // I2C1中断服务程序. void I2C1_EV_IRQHandler(void) { u32 lu32Event; static u8 lu8BusyCount = 0; static u16 lu16StaCounter = 0; lu32Event = I2C_GetLastEvent(I2C1); #ifdef _I2C_DEBUG_ if( lu32Event != I2C_EVENT_MASTER_BYTE_TRANSMITTING) { gAu32Tmp[lu16StaCounter++] = lu32Event; if( lu16StaCounter >= 100 ) { lu16StaCounter = 0; } } #endif if( (lu32Event & 0x00020000) == 0x00020000) { lu8BusyCount ++; } else { lu8BusyCount = 0; } switch ( lu32Event ) { case I2C_EVENT_MASTER_MODE_SELECT: // 0x00030001. 发启动条件时产生的事件: EV5 if( gstruI2C_ComInst.m_u8Direct == CNT_I2C_TRANSMITTER ) { // Master Transmitter, then Send slave Address for write. I2C_Send7bitAddress(I2C1, gstruI2C_ComInst.m_u8DevAdd, I2C_Direction_Transmitter); } else { // Master Receiver, Send slave Address for read. I2C_Send7bitAddress(I2C1, gstruI2C_ComInst.m_u8DevAdd, I2C_Direction_Receiver); } break; // Master Transmitter, then Test on I2C1 EV6 and first EV8 and clear them. case I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED: // 0x00070082. 发出写指定I2C从设备时产生的事件:EV8 just after EV6 // Send the first data. I2C_SendData(I2C1, gstruI2C_ComInst.m_Au8SndOrRecBuf[gstruI2C_ComInst.m_u8DatIdx]); gstruI2C_ComInst.m_u8DatIdx++; break; case I2C_EVENT_MASTER_BYTE_TRANSMITTING: // 0x00070080. 正在发送数据中...... lu8BusyCount = 0; break; // Test on I2C1 EV8 and clear it. case I2C_EVENT_MASTER_BYTE_TRANSMITTED: // 0x00070084. 一个字节数据发送完成. if(gstruI2C_ComInst.m_u8DatIdx < gstruI2C_ComInst.m_u8SndOrRecLen) { // Transmit I2C1 data I2C_SendData(I2C1, gstruI2C_ComInst.m_Au8SndOrRecBuf[gstruI2C_ComInst.m_u8DatIdx]); gstruI2C_ComInst.m_u8DatIdx ++; } else { // Send I2C1 STOP Condition I2C_GenerateSTOP(I2C1, ENABLE); delay( 50 ); // NOTE: 非常关键哟,不同的器件,延时可能不一样. gstruI2C_ComInst.m_u8Finished = CNT_I2C_FINISHED_YES; } lu8BusyCount = 0; // 发送了数据,不为busy. break; // Master Receiver case I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED: // 0x00030002. 发出读指定I2C从设备时产生的事件:EV6 if(gstruI2C_ComInst.m_u8SndOrRecLen == 1) { // Disable I2C1 acknowledgement I2C_AcknowledgeConfig(I2C1, DISABLE); // Send I2C1 STOP Condition I2C_GenerateSTOP(I2C1, ENABLE); } break; case I2C_EVENT_MASTER_BYTE_RECEIVED: //0x00030040. 主收到一个字节时产生的事件:EV7. // BUSY, MSL and RXNE flags. case 0x00030044: // BUSY, MSL and RXNE, BTF flags. // Store I2C1 received data gstruI2C_ComInst.m_Au8SndOrRecBuf[gstruI2C_ComInst.m_u8DatIdx++] = I2C_ReceiveData(I2C1); if( gstruI2C_ComInst.m_u8DatIdx >= gstruI2C_ComInst.m_u8SndOrRecLen ) { gstruI2C_ComInst.m_u8Finished = CNT_I2C_FINISHED_YES; } // Disable ACK and send I2C1 STOP condition before receiving the last data // 收到倒数第二个数后,应设置NACK和产生STOP标志. if( gstruI2C_ComInst.m_u8DatIdx == (gstruI2C_ComInst.m_u8SndOrRecLen - 1)) { // Disable I2C1 acknowledgement. I2C_AcknowledgeConfig(I2C1, DISABLE); // Send I2C1 STOP Condition. I2C_GenerateSTOP(I2C1, ENABLE); } lu8BusyCount = 0; // 收到数据,不为busy. break; case 0x00030201: case 0x00030401: case 0x00030501: I2C_GenerateSTOP(I2C1, ENABLE); break; default: if( lu8BusyCount > 200 ) { lu8BusyCount = 0; I2C1_Configuration(); I2C_Cmd( I2C1, DISABLE ); I2C_Cmd( I2C1, ENABLE ); } break; } } |