我网上了找了新唐M051 IIC使用程序,这个程序,修改了一下,做成I2C256的程序,读写很不稳定?不知道哪里有问题跪求高手帮忙啊?
#include "M051.h"
//#include "Register_Bit.h"
#include "I2C.h"
//#include "delay.h"
//#include "SysConfig.h"
#define CLOCK_SETUP 0
#define CLOCK_EN 0xF
#define PLL_Engine_Enable 0
#define PLL_SEL 0x00080000
#define CLOCK_SEL 0x0
#define I2C_CLOCK 99 //40K: 16m/(4*(I2C_CLOCK+1))
#define EEPROM_SLA 0xa0
#define DS1307_SLA 0xd0
#define WRITE 0x00
#define I2CREAD 0x01
#define SAFETY_COUNTER_LIMIT 5000
#define PageSize 64
uint16_t CycleTimerOut = 0 ;
void Init_I2C(void)
{
APBCLK |= I2C0_CLKEN; //Enable I2C0 clock
I2CLK = I2C_CLOCK; //Set I2C0 clock rate
I2CON |= ENSI; //Enable I2C
}
/*****************************************
*º¯ÊýÃû³Æ:Timed_Write_Cycle
*Êä Èë:ÎÞ
*Êä ³ö:ÎÞ
*¹¦ ÄÜ:ͬ²½µÄдÖÜÆÚ
******************************************/
void Timed_Write_Cycle(uint8_t div,uint16_t addr)
{
uint8_t ucDeviceAddr = 0;
uint16_t loopSafetyCounter = 0;
if(div == EEROM)
{
ucDeviceAddr = EEPROM_SLA;
}
else ucDeviceAddr = DS1307_SLA;
CycleTimerOut = 0;
while ((I2STATUS != 0x18)&&(CycleTimerOut<1000))
{
//Æô¶¯
CycleTimerOut++ ;
I2CON |= STA;
I2CON |= SI;
//I2c_Delay();
loopSafetyCounter = 0;
while ((I2CON & SI) != SI)
{
loopSafetyCounter ++;
if (loopSafetyCounter > SAFETY_COUNTER_LIMIT )
{
break ;
}
}
I2CON &= ((~STA) & (~SI)); //SAFETY_COUNTER_LIMIT
//É豸µØÖ·
I2DAT = ucDeviceAddr | WRITE;
I2CON |= SI;
//I2c_Delay();
loopSafetyCounter = 0;
while ((I2CON & SI) != SI)
{
loopSafetyCounter ++;
if (loopSafetyCounter >SAFETY_COUNTER_LIMIT )
{
break ;
}
}
if (I2STATUS != 0x18) //¼ì²éÓ¦´ð
{
// DEBUGMSG("Not ACK returned!");
}
//Í£Ö¹
}
I2CON |= STO;
I2CON |= SI;
// I2c_Delay();
loopSafetyCounter = 0;
while (I2CON & STO)
{
loopSafetyCounter ++;
if (loopSafetyCounter > SAFETY_COUNTER_LIMIT)
{
break ;
}
}
}
uint8_t I2C_Write_code(uint8_t div,uint16_t addr,uint8_t *data,uint16_t uclength)
{
//TCSR0 &= ~CEN;,tmp = 0
uint8_t ucDeviceAddr = 0,i=0;
uint16_t loopSafetyCounter = 0;
ucDeviceAddr = EEPROM_SLA;
// I2CON &= (~SI);
I2CON |= STA;
I2CON |= SI; //¿ªÖжÏ
loopSafetyCounter = 0;
while ((I2CON & SI) != SI)
{
loopSafetyCounter ++;
if (loopSafetyCounter > SAFETY_COUNTER_LIMIT)
{
break ;
}
}
// //I2CON &= (~STA);
//tmp = I2STATUS ;
if(I2STATUS != 0x08)
{
//return 0 ;
}
I2CON &= ((~STA)&(~SI));
I2DAT = (ucDeviceAddr | WRITE);
//I2CON |= AA; //Ó¦´ð
I2CON |= SI;
loopSafetyCounter = 0;
while ((I2CON & SI) != SI)
{
loopSafetyCounter ++;
if (loopSafetyCounter > SAFETY_COUNTER_LIMIT)
{
break ;
}
}
if(I2STATUS != 0x18)
{
//return 0 ;
}
if(div == EEROM)
{
I2DAT = (uint8_t)(addr>>8); //gao di zhi
//I2CON |= AA;
I2CON |= SI;
//I2c_Delay();
loopSafetyCounter = 0;
while ((I2CON & SI) != SI)
{
loopSafetyCounter ++;
if (loopSafetyCounter > SAFETY_COUNTER_LIMIT)
{
break ;
}
}
if(I2STATUS != 0x28)
{
//return 0 ;
}
I2DAT = (uint8_t)(addr);//(addr>>8); //di di zhi
// I2CON |= AA; //20170316
I2CON |= SI;
loopSafetyCounter = 0;
while ((I2CON & SI) != SI)
{
loopSafetyCounter ++;
if (loopSafetyCounter > SAFETY_COUNTER_LIMIT)
{
break ;
}
}
if(I2STATUS != 0x28)
{
//return 0 ;
}
}
for(i=0 ;i<uclength ;i++)
{
I2CON |= AA;
I2DAT = *(data+i);
//I2CON &= (~AA);
I2CON |= SI;
// I2c_Delay();
loopSafetyCounter = 0;
while ((I2CON & SI) != SI)
{
loopSafetyCounter ++;
if (loopSafetyCounter > SAFETY_COUNTER_LIMIT)
{
break ;
}
}
}
//I2CON &= (~AA); 2018 06 20
I2CON |= STO;
I2CON |= SI;
// I2c_Delay();
loopSafetyCounter = 0;
while (I2CON & STO)
{
loopSafetyCounter ++;
if (loopSafetyCounter > SAFETY_COUNTER_LIMIT)
{
break ;
}
}
Timed_Write_Cycle(div,addr);
return I2STATUS;
}
uint8_t I2C_Read(uint8_t div,uint16_t addr,uint8_t *pdata,uint8_t length)
{
uint8_t temp,i;
uint8_t ucDeviceAddr = 0;//,i = 0
uint16_t loopSafetyCounter = 0;
//if(div == EEROM)
//ucDeviceAddr = EEPROM_SLA;
if(div == EEROM)
{
ucDeviceAddr = EEPROM_SLA;
}
else ucDeviceAddr = DS1307_SLA;
//TCSR0 &= ~CEN;
// START
//I2CON &= (~SI);
I2CON |= STA;
I2CON |= SI;
loopSafetyCounter = 0;
while ((I2CON & SI) != SI)
{
loopSafetyCounter ++;
if (loopSafetyCounter > SAFETY_COUNTER_LIMIT)
{
break ;
}
} //Check SI set or not
I2CON &= ((~STA)&(~SI)); //I2CON &= (~STA);
// temp = I2STATUS ; //STA needs to be cleared after START codition is generated
// if (I2STATUS != 0x08) //Check status value after every step
// {
// return 0 ;
// }
//Control byte (SLA+W)
//I2CON |=AA; //Ó¦´ð
I2DAT = (ucDeviceAddr | WRITE);
I2CON |= SI;
loopSafetyCounter = 0;
while ((I2CON & SI) != SI)
{
loopSafetyCounter ++;
if (loopSafetyCounter > SAFETY_COUNTER_LIMIT)
{
break ;
}
}
if(div == EEROM)
{
//I2CON |= AA;
temp = (uint8_t)(addr>>8);
I2DAT = temp ; //gao di zhi
//Short_Delay() ;
I2CON |= SI;
loopSafetyCounter = 0;
while ((I2CON & SI) != SI)
{
loopSafetyCounter ++;
if (loopSafetyCounter > SAFETY_COUNTER_LIMIT)
{
break ;
}
}
temp = (uint8_t)(addr);
I2DAT = temp;//(addr>>8); //di di zhi
//Short_Delay() ;
I2CON |= SI;
loopSafetyCounter = 0;
while ((I2CON & SI) != SI)
{
loopSafetyCounter ++;
if (loopSafetyCounter > SAFETY_COUNTER_LIMIT)
{
break ;
}
}
}
else
{
I2DAT = (uint8_t)(addr);//(addr>>8);
I2CON |= SI;
loopSafetyCounter = 0;
while ((I2CON & SI) != SI)
{
loopSafetyCounter ++;
if (loopSafetyCounter > SAFETY_COUNTER_LIMIT)
{
break ;
}
}
}
I2CON |= STA;
I2CON |= SI;
loopSafetyCounter = 0;
while ((I2CON & SI) != SI)
{
loopSafetyCounter ++;
if (loopSafetyCounter > SAFETY_COUNTER_LIMIT)
{
break ;
}
} //Check SI set or not
I2CON &= ((~STA)&(~SI)); //I2CON &= (~STA);
I2DAT = (ucDeviceAddr | I2CREAD);
I2CON |= SI;
loopSafetyCounter = 0;
while ((I2CON & SI) != SI)
{
loopSafetyCounter ++;
if (loopSafetyCounter > SAFETY_COUNTER_LIMIT)
{
break ;
}
}
//I2CON &= ((~STA)&(~SI));
I2CON |= AA;
// I2CON |= SI;
// loopSafetyCounter = 0;
// while ((I2CON & SI) != SI)
// {
// loopSafetyCounter ++;
// if (loopSafetyCounter > SAFETY_COUNTER_LIMIT)
// {
// break ;
// }
// }
for(i=0;i<length;i++)
{
I2CON |= SI;
loopSafetyCounter = 0;
while ((I2CON & SI) != SI)
{
loopSafetyCounter ++;
if (loopSafetyCounter > SAFETY_COUNTER_LIMIT)
{
break ;
}
}
*(pdata+i) = I2DAT;
}
I2CON &= (~AA);
I2CON |= SI;
loopSafetyCounter = 0;
while ((I2CON & SI) != SI)
{
loopSafetyCounter ++;
if (loopSafetyCounter > SAFETY_COUNTER_LIMIT)
{
break ;
}
}
//STOP
I2CON |= STO;
I2CON |= SI;
loopSafetyCounter = 0;
while (I2CON & STO)
{
loopSafetyCounter ++;
if (loopSafetyCounter > SAFETY_COUNTER_LIMIT)
{
break ;
}
} //To ensure STOP condition is generated, polling STO flag
return 1;
}
void AT2ICcontinousWite(uint16_t addr,uint8_t *data,uint16_t uclength)
{
uint32_t m,n ;
//uint32_t unCurWirteCount = 0 ;
// if(addr<64)
// {
// m=1 ;
// }
// else
// {
m= (addr/PageSize)+1 ;
// }
if((addr+uclength)<(PageSize*m))
{
I2C_Write_code(EEROM,addr,data,uclength) ;
}
else
{
n= ((addr+uclength)%PageSize) ;
I2C_Write_code(EEROM,addr,data,uclength-n) ;
Short_Delay() ;
//Short_Delay() ;
I2C_Write_code(EEROM,addr+uclength-n,data+uclength-n,n) ;
}
// if(uclength<=m)
// {
// I2C_Write_code(0,addr,data,uclength) ;
// }
// else
// {
// I2C_Write_code(0,addr,data,m) ;
// uclength-=m ;
// unCurWirteCount +=m ;
// m = uclength/PageSize ;
// n = uclength%PageSize ;
// for(i=0 ;i<m ;i++)
// {
// I2C_Write_code(0,addr+unCurWirteCount,data+unCurWirteCount,PageSize) ;
// unCurWirteCount += PageSize ;
// }
// I2C_Write_code(0,addr+unCurWirteCount,data+unCurWirteCount,n) ;
// }
} |