📄 x5083.c
字号:
#include "port.h"
#include "function.h"
//0x00使能看门狗
//0x30关闭看门狗
void fEepromInit(byte InitCommandnnn)
{
EPCS = 1;
EPSI = 1;
EpmWrsr(InitCommandnnn); // initiate x5045 state register, enable watch dog ,1.4s
// initiate x5045 state register, disable watch dog
}
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;
}
}
void EpmPutAddr(word srcword)
{
byte i;
byte srcbyteL,srcbyteH;
srcbyteL=srcword;
srcword>>=8;
srcbyteH =srcword;
for (i = 1; i <= 8; i++)
{
EPSCK = 0;
// for (j=0;j<EPCLKWAITING;j++);
EPSI = 0x80 & srcbyteH ? 1 : 0;
EPSCK = 1;
srcbyteH <<= 1;
}
for (i = 1; i <= 8; i++)
{
EPSCK = 0;
// for (j=0;j<EPCLKWAITING;j++);
EPSI = 0x80 & srcbyteL ? 1 : 0;
EPSCK = 1;
srcbyteL <<= 1;
}
}
///////////////////////////////////////////////////
// get one byte from EPSO
///////////////////////////////////////////////////
byte EpmGetByte()
{
byte i, 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&0xfe;
}
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 WriteCom;
EpmWren_di(0xff); // write enable
EPSCK = 0;
EPCS = 0;
WriteCom =0x02 ;
EpmPutByte(WriteCom);
epaddr &= 0x03ff;
EpmPutAddr(epaddr);
EpmPutByte(*srcdata);
}
///////////////////////////////////////////////
///////////////////////////////////////////////
// Read alarm data
// The operation rule is the same as EpmWrite
// epaddr is between 0 and 511
///////////////////////////////////////////////
void fEepromRead(byte * destbuf, word epaddr)
{
byte ReadCom;
EPSCK = 0;
EPCS = 0;
ReadCom =0x03 ;
epaddr &= 0x03ff;
EpmPutByte(ReadCom);
EpmPutAddr(epaddr);
EPSO = 1;
*destbuf = EpmGetByte();
EPCS = 1;
}
void WriteDisable(void)
{
EpmWren_di(0x00); // write disable
}
///////////////////////////////////////////////////
///////////////////////////////////////////////////
// Write alarm data
// Be careful when using this function
// epaddr is between 0 and 511
///////////////////////////////////////////////////
void fEepromWriteS(byte * srcdata, word epaddr, byte sum)
{
byte j, WriteCom;
sum = epaddr + sum > 1024 ? 1024 - epaddr : sum; // avoid wirte beyond the boundary
EpmWren_di(0xff); // write enable
EPSCK = 0;
EPCS = 0;
WriteCom =0x02 ;
EpmPutByte(WriteCom);
epaddr &= 0x03ff;
EpmPutAddr(epaddr);
for (j = 0; j <= sum; j++)
{
EpmPutByte(*srcdata++);
}
}
///////////////////////////////////////////////////
///////////////////////////////////////////////////
// Write alarm data
// Be careful when using this function
// epaddr is between 0 and 511
///////////////////////////////////////////////////
void fEepromWriteSA(byte * srcdata, word epaddr, byte sum)
{
byte j, WriteCom;
sum = epaddr + sum > 1024 ? 1024 - epaddr : sum; // avoid wirte beyond the boundary
EpmWren_di(0xff); // write enable
EPSCK = 0;
EPCS = 0;
WriteCom =0x02 ;
EpmPutByte(WriteCom);
epaddr &= 0x03ff;
EpmPutAddr(epaddr);
for (j = 0; j <= sum; j++)
{
EpmPutByte(*srcdata);
}
}
///////////////////////////////////////////////
///////////////////////////////////////////////
// Read alarm data
// The operation rule is the same as EpmWrite
// epaddr is between 0 and 511
///////////////////////////////////////////////
void fEepromReadS(byte * destbuf, word epaddr, byte sum)
{
byte addr;
byte ReadCom;
byte j;
sum = epaddr + sum > 1024 ? 1024 - epaddr : sum; // avoid wirte beyond the boundary
EPSCK = 0;
EPCS = 0;
ReadCom =0x03 ;
addr = (unsigned char) (epaddr & 0x03ff);
EpmPutByte(ReadCom);
EpmPutAddr(addr);
EPSO = 1;
for (j = 0; j <= sum; j++)
{
*destbuf++ = EpmGetByte();
}
EPCS = 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -