利用I2C接口,采用轮询方式读写EEPROM芯片
#define TESTI2C 2 //I2C1 = 1 I2C2 = 2
#define I2C1_SCL_GPIO_PORT CW_GPIOB
#define I2C1_SCL_GPIO_PIN GPIO_PIN_10 //如果改动口线则GPIO初始化代码需要做同步修改
#define I2C1_SDA_GPIO_PORT CW_GPIOB
#define I2C1_SDA_GPIO_PIN GPIO_PIN_11 //如果改动口线则GPIO初始化代码需要做同步修改
#define I2C2_SCL_GPIO_PORT CW_GPIOB
#define I2C2_SCL_GPIO_PIN GPIO_PIN_0 //如果改动口线则GPIO初始化代码需要做同步修改
#define I2C2_SDA_GPIO_PORT CW_GPIOB
#define I2C2_SDA_GPIO_PIN GPIO_PIN_1 //如果改动口线则GPIO初始化代码需要做同步修改
//EEPROM内部地址
#define WRITEADDRESS 0 //写入或者读取的EEPROM的地址
#define WriteReadCycle 22 //写读次数,每次写入数据为n+i(n为次数,i=0~7)
uint8_t u8Senddata[8] = {0x66,0x02,0x03,0x04,0x05,0x60,0x70,0x20};
uint8_t u8Senddata2[8] = {0x55,0xAA,0xAA,0x55,0x55,0xAA,0x55,0xAA};
uint8_t u8Recdata[16]={0x00};
uint8_t u8SendLen=0;
uint8_t u8RecvLen=0;
uint8_t Send** = 0,Comm_** = 0;
uint8_t u8recv**=0;
uint8_t u8State = 0;
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
int32_t main(void)
{
uint16_t tempcnt = 0 ;
I2C_InitTypeDef I2C_InitStruct;
//时钟初始化
RCC_Configuration();
//IO口初始化
GPIO_Configuration();
//I2C初始化
I2C_InitStruct.I2C_BaudEn = ENABLE;
I2C_InitStruct.I2C_Baud = 0x01;//500K=(8000000/(8*(1+1))
I2C_InitStruct.I2C_FLT = DISABLE;
I2C_InitStruct.I2C_AA = DISABLE;
#if(0X01 == TESTI2C)
I2C1_DeInit();
I2C_Master_Init(CW_I2C1,&I2C_InitStruct);//初始化模块
I2C_Cmd(CW_I2C1,ENABLE); //模块使能
#elif(0X02 == TESTI2C)
I2C2_DeInit();
I2C_Master_Init(CW_I2C2,&I2C_InitStruct);//初始化模块
I2C_Cmd(CW_I2C2,ENABLE); //模块使能
#endif
while(1)
{
#if(0X01 == TESTI2C)
I2C_MasterWriteEepromData(CW_I2C1,WRITEADDRESS,u8Senddata,8);
FirmwareDelay(3000);
I2C_MasterWriteEepromData(CW_I2C1,WRITEADDRESS+8,u8Senddata2,8);
FirmwareDelay(3000);
I2C_MasterReadEepomData(CW_I2C1,WRITEADDRESS,u8Recdata,16);
#elif(0X02 == TESTI2C)
I2C_MasterWriteEepromData(CW_I2C2,WRITEADDRESS,u8Senddata,8);
FirmwareDelay(3000);
I2C_MasterWriteEepromData(CW_I2C2,WRITEADDRESS+8,u8Senddata2,8);
FirmwareDelay(3000);
I2C_MasterReadEepomData(CW_I2C2,WRITEADDRESS,u8Recdata,16);
#endif
tempcnt++;
for(uint8_t i=0;i<8;i++)
{
u8Senddata[i] =tempcnt+i;
}
if(tempcnt >=WriteReadCycle)
{
break;
}
}
while(1)
{
}
}
iic的IO配置
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
#if(0X01 == TESTI2C)
PB10_AFx_I2C1SCL();
PB11_AFx_I2C1SDA();
GPIO_InitStructure.Pins = I2C1_SCL_GPIO_PIN | I2C1_SDA_GPIO_PIN;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
GPIO_Init(I2C1_SCL_GPIO_PORT, &GPIO_InitStructure);
#elif(0X02 == TESTI2C)
PB00_AFx_I2C2SCL();
PB01_AFx_I2C2SDA();
GPIO_InitStructure.Pins = I2C2_SCL_GPIO_PIN | I2C2_SDA_GPIO_PIN;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
GPIO_Init(I2C2_SCL_GPIO_PORT, &GPIO_InitStructure);
#endif
}
|