void I2C_GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStruct; /* Enable GPIOA clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); /*!< sEE_I2C Periph clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1 , ENABLE); /*!< GPIO configuration */ /*!< Configure sEE_I2C pins: SCL */ GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;//GPIO_Mode_IN GPIO_InitStruct.GPIO_Speed = GPIO_Speed_Level_3; GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;//open-drain GPIO_Init(GPIOA , &GPIO_InitStruct); /*!< Configure sEE_I2C pins: SDA */ GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10; GPIO_Init(GPIOA , &GPIO_InitStruct); /* Connect PXx to I2C_SCL*/ GPIO_PinAFConfig( GPIOA , GPIO_PinSource9, GPIO_AF_1); /* Connect PXx to I2C_SDA*/ GPIO_PinAFConfig( GPIOA ,GPIO_PinSource10, GPIO_AF_1); } void I2C_Configuration(void) { I2C_InitTypeDef I2C_InitStruct; /* I2C configuration */ I2C_InitStruct.I2C_Mode = I2C_Mode_I2C; I2C_InitStruct.I2C_AnalogFilter = I2C_AnalogFilter_Enable; I2C_InitStruct.I2C_DigitalFilter = 0x00; I2C_InitStruct.I2C_OwnAddress1 =0xA0; I2C_InitStruct.I2C_Ack = I2C_Ack_Enable; I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStruct.I2C_Timing = 0x00100000;//100Kbits /* Apply I2C configuration after enabling it */ I2C_ITConfig(I2C2,I2C_IT_ADDRI,ENABLE); I2C_Cmd(I2C1, ENABLE); I2C_Init(I2C1, &I2C_InitStruct); /* I2C Peripheral Enable */ } void I2C1_IRQHandler(void) { I2C_ClearITPendingBit(I2C1, I2C_ISR_ADDR|I2C_ISR_STOPF ); LED_ON(); } #if 1 void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; /* Configure the NVIC Preemption Priority Bits */ NVIC_InitStructure.NVIC_IRQChannel = I2C1_IRQn ; //I2C???? NVIC_InitStructure.NVIC_IRQChannelPriority = 0x01; //?????1 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //?????? NVIC_Init(&NVIC_InitStructure); //??NVIC_InitStruct???????????NVIC??? //I2C_ITConfig(I2C2, I2C_IT_EVT, ENABLE); } #else void NVIC_Configuration(void){} #endif int main(void) { ALL_Config(); NVIC_Configuration(); I2C_GPIO_Configuration(); I2C_Configuration(); while(1) { } } |
将 GPIO_AF_1改 GPIO_AF_4试试吧,我也刚搞这个,还没搞好,大家分享一下 |