📄 eeprom.c
字号:
//**************************************************************************************************************************
#include <c8051F020.h>
#include <stdio.h>
#include <string.h>
#include "HEAD.H"
//**************************************************************************************************************************
void EEPROM_Init(void)
{
xdata unsigned char i;
NowWellNum=0x00;
for(i=0;i<MaxWellSaveNum;i++)
{
if( (Read_Eeprom(0x400*i+HollDateLong)==0xA5) && (Read_Eeprom(0x400*i+HollDateLong+1)==0x5A) )
{
WellSaveStation[i]=0xAA;
NowWellNum++;
}
else
{
NowSaveWellAddr=i;
}
}
if(NowWellNum==MaxWellSaveNum)
{
NowSaveWellAddr=0x00;
}
LCD_SHOW_WellSaveStation();
}
//**************************************************************************************************************************
void Start_I2C(void)
{
EE_SDA=1; Some_nop(5);
EE_SCL=1; Some_nop(5);
EE_SDA=0; Some_nop(5);
EE_SCL=0; Some_nop(5);
}
//**************************************************************************************************************************
void Stop_I2C(void)
{
EE_SDA=0; Some_nop(5);
EE_SCL=1; Some_nop(5);
EE_SDA=1; Some_nop(5);
}
//**************************************************************************************************************************
bit WriteEepromByet(unsigned char date)
{
bit Ack_f;
xdata unsigned char i;
for(i=0;i<8;i++)
{
if((date<<i)&0x80)
EE_SDA=1;
else
EE_SDA=0;
EE_SCL=1; Some_nop(5);
EE_SCL=0; Some_nop(5);
}
EE_SDA=1; Some_nop(5);
EE_SCL=1; Some_nop(5);
if(EE_SDA==1)
Ack_f=0;
else
Ack_f=1;
EE_SCL=0; Some_nop(5);
return Ack_f;
}
//**************************************************************************************************************************
unsigned char ReadEepromBaye(void)
{
xdata unsigned char date;
xdata unsigned char i;
date=0;
EE_SDA=1; Some_nop(5);
for(i=0;i<8;i++)
{
EE_SCL=0; Some_nop(5);
EE_SCL=1; Some_nop(5);
date<<=1;
if(EE_SDA==1)
date++;
}
EE_SCL=0; Some_nop(5);
return(date);
}
//**************************************************************************************************************************
void Ack_I2C(bit a)
{
EE_SDA=a; Some_nop(5);
EE_SCL=1; Some_nop(5);
EE_SCL=0; Some_nop(5);
}
//**************************************************************************************************************************
bit Write_Eeprom(unsigned int add,unsigned char date)
{
xdata unsigned char addh,addl;
if(add >= EEPROM_CAP)
add-=EEPROM_CAP;
addh=add>>8;
addl=add;
Start_I2C();
if(!WriteEepromByet(0xA0))return(0);
if(!WriteEepromByet(addh))return(0);
if(!WriteEepromByet(addl))return(0);
if(!WriteEepromByet(date))return(0);
Stop_I2C();
Delay_ms(100);
return 1;
}
//**************************************************************************************************************************
unsigned char Read_Eeprom(unsigned int add)
{
xdata unsigned char addh,addl;
xdata unsigned char date;
if(add >= EEPROM_CAP)
add-=EEPROM_CAP;
addh=add>>8;
addl=add;
Start_I2C();
if(!WriteEepromByet(0xA0))return(0);
if(!WriteEepromByet(addh))return(0);
if(!WriteEepromByet(addl))return(0);
Start_I2C();
if(!WriteEepromByet(0xA1))return(0);
date=ReadEepromBaye();
Ack_I2C(1);
Stop_I2C();
return(date);
}
//**************************************************************************************************************************
void Some_nop(unsigned char num)
{
xdata unsigned char i;
for(i=0;i<num;i++);
}
//**************************************************************************************************************************
bit Write_Well_date(unsigned int add,unsigned char * date,unsigned int D_Long)
{
xdata unsigned char addh,addl;
xdata unsigned int i,j;
if(add >= EEPROM_CAP)
add-=EEPROM_CAP;
for(j=0;j<D_Long/64+1;j++)
{
addh=(add+j*64)>>8;
addl=add+j*64;
Start_I2C();
if(!WriteEepromByet(0xA0))return(0);
if(!WriteEepromByet(addh))return(0);
if(!WriteEepromByet(addl))return(0);
for(i=0;i<64;i++)
{
if( (j*64+i) >= D_Long)break;
if(!WriteEepromByet(date[j*64+i]))return(0);
if(i%10==0x00)
{
sprintf(Str_Buffer,":%3.0f/100 ",(float)((float)(j*64+i)*100/HollDateLong));
//LCD_DispStr(Str_Buffer,32,5);
LCD_DispStr(Str_Buffer,40,34);
disp_chn_str("比值",38,10,12,1);
Display_Data();
}
}
Stop_I2C();
Delay_ms(100);
}
return 1;
}
//**************************************************************************************************************************
void UpLoadWellDateTure(unsigned char truenum)
{
xdata unsigned char i,j=0x00;
EEPROM_Init();
for(i=0;i<MaxWellSaveNum;i++)
{
if( (Read_Eeprom(0x400*i+HollDateLong)==0xA5) && (Read_Eeprom(0x400*i+HollDateLong+1)==0x5A) )
j++;
if(j==truenum)
{
UpLoadWellDate(i);
return ;
}
}
}
//**************************************************************************************************************************
void UpLoadWellDate(unsigned char num)
{
xdata unsigned int i,temp_datelong;
xdata unsigned char tempa,tempb;
xdata unsigned char temp_data[9],temp_time[9];
xdata unsigned char temp_wellnum;
EA = 0;
// 读取数据
for(i=0;i<HollDateLong;i++)
{
HollDate[i]=Read_Eeprom(0x400*num+i);
}
// 读取数据长度
tempa=Read_Eeprom(0x400*num+HollDateLong+2);
tempb=Read_Eeprom(0x400*num+HollDateLong+3);
temp_datelong=tempa*256+tempb;
// 读取日期
for(i=0;i<9;i++)
{
temp_data[i]=Read_Eeprom(0x400*num+HollDateLong+4+i);
}
// 读取时间
for(i=0;i<9;i++)
{
temp_time[i]=Read_Eeprom(0x400*num+HollDateLong+13+i);
}
// 读井号
temp_wellnum=Read_Eeprom(0x400*num+HollDateLong+22);
Com0_Send_Char(temp_wellnum);
Com0_Send_Char(tempa);
Com0_Send_Char(tempb);
Com0_Send_String(temp_data,9);
Com0_Send_String(temp_time,9);
Com0_Send_String(HollDate,HollDateLong);
EA = 1;
/*
Com_Send_Char(temp_wellnum,ComFlag);
Com_Send_Char(tempa,ComFlag);
Com_Send_Char(tempb,ComFlag);
Com_Send_String(temp_data,9,ComFlag);
Com_Send_String(temp_time,9,ComFlag);
Com_Send_String(HollDate,HollDateLong,ComFlag);
*/
}
//**************************************************************************************************************************
void Well_Show(unsigned char num)
{
xdata unsigned int i,temp_datelong;
xdata unsigned int tempa,tempb,tempc;
xdata unsigned char temp_data[9],temp_time[9];
xdata unsigned char temp_wellnum;
LCD_CLEAR();
// 读取数据
for(i=0;i<HollDateLong;i++)
{
HollDate[i]=Read_Eeprom(0x400*num+i);
}
// 读取数据长度
tempa=Read_Eeprom(0x400*num+HollDateLong+2);
tempb=Read_Eeprom(0x400*num+HollDateLong+3);
temp_datelong=tempa*256+tempb;
// 读取日期
for(i=0;i<9;i++)
{
temp_data[i]=Read_Eeprom(0x400*num+HollDateLong+4+i);
}
temp_data[8]=0x00;
// 读取时间
for(i=0;i<9;i++)
{
temp_time[i]=Read_Eeprom(0x400*num+HollDateLong+13+i);
}
temp_time[8]=0x00;
// 读井号
temp_wellnum=Read_Eeprom(0x400*num+HollDateLong+22);
// 显示图形
draw_line(20,50,120,50);
draw_line(20,0,20,50);
for(i=0;i<HollDateLong/40;i++)
{
tempa=HollDate[i*40+0];
tempb=HollDate[i*40+39];
if(tempb>=tempa)
{
tempc=tempb-tempa;
}
else
{
tempc=0xFF-tempa+tempb;
}
if(tempc>5)
tempc=5;
draw_line(i*4+20,50-tempc*5,i*4+20+4,50-tempc*5);
}
// 显示平均转速
tempa=0x00;
for(i=0;i<HollDateLong-1;i++)
{
if(HollDate[i]>HollDate[i+1])
tempa++;
}
tempb=HollDate[HollDateLong-1];
tempc=tempa*256+tempb;
sprintf(Str_Buffer,"%4.0fc",(float)tempc);
LCD_DispStr(Str_Buffer,0,30);
sprintf(Str_Buffer,"%4.1fc/m",(float)tempc/(float)(temp_datelong/40)*60);
LCD_DispStr(Str_Buffer,0,75);
// 显示测试时间
sprintf(Str_Buffer,"%03.0fs",(float)temp_datelong/40);
LCD_DispStr(Str_Buffer,0,5);
// 显示井名
sprintf(Str_Buffer,"%03.0f",(float)temp_wellnum);
LCD_DispStr(Str_Buffer,56,8);
// 显示日期
strcpy(Str_Buffer,temp_data);
LCD_DispStr(Str_Buffer,56,29);
// 显示时间
strcpy(Str_Buffer,temp_time);
LCD_DispStr(Str_Buffer,56,80);
Display_Data();
}
//**************************************************************************************************************************
/*
bit EEPROM_TEST(void)
{
unsigned int i;
unsigned char tempdate,xxx;
for(i=0;i<EEPROM_CAP;i+=10)
{
tempdate=i;
if(!Write_Eeprom(i,tempdate))
return 0;
}
for(i=0;i<EEPROM_CAP;i+=10)
{
tempdate=i;
xxx=Read_Eeprom(i);
if(tempdate!=xxx)
return 0;
}
return 1;
}
*/
//**************************************************************************************************************************
// The End
//**************************************************************************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -