大虾们,我遇到一个问题,如下: 我用一个NXP单片机做I2C的主机,用一个STM32单片机做从机,STM32单片机用中断接收数据,但是我发现当它们互连之后,NXP单片机主机发不出数据,用示波器查看STM32的SCL和SDA管脚的电平为“0“,如果我把STM32单片机移走之后,NXP单片机发送数据正常,我把源码贴出来让各位给我分析分析. void GPIO_Init(void) { IO_InitTypeDef GPIOB_InitStructure; GPIOB_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIOB_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIOB_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIOB_InitStructure);
/* configure I2C1 pins: SCL and SDA ---------------*/ GPIOB_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11; GPIOB_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIOB_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_Init(GPIOB, &GPIOB_InitStructure); } void MyI2CInit(void) {
I2C_InitTypeDef I2C_InitStructure; I2C_DeInit(I2C2);
// Enable I2C2 I2C_Cmd(I2C2, ENABLE);
// Enable I2C2 event and buffer interrupt I2C_ITConfig(I2C2, I2C_IT_EVT | I2C_IT_BUF, ENABLE);
// I2C2configuration I2C_InitStructure.I2C_Mode = I2C_Mode_SMBusDevice; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = I2C1_SLAVE_ADDRESS7; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = cknowledgedAddress_7bit; //I2C_InitStructure.I2C_ClockSpeed = ClockSpeed; I2C_Init(I2C2, &I2C_InitStructure); } void I2C2_EV_IRQHandler(void) { switch (I2C_GetLastEvent(I2C2)) { //Test on I2C2 EV1 and clear it case I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED: break;
// Test on I2C2 EV2 and clear it case I2C_EVENT_SLAVE_BYTE_RECEIVED: if(GPIOB->ODR & GPIO_Pin_9) GPIOB->BSRR=GPIO_Pin_9; else GPIOB->BRR=GPIO_Pin_9; break;
// Test on I2C2 EV4 and clear it case I2C_EVENT_SLAVE_STOP_DETECTED: // Clear STOPF flag I2C_ClearFlag(I2C2, I2C_FLAG_STOPF); // Disable I2C2 interrupts //I2C_ITConfig(I2C2, I2C_IT_EVT | I2C_IT_BUF, DISABLE); break; default: break; } } void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); // Set the Vector Table base location at 0x20000000
#else // VECT_TAB_FLASH NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); // Set the Vector Table base location at 0x08000000 #endif NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); /* Configure and enable I2C1 interrupt ----------------*/ NVIC_InitStructure.NVIC_IRQChannel = I2C2_EV_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);
}
int main (void) { SetupClock(); NVIC_Configuration(); SysTick_CounterCmd(SysTick_Counter_Enable); GPIOInit(); MyI2CInit(); while (1) ; }
感觉好像我的MyI2CInit()配置有问题,但是又不知道问题在哪里,请大虾们不吝赐教。 我已经把两个模块共地了。
|