原创 NE64硬件IIC读写24C02源代码

2010-3-23 01:04 1192 3 3 分类: MCU/ 嵌入式


#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);
}
}
PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
3
关闭 站长推荐上一条 /3 下一条