程序很简单,就是先初始化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("写入数据
");
就这么简短的几句话,卡在那里了。配置检查了很久都没有看出问题来!
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
回复【3楼】parallax: --------------------------------- 还没有找出原因来
---------------------------------
麻烦楼主找出来后公布一下原因,我也正在找原因在。
库函数I2C.C中的说明
How to use this driver
* ===================================================================
* 1. Enable peripheral clock using RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2Cx, ENABLE)
* function for I2C1, I2C2 or I2C3.
*
* 2. Enable SDA, SCL and SMBA (when used) GPIO clocks using
* RCC_AHBPeriphClockCmd() function.
*
* 3. eripherals alternate function:
* - Connect the pin to the desired peripherals' Alternate
* Function (AF) using GPIO_PinAFConfig() function
* - Configure the desired pin in alternate function by:
* GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF
* - Select the type, pull-up/pull-down and output speed via
* GPIO_PuPd, GPIO_OType and GPIO_Speed members
* - Call GPIO_Init() function
* Recommended configuration is ush-Pull, ull-up, Open-Drain.
* Add an external pull up if necessary (typically 4.7 KOhm).
*
* 4. rogram the Mode, duty cycle , Own address, Ack, Speed and Acknowledged
* Address using the I2C_Init() function.
*
* 5. Optionally you can enable/configure the following parameters without
* re-initialization (i.e there is no need to call again I2C_Init() function):
* - Enable the acknowledge feature using I2C_AcknowledgeConfig() function
* - Enable the dual addressing mode using I2C_DualAddressCmd() function
* - Enable the general call using the I2C_GeneralCallCmd() function
* - Enable the clock stretching using I2C_StretchClockCmd() function
* - Enable the fast mode duty cycle using the I2C_FastModeDutyCycleConfig()
* function.
* - Configure the NACK position for Master Receiver mode in case of
* 2 bytes reception using the function I2C_NACKPositionConfig().
* - Enable the EC Calculation using I2C_CalculatePEC() function
* - For SMBus Mode:
* - Enable the Address Resolution rotocol (ARP) using I2C_ARPCmd() function
* - Configure the SMBusAlert pin using I2C_SMBusAlertConfig() function
*
* 6. Enable the NVIC and the corresponding interrupt using the function
* I2C_ITConfig() if you need to use interrupt mode.
*
* 7. When using the DMA mode
* - Configure the DMA using DMA_Init() function
* - Active the needed channel Request using I2C_DMACmd() or
* I2C_DMALastTransferCmd() function.
* @note When using DMA mode, I2C interrupts may be used at the same time to
* control the communication flow (Start/Stop/Ack... events and errors).
*
* 8. Enable the I2C using the I2C_Cmd() function.
*
* 9. Enable the DMA using the DMA_Cmd() function when using DMA mode in the
* transfers.
的第3条的最后一句话,Recommended configuration is ush-Pull, ull-up, Open-Drain.这个地方到底应该怎么配置,又是Push-Pull,又是 Open-Drain?
我按照库文件的说明一项一项的来配置,还是只要是把IO初始化完后,SDA就拉低了
一周热门 更多>