📄 5045.c
字号:
//EEPROM X5045相关函数定义
#include "fDETDeclare.H"
#include "DETConst.H"
///////////////////////////////////////////////////
// put one byte to EPSI
///////////////////////////////////////////////////
void EpmPutByte(byte srcbyte)
{
byte i;
for (i=1; i<=8; i++) {
EPSCK = 0;
// for (j=0;j<EPCLKWAITING;j++);
EPSI = 0x80&srcbyte?1:0;
EPSCK = 1;
srcbyte <<= 1;
}
}
///////////////////////////////////////////////////
// get one byte from EPSO
///////////////////////////////////////////////////
byte EpmGetByte()
{
byte i,j,destbyte;
for (j=0;j<EPCLKWAITING;j++) ;
for (i=1; i<=8; i++) {
destbyte <<= 1;
EPSCK = 0;
// for (j=0;j<EPCLKWAITING;j++);
EPSCK = 1;
destbyte = EPSO?0x01|destbyte:destbyte;
}
return destbyte;
}
///////////////////////////////////////////////////
///////////////////////////////////////////////////
// write enable or disable
// enordis is 0xff,then enable,
// or else is 0x00,then disable
///////////////////////////////////////////////////
void EpmWren_di(byte enordis)
{
byte Com;
switch (enordis) {
case 0xff : // enable, entry para:0xff
Com = 0x06;
break;
case 0x00 :
Com = 0x04; // disnable, entr para:0x00
break;
}
EPCS =0;
EpmPutByte(Com);
EPCS = 1;
}
///////////////////////////////////////////////////
///////////////////////////////////////////////////
// read state register
///////////////////////////////////////////////////
byte EpmRdsr()
{
byte Com,getbyte;
EPSO = 1;
EPSCK = 0;
EPCS =0;
Com = 0x05;
EpmPutByte(Com);
getbyte = EpmGetByte();
EPCS =1;
return getbyte;
}
///////////////////////////////////////////////////
///////////////////////////////////////////////////
// write state register
///////////////////////////////////////////////////
void EpmWrsr(byte srcbyte)
{
byte Com;
EpmWren_di(0xff);
EPCS =0;
Com =0x01;
EpmPutByte(Com);
EpmPutByte(srcbyte);
EPCS =1;
EpmWren_di(0x00);
}
///////////////////////////////////////////////////
///////////////////////////////////////////////////
// Write alarm data
// Be careful when using this function
// epaddr is between 0 and 511
///////////////////////////////////////////////////
void fEepromWrite(byte *srcdata,word epaddr,byte sum)
{
byte i,j,WriteCom;
byte epmstate,addr;
sum = epaddr+sum>512?512-epaddr:sum; // avoid wirte beyond the boundary
for (j=1; j<=sum; j++) {
epmstate = 0x01;
for (i=1;i<=10;i++);
#ifndef DBG_EPM
while (epmstate == 1) {
epmstate = EpmRdsr();
epmstate &= 0x01;
}
#endif
EpmWren_di(0xff); // write enable
EPSCK = 0;
EPCS =0;
WriteCom = epaddr>0x0ff?0x0a:0x02; //决定写5045上半部分还是下半部分
addr = (unsigned char)(epaddr&0xff);
for (i=1;i<= 2;i++);
EpmPutByte(WriteCom);
for (i=1;i<=2;i++);
EpmPutByte(addr);
EpmPutByte(*srcdata++);
epaddr++;
EPCS =1;
EpmWren_di(0x00); // write disable
epmstate = 0x01;
#ifndef DBG_EPM
while (epmstate == 1) {
epmstate = EpmRdsr();
epmstate &= 0x01;
}
#endif
}
}
///////////////////////////////////////////////
///////////////////////////////////////////////
// Read alarm data
// The operation rule is the same as EpmWrite
// epaddr is between 0 and 511
///////////////////////////////////////////////
void fEepromRead(byte *destbuf, word epaddr, byte sum)
{
byte epmstate,addr;
byte ReadCom;
byte i,j;
sum = epaddr+sum>512?512-epaddr:sum; // avoid wirte beyond the boundary
for (j=1; j<=sum; j++) {
epmstate = 0x01;
#ifndef DBG_EPM
while (epmstate == 1) {
epmstate = EpmRdsr();
epmstate &= 0x01;
}
#endif
EPSCK = 0;
EPCS =0;
ReadCom = epaddr>0x0ff?0x0b:0x03;
addr = (unsigned char)(epaddr&0xff);
for (i=1;i<=2;i++);
EpmPutByte(ReadCom);
for (i=1;i<=2;i++);
EpmPutByte(addr);
EPSO = 1;
*destbuf++ = EpmGetByte();
epaddr++;
EPCS = 1;
epmstate = 0x01;
#ifndef DBG_EPM
while (epmstate == 1) {
epmstate = EpmRdsr();
epmstate &= 0x01;
}
#endif
}
}
//0x00使能看门狗
//0x60关闭看门狗
void fEepromInit(byte InitCommand)
{
EPCS = 1;
EPSI = 1;
EpmWrsr(InitCommand); // initiate x5045 state register, enable watch dog ,1.4s
// initiate x5045 state register, disable watch dog
}
// 往5045写一个整型数
void fEepromWriteW(word WriteData,word epaddr)
{
unsigned char buffer[3],temp;
temp=WriteData;
buffer[0]=(char)temp;
buffer[1]=(char)(WriteData>>8);
fEepromWrite(buffer,epaddr,2);
}
//从5045中读一个字
void fEepromReadW(word *ReadData,word epaddr)
{
unsigned char buffer[3];
fEepromRead(buffer,epaddr,2);
(*ReadData)=(buffer[1]<<8)+buffer[0];
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -