调试通过。
#include 
#include 
#include 
#include 
#define uchar unsigned char
#define uint unsigned int
#define Fclk 11059200UL /*使用11.0592M体*/
#define BitRate 9600UL /*波特率定义为9600*/
#define DEBUG0
sbit sda= P1^2; //2ic串行数据
sbit scl= P1^5; //2ic串行时钟
sbit WP = P3^2; //2ic硬件写保
sbit a0=ACC^0;
sbit a1=ACC^1;
sbit a2=ACC^2;
sbit a3=ACC^3;
sbit a4=ACC^4;
sbit a5=ACC^5;
sbit a6=ACC^6;
sbit a7=ACC^7;
sbit led_g= P3^4; //red led
sbit led_r= P2^3; //green led
sbit kr = P2^1; //kr
void outChar(uchar c);
void outStr(uchar *buf,uchar len);
uchar xKey;
uint Life;
uint KB_Life;
uchar recvBuf[16+6];
uchar exeCmd;
uchar sendBuf[16+6];
unsigned char checksum,count3;
bit read_flag=0;
volatile uchar Sending;
volatile uchar Accepting;
volatile uchar len,temp,i;
volatile unsigned int j;
//延时10微秒(包括调用和返回的时间),base=12M
void Delay(void)
{
 P3_5 = ~P3_5;
 _nop_();
 _nop_();
 _nop_();
 _nop_();
 _nop_();
 _nop_();
}
/*********************************************************
* AT24C01读写函数 *
**********************************************************/
void Start(void) //起始条件
{
scl=0;
sda=1;
scl=1;
sda=0;
scl=0;
}
void Stop(void) //停止条件
{
scl=0;
sda=0;
scl=1;
sda=1;
scl=0;
}
void WaitAck(void)
{
 scl=0;
 sda=0;
 scl=1;
 scl=0;
 sda=1;
}
void Ack(void) //应答位
{
 scl=0;
 _nop_();
 scl=1;
 _nop_();
 scl=0;
 _nop_();
 sda=1;
 _nop_();
}
void NoAck(void) //反向应答位
{
 scl=0;
 sda=1;
 scl=1;
 scl=0;
 sda=1;
}
uchar Read(void) //读一个字节的数据,并返回该字节值
{
scl=1;_nop_();_nop_();_nop_();
a7=sda;_nop_();_nop_();scl=0;_nop_();sda=1;_nop_();scl=1;_nop_();_nop_();
a6=sda;_nop_();_nop_();scl=0;_nop_();sda=1;_nop_();scl=1;_nop_();_nop_();
a5=sda;_nop_();_nop_();scl=0;_nop_();sda=1;_nop_();scl=1;_nop_();_nop_();
a4=sda;_nop_();_nop_();scl=0;_nop_();sda=1;_nop_();scl=1;_nop_();_nop_();
a3=sda;_nop_();_nop_();scl=0;_nop_();sda=1;_nop_();scl=1;_nop_();_nop_();
a2=sda;_nop_();_nop_();scl=0;_nop_();sda=1;_nop_();scl=1;_nop_();_nop_();
a1=sda;_nop_();_nop_();scl=0;_nop_();sda=1;_nop_();scl=1;_nop_();_nop_();
 a0=sda;_nop_();_nop_();scl=0;_nop_();_nop_();sda=1;_nop_();_nop_();
return(ACC);
}
void Send(uchar Data) //发送数据子程序,Data为要求发送的数据
{
ACC=Data;
scl=0;
sda=a7;_nop_();_nop_();scl=1;_nop_();_nop_();scl=0;_nop_();_nop_();
sda=a6;_nop_();_nop_();scl=1;_nop_();_nop_();scl=0;_nop_();_nop_();
sda=a5;_nop_();_nop_();scl=1;_nop_();_nop_();scl=0;_nop_();_nop_();
sda=a4;_nop_();_nop_();scl=1;_nop_();_nop_();scl=0;_nop_();_nop_();
sda=a3;_nop_();_nop_();scl=1;_nop_();_nop_();scl=0;_nop_();_nop_();
sda=a2;_nop_();_nop_();scl=1;_nop_();_nop_();scl=0;_nop_();_nop_();
sda=a1;_nop_();_nop_();scl=1;_nop_();_nop_();scl=0;_nop_();_nop_();
sda=a0;_nop_();_nop_();scl=1;_nop_();_nop_();scl=0;_nop_();_nop_();
}
void RdFromROM(uchar *Data,uchar Address,uchar Num)
{
 uchar i;
 Start();
 Send(0xa0); //读操作
 Ack();
 Send(Address); //读的地址
 Ack();
 Start();
 Send(0xa1);
 Ack();
 for(i=0;i
 Data[i]=Read();
 WaitAck();  
 }
 NoAck();
 Stop();
}
void WrToROM(uchar *Data,uchar Address,uchar Num)
{
 uchar i;
 Start();
 Send(0xa0); //写操作
 Ack();
 Send(Address);//写的地址
 Ack();
 for(i=0;i
 Send(Data[i]);
 Ack();
 }
 Stop();
 Delay();
 Delay();
 Delay();
}
/***********************************************************************/
//初始化串口通讯参数
void InitialUart()
{
 PCON = 0x00;
 TMOD = 0x21;
 TH0 = 0x52; //0x4c;
 TL0 = 0x00; //0x00;
 TR0 = 1; //开启定时器
 ET0 = 1; //开启定时器1的中断
/*
 TH1 = 0xFD; //Baud:9600 fosc=11.0592MHz
 TL1= 0xFD;
 TR1 = 1; //启动定时器1
 SCON = 0x50; //串口工作模式为1,允许接收串行数据
 ES = 1; //开启串口中断
 EA = 1; //开启中断
*/
EA=0; //暂时关闭中断
TMOD&=0x0F; //定时器1模式控制在高4位
TMOD|=0x20; //定时器1工作在模式2,自动重装模式
SCON=0x50; //串口工作在模式1
TH1=256-Fclk/(BitRate*12*16); //计算定时器重装值
TL1=256-Fclk/(BitRate*12*16);
PCON|=0x80; //串口波特率加倍
ES=1; //串行中断允许
TR1=1; //启动定时器1
REN=1; //允许接收
EA=1; //允许中断
PS=1;
}
//向串口发送一字符
/*
void OutChar(uchar c)
{
 SBUF=c;
 while(TI==0);
 TI=0;
}
*/
void OutChar(uchar c)
{
 SBUF=c;
 Sending=1;
 while(Sending);
}
void OutStr(uchar *buf,uchar len)
{
 uchar i;
 uchar check = 0x00;
 OutChar(buf[0]);
 for(i=1;i
 OutChar(buf[i]);
 check ^= buf[i];
 }
 OutChar(check);
}
/**************************************
*功能: 串口中断函数 *
**************************************/
void UartISR(void) interrupt 4 using 3
{
// uchar len, i;
// unsigned int j=0;
 if(RI)
 {
 RI=0;
if(!read_flag)
{
len=SBUF;
recvBuf[0]=len;
read_flag=1;
}
else
{
 ++i;
 recvBuf[i]=SBUF;
if(i==len)
{
 exeCmd = 1;
 read_flag=0;
i=0;
}
 }
 /*
  SCON =0x40; //接收不允许
 tChar(0x88); //发送数据
 OutChar(len);
  OutChar(recvBuf[0]);
 OutChar(recvBuf[1]);
 OutChar(recvBuf[2]);
 OutChar(recvBuf[3]);
 OutChar(0x88);
 OutChar(0x68);
  outStr(sendBuf,len);
 SCON =0x50; //接收允许
*/
 }
 else
 {
 TI=0;
Sending=0;
 }
}
/*
void COMISP(void) interrupt 4
{
 uchar len, i;
 unsigned int j=0;
 if(RI)
 {
len=SBUF;
recvBuf[0] = len;
RI=0;
for(i=0;i
while(!RI)
{
j++;
if(j>1000) break;
}
if(j<1000)
{
recvBuf[i+1]=SBUF;
RI=0;
 j=0;
}
else
{
 break;
}
}
if(i==len)
{
 exeCmd = 1;
}
}
}
*/