www.pudn.com > EEPROM.zip > I2C总线通讯.c, change:2003-01-01,size:4670b


#include<reg52.h> 
#define uint unsigned int 
#define uchar unsigned char 
 
 
//硬件准备 接口 
sbit SCL = P2^1; 
sbit SDA  = P2^0;  
 
 
 
 
sbit Buzzer =P2^3; 
 
 
// 
#define Buzzer_ON   Buzzer=0 
#define Buzzer_OFF  Buzzer=1 
 
 
//准备工作 
 
 
//7us延时 
void delays(uchar i) 
{ 
	while(i--);	 
} 
//1ms延时 
void delayl(uint n) 
{ 
	uchar j; 
	uint i; 
	for(i=n;i>0;i--) 
		for(j=110;j>0;j--); 
 
} 
 
void Buzzerticks(uchar num) 
{ 
	 for(num;num>0;num--) 
	 {	  
        Buzzer_ON; 
        delayl(500); 
        Buzzer_OFF; 
        delayl(500);	  
	 } 
} 
 
 
 
//I2C接口时序模拟 
 
 
void I2C_init() 
{ 
	SCL=1; 
	delays(2); 
	SDA=1; 
	delays(2); 
} 
 
 
 
void I2C_start() 
{ 
   	SDA=1; 
	delays(3);// 
	SCL=1; 
	delays(3); 
	SDA=0; 
	delays(3); 
} 
 
 
void I2C_stop() 
{ 
	SCL=0; 
	delays(1);		 
	SDA=0; 
	delays(1); 
	SCL=1; 
	delays(2);	 
	SDA=1; 
	delays(2); 
} 
 
void I2C_response() 
{ 
	uchar i; 
	SCL=1; 
	while((i<200)&&(SDA==1))// 
		i++;  
// 
 
	SCL=0;		    
	delays(2); 
} 
 
 
void I2C_sendbyte(uchar aa) 
{ 
	uchar i,temp; 
	temp=aa; 
	for(i=0;i<8;i++) 
	{ 
 
 
 
		temp<<=1; 
		SCL=0; 
		delays(2);			 
		SDA=CY; 
		delays(2); 
		SCL=1; 
		delays(2); 			 
	} 
	SCL=0; 
	delays(2); 
	SDA=1; 
	delays(2); 
} 
 
uchar I2C_sendbyte_y(uchar aa) 
{ 
	uchar i; 
//	uchar tmp; 
	//数据从最高位发送 
	for(i=0;i<8;i++) 
	{ 
	//产生时钟周期 
		SCL=0; 
		delays(2);			 
		SDA=(bit)((aa&0x80)>>7); 
		delays(2); 
		SCL=1; 
    //准备下一个数据 
	    aa=(aa<<1); 
		delays(2); 			 
	} 
 
 
 
//判断从机是否应答; 
	SCL=0; 
	delays(2); 
	SDA=1; 
	delays(2); 
	SCL=1; 
 
 
     i=0; 
	while((i<200)&&(SDA==1))// 
		i++;  
	SCL=0; 
	delays(2);     
	//判断应答否 
	if(SDA==1) 
	 return 1;//发送失败; 
	else 
	 return 0;//发送成功 
} 
 
 
 
 
uchar I2C_readbyte(void) 
{ 
	uchar i,temp; 
 
	SCL=0; 
	delays(2); 
	SDA=1; 
	delays(2); 
	for(i=0;i<8;i++) 
	{ 
		SCL=1; 
		delays(2); 
		temp=(temp<<1)|SDA; 
		SCL=0; 
		delays(2);		 
	} 
	return temp; 
} 
 
uchar I2C_readbyte_y(void) 
{ 
	uchar i,temp=0; 
 
 
	SCL=0; 
 
	SDA=1;//释放SDA 
 
	delays(2); 
 
 
 
	for(i=0;i<8;i++) 
	{ 
		SCL=1; 
		delays(2); 
 
//       	SDA=1;//yy加		 
		if(SDA==1) 
		{ 
		temp=((temp<<1)|0x01); 
		} 
		else 
		{ 
		temp=((temp<<1)&0xfe); 
		} 
		//		temp=(temp<<1)|SDA; 
		/*  temp=temp<<1; 
		  if(SDA==1) 
		    temp=temp|0x01; 
		  else 
		    temp=temp&0xfe;*/ 
 
 
		SCL=0; 
		delays(2); 
//		SDA=1;//释放SDA 
	} 
	//不应达 
 
//产生应答信号 
/*	SCL=0; 
 
	SDA=1; 
	delays(2); 
	SCL=1; 
	delays(2);  */ 
 
	return temp; 
} 
 
//主机应答 
void I2C_Send_Master_ACK_Y(void) 
{ 
 
	SCL=0; 
	delays(2); 
	SDA=0; 
	delays(2); 
	SCL=1; 
	delays(2);   
} 
 
//主机不应答 
void I2C_Send_Master_NOACK_Y(void) 
{ 
 
	SCL=0; 
	delays(2); 
	SDA=1; 
	delays(2); 
	SCL=1; 
	delays(2);   
} 
 
//操作芯片; 
 
 
//写数据	   
void write_add_char(uchar add,uchar date) 
{ 
//产生开始条件 
	I2C_start(); 
 
//发送设备地址 0:write ;1--read 
	I2C_sendbyte(0xa0); 
 
 
	I2C_response();	 
 
	I2C_sendbyte(add); 
 
	I2C_response(); 
	I2C_sendbyte(date); 
 
	I2C_response();	 
	I2C_stop(); 
 
}  
//写数据	   
void write_add_char_y(uchar add,uchar date) 
{ 
//产生开始条件 
	I2C_start(); 
 
//发送设备地址 0:write ;1--read 
	I2C_sendbyte_y(0xa0); 
//	I2C_response();	 
	delays(2); 
	I2C_sendbyte_y(add); 
		delays(2); 
//	I2C_response();	 
	I2C_sendbyte_y(date); 
		delays(2); 
//	I2C_response();	 
	I2C_stop(); 
 
}  
 
 
//读数据 
 
uchar read_add_char(uchar add) 
{ 
	uchar temp; 
	//产生 
	I2C_start(); 
 
//发送设备地址 0:write ;1--read 
	I2C_sendbyte(0xa0); 
	I2C_response(); 
//发送读的地址 
	I2C_sendbyte(add); 
	I2C_response(); 
 
	I2C_start();	  // 
	//读设备 
	I2C_sendbyte(0xa1); 
    I2C_response(); 
	 
	temp=I2C_readbyte_y();//I2C_readbyte 
 
	//I2C_readbyte_yI2C_readbyte_y() 
 
    I2C_Send_Master_NOACK_Y(); 
	I2C_stop(); 
	return temp; 
}  
 
 
// 
 
uchar tmp1,tmp2; 
 
 
/*void main() 
{ 
 
	I2C_init(); 
	write_add_char(0x03,0x1a); 
 
	delayl(100); 
	write_add_char(0x01,0x55); 
	delayl(100); 
	tmp1=read_add_char(0x03); 
	delayl(500); 
	tmp2=read_add_char(0x01); 
	delayl(500); 
	if(tmp1==0x1a) 
	Buzzerticks(3); 
	if(tmp2==0x55) 
     Buzzerticks(5); 
	while(1); 
 
}*/ 
 
 
void main() 
{ 
//初始化 
 
//start 
//eeprom NIIT 
	I2C_init(); 
 
//BUZZER init 
 
   Buzzer_OFF; 
 
//end init 
 
 
 
 
 
  
 
 //写 
	write_add_char_y(0x13,0x4a); //write_add_char_y 
// write_add_char 
	delayl(100); 
 
 
 
	write_add_char_y(0x11,0x45); //write_add_char_y 
 
	delayl(100); 
 
 
//读 
	tmp1=read_add_char(0x13); 
	delayl(500); 
	tmp2=read_add_char(0x11); 
	delayl(500); 
 
  
 
//判断 
	if(tmp1==0x4a) 
	Buzzerticks(3); 
	if(tmp2==0x45) 
     Buzzerticks(5); 
	while(1); 
 
}