大家好,STM8S005K6硬件I2C无法进入中断问题已经调试了五天了,目前剩下最后一招是自杀了。:Q
其中代码都是用官网自带的库,调试过程每个寄存器的值也看过,设置进去的值是对的。
先看代码
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); //时钟配置16M
//I2C初始化,全部都是官网固件库函数
void initI2C(void)
{
I2C_DeInit();
#ifdef I2C_slave_7Bits_Address
I2C_Init(100000, SLAVE_ADDRESS, I2C_DUTYCYCLE_2, I2C_ACK_CURR, I2C_ADDMODE_7BIT, 16); //我的是选择这个,地址0X30
#else
I2C_Init(100000, SLAVE_ADDRESS, I2C_DUTYCYCLE_2, I2C_ACK_CURR,I2C_ADDMODE_10BIT, 16);
#endif
/* Enable Error Interrupt*/
I2C_ITConfig((I2C_IT_TypeDef)(I2C_IT_ERR | I2C_IT_EVT | I2C_IT_BUF), 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->SR2 = 0;
}
Event = I2C_GetLastEvent();
switch (Event)
{
/******* Slave transmitter ******/
/* check on EV1 */
case I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED:
Tx_Idx = 0;
break;
/* check on EV3 */
case I2C_EVENT_SLAVE_BYTE_TRANSMITTING:
/* Transmit data */
I2C_SendData(Slave_Buffer_Rx[Tx_Idx++]);
break;
/******* Slave receiver **********/
/* check on EV1*/
case I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED:
break;
/* Check on EV2*/
case I2C_EVENT_SLAVE_BYTE_RECEIVED:
Slave_Buffer_Rx[Rx_Idx++] = I2C_ReceiveData();
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;
}
if(Tx_Idx == 255) Tx_Idx=0;
if(Rx_Idx == 255) Rx_Idx=0;
}
更让我不解的是同一个代码我工程配置为STM8S003F3则中断正常,配置为STM8S005K6则就进不了中断。
我的STM8S005K6 I2C是做从机,主机发送过来波行已经量过是正确的,STM8S005K6从机没有返回ACK。
请大家赐教啊...项目万分火急中...
|