proteus仿真之EEprom读写实验
实验要求
利用eeprom存储lcd1602的上的数据 并且掉电后仍可显示先前存储的值。
效果图
功能介绍:
- KEY1:DATA 数据加
- KEY2:DATA 数据减
- KEY3:将DATA数据写入at24c02中
代码
main.c
#include <REGX51.H>
#include "LCD1602.h"//调用LCD1602文件
#include "stdio.h"
#include "iic.h"
#include "delay.h"
unsigned char date[10];
unsigned char numl;
sbit KEY1 = P3^0;
sbit KEY2 = P3^2;
sbit KEY3 = P3^4;
sbit KEY4 = P3^6;
//按键任务
void key_task()
{
if(KEY1 == 0)
{
Delay(10);
if(KEY1 == 0)
{
numl++;
while(!KEY1);
}
}
if(KEY2 == 0)
{
Delay(10);
if(KEY2 == 0)
{
numl--;
while(!KEY2);
}
}
//写入
if(KEY3 == 0)
{
Delay(10);
if(KEY3 == 0)
{
write_at24c02(1,numl);
while(!KEY3);
}
}
}
// lcd显示任务
void lcd_task()
{
sprintf((char *)date,"%02d",(int)numl);
LCD_ShowString(1,7,date);
}
void main()
{
LCD_Init();//初始化LCD1602
LCD_ShowString(1,1,"DATA");//调用LCD_ShowString函数
numl = read_at24c02(1);
while(1)
{
key_task();
lcd_task();
}
}
lcd1602.c
#include <REGX52.H>
#include "INTRINS.H"
#define LCD_DataPort P0
sbit LCD_RS=P2^2; //根据接入不同引脚自行更改
sbit LCD_RW=P2^1;
sbit LCD_EN=P2^0;
//函数定义:
/**
* @brief LCD1602延时函数,11.0592MHz调用可延时1ms
* @param 无
* @retval 无
*/
void LCD_Delay()
{
unsigned char i, j;
_nop_();
_nop_();
_nop_();
i = 11;
j = 190;
do
{
while (--j);
} while (--i);
}
/**
* @brief LCD1602写命令
* @param Command 要写入的命令
* @retval 无
*/
void LCD_WriteCommand(unsigned char Command)
{
LCD_RS=0;
LCD_RW=0;
LCD_DataPort=Command;
LCD_EN=1;
LCD_Delay();
LCD_EN=0;
LCD_Delay();
}
/**
* @brief LCD1602写数据
* @param Data 要写入的数据
* @retval 无
*/
void LCD_WriteData(unsigned char Data)
{
LCD_RS=1;
LCD_RW=0;
LCD_DataPort=Data;
LCD_EN=1;
LCD_Delay();
LCD_EN=0;
LCD_Delay();
}
/**
* @brief LCD1602设置光标位置
* @param Line 行位置,范围:1~2
* @param Column 列位置,范围:1~16
* @retval 无
*/
void LCD_SetCursor(unsigned char Line,unsigned char Column)
{
if(Line==1)
{
LCD_WriteCommand(0x80|(Column-1));
}
else if(Line==2)
{
LCD_WriteCommand(0x80|(Column-1+0x40));
}
}
/**
* @brief LCD1602初始化函数
* @param 无
* @retval 无
*/
void LCD_Init()
{
LCD_WriteCommand(0x38);//八位数据接口,两行显示,5*7点阵
LCD_WriteCommand(0x0c);//显示开,光标关,闪烁关
LCD_WriteCommand(0x06);//数据读写操作后,光标自动加一,画面不动
LCD_WriteCommand(0x01);//光标复位,清屏
}
/**
* @brief 在LCD1602指定位置开始显示所给字符串
* @param Line 起始行位置,范围:1~2
* @param Column 起始列位置,范围:1~16
* @param String 要显示的字符串
* @retval 无
*/
void LCD_ShowString(unsigned char Line,unsigned char Column,char *String)
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=0;String[i]!='\0';i++)
{
LCD_WriteData(String[i]);
}
}
iic.c
#include <REGX51.H>
#include "iic.h"
#include "intrins.h"
#define DELAY_TIME 5
sbit sda = P1^6;
sbit scl = P1^7;
static void I2C_Delay(unsigned char n)
{
do
{
_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();
}
while(n--);
}
void Delay100ms(void) //@12.000MHz
{
unsigned char data i, j, k;
_nop_();
_nop_();
i = 5;
j = 144;
k = 71;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
//起始信号
void I2CStart(void)
{
sda = 1;
scl = 1;
I2C_Delay(DELAY_TIME);
sda = 0;
I2C_Delay(DELAY_TIME);
scl = 0;
}
//停止信号
void I2CStop(void)
{
sda = 0;
scl = 1;
I2C_Delay(DELAY_TIME);
sda = 1;
I2C_Delay(DELAY_TIME);
}
//发送一个字节
void I2CSendByte(unsigned char byt)
{
unsigned char i;
for(i=0; i<8; i++){
scl = 0;
I2C_Delay(DELAY_TIME);
if(byt & 0x80){
sda = 1;
}
else{
sda = 0;
}
I2C_Delay(DELAY_TIME);
scl = 1;
byt <<= 1;
I2C_Delay(DELAY_TIME);
}
scl = 0;
}
//接收一个字节
unsigned char I2CReceiveByte(void)
{
unsigned char da;
unsigned char i;
for(i=0;i<8;i++){
scl = 1;
I2C_Delay(DELAY_TIME);
da <<= 1;
if(sda)
da |= 0x01;
scl = 0;
I2C_Delay(DELAY_TIME);
}
return da;
}
//等待应答
unsigned char I2CWaitAck(void)
{
unsigned char ackbit;
scl = 1;
I2C_Delay(DELAY_TIME);
ackbit = sda;
scl = 0;
I2C_Delay(DELAY_TIME);
return ackbit;
}
//发送应答
void I2CSendAck(unsigned char ackbit)
{
scl = 0;
sda = ackbit;
I2C_Delay(DELAY_TIME);
scl = 1;
I2C_Delay(DELAY_TIME);
scl = 0;
sda = 1;
I2C_Delay(DELAY_TIME);
}
//AT24c02 写数据
void write_at24c02(unsigned char addr,unsigned char date)
{
I2CStart();
I2CSendByte(0xA0);
I2CWaitAck();
I2CSendByte(addr);
I2CWaitAck();
I2CSendByte(date);
I2CWaitAck();
I2CStop();
Delay100ms(); //延时函数必须加
}
//AT24c02 读数据
unsigned char read_at24c02(unsigned char addr)
{
unsigned char dat;
I2CStart();
I2CSendByte(0xA0);
I2CWaitAck();
I2CSendByte(addr);
I2CWaitAck();
I2CStart();
I2CSendByte(0xA1);
I2CWaitAck(); //发送数据后必须加等待函数
dat = I2CReceiveByte();
I2CSendAck(1); //接收之后必须加一个发送应答
I2CStop();
Delay100ms(); //延时函数必须加
return dat;
}