???? linux?????M051 IIC??ó???
????M051??????? IIC???????#include "SmartM_M0.h"
#define DEBUGMSG printf
#define EEPROM_SLA 0xA0
#define EEPROM_WR 0x00
#define EEPROM_RD 0x01
#define I2C_CLOCK 13
#define PAGE_SIZE 8
/*****************************************
*????????:Timed_Write_Cycle
*?? ??:??
*?? ??:??
*?? ??:?????д????
******************************************/
void Timed_Write_Cycle(void)
{
while (I2STATUS != 0x18)
{
//????
I2CON |= STA;
I2CON |= SI;
while ((I2CON & SI) != SI);
I2CON &= ((~STA) & (~SI));
//?豸???
I2DAT = EEPROM_SLA | EEPROM_WR;
I2CON |= SI;
while ((I2CON & SI) != SI);
}
if (I2STATUS != 0x18) //??????
{
DEBUGMSG("Not ACK returned!");
}
//??
I2CON |= STO;
I2CON |= SI;
while (I2CON & STO);
}
/*****************************************
*????????:I2CInit
*?? ??:??
*?? ??:??
*?? ??:I2C?????
******************************************/
VOID I2CInit(VOID)
{
P3_PMD &= ~(Px4_PMD | Px5_PMD);
P3_PMD |= (Px4_OD | Px5_OD); //???I2C0????
P3_MFP &= ~(P34_T0_I2CSDA | P35_T1_I2CSCL);
P3_MFP |= (I2CSDA | I2CSCL); //???P3.4,P3.5???I2C0????????
APBCLK |= I2C0_CLKEN; //???I2C0???
I2CLK = I2C_CLOCK;
I2CON |= ENSI; //???I2C
}
/*****************************************
*????????:AT24C0XWrite
*?? ??:unAddr д???
pucData д????
unLength д????
*?? ??:TRUE/FALSE
*?? ??:AT24C0Xд????
******************************************/
BOOL AT24C0XWrite(UINT32 unAddr,UINT8 *pucData,UINT32 unLength)
{
UINT32 i;
I2CON |= STA; //????
I2CON |= SI;
while ((I2CON & SI) != SI);
I2CON &= ((~STA)&(~SI));
if (I2STATUS != 0x08)
{
DEBUGMSG("I2CStart fail,I2STATUS %02X
",I2STATUS);
return FALSE;
}
//?????д???????
I2DAT = EEPROM_SLA | EEPROM_WR;
I2CON |= SI;
while ((I2CON & SI) != SI);
if (I2STATUS != 0x18)
{
DEBUGMSG("I2C write control fail
");
return FALSE;
}
//д???
I2DAT = unAddr;
I2CON |= SI;
while ((I2CON & SI) != SI);
if (I2STATUS != 0x28)
{
DEBUGMSG("I2C write addr fail
");
return FALSE;
}
//д????
for(i=0; i{
I2DAT = *(pucData+i);
I2CON |= SI;
while ((I2CON & SI) != SI);
if (I2STATUS != 0x28)
{
DEBUGMSG("I2C write data fail
");
return FALSE;
}
}
//??
I2CON |= STO;
I2CON |= SI;
while (I2CON & STO);
//DEBUGMSG("I2C stop ok
");
Timed_Write_Cycle();
return TRUE;
}
/*****************************************
*????????:AT24C0XRead
*?? ??:unAddr ?????
pucData ??????
unLength ??????
*?? ??:TRUE/FALSE
*?? ??:AT24C0X??????
******************************************/
BOOL AT24C0XRead(UINT32 unAddr,UINT8 *pucData,UINT32 unLength)
{
UINT32 i;
I2CON |= STA; //????
I2CON |= SI;
while ((I2CON & SI) != SI);
I2CON &= ((~STA)&(~SI));
if (I2STATUS != 0x08)
{
DEBUGMSG("I2CStart fail,I2STATUS %02X
",I2STATUS);
return FALSE;
}
//?????д???????
I2DAT = EEPROM_SLA | EEPROM_WR;
I2CON |= SI;
while ((I2CON & SI) != SI);
if (I2STATUS != 0x18)
{
DEBUGMSG("I2C write control fail
");
return FALSE;
}
//д??????
I2DAT = unAddr;
I2CON |= SI;
while ((I2CON & SI) != SI);
if (I2STATUS != 0x28)
{
DEBUGMSG("I2C write addr fail
");
return FALSE;
}
// ????????
I2CON |= STA;
I2CON |= SI;
while ((I2CON & SI) != SI);
I2CON &= ((~STA)&(~SI));
if (I2STATUS != 0x10)
{
DEBUGMSG("I2C repeated start fail
");
return FALSE;
}
//?????????
I2DAT = EEPROM_SLA | EEPROM_RD;
I2CON |= SI;
while ((I2CON & SI) != SI);
if (I2STATUS != 0x40)
{
DEBUGMSG("I2C write control fail
");
while (1);
}
//???????
I2CON |= AA;
for(i=0; i{
I2CON |= SI;
while ((I2CON & SI) != SI);
if (I2STATUS != 0x50)
{
DEBUGMSG("I2C read fail
");
return FALSE;
}
*(pucData+i) = I2DAT;
}
//????NACK??AT24C02????ж?????????
I2CON &= (~AA);
I2CON |= SI;
while ((I2CON & SI) != SI);
//??
I2CON |= STO;
I2CON |= SI;
while (I2CON & STO);
DEBUGMSG("I2C read ok
");
return TRUE;
}
VOID AT24C02ContinousWrite(UINT32 unAddr,UINT8 *pucData,UINT32 unLength)
{
UINT32 i,m,n;
UINT32 unCurWirteCount=0;
//if(unLength <= 32)
m = PAGE_SIZE-(unAddr % PAGE_SIZE); //?????32?????????????????
if(unLength <= m)
{
AT24C0XWrite(unAddr,pucData,unLength);
}
else
{
AT24C0XWrite(unAddr,pucData,m);
unLength -=m;
unCurWirteCount+=m;
m = unLength/PAGE_SIZE;
n = unLength%PAGE_SIZE;
for(i=0; i{
AT24C0XWrite(unAddr+unCurWirteCount,pucData+unCurWirteCount,PAGE_SIZE);
unCurWirteCount += PAGE_SIZE;
}
AT24C0XWrite(unAddr+unCurWirteCount,pucData+unCurWirteCount,n);
}
}
/*****************************************
*????????:main
*?? ??:??
*?? ??:??
*?? ??:????????
******************************************/
INT32 main(VOID)
{
UINT8 i,buf[32];
Un_Lock_Reg(); //ISP?????????FLASH?洢??
PWRCON |= XTL12M_EN; //??????????????
while((CLKSTATUS & XTL12M_STB) == 0); //???12MHz??????
CLKSEL0 = (CLKSEL0 & (~HCLK)) | HCLK_12M; //????????????????
UartInit(12000000,9600); //???????????9600bps
I2CInit();
DEBUGMSG("I2C Test
");
// while(1)
// {
for(i=0; i{
buf[i]=0x55;
}
DEBUGMSG("
AT24C0XWrite Test
");
AT24C0XWrite(0,buf,sizeof(buf)); //???д????
Delayms(500);
//==================================================
DEBUGMSG("AT24C0XRead Test
");
for(i=0; i{
buf[i]=0x00;
}
AT24C0XRead(0,buf,sizeof(buf)); //??ж?????
for(i=0; i<10; i++)
{
DEBUGMSG("%02X ",buf[i]); //???????????
}
Delayms(500);
// }
while(1);
}

????zzy ?????????2022-05-28