×
单片机 > 单片机程序设计 > 详情

24CXX读写驱动C51程序

发布时间:2020-06-19 发布时间:
|

/**************************
 文件所用资源
1.端口:P0.2,P0.3
2.调用delay_ms函数
**************************/
/************************
 端口定义
************************/
sbit i2c_dat =P0^2;
sbit i2c_clk =P0^3;
#define IIC_TIME   10  //IIC操作延时
//========在此设定芯片地址=============
#define W_ADD_COM 0 //写字节命令及器件地址(根据地址实际情况改变), 1010 A2 A1 A0 0
#define R_ADD_COM 0xa1 //读命令字节及器件地址(根据地址实际情况改变), 1010 A2 A1 A0 1
//=======在此设定芯片型号, 1代表24C01; 16代表24C16; 512代表24C512
#define e2prom 256 // #if e2prom==1
 #define PAGE_SIZE 8
 #define SIZE 0x007f
#elif e2prom==2
 #define PAGE_SIZE 8
 #define SIZE 0x00ff
#elif e2prom==4
 #define PAGE_SIZE 16
 #define SIZE 0x01ff
#elif e2prom==8
 #define PAGE_SIZE 16
 #define SIZE 0x03ff
#elif e2prom==16
 #define PAGE_SIZE 16
 #define SIZE 0x07ff
#elif e2prom==32
 #define PAGE_SIZE 32
 #define SIZE 0x0fff
#elif e2prom==64
 #define PAGE_SIZE 32
 #define SIZE 0x1fff
#elif e2prom==128
 #define PAGE_SIZE 64
 #define SIZE 0x3fff
#elif e2prom==256
 #define PAGE_SIZE 64
 #define SIZE 0x7fff
#elif e2prom==512
 #define PAGE_SIZE 128
 #define SIZE 0xffff
#endif
/************************
 24c256 start
************************/
void start()
{
    i2c_dat=1;  //数据
 i2c_clk=1;  //时钟
 i2c_dat=0;  //数据
 i2c_clk=0;  //时钟
}
/************************
 24c256 stop
************************/
void stop()

 i2c_dat=0;  //数据
    i2c_clk=1;  //时钟
 i2c_dat=1;  //数据
}
/************************
 24c256 mack
************************/
void mack()
{
 i2c_dat=0;  //数据
 i2c_clk=1;  //时钟
 i2c_clk=0;  //时钟
}
/************************
 24c256 mnack
************************/
void mnack()
{
 i2c_dat=1;  //数据
 i2c_clk=1;  //时钟
 i2c_clk=0;  //时钟
}
/************************
 24c256 ack
************************/
void ask()
{
 uint i;
    i2c_dat=1;  //数据 
 i2c_clk=1;  //时钟
 for(i=0;i<200;i++)
 { 
  if(!i2c_dat)break;
 }
 i2c_clk=0;  //时钟
}
/************************
 24c256写一个字节
************************/
void write_i2c_byte(uchar dat)

 uchar i;
   for(i=0;i<8;i++)
   {
     if(dat & 0x80) 
     i2c_dat=1; //数据
     else i2c_dat=0; 
     i2c_clk=1; //时钟
     dat = dat << 1;  
     i2c_clk=0; //时钟
   }
}
/************************
 24c256写一个字
************************/
void write_i2c_word(uint x)
{  
 uchar i,j;
 for(j=0;j<2;j++)
   {
       for(i=0;i<8;i++)
       {
           if(x & 0x8000) 
            i2c_dat=1; //数据
           else i2c_dat=0; 
            i2c_clk=1; //时钟
         x = x << 1;  
         i2c_clk=0; //时钟
    }
     ask();      
   }  
}
/************************
 24c256读一个字节
************************/
uchar read_i2c_byte()
{  
 uchar i,dat=0;
 for(i=0;i<8;i++)
   {  
    i2c_dat=1;  //数据
  i2c_clk=1;  //时钟
  dat=dat<<1; 
  if(i2c_dat)dat=dat|0x01;
  i2c_clk=0;  //时钟
   } 
    return(dat);
}
/*****************************
 读24c256指定地址的一个字节
*****************************/
uchar read_i2c_dat(uint addrr)
{  
 uchar dat;
 start();
 write_i2c_byte(W_ADD_COM);
 ask();
 write_i2c_word(addrr); 
 start();
 write_i2c_byte(R_ADD_COM);
 ask(); 
    dat=read_i2c_byte(); 
    mnack();
 stop();
 return (dat);
}
/*****************************
 写24c256指定地址的一个字节
*****************************/
void write_i2c_dat(uint addrr,uchar dat)
{
 start();
 write_i2c_byte(W_ADD_COM);
 ask();
 write_i2c_word(addrr);
 write_i2c_byte(dat);
 ask();
 stop();
}
/***********************************
 读24c256指定地址连续读取N个字节
***********************************/
void read_i2c_n_dat(uint addrr,uint number,uchar *p)
{  
 uint i;
 start();
 write_i2c_byte(W_ADD_COM);
 ask();
 write_i2c_word(addrr); 
 start();
 write_i2c_byte(R_ADD_COM);
 ask(); 
    for(i=0;i {
  *p++=read_i2c_byte();
   mack();
    }
 *p++=read_i2c_byte();
 mnack();
 stop();
}
/*********************************
 从24c256指定地址连续写N个字节
*********************************/
void write_i2c_n_dat(uint addrr,uint number,uchar *p)
{
 uint i;
 start();
 write_i2c_byte(W_ADD_COM);
 ask();
 write_i2c_word(addrr);
 for(i=0;i {
  write_i2c_byte(*p++);
  ask();
  addrr++;
  if(addrr%PAGE_SIZE==0)
  {
   stop();             
   delay_ms(IIC_TIME);
   start();
   write_i2c_byte(W_ADD_COM);
   ask();
   write_i2c_word(addrr);
  }
    }
   stop();             
}

关键字:24CXX  读写驱动  C51程序 

『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
单片机中高阻态的实质及意义