void DrcI2C_WriteByte(uint32_t address,uint8_t data )
{
uint32_t i;
SystemCoreClock = DrvSYS_GetHCLKFreq();
//Open I2C1 and set clock = 50Kbps
if(DrvI2C_Open(I2C_PORT1, 50000))
printf("Open i2c1 fail.\n");
//send i2c start
DrvI2C_Ctrl(I2C_PORT1, 1, 0, 0, 0); //set start
while (I2C1->I2CON.SI == 0); //poll si flag
//send writer command
I2C1->I2CDAT = 0x78; //send writer command
DrvI2C_Ctrl(I2C_PORT1, 0, 0, 1, ); //clr si flag
while( I2C1->I2CON.SI == 0 ); //poll si flag
// //send address high
// I2C1->I2CDAT = (address>>8)&0XFF;
// DrvI2C_Ctrl(I2C_PORT1, 0, 0, 1, 1); //clr si and set ack
// while( I2C1->I2CON.SI == 0 ); //poll si flag
// //send address low
// I2C1->I2CDAT = address&0XFF;
// DrvI2C_Ctrl(I2C_PORT1, 0, 0, 1, 1); //clr si and set ack
// while( I2C1->I2CON.SI == 0 ); //poll si flag
//send data
I2C1->I2CDAT = data; //write data to
DrvI2C_Ctrl(I2C_PORT1, 0, 0, 1, 1); //clr si and set ack
while( I2C1->I2CON.SI == 0 ); //poll si flag
//send i2c stop
DrvI2C_Ctrl(I2C_PORT1, 0, 1, 1, 0); //send stop
while( I2C1->I2CON.STO);
//while( I2C1->CON.SI == 0 );
for(i=0;i<60;i++);
DrvI2C_Close(I2C_PORT1);
for(i=0;i<6000;i++);
for(i=0;i<6000;i++);
}
uint8_t DrcI2C_ReadByte(uint32_t address)
{
uint8_t TEMP;
//Open I2C1 and set clock = 50Kbps
SystemCoreClock = DrvSYS_GetHCLKFreq();
DrvI2C_Open(I2C_PORT1, 50000);
//send i2c start
DrvI2C_Ctrl(I2C_PORT1, 1, 0, 1, 0); //set start
while (I2C1->I2CON.SI == 0); //poll si flag
//send writer command
I2C1->I2CDAT = 0XA0;
DrvI2C_Ctrl(I2C_PORT1, 0, 0, 1, 0); //clr si
while( I2C1->I2CON.SI == 0 ); //poll si flag
//send address high
I2C1->I2CDAT = (address>>8)&0XFF;
DrvI2C_Ctrl(I2C_PORT1, 0, 0, 1, 1); //clr si and set ack
while( I2C1->I2CON.SI == 0 ); //poll si flag
//send address low
I2C1->I2CDAT = address&0XFF;
DrvI2C_Ctrl(I2C_PORT1, 0, 0, 1, 0); //clr si and set ack
while( I2C1->I2CON.SI == 0 ); //poll si flag
//send start flag
DrvI2C_Ctrl(I2C_PORT1, 1, 0, 1, 0); //clr si and send start
while( I2C1->I2CON.SI == 0 ); //poll si flag
//send read command
I2C1->I2CDAT = 0XA1;
DrvI2C_Ctrl(I2C_PORT1, 0, 0, 1, 1); //clr si
while( I2C1->I2CON.SI == 0 ); //poll si flag
//resive data
I2C1->I2CDAT = 0XFF;
DrvI2C_Ctrl(I2C_PORT1, 0, 0, 1, 0); //clr si
while( I2C1->I2CON.SI == 0 ); //poll si flag
TEMP= I2C1->I2CDAT;
//send i2c stop
DrvI2C_Ctrl(I2C_PORT1, 0, 1, 1, 0); //clr si and set stop
while( I2C1->I2CON.STO);
DrvI2C_Close(I2C_PORT1);
return TEMP;
}
这.......看着就蛋疼
比较一下STM32的
[code]uint8_t I2C_7588lcd_Write(u8* pBuffer, u8 WriteAddr, u8 NumByteToWrite)
{
unsigned int i;
uint32_t u32delayno;
I2C_GenerateSTART(I2C_MASTER, ENABLE); //启动I2C
u32delayno=0;
while(!I2C_CheckEvent(I2C_MASTER, I2C_EVENT_MASTER_MODE_SELECT)); //等待响应,超时退出,放置死循
//发送器件地址
I2C_Send7bitAddress(I2C_MASTER, 0x78, I2C_Direction_Transmitter);
u32delayno=0;
while(!I2C_CheckEvent(I2C_MASTER, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) //等待器件响应
{
u32delayno++;
if ( u32delayno >= I2C_TIME_OUT )
{
USART1_Puts("error:send address\n");
return ERROR;
}
}
while(NumByteToWrite--)
{ //写数据
I2C_SendData(I2C_MASTER, *pBuffer);
//发送数据
pBuffer++;
for(i=50;i>0;i--) //等待器件响应 LCD答应ACK有点BUG所以这里采用延时
{
__nop();
}
}
I2C_GenerateSTOP(I2C_MASTER, ENABLE);
//停止I2C
return SUCCESS;
}
[/code]