新手上路
- 积分
- 25
- 金钱
- 25
- 注册时间
- 2013-1-8
- 在线时间
- 0 小时
|
发表于 2013-1-11 14:06:18
|
显示全部楼层
回复【2楼】304301959:
---------------------------------
你用下面的初始化步骤试一下,我的I2C现在正常了
void I2CMasterInit(void)
{
// I2C_DeInit(I2C_MASTER);
I2CMaster_RCC_Configuration();
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);// enable GPIOB CLOCK
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
/* Reset sEE_I2C IP */
RCC_APB1PeriphResetCmd(I2C_MASTER_CLK, ENABLE);
/* Release reset signal of sEE_I2C IP */
RCC_APB1PeriphResetCmd(I2C_MASTER_CLK, DISABLE);//非常重要,没有这句RESER语句会导致总线不能释放
I2CMaster_GPIO_Configuration();
// I2C_InitStructure.I2C_Mode = I2C_Mode_SMBusHost;//;I2C_Mode_I2C, very importan ,or I2C can not be work
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;//for fast model
I2C_InitStructure.I2C_OwnAddress1 = 0x00;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = I2C_MSATER_SPEED;
// I2CMaster_NVIC_Configuration();
/*!< Enable SMBus EVT interrupt */
I2C_Cmd(I2C_MASTER, ENABLE);
I2C_Init(I2C_MASTER, &I2C_InitStructure);
I2CMaster_NVIC_Configuration();//必须放在I2C_Init(I2C_MASTER, &I2C_InitStructure)函数后面,放在前面就不正常。2013.1.10 cheng yun
I2C_ITConfig(I2C_MASTER, I2C_IT_EVT | I2C_IT_ERR| I2C_IT_BUF, ENABLE);//| I2C_IT_ERR very importan ,or I2C can not be work
}
void I2CMaster_RCC_Configuration(void)
{
/* Enable peripheral clocks --------------------------------------------------*/
RCC_APB1PeriphClockCmd(I2C_MASTER_CLK, ENABLE);
}
void I2CMaster_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// I2C I0口初始化.
// Configure I2C1 pins: SCL and SDA ---------------------------------------
// GPIO_PinAFConfig(GPIOB, GPIO_PinSource6|GPIO_PinSource9, GPIO_AF_I2C1);//非常重要,一定不能这么写,否则SCL SDA都拉低,一定要分开写,见下面。2013.1.10 cheng yun
GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1);
// GPIO_InitStructure.GPIO_Pin = I2C_MASTER_SCL | I2C_MASTER_SDA;
//CONFIG scl
GPIO_InitStructure.GPIO_Pin = I2C_MASTER_SCL;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(I2C_MASTER_GPIO, &GPIO_InitStructure);
//CONFIG SDA
GPIO_InitStructure.GPIO_Pin = I2C_MASTER_SDA;
//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
//GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
//GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
//GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(I2C_MASTER_GPIO, &GPIO_InitStructure);
}
void I2CMaster_NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* 1 bit for pre-emption priority, 3 bits for subpriority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/* Configure and enable I2CMASTER interrupt --------------------------------*/
NVIC_InitStructure.NVIC_IRQChannel = I2C_MASTER_EV_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = NVICPreemptionPriority_I2CMASTER; //same as mido
NVIC_InitStructure.NVIC_IRQChannelSubPriority = NVICSubPriorityPriority_I2CMASTER_EV;
NVIC_Init(&NVIC_InitStructure);
/* Configure and enable SI2CMASTER interrupt --------------------------------*/
NVIC_InitStructure.NVIC_IRQChannel = I2C_MASTER_ER_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = NVICPreemptionPriority_I2CMASTER; //same as mido
NVIC_InitStructure.NVIC_IRQChannelSubPriority = NVICSubPriorityPriority_I2CMASTER_ER;
NVIC_Init(&NVIC_InitStructure);
}
你可以参考下库函数文件夹里面的例程,有个I2C的文件夹,也可以按照它的初始化步骤来。 |
|