一、RCC初始化
-
/* Setup the microcontroller system. Initialize the Embedded Flash Interface,
-
initialize the PLL and update the SystemFrequency variable. */
-
SystemInit();
-
-
/* Enable peripheral clocks --------------------------------------------------*/
-
/* GPIOB Periph clock enable */
-
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
-
-
//启动GPIO模块时钟
-
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
-
//把雕饰设置普通IO口
-
GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable,ENABLE);
-
-
/* I2C1 Periph clock enable */
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
二、I2C_初始化
-
-
/* Configure I2C1 pins: SCL and SDA */
-
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
-
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
-
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
-
GPIO_Init(GPIOB, &GPIO_InitStructure);
-
-
I2C_InitTypeDef I2C_InitStructure;
-
-
/* I2C configuration */
-
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
-
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 = I2C_AcknowledgedAddress_7bit;
-
I2C_InitStructure.I2C_ClockSpeed = 100000;
-
-
/* I2C Peripheral Enable */
-
I2C_Cmd(I2C1, ENABLE);
-
/* Apply I2C configuration after enabling it */
- I2C_Init(I2C1, &I2C_InitStructure);
三、USART初始化
-
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
-
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
-
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
-
GPIO_Init(GPIOA, &GPIO_InitStructure);
-
-
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
-
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
-
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
-
GPIO_Init(GPIOA, &GPIO_InitStructure);
-
-
USART_InitStructure.USART_BaudRate = 115200;
-
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
-
USART_InitStructure.USART_StopBits = USART_StopBits_1;
-
USART_InitStructure.USART_Parity = USART_Parity_No;
-
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
-
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
-
USART_Init(USART1, &USART_InitStructure);
- USART_Cmd(USART1, ENABLE);
四、main函数测试程序
-
while (1)
- {
- I2C_EE_BufferWrite(ReadBuff, 0, 3);
-
I2C_EE_BufferRead(SendBuff, 0, 3);
-
Delay(0xfffff);
- fPutString(SendBuff, 15);
- }
遇到的问题,我用以前的RCC初始化程序怎么都无法启动I2C。一直在 while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)); 中跑。
现在还没有搞清楚,I2C需要怎么设置时钟。