/******************************************************************** 
                            汇诚科技 
网址:http://www.ourhc.cn 
产品有售淘宝店:http://shop36330473.taobao.com    
*********************************************************************/ 
#include <reg52.H> 
 
/**************************************************************************** 
                                        24C08 读写驱动程序        开始 
*****************************************************************************/ 
#include <reg52.H> 
#include <stdio.h> 
#include <absacc.h> 
#define uchar unsigned char  
#define uint unsigned int 
 
sbit scl=P3^3;  //24c02 SCL 
sbit sda=P3^4;  //24c02 SDA 
 
uchar x24c02_read(uchar address); //从24c02的地址address中读取一个字节数据 
void x24c02_write(uchar address,uchar info); 
  //向24c02的address地址中写入一字节数据info 
void x24c02_init();   //24c02初始化子程序 
void delay1(uchar x); 
void flash(); 
void x24c01_init(); 
void start(); 
void stop(); 
void writex(uchar j); 
uchar readx(); 
void clock(); 
void delay1(uchar x) 
{ 
   uint i; 
   for(i=0;i<x;i++); 
} 
void flash() 
{} 
void x24c02_init() 
{ 
   scl=1; flash(); sda=1; flash(); 
} 
void start() 
{ 
   sda=1; flash(); scl=1; flash(); sda=0; flash(); scl=0; flash(); 
} 
void stop() 
{ 
   sda=0; flash(); scl=1; flash(); sda=1; flash(); 
} 
void writex(uchar j) 
{ 
   uchar i,temp; 
   temp=j; 
   for (i=0;i<8;i++){ 
      temp=temp<<1; scl=0; flash(); sda=CY; flash(); scl=1; flash(); 
   } 
   scl=0; flash(); sda=1; flash(); 
} 
uchar readx() 
{ 
   uchar i,j,k=0; 
   scl=0; flash(); sda=1; 
   for (i=0;i<8;i++){ 
      flash(); scl=1; flash(); 
      if (sda==1) j=1; 
      else j=0; 
      k=(k<<1)|j; scl=0; 
   } 
   flash(); return(k); 
} 
void clock() 
{ 
   uchar i=0; 
   scl=1; flash(); 
   while ((sda==1)&&(i<255))i++; 
   scl=0; flash(); 
} 
uchar x24c02_read(uchar address) 
{ 
   uchar i; 
   start(); writex(0xa0); 
   clock(); writex(address); 
   clock(); start(); 
   writex(0xa1); clock(); 
   i=readx(); stop(); 
   delay1(10); 
   return(i); 
} 
void x24c02_write(uchar address,uchar info) 
{ 
   EA=0; 
   start(); writex(0xa0); 
   clock(); writex(address); 
   clock(); writex(info); 
   clock(); stop(); 
   EA=1; 
   delay1(50); 
} 
 
 
//====================================================================// 
 main() 
 { 
signed char sec; //定义数值 
x24c02_init(); //初始化24C08 
sec=x24c02_read(2);//读出保存的数据赋于sec 
 
//sec=10;        //设置使用次数 
 
sec--; 
x24c02_write(2,sec); //在24c08 的地址2 中写入数据sec 
 
while(1) 
{ 
if(sec>=1)P2=0XAA;         //显示结果 
 
} 
}  
 
 
 
 |