标题:
[原创]NE64硬件IIC读写24C02源代码
[打印本页]
作者:
concheck
时间:
2007-9-17 16:27
标题:
[原创]NE64硬件IIC读写24C02源代码
#define WriteDeviceAdd 0xA0
#define ReadDeviceAdd 0xA1
void Delayms(byte ms)
{
int i;
for(ms;ms>0;ms--)
{
for(i=0;i<250;i++);
}
}
void Start(void)
{
IBCR |=0x30;
}
void Restart(void)
{
IBCR_RSTA = 1;
}
void Stop(void)
{
IBCR_MS_SL = 0;
}
void WaitAck(void)
{
while(IBSR_RXAK==1);
}
void Write8Bit(unsigned char wdata)
{
IBDR = wdata;
while (!IBSR_IBIF);
IBSR_IBIF=1;
}
void i2cWrite(unsigned char Writedata,unsigned char RomAddress)
{
Start();
Write8Bit(0xA0 ); //写I2C从器件地址和写方式
WaitAck();
Write8Bit(RomAddress); //写24C02的ROM地址
WaitAck();
Write8Bit(Writedata); //写数据到24C02的ROM
WaitAck();
Stop(); //STOP
Delayms(20);
}
unsigned char Read8Bit(void)
{
unsigned char data;
data = IBDR;
while (!IBSR_IBIF);
IBSR_IBIF=1;
return data;
}
unsigned char i2cRead(unsigned char RomAddress)
{
unsigned char rData;
Start();
Write8Bit(0xA0);
WaitAck();
Write8Bit(RomAddress); //要读的ROM地址
WaitAck();
Restart();
Write8Bit(0xA1);
WaitAck();
IBCR_TX_RX = 0;
rData=Read8Bit(); //Dummy read 必须执行,否则数据为0xFF
IBCR_TXAK = 1;
rData=Read8Bit(); //读取数据
Stop();
Delayms(20);
return rData;
}
void IIC2_Init(void)
{
IBDF=0x87; //25M 78.12KZ
IBAD=0x00;
IBCR=0x80;
IBSR_IBAL = 1;
}
void main(void)
{
unsigned char wdata[10]={143,118,192,114,165,160,187,18,119,209};
unsigned char rdata[10]={0,0,0,0,0,0,0,0,0,0};
unsigned int romadd;
PE_low_level_init();
IIC2_Init();
for(romadd=0;romadd<10;romadd++)
{
i2cWrite(wdata[romadd],romadd);
}
for(romadd=0;romadd<10;romadd++)
{
rdata[romadd]=i2cRead(romadd);
}
}
作者:
kaxinuo
时间:
2007-9-27 20:35
坚决支持拥护原创。
作者:
wr321978
时间:
2008-10-28 19:02
我用了你的这个程序,收不到数据呀,还有什么需要注意的地方么?请多多指教!谢谢!其中PE_low_level_init();都作了什么处理?
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0