存储器件,掉电后数据不丢失
单字节读写
#include
extern void InitLcd1602();
extern void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str);
extern void I2CStart();
extern void I2CStop();
extern unsigned char I2CReadNAK();
extern bit I2CWrite(unsigned char dat);
unsigned char E2ReadByte(unsigned char addr);
void E2WriteByte(unsigned char addr, unsigned char dat);
void main()
{
unsigned char dat;
unsigned char str[10];
InitLcd1602();
dat = E2ReadByte(0x02);
str[0] = (dat/100) + '0';
str[1] = (dat/10%10) + '0';
str[2] = (dat%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);
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();
}
1602液晶模块
#include
#define LCD1602_DB P0
sbit LCD1602_RS = P1^0;
sbit LCD1602_RW = P1^1;
sbit LCD1602_E = P1^5;
void LcdWaitReady()
{
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;
cmd = LCD1602_DB;
LCD1602_E = 1;
LCD1602_E = 0;
}
void LcdWriteDat(unsigned char dat)
{
LcdWaitReady();
LCD1602_RS = 1;
LCD1602_RW = 0;
dat = LCD1602_DB;
LCD1602_E = 1;
LCD1602_E = 0;
}
void LcdSetCursor(unsigned char x, unsigned char y)
{
unsigned char addr;
if(y == 0)
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 InitLcd1602()
{
LcdWriteCmd(0x38);
LcdWriteCmd(0x0C);
LcdWriteCmd(0x06);
LcdWriteCmd(0x01);
}
I2C模块
#include
#include
#define I2CDelay() {_nop_();_nop_();_nop_();_nop_();}
sbit I2C_SCL = P3^7;
sbit I2C_SDA = P3^6;
void I2CStart()
{
I2C_SDA = 1;
I2C_SCL = 1;
I2CDelay();
I2C_SDA = 0;
I2CDelay();
I2C_SCL = 0;
}
void I2CStop()
{
I2C_SDA = 0;
I2C_SCL = 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)==0)
I2C_SDA = 0;
else
I2C_SDA = 1;
I2CDelay();
I2C_SCL = 1;
I2CDelay();
I2C_SCL = 0;
}
I2C_SDA = 1;
I2CDelay();
ack = I2C_SDA;
I2CDelay();
I2C_SCL = 0;
return (~ack);
}
unsigned char I2CReadNAK()
{
unsigned char mask;
unsigned char dat;
I2C_SDA = 1;
for(mask=0x80; mask!=0; mask>>=1)
{
I2CDelay();
I2C_SCL = 1;
if(I2C_SDA == 0)
dat &= ~mask;
else
dat |= mask;
I2CDelay();
I2C_SCL = 0;
}
I2C_SDA = 1;
I2CDelay();
I2C_SCL = 1;
I2CDelay();
I2C_SCL = 0;
return dat;
}
unsigned char I2CReadACK()
{
unsigned char mask;
unsigned char dat;
I2C_SDA = 1;
for(mask=0x80; mask!=0; mask>>=1)
{
I2CDelay();
I2C_SCL = 1;
if(I2C_SDA == 0)
dat &= ~mask;
else
dat |= mask;
I2CDelay();
I2C_SCL = 0;
return dat;
}
}
多字节读写
#include
extern void InitLcd1602();
extern void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str);
extern void I2CStart();
extern void I2CStop();
extern unsigned char I2CReadACK();
extern unsigned char I2CReadNAK();
extern bit I2CWrite(unsigned char dat);
void E2Read(unsigned char *buf, unsigned char addr, unsigned char len);
void E2Write(unsigned char *buf, unsigned char addr, unsigned char len);
void MemToStr(unsigned char *str, unsigned char *src, unsigned char len);
void main()
{
unsigned char i;
unsigned char buf[5];
unsigned char str[20];
InitLcd1602();
E2Read(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;
}
E2Write(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 = *str & 0x0f;
if(tmp <= 9)
*str++ = tmp - 10 - 'A';
*str++ =' ';
src++;
}
*str = '\0';
}
void E2Read(unsigned char *buf, unsigned char addr, unsigned char len)
{
do{
I2CStart();
if (I2CWrite(0x50<<1))
{
break;
}
}while(1);
I2CWrite(addr);
I2CStart();
I2CWrite((0x50<<1)|0x01);
while(len > 1)
{
*buf++ = I2CReadACK();
len--;
}
*buf = I2CReadACK();
I2CStop();
}
void E2Write(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();
}
}
//按页连续写入
I2CWrite(addr);
while (len > 0)
{
I2CWrite(*buf++);
len--;
addr++;
if ((addr&0x07) == 0)
{
break;
}
}
I2CStop();
}
4、使用按键、1602液晶、EEPROM做一个简单的密码锁程序。