程序很简单,就是先初始化GPIO,然后模式设置,结果程序就卡在while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY));一直不出来。麻烦大家帮我分析一下!
#include <stm32f4xx.h>
static void IIC1_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//打开B口的时钟,PB6->SCL,PB7->SDA
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
//先将对应寄存器配置为缺省值
GPIO_StructInit(&GPIO_InitStructure);
//选中6,7管脚
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7;
//复用功能
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
//IIC最大速度为400K
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
//开漏输出
GPIO_InitStructure.GPIO_OType=GPIO_OType_OD;
//不带上拉
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;
//将IO配置应用于对应寄存器
GPIO_Init(GPIOB,&GPIO_InitStructure);
//管脚映射
GPIO_PinAFConfig(GPIOB,GPIO_Pin_6,GPIO_AF_I2C1);
GPIO_PinAFConfig(GPIOB,GPIO_Pin_7,GPIO_AF_I2C1);
}
static void IIC1_MODE_Configuration(void)
{
I2C_InitTypeDef I2C_InitStrycture;
//打开IIC1时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
//典型IIC速度为100K
I2C_InitStrycture.I2C_ClockSpeed=100000;
//IIC模式
I2C_InitStrycture.I2C_Mode=I2C_Mode_I2C;
//
I2C_InitStrycture.I2C_DutyCycle=I2C_DutyCycle_2;
//在STM32作为从机时的应答地址,做主机时不需理会
I2C_InitStrycture.I2C_OwnAddress1=0x00;
//使能IIC应答
I2C_InitStrycture.I2C_Ack=I2C_Ack_Enable;
//应答地址为7位(从机)
I2C_InitStrycture.I2C_AcknowledgedAddress=I2C_AcknowledgedAddress_7bit;
//使能IIC
I2C_Cmd(I2C1,ENABLE);
//配置寄存器
I2C_Init(I2C1,&I2C_InitStrycture);
//允许一字节一应答的时序
I2C_AcknowledgeConfig(I2C1, ENABLE);
}
void IIC1_Init(void)
{
IIC1_GPIO_Configuration();
IIC1_MODE_Configuration();
}
主程序很简单(串口程序就不贴出来了)
USART1_Init();
IIC1_Init();
printf("
开始测试
");
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY));
printf("写入数据
");
就这么简短的几句话,卡在那里了。配置检查了很久都没有看出问题来!
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
---------------------------------
看来你和我遇到的问题一样啊。我今天早上还是没有找出来。先用软件模拟算了。
---------------------------------
有没有ST的FAE的联系方式啊,初始化很简单的过程,就是配置几个寄存器而已,不知道为什么会出问题,用F103的用同样的步骤配置,是正常的,F407就不行,不知道什么原因。
---------------------------------
有打开I2C时钟的语句
---------------------------------
你用下面的初始化步骤试一下,我的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的文件夹,也可以按照它的初始化步骤来。
一周热门 更多>