请香版主跟哪位用过STR912的同仁指点一下 代码如下:
/********************************************************************* 函数名:EEPROM_I2CInit() *********************************************************************/ void EE_I2CInit(void) { I2C_InitTypeDef I2C_Struct; /* Peripherals enable */ SCU_APBPeriphClockConfig(__I2C1,ENABLE); I2C_DeInit(I2C1);
/* I2C0 Configuration */ I2C_Struct.I2C_GeneralCall = I2C_GeneralCall_Disable; I2C_Struct.I2C_Ack = I2C_Ack_Enable; I2C_Struct.I2C_CLKSpeed = 400000; I2C_Struct.I2C_OwnAddress = 0xA0; I2C_Init(I2C1, &I2C_Struct);
//I2C_AcknowledgeConfig(I2C1,ENABLE); /* Enable I2C0, I2C1 */ I2C_Cmd(I2C1, ENABLE); }
/********************************************************************* *函数名:EE_WriteOneByte() *功能描述:向E2的指定地址写入一个字节的数据 *********************************************************************/ void EE_WriteOneByte(u16 adr,u8 dat) { u8 addresstemp=0; //I2C_AcknowledgePolling();
GPIO_WriteBit(GPIO0,GPIO_Pin_4,Bit_RESET);
addresstemp=adr>>8; addresstemp=(((addresstemp&0x07)<<1)&0xfe)|0xa0; I2C_GenerateStart(I2C1, ENABLE);
/* Test on EV5 and clear it */ while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)); /* Send RTC address for write */ I2C_Send7bitAddress(I2C1, addresstemp, I2C_MODE_TRANSMITTER);
/* Test on EV6 and clear it */ while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECTED)); I2C_Cmd(I2C1, ENABLE);
I2C_SendData(I2C1,(u8)adr); /* Test on EV8 and clear it */ while(! I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C1,dat); /* Test on EV8 and clear it */ while(! I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTOP(I2C1,ENABLE); }
/********************************************************************* 函数名:EE_WriteOneByte() 功能描述:向E2的随机地址读出一个字节的数据 *********************************************************************/ u8 EE_ReadOneByte(u16 adr) { unsigned char addresstemp=0; unsigned char result = 0; //I2C_AcknowledgePolling(); GPIO_WriteBit(GPIO0,GPIO_Pin_4,Bit_SET); addresstemp=adr>>8; addresstemp=(((addresstemp&0x07)<<1)&0xfe)|0xa0; I2C_GenerateStart(I2C1,ENABLE);
/* Test on EV5 and clear it */ while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
/* Send RTC address for write */ I2C_Send7bitAddress(I2C1, addresstemp, I2C_MODE_TRANSMITTER); /* Test on EV6 and clear it */ while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECTED)); I2C_Cmd(I2C1, ENABLE);
I2C_SendData(I2C1, ( u8)adr); /* Test on EV8 and clear it */ while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateStart(I2C1,ENABLE); /* Test on EV5 and clear it */ while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
/* Send RTC address for read */ I2C_Send7bitAddress(I2C1, addresstemp, I2C_MODE_RECEIVER); /* Test on EV6 and clear it */ while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECTED)); I2C_Cmd(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)); result = I2C_ReceiveData(I2C1); __nop(); //I2C_AcknowledgeConfig(I2C1, DISABLE); I2C_GenerateSTOP(I2C1,ENABLE); return result; }
在主程序中: 只要我读取24C16中的一个随机地址的数据后,单片机就不能再对24C16进行读或者写操作了
每当我读取数据后,看到i2c的SR1寄存器的值为0x9A,其中第四位为BUSY被置1了, 可能就是这个原因导致我下面不能正常读写了
我每次读的子程序后面都会有一个 I2C_stop()执行,怎么还会有BUSY呢
香版主指点一下,谢谢 期待您的回复 |
|