#include <reg52.h>
extern void InitLcd1602(void);
extern void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str);
extern void I2CStart(void);
extern void I2CStop(void);
extern bit I2CWrite(unsigned char dat);
extern unsigned char I2CReadNAK(void);
extern unsigned char I2CReadACK(void);
void MemToStr(unsigned char *str, unsigned char *src, unsigned char len);
void E2PRead(unsigned char *buf, unsigned char addr, unsigned char len);
void E2PWrite(unsigned char *buf, unsigned char addr, unsigned char len);
void main(void)
{
unsigned char i;
unsigned char buf[5];
unsigned char str[20];
InitLcd1602();
E2PRead(buf,0x90,sizeof(buf));
MemToStr(str,buf,sizeof(buf));
LcdShowStr(0,0,str);
for(i=0; i<sizeof(buf); i++)
{
buf[i] = buf[i] + 1 + i;
}
E2PWrite(buf,0x90,sizeof(buf));
while(1){}
}
void MemToStr(unsigned char *str, unsigned char *src, unsigned char len)
{
unsigned char tmp;
while(len--)
{
tmp = *src >> 4;
if(tmp <= 9)
{
*str++ = tmp + '0';
}
else
{
*str++ = tmp - 10 + 'A';
}
tmp = *src & 0x0F;
if(tmp <= 9)
{
*str++ = tmp + '0';
}
else
{
*str++ = tmp - 10 + 'A';
}
*str++ = ' ';
src++;
}
*str = '\0';
}
void E2PRead(unsigned char *buf, unsigned char addr, unsigned char len)
{
do
{
I2CStart(); //开始
if(I2CWrite(0x50<<1)) //写机器号-写
{
break;
}
I2CStop();
}while(1);
I2CWrite(addr); //写地址
I2CStart(); //开始
I2CWrite((0x50<<1)|0x01); //写机器号-读
while(len>1)
{
*buf++ = I2CReadACK(); //读数据
len--;
}
*buf = I2CReadNAK();
I2CStop(); //停止
}
void E2PWrite(unsigned char *buf, unsigned char addr, unsigned char len)
{
while(len--)
{
do
{
I2CStart();
if(I2CWrite(0x50<<1))
{
break;
}
I2CStop();
}while(1);
I2CWrite(addr++);
I2CWrite(*buf++);
I2CStop();
}
}
评论0
最新资源