大学生
最后登录1970-1-1
在线时间 小时
注册时间2014-3-31
|
各位大神,本人写了一份硬件IIC 启动后一直卡在while(I2C_GetFlagStatus(IICx, I2C_FLAG_BUSY));这里 如果注释掉以后也不能够初始化成功,哪位高手麻烦指导下谢谢,调了一整天了。。。
下面是代码部分:
#include "bsp_iic.h"
#include "bsp_usart.h"
static __IO uint32_t I2CTimeout;
void IIC_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(IIC_CLK, ENABLE); // ¿aÆôIIC¸′óÃê±Öó
RCC_APB2PeriphClockCmd(IIC_GPIO_CLK, ENABLE); // ¿aÆôGPIOê±Öó
GPIO_InitStructure.GPIO_Pin = IIC_GPIO_PIN_SCL;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(IIC_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = IIC_GPIO_PIN_SDA;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(IIC_GPIO_PORT, &GPIO_InitStructure);
}
void IIC_TypeDef_Init(void)
{
I2C_InitTypeDef IIC_InitStructure;
IIC_InitStructure.I2C_ClockSpeed = IIC_CLK_Speed;
IIC_InitStructure.I2C_Mode = I2C_Mode_I2C;
IIC_InitStructure.I2C_DutyCycle = I2C_DutyCycle_16_9;
IIC_InitStructure.I2C_OwnAddress1 = IIC_OWNADDRESS;
IIC_InitStructure.I2C_Ack = I2C_Ack_Enable;
IIC_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_Init(IICx, &IIC_InitStructure);
I2C_Cmd (IICx, ENABLE);
}
void IIC_Init(void)
{
IIC_GPIO_Init();
IIC_TypeDef_Init();
}
static uint8_t IIC_TIMEOUT_USERCALLBACK(uint8_t errcode)
{
printf(" IIC wait timeout! errcode = %d\n",errcode);
return 0;
}
uint8_t IIC_Byte_Write(uint8_t DeviceAddr, uint8_t WriteAddr, uint8_t* wpBuffer)
{
while(I2C_GetFlagStatus(IICx, I2C_FLAG_BUSY));
// IIC start
I2C_GenerateSTART(IICx, ENABLE);
// IIC sendDeviceAddr EV5 set
I2CTimeout = I2CT_FLAG_TIMEOUT;
while(!I2C_CheckEvent(IICx, I2C_EVENT_MASTER_MODE_SELECT))
{
if((I2CTimeout--) == 0)
return IIC_TIMEOUT_USERCALLBACK(0);
}
I2C_Send7bitAddress(IICx, DeviceAddr, I2C_Direction_Transmitter);
// IIC sendRegisterAddr EV6 set
I2CTimeout = I2CT_FLAG_TIMEOUT;
while(!I2C_CheckEvent(IICx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
{
if((I2CTimeout--) == 0)
return IIC_TIMEOUT_USERCALLBACK(1);
}
I2C_SendData(IICx, WriteAddr);
// IIC sendData EV8
I2CTimeout = I2CT_FLAG_TIMEOUT;
while(!I2C_CheckEvent(IICx, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
{
if((I2CTimeout--) == 0)
return IIC_TIMEOUT_USERCALLBACK(2);
}
I2C_SendData(IICx, *wpBuffer);
// IIC Stop EV8_2
I2CTimeout = I2CT_FLAG_TIMEOUT;
while(!I2C_CheckEvent(IICx, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
{
if((I2CTimeout--) == 0)
return IIC_TIMEOUT_USERCALLBACK(3);
}
I2C_GenerateSTOP(IICx, ENABLE);
return 1;
}
uint8_t IIC_Byte_Read(uint8_t DeviceAddr, uint8_t ReadAddr, uint8_t* rpBuffer)
{
while(I2C_GetFlagStatus(IICx, I2C_FLAG_BUSY));
// IIC start
I2C_GenerateSTART(IICx, ENABLE);
// IIC sendDeviceAddr EV5 set
I2CTimeout = I2CT_FLAG_TIMEOUT;
while(!I2C_CheckEvent(IICx, I2C_EVENT_MASTER_MODE_SELECT))
{
if((I2CTimeout--) == 0)
return IIC_TIMEOUT_USERCALLBACK(4);
}
I2C_Send7bitAddress(IICx, DeviceAddr, I2C_Direction_Transmitter);
// IIC sendRegisterAddr EV6 set
I2CTimeout = I2CT_FLAG_TIMEOUT;
while(!I2C_CheckEvent(IICx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
{
if((I2CTimeout--) == 0)
return IIC_TIMEOUT_USERCALLBACK(5);
}
I2C_SendData(IICx, ReadAddr);
// IIC sendData EV7
I2CTimeout = I2CT_FLAG_TIMEOUT;
while(!I2C_CheckEvent(IICx, I2C_EVENT_MASTER_BYTE_RECEIVED))
{
if((I2CTimeout--) == 0)
return IIC_TIMEOUT_USERCALLBACK(6);
}
*rpBuffer = I2C_ReceiveData(IICx);
// IIC Stop EV7_1/EV7
I2CTimeout = I2CT_FLAG_TIMEOUT;
while(!I2C_CheckEvent(IICx, I2C_EVENT_MASTER_BYTE_RECEIVED))
{
if((I2CTimeout--) == 0)
return IIC_TIMEOUT_USERCALLBACK(7);
}
I2C_GenerateSTOP(IICx, ENABLE);
return 1;
}
//------------------------IIC_EEPROM_test----------------------------
void IIC_EEPROM_test(void)
{
uint8_t ReadRegAddr = 0x01;
uint8_t WriteRegAddr = 0x01;
uint8_t ReadData;
uint8_t WriteData = 0x23;
IIC_Byte_Write(EEPROM_ADDRESS, WriteRegAddr, &WriteData);
IIC_Byte_Read(EEPROM_ADDRESS, ReadRegAddr, &ReadData);
printf("WriteData = 0x%x\n",WriteData);
printf("ReadData = 0x%x\n",ReadData);
}
int main(void)
{
Usart_Init();
IIC_Init();
// IIC_EEPROM_test();
printf("This is a IIC_test!!!\n");
while(1)
{
IIC_EEPROM_test();
}
}
|
|