I2C 总线 ——E2PROM

IIC总线工作原理

        I2C总线进行数据传送时,时钟信号为高电平期间,数据线上的数据必须保持稳定,只有在时钟线上的信号为低电平期间,数据线上的高电平或低电平状态才允许变化。

         I2C 总线 ——E2PROM_第1张图片

起始和终止信号 :SCL线为高电平期间,SDA线由高电平向低电平的变化表示起始信号;SCL线为高电平期间,SDA线由低电平向高电平的变化表示终止信号。

数据传送格式(1)字节传送与应答

每一个字节必须保证是8位长度。数据传送时,先传送最高位(MSB),每一个被传送的字节后面都必须跟随一位应答位(即一帧共有9位)。如果一段时间内没有收到从机的应答信号,则自动认为从机已正确接收到数据。

I2C 总线 ——E2PROM_第2张图片

 



I2C 总线 ——E2PROM_第3张图片

void respons()		//回应信号
{
	uchar i=0;SCL=1;delay();
        while((SDA==1)&&(i<255))i++;
	SCL=0;delay();
}
应答信号时序看了肾长时间,DATA IN 和DATA OUT 是一条总线,DATA IN 默认为高电平,while()循环不停循环等待DATA 的信号应答 ,如有应答此时的DATA 会被置为低电平


#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit sda=P2^0;
sbit scl=P2^1;	

void delay2(uchar x)
{
     unsigned char i,j;
	for(i=x;i>0;i--)
	  for(j=110;j>0;j--)
	  ;

}
void delay()
{;;} 

void start()
{
   	scl=1;
    delay();
    sda=1;
	delay(); 	
	sda=0;
	delay();

}  

void stop()
{  
	scl=1;
	delay();
    sda=0;
	delay(); 	
	sda=1;
	delay();
}	
	




void respons()
{  
	unsigned char i;
   scl=0;
	delay();
    sda=1;
	delay();
	scl=1; 
	delay();	 
	while((sda==1)&&(i<250))
	   i++;	   

	scl=0;
	delay();


	
}


void write_byte(uchar dat)
{
    unsigned char temp,i;
	temp=dat;
//	sda=1;
//	delay();
//	scl=0;
////	delay();
	for(i=0;i<8;i++)
	{
		temp=(temp<<1);
		scl=0;
		delay();


		sda=CY;
		delay();




		scl=1;
		delay();
		//scl=0;	 错误一: 写时序,先保持SCl为低电平,在写入数据保持稳定再置为高电平,
                                        //读时序时,先SCL=1再保持数据总线的数据稳定,再SCl再置为低电平
	//	delay(10);
	
	}

	scl=0;
	delay();
	sda=1;
	delay();
	

}
 void init()
{
	sda=1;
	delay();
	scl=1;
	delay();
}



uchar read_byte()
{
      unsigned char i,temp;
      sda=1;
  	  delay();
	  scl=0;
	  delay();
      for(i=0;i<8;i++)
	  {
	  	
		scl=1;
		delay();
		temp=(temp<<1)|sda;
		delay();
	///	scl=1;
	//	delay();
	//	temp=(temp<<1)|sda;
		scl=0;
		delay();
	  
	  
	  }
	  sda=1;
	  delay();
	  scl=1;
	  delay();
	  return temp;

}
 

void write_add(uchar address,uchar date)
{
	start();
	write_byte(0xa0);
	respons();
	write_byte(address);
	respons();
	write_byte(date);
	respons();
	stop();
}

uchar read_add(uchar address)
{
	uchar date;
	start();
	write_byte(0xa0);
	respons();
	write_byte(address);
	respons();
	start();
	write_byte(0xa1);
	respons();
	date=read_byte();
	stop();
	return date;
}

void main()
{
    P1=0x00;
//	while(1);
	init();
	write_add(14,0xaa);
	delay2(100);
	P1=read_add(14);
	while(1);
}


注意只能是

delay()

{;;}

延迟太长无法存储



数码管记忆显示,EEPROM

#include<reg52.h>
#define uchar unsigned char
unsigned char code table[]={ 0xc0,0xf9,0xa4,0xb0, 0x99,0x92,0x82,0xf8, 0x80,0x90,0x88,0x83, 0xc6,0xa1,0x86,0x8e,0x7f};
//unsigned char code table[]={ 0xc0,0xf9,0xa4,0xb0, 0x99,0x92,0x82,0xf8, 0x80,0x90,0x88,0x83, 0xc6,0xa1,0x86,0x8e,0x7f};
sbit sda=P2^0;
sbit scl=P2^1;
uchar a;
bit flag;
void delay()
{ ;; }
void delay2(unsigned int x)
{
    unsigned int i,j;
   for(i=x;i>0;i--)
    for(j=0;j<110;j++) 
	;

}
uchar time,count;
void start()  //开始信号
{	
	sda=1;
	delay();
	scl=1;
	delay();
	sda=0;
	delay();
}

void stop()   //停止
{
	sda=0;
	delay();
	scl=1;
	delay();
	sda=1;
	delay();
}

void respons()  //应答
{
	uchar i;
	scl=1;
	delay();
	while((sda==1)&&(i<250))i++;
	scl=0;
	delay();
}

void init()
{
	sda=1;
	delay();
	scl=1;
	delay();
    TMOD=0x01;
	EA=1;
	ET0=1;
	TR0=1;	
	TH0=(65536-50000)/256;
	TL0=(65536-50000)%256; 
}

void write_byte(uchar date)
{
	uchar i,temp;
	temp=date;
	

	for(i=0;i<8;i++)
	{
		temp=temp<<1;
		scl=0;
	    delay();
		sda=CY;
		delay();
		scl=1;
		delay();
	
	}
	scl=0;
	delay();
	sda=1;
	delay();
}

uchar read_byte()
{
	uchar i,k;
	scl=0;
	delay();
	sda=1;
	delay();
	for(i=0;i<8;i++)
	{
		scl=1;
		delay();	
		k=(k<<1)|sda;
		scl=0;
		delay();	
	}
	return k;
}

void delay1(uchar x)
{
	uchar a,b;
	for(a=x;a>0;a--)
	 for(b=100;b>0;b--)
	 ;
}

void write_add(uchar address,uchar date)
{
	start();
	write_byte(0xa0);
	respons();
	write_byte(address);
	respons();
	write_byte(date);
	respons();
	stop();
}

uchar read_add(uchar address)
{
	uchar date;
	start();
	write_byte(0xa0);
	respons();
	write_byte(address);
	respons();
	start();
	write_byte(0xa1);
	respons();
	date=read_byte();
	stop();
	return date;
}


void main()
{
	init();
	
	time=read_add(2);//记忆读取
	if(time==15)
	{
	   time=0;	
	}  	
	while(1)
	{  
	   P1=table[time];
	   if(flag==1)
	   {
	   	 flag=0;
		 write_add(2,time);	//每隔一秒写入一个数据   
	   }	  	   
	   	 	 
	}
}


  
void time0() interrupt 1
{
    
	TH0=(65536-50000)/256;
    TL0=(65536-50000)%256;
	count++; 
	if(count==20)
	{
	    count=0;		
	    time++;
		flag=1;
		if(time==15)
		{
		  time=0;
		} 
	}     
}













   









你可能感兴趣的:(I2C 总线 ——E2PROM)