51单片机-I2C-E2PROM

51单片机-I2C-E2PROM


1. 文件

文件下载地址:

https://download.csdn.net/download/weixin_43130546/12260893

2. I2C总线

main.c


#include 
#include 

#define I2CDelay() {_nop_();_nop_();_nop_();_nop_();}

sbit I2C_SCL = P2^1;
sbit I2C_SDA = P2^0;

extern void InitLcd1602(void);
extern void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str);

bit I2CAddressing(unsigned char addr);

void main(void)
{
	bit ack;
	unsigned char str[10];

	InitLcd1602();

	ack = I2CAddressing(0x50);
	str[0] = (unsigned char)ack + '0';
	str[1] = '\0';
	LcdShowStr(0,0,str);

	ack = I2CAddressing(0x60);
	str[0] = (unsigned char)ack + '0';
	str[1] = '\0';
	LcdShowStr(0,1,str);

	while(1){}
}

void I2CStart(void)
{
	I2C_SCL = 1;
	I2C_SDA = 1;
	I2CDelay();
	I2C_SDA = 0;
	I2CDelay();
	I2C_SCL = 0;
	//I2CDelay();
}

void I2CStop(void)
{
	I2C_SCL = 0;
	I2C_SDA = 0;
	I2CDelay();
	I2C_SCL = 1;
	I2CDelay();
	I2C_SDA = 1;
	I2CDelay();
}

bit I2CWrite(unsigned char dat)
{
	bit ack;
	unsigned char mask;

	for(mask=0x80; mask!=0; mask>>=1)
	{
		if(mask&dat)
		{
			I2C_SDA = 1;
		}
		else
		{
			I2C_SDA = 0;
		}
		I2CDelay();
		I2C_SCL = 1;
		I2CDelay();
		I2C_SCL = 0;
		I2CDelay();
	}

	I2C_SDA = 1;
	I2CDelay();
	I2C_SCL = 1;
	I2CDelay();
	ack = I2C_SDA;
	I2C_SCL = 0;
	I2CDelay();

	return ack;
}

bit I2CAddressing(unsigned char addr)
{
	bit ack;

	I2CStart();
	ack = I2CWrite(addr<<1);  //有局限性
	I2CStop();

	return ack;
}

LCD1602.c


#include 

#define LCD1602_DB P0
sbit LCD1602_RS = P3^5;
sbit LCD1602_RW = P3^6;
sbit LCD1602_E = P3^4;

sbit DULA = P2^6;
sbit WELA = P2^7;

void LcdWaitReady(void)
{
	unsigned char sta;
	LCD1602_DB = 0xFF;
	LCD1602_RS = 0;
	LCD1602_RW = 1;
	do
	{
		LCD1602_E = 1;
		sta = LCD1602_DB;
		LCD1602_E = 0;
	}
	while(sta & 0x80);
}

void LcdWriteCmd(unsigned char cmd)
{
	LcdWaitReady();
	LCD1602_RS = 0;
	LCD1602_RW = 0;
	LCD1602_DB = cmd;
	LCD1602_E = 1;
	LCD1602_E = 0;
}

void LcdWriteDat(unsigned char dat)
{
	LcdWaitReady();
	LCD1602_RS = 1;
	LCD1602_RW = 0;
	LCD1602_DB = dat;
	LCD1602_E = 1;
	LCD1602_E = 0;
}

void LcdSetCursor(unsigned char x, unsigned char y)
{
	unsigned char addr;
	if(0 == y)
	{
		addr = 0x80 + 0x00 + x;
	}
	else
	{
		addr = 0x80 + 0x40 + x;
	}
	LcdWriteCmd(addr);
}

void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str)
{
	LcdSetCursor(x,y);
	while(*str != '\0')
	{
		LcdWriteDat(*str++);
	}
}

void LcdAreaClear(unsigned char x, unsigned char y, unsigned char len)
{
	LcdSetCursor(x,y);
	while(len--)
	{
		LcdWriteDat(' ');
	}
}

void LcdFullClear(void)
{
	LcdWriteCmd(0x01);
}

void InitLcd1602(void)
{
	P0 = 0x00;
	DULA = 1;
	DULA = 0;
	P0 = 0xFF;
	WELA = 1;
	WELA = 0;

	LCD1602_E = 0;

	LcdWriteCmd(0x38);
	LcdWriteCmd(0x0F);
	LcdWriteCmd(0x06);
	LcdWriteCmd(0x01);
}

3. E2PROM单字节读写

main.c


#include 

extern void InitLcd1602(void);
unsigned char E2ReadByte(unsigned char addr);
void E2WriteByte(unsigned char addr, unsigned char dat);
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);

void main(void)
{
	unsigned char dat;
	unsigned char str[10];

	InitLcd1602();

	dat = E2ReadByte(0x02);
	str[0] = (dat/100%10) + '0';
	str[1] = (dat/10%10) + '0';
	str[2] = (dat/1%10) + '0';
	str[3] = '\0';
	LcdShowStr(0,0,str);
	dat++;
	E2WriteByte(0x02,dat);

	while(1){}
}

unsigned char E2ReadByte(unsigned char addr)
{
	unsigned char dat;

	I2CStart();							 //开始
	I2CWrite(0x50<<1);					  //写机器号   ,写
	I2CWrite(addr);						  //写地址

	I2CStart();							   //开始
	I2CWrite((0x50<<1) | 0x01);			 	//写机器号 ,读
	dat = I2CReadNAK();					   //读数据
	I2CStop();

	return dat;
}

void E2WriteByte(unsigned char addr, unsigned char dat)
{
	I2CStart();				 //开始
	I2CWrite(0x50<<1);		  //写机器号 , 写
	I2CWrite(addr);			  //写地址

	I2CWrite(dat);			  //写数据
	I2CStop();
}

I2C.c


#include 
#include 

#define I2CDelay() {_nop_(); _nop_(); _nop_(); _nop_();}

sbit I2C_SCL = P2^1;
sbit I2C_SDA = P2^0;

void I2CStart(void)
{
	I2C_SDA = 1;
	I2C_SCL = 1;
	I2CDelay();
	I2C_SDA = 0;
	I2CDelay();
	I2C_SCL = 0;
	I2CDelay();
}

void I2CStop(void)
{
	I2C_SCL = 0;
	I2C_SDA = 0;
	I2CDelay();
	I2C_SCL = 1;
	I2CDelay();
	I2C_SDA = 1;
	I2CDelay();
}

bit I2CWrite(unsigned char dat)
{
	bit ack;
	unsigned char mask; 

	for(mask=0x80; mask!=0; mask>>=1)
	{
		if(0 == (mask&dat))
		{
			I2C_SDA = 0;
		}
		else
		{
			I2C_SDA = 1;
		}
		I2CDelay();
		I2C_SCL = 1;
		I2CDelay();
		I2C_SCL = 0;
		I2CDelay();
	}

	I2C_SDA = 1;
	I2CDelay();
	I2C_SCL = 1;
	I2CDelay();
	ack = I2C_SDA;
	I2C_SCL = 0;
	I2CDelay();

	return (~ack);
}

unsigned char I2CReadNAK(void)
{
	unsigned char mask;
	unsigned char dat;

	I2C_SDA = 1;
	I2CDelay();
	for(mask=0x80; mask!=0; mask>>=1)
	{
		I2C_SCL = 1;
		I2CDelay();
		if(0 == I2C_SDA)
		{
			dat &= ~mask;
		}
		else
		{
			dat |= mask;
		}
		I2C_SCL = 0;
		I2CDelay();
	}

	I2C_SDA = 1;
	I2CDelay();
	I2C_SCL = 1;
	I2CDelay();
	I2C_SCL = 0;
	I2CDelay();

	return dat;
}

unsigned char I2CReadACK(void)
{
	unsigned char mask;
	unsigned char dat;

	I2C_SDA = 1;
	I2CDelay();
	for(mask=0x80; mask!=0; mask>>=1)
	{
		I2C_SCL = 1;
		I2CDelay();
		if(0 == I2C_SDA)
		{
			dat &= ~mask;
		}
		else
		{
			dat |= mask;
		}
		I2C_SCL = 0;
		I2CDelay();
	}

	I2C_SDA = 0;
	I2CDelay();
	I2C_SCL = 1;
	I2CDelay();
	I2C_SCL = 0;
	I2CDelay();

	return dat;
}

LCD1602.c


#include 

#define LCD1602_DB P0
sbit LCD1602_RS = P3^5;
sbit LCD1602_RW = P3^6;
sbit LCD1602_E = P3^4;

sbit DULA = P2^6;
sbit WELA = P2^7;

void LcdWaitReady(void)
{
	unsigned char sta;
	LCD1602_DB = 0xFF;
	LCD1602_RS = 0;
	LCD1602_RW = 1;
	do
	{
		LCD1602_E = 1;
		sta = LCD1602_DB;
		LCD1602_E = 0;
	}
	while(sta & 0x80);
}

void LcdWriteCmd(unsigned char cmd)
{
	LcdWaitReady();
	LCD1602_RS = 0;
	LCD1602_RW = 0;
	LCD1602_DB = cmd;
	LCD1602_E = 1;
	LCD1602_E = 0;
}

void LcdWriteDat(unsigned char dat)
{
	LcdWaitReady();
	LCD1602_RS = 1;
	LCD1602_RW = 0;
	LCD1602_DB = dat;
	LCD1602_E = 1;
	LCD1602_E = 0;
}

void LcdSetCursor(unsigned char x, unsigned char y)
{
	unsigned char addr;
	if(0 == y)
	{
		addr = 0x80 + 0x00 + x;
	}
	else
	{
		addr = 0x80 + 0x40 + x;
	}
	LcdWriteCmd(addr);
}

void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str)
{
	LcdSetCursor(x,y);
	while(*str != '\0')
	{
		LcdWriteDat(*str++);
	}
}

void LcdAreaClear(unsigned char x, unsigned char y, unsigned char len)
{
	LcdSetCursor(x,y);
	while(len--)
	{
		LcdWriteDat(' ');
	}
}

void LcdFullClear(void)
{
	LcdWriteCmd(0x01);
}

void InitLcd1602(void)
{
	P0 = 0x00;
	DULA = 1;
	DULA = 0;
	P0 = 0xFF;
	WELA = 1;
	WELA = 0;

	LCD1602_E = 0;

	LcdWriteCmd(0x38);
	LcdWriteCmd(0x0F);
	LcdWriteCmd(0x06);
	LcdWriteCmd(0x01);
}

4. E2PROM多字节读写

main.c


#include 

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();
	}
}

I2C.c


#include 
#include 

#define I2CDelay() {_nop_(); _nop_(); _nop_(); _nop_();}

sbit I2C_SCL = P2^1;
sbit I2C_SDA = P2^0;

void I2CStart(void)
{
	I2C_SDA = 1;
	I2C_SCL = 1;
	I2CDelay();
	I2C_SDA = 0;
	I2CDelay();
	I2C_SCL = 0;
	I2CDelay();
}

void I2CStop(void)
{
	I2C_SCL = 0;
	I2C_SDA = 0;
	I2CDelay();
	I2C_SCL = 1;
	I2CDelay();
	I2C_SDA = 1;
	I2CDelay();
}

bit I2CWrite(unsigned char dat)
{
	bit ack;
	unsigned char mask; 

	for(mask=0x80; mask!=0; mask>>=1)
	{
		if(0 == (mask&dat))
		{
			I2C_SDA = 0;
		}
		else
		{
			I2C_SDA = 1;
		}
		I2CDelay();
		I2C_SCL = 1;
		I2CDelay();
		I2C_SCL = 0;
		I2CDelay();
	}

	I2C_SDA = 1;
	I2CDelay();
	I2C_SCL = 1;
	I2CDelay();
	ack = I2C_SDA;
	I2C_SCL = 0;
	I2CDelay();

	return (~ack);
}

unsigned char I2CReadNAK(void)
{
	unsigned char mask;
	unsigned char dat;

	I2C_SDA = 1;
	I2CDelay();
	for(mask=0x80; mask!=0; mask>>=1)
	{
		I2C_SCL = 1;
		I2CDelay();
		if(0 == I2C_SDA)
		{
			dat &= ~mask;
		}
		else
		{
			dat |= mask;
		}
		I2C_SCL = 0;
		I2CDelay();
	}

	I2C_SDA = 1;
	I2CDelay();
	I2C_SCL = 1;
	I2CDelay();
	I2C_SCL = 0;
	I2CDelay();

	return dat;
}

unsigned char I2CReadACK(void)
{
	unsigned char mask;
	unsigned char dat;

	I2C_SDA = 1;
	I2CDelay();
	for(mask=0x80; mask!=0; mask>>=1)
	{
		I2C_SCL = 1;
		I2CDelay();
		if(0 == I2C_SDA)
		{
			dat &= ~mask;
		}
		else
		{
			dat |= mask;
		}
		I2C_SCL = 0;
		I2CDelay();
	}

	I2C_SDA = 0;
	I2CDelay();
	I2C_SCL = 1;
	I2CDelay();
	I2C_SCL = 0;
	I2CDelay();

	return dat;
}

LCD1602.c


#include 

#define LCD1602_DB P0//数据总线
sbit LCD1602_RS = P3^5;//数据1&命令0
sbit LCD1602_RW = P3^6;//读1&写0
sbit LCD1602_E = P3^4;//脉冲

sbit DULA = P2^6;
sbit WELA = P2^7;

void LcdWaitReady(void)
{
	unsigned char sta;
	LCD1602_DB = 0xFF;
	LCD1602_RS = 0;
	LCD1602_RW = 1;
	do
	{
		LCD1602_E = 1;
		sta = LCD1602_DB;
		LCD1602_E = 0;
	}
	while(sta & 0x80);
}

void LcdWriteCmd(unsigned char cmd)
{
	LcdWaitReady();
	LCD1602_RS = 0;
	LCD1602_RW = 0;
	LCD1602_DB = cmd;
	LCD1602_E = 1;
	LCD1602_E = 0;
}

void LcdWriteDat(unsigned char dat)
{
	LcdWaitReady();
	LCD1602_RS = 1;
	LCD1602_RW = 0;
	LCD1602_DB = dat;
	LCD1602_E = 1;
	LCD1602_E = 0;
}

void LcdSetCursor(unsigned char x, unsigned char y)
{
	unsigned char addr;
	if(0==y)
	{
		addr = 0x00 + x;
	}
	else
	{
		addr = 0x40 + x;
	}
	LcdWriteCmd(addr | 0x80);
}

void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str)
{
	LcdSetCursor(x,y);
	while(*str != '\0')
	{
		LcdWriteDat(*str++);
	}
}

void LcdAreaClear(unsigned char x, unsigned char y, unsigned char len)
{
	LcdSetCursor(x,y);
	while(len--)
	{
		LcdWriteDat(' ');
	}
}

void LcdFullClear(void)
{
	LcdWriteCmd(0x01);
}

void InitLcd1602(void)
{
	P0 = 0x00;
	DULA = 1;
	DULA = 0;
	P0 = 0xFF;
	WELA = 1;
	WELA = 0;

	LCD1602_E = 0;

	LcdWriteCmd(0x38);//设置显示模式0x38:16*2显示;5*7点阵;8位数据接口
	LcdWriteCmd(0x0F);//0b00001DCB:D-开显示1&关显示0;C-开光标1&关光标0;B-开光标闪烁1&关光标闪烁0
	LcdWriteCmd(0x06);//0b000001NS:N-指针自加1&指针自减0;S-开移屏1&关移屏0
	LcdWriteCmd(0x01);//清屏
}

5. E2PROM页读写

main.c


#include 

extern void InitLcd1602(void);
extern void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str);
extern void E2PRead(unsigned char *buf, unsigned char addr, unsigned char len);
extern void E2PWrite(unsigned char *buf, unsigned char addr, unsigned char len);

void MemToStr(unsigned char *str, unsigned char *src, unsigned char len);

void main(void)
{
	unsigned char i;
	unsigned char buf[5];
	unsigned char str[20];

	InitLcd1602();
	E2PRead(buf,0x8E,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,0x8E,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';
}

I2C.c


#include 
#include 

#define I2CDelay() {_nop_(); _nop_(); _nop_(); _nop_();}

sbit I2C_SCL = P2^1;
sbit I2C_SDA = P2^0;

void I2CStart(void)
{
	I2C_SDA = 1;
	I2C_SCL = 1;
	I2CDelay();
	I2C_SDA = 0;
	I2CDelay();
	I2C_SCL = 0;
	I2CDelay();
}

void I2CStop(void)
{
	I2C_SCL = 0;
	I2C_SDA = 0;
	I2CDelay();
	I2C_SCL = 1;
	I2CDelay();
	I2C_SDA = 1;
	I2CDelay();
}

bit I2CWrite(unsigned char dat)
{
	bit ack;
	unsigned char mask; 

	for(mask=0x80; mask!=0; mask>>=1)
	{
		if(0 == (mask&dat))
		{
			I2C_SDA = 0;
		}
		else
		{
			I2C_SDA = 1;
		}
		I2CDelay();
		I2C_SCL = 1;
		I2CDelay();
		I2C_SCL = 0;
		I2CDelay();
	}

	I2C_SDA = 1;
	I2CDelay();
	I2C_SCL = 1;
	I2CDelay();
	ack = I2C_SDA;
	I2C_SCL = 0;
	I2CDelay();

	return (~ack);
}

unsigned char I2CReadNAK(void)
{
	unsigned char mask;
	unsigned char dat;

	I2C_SDA = 1;
	I2CDelay();
	for(mask=0x80; mask!=0; mask>>=1)
	{
		I2C_SCL = 1;
		I2CDelay();
		if(0 == I2C_SDA)
		{
			dat &= ~mask;
		}
		else
		{
			dat |= mask;
		}
		I2C_SCL = 0;
		I2CDelay();
	}

	I2C_SDA = 1;
	I2CDelay();
	I2C_SCL = 1;
	I2CDelay();
	I2C_SCL = 0;
	I2CDelay();

	return dat;
}

unsigned char I2CReadACK(void)
{
	unsigned char mask;
	unsigned char dat;

	I2C_SDA = 1;
	I2CDelay();
	for(mask=0x80; mask!=0; mask>>=1)
	{
		I2C_SCL = 1;
		I2CDelay();
		if(0 == I2C_SDA)
		{
			dat &= ~mask;
		}
		else
		{
			dat |= mask;
		}
		I2C_SCL = 0;
		I2CDelay();
	}

	I2C_SDA = 0;
	I2CDelay();
	I2C_SCL = 1;
	I2CDelay();
	I2C_SCL = 0;
	I2CDelay();

	return dat;
}

E2PROM.c


#include 

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 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 > 0)
	{
		do
		{
			I2CStart();					  //开始
			if(I2CWrite(0x50 << 1))		  //写机器码-写
			{
				break;
			}
			I2CStop();
		}while(1);
		I2CWrite(addr);					  //写地址
		while(len > 0)
		{
			I2CWrite(*buf++);			  //写数据
			len--;
			addr++;
			if(0 == (addr & 0x07))		  //分页
			{
				break;
			}
		}
		I2CStop();						  //结束
	}
}

LCD1602.c


#include 

#define LCD1602_DB P0//数据总线
sbit LCD1602_RS = P3^5;//数据1&命令0
sbit LCD1602_RW = P3^6;//读1&写0
sbit LCD1602_E = P3^4;//脉冲

sbit DULA = P2^6;
sbit WELA = P2^7;

void LcdWaitReady(void)
{
	unsigned char sta;
	LCD1602_DB = 0xFF;
	LCD1602_RS = 0;
	LCD1602_RW = 1;
	do
	{
		LCD1602_E = 1;
		sta = LCD1602_DB;
		LCD1602_E = 0;
	}
	while(sta & 0x80);
}

void LcdWriteCmd(unsigned char cmd)
{
	LcdWaitReady();
	LCD1602_RS = 0;
	LCD1602_RW = 0;
	LCD1602_DB = cmd;
	LCD1602_E = 1;
	LCD1602_E = 0;
}

void LcdWriteDat(unsigned char dat)
{
	LcdWaitReady();
	LCD1602_RS = 1;
	LCD1602_RW = 0;
	LCD1602_DB = dat;
	LCD1602_E = 1;
	LCD1602_E = 0;
}

void LcdSetCursor(unsigned char x, unsigned char y)
{
	unsigned char addr;
	if(0==y)
	{
		addr = 0x00 + x;
	}
	else
	{
		addr = 0x40 + x;
	}
	LcdWriteCmd(addr | 0x80);
}

void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str)
{
	LcdSetCursor(x,y);
	while(*str != '\0')
	{
		LcdWriteDat(*str++);
	}
}

void LcdAreaClear(unsigned char x, unsigned char y, unsigned char len)
{
	LcdSetCursor(x,y);
	while(len--)
	{
		LcdWriteDat(' ');
	}
}

void LcdFullClear(void)
{
	LcdWriteCmd(0x01);
}

void InitLcd1602(void)
{
	P0 = 0x00;
	DULA = 1;
	DULA = 0;
	P0 = 0xFF;
	WELA = 1;
	WELA = 0;
	
	LCD1602_E = 0;
	
	LcdWriteCmd(0x38);//设置显示模式0x38:16*2显示;5*7点阵;8位数据接口
	LcdWriteCmd(0x0F);//0b00001DCB:D-开显示1&关显示0;C-开光标1&关光标0;B-开光标闪烁1&关光标闪烁0
	LcdWriteCmd(0x06);//0b000001NS:N-指针自加1&指针自减0;S-开移屏1&关移屏0
	LcdWriteCmd(0x01);//清屏
}

你可能感兴趣的:(51单片机-I2C-E2PROM)