⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iic_host_sim.c

📁 gpio 采用C51方式读写EEprom的程序,已经调试通过
💻 C
📖 第 1 页 / 共 2 页
字号:
	BWPR = 0x9B;         			 //enable the registers written 
	P3CFG = P3CFG & 0xBF;			 //P3_6 was configued as general I/O
	BWPR = 0xA8;         			 //disable the register written  
	DDRP3 = DDRP3 | 0x40;			 //P3_6 was configued output mode 
	P3 = P3 & 0xBF;					 //P3_6 = 0 : open eeprom written-protect 	
}

void  delayNOP(void)
{
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
}
/*
Discription:	Write one byte to E2PROM
Input: 			Device's address, The address to be written(16 bits), The data to be written 
*/
void	Write_Byte_To_E2PROM(unsigned char device_address, unsigned int word_address, unsigned char byte_data)
{
	unsigned char high_address, low_address;
	SCIIF = 0x00;					 //clear I2C flag register 
	high_address = (word_address & 0xFF00) >> 8;
	low_address = word_address & 0x00FF; 
	I2ADR = device_address;			 //the device's address 				  
	I2DTR = high_address;            //High address to be transfered
	I2MCR = I2MCR | 0x07; 			 //Set I2C frequency   	
									 //			I2MCR.0  I2MCR.1 I2MCR.2 	
									 //fsys/1024   1		1	    1		
	I2MCR = I2MCR & 0xF7; 			 //Master Send mode: 								
									 //			Send mode	Receive Mode 	
									 //I2MCR.3		0			 1			
	I2MCR = I2MCR | 0x10; 			 //I2C start												
									 //			Start	Stop				
								     //I2MCR.4	  1		  0 				
	while(I2SR & 0x08);				 //receive ack signal?	1: no		0: yes					
//	while(!(I2MCR & 0x20)); 		 //busy?  				1: busy		0: idle					
	while(!(I2SR & 0x02));			 //Tx buff empty?		1: empty	0: full					

	I2DTR = low_address;			 //Low address to be transfered
	while(I2SR & 0x08);				 //receive ack signal?	1: no		0: yes
	while(!(I2SR & 0x02));			 //Tx buff empty?		1: empty	0: full

	I2DTR = byte_data;				 //Data to be transfered 
	while(I2SR & 0x08);				 //receive ack signal?	1: no		0: yes 
	while(!(I2SR & 0x02));			 //Tx buff empty?		1: empty	0: full
}


/*
Discription:	Write one byte to E2PROM
Input: 			Device's address, The address to be written(16 bits), The data to be written 
*/
void	Write_Byte_To_E2PROM_s(unsigned char device_address, unsigned int word_address, unsigned char byte_data)
{
	unsigned char high_address, low_address;
	SCIIF = 0x00;					 //clear I2C flag register 
	high_address = (word_address & 0xFF00) >> 8;
	low_address = word_address & 0x00FF; 
	I2ADR = device_address;			 //the device's address 				  
	I2DTR = high_address;            //High address to be transfered
	I2MCR = I2MCR | 0x07; 			 //Set I2C frequency   	
									 //			I2MCR.0  I2MCR.1 I2MCR.2 	
									 //fsys/1024   1		1	    1		
	I2MCR = I2MCR & 0xF7; 			 //Master Send mode: 								
									 //			Send mode	Receive Mode 	
									 //I2MCR.3		0			 1			
	I2MCR = I2MCR | 0x10; 			 //I2C start												
									 //			Start	Stop				
								     //I2MCR.4	  1		  0 				
	while(I2SR & 0x08);				 //receive ack signal?	1: no		0: yes					
//	while(!(I2MCR & 0x20)); 		 //busy?  				1: busy		0: idle					
	while(!(I2SR & 0x02));			 //Tx buff empty?		1: empty	0: full					

	I2DTR = low_address;			 //Low address to be transfered
	while(I2SR & 0x08);				 //receive ack signal?	1: no		0: yes
	while(!(I2SR & 0x02));			 //Tx buff empty?		1: empty	0: full

	I2DTR = byte_data;				 //Data to be transfered 
	while(I2SR & 0x08);				 //receive ack signal?	1: no		0: yes 
	while(!(I2SR & 0x02));			 //Tx buff empty?		1: empty	0: full
}

/*
Discription:	Read one byte from E2PROM
Input:			Device's address, The address to be written(16 bits)
Output:			The data of specified address 
*/
unsigned char 	Read_Byte_From_E2PROM(unsigned char device_address, unsigned int word_address)
{
	unsigned char high_address, low_address;
	unsigned char temp_data;
	SCIIF = 0x00;					 //clear I2C flag register 
	high_address = (word_address & 0xFF00) >> 8;
	low_address = word_address & 0x00FF;
	I2ADR = device_address;			 //the device's address 
	I2DTR = high_address;			 //High address to be transfered 
	I2MCR = I2MCR | 0x07; 			 //Set I2C frequency   fsys/1024 
	I2MCR = I2MCR & 0xF7; 			 //Master mode: Send mode
	I2MCR = I2MCR | 0x10; 			 //I2C start
	while(I2SR & 0x08);				 //receive ack signal	1: no				0: yes
	while(!(I2SR & 0x02));			 //Tx buff empty?		1: empty			0: full
	
	I2DTR = low_address;			 //Low address to be transfered 
	while(I2SR & 0x08);				 //receive ack signal	1: no				0: yes
	while(!(I2SR & 0x02));			 //Tx buff empty?		1: empty			0: full
	
	I2MCR = I2MCR | 0x08; 			 //Master mode: Receive mode 	 
//	I2MCR = I2MCR & 0x7F;			 //ack after receiving 8 bits
	I2MCR = I2MCR | 0x80;			 //no ack after receiving 8 bits
	I2MCR = I2MCR | 0x50;			 //Restart 
	while(!(I2SR & 0x01));			 //receive buff full?		 
	temp_data = I2DRR;				 //the data read from E2PROM 
	_nop_();
	_nop_();
	return temp_data;
}


/*
Discription:	Read one byte from E2PROM with  function sim
Input:			Device's address, The address to be written(16 bits)
Output:			The data of specified address 
*/
unsigned char 	Read_Byte_From_E2PROM_s(unsigned char device_address, unsigned int word_address)
{
	unsigned char high_address, low_address;
	unsigned char temp_data;
	SCIIF = 0x00;					 //clear I2C flag register 
	high_address = (word_address & 0xFF00) >> 8;
	low_address = word_address & 0x00FF;
	I2ADR = device_address;			 //the device's address 
	I2DTR = high_address;			 //High address to be transfered 
	I2MCR = I2MCR | 0x07; 			 //Set I2C frequency   fsys/1024 
	I2MCR = I2MCR & 0xF7; 			 //Master mode: Send mode
	I2MCR = I2MCR | 0x10; 			 //I2C start
	while(I2SR & 0x08);				 //receive ack signal	1: no				0: yes
	while(!(I2SR & 0x02));			 //Tx buff empty?		1: empty			0: full
	
	I2DTR = low_address;			 //Low address to be transfered 
	while(I2SR & 0x08);				 //receive ack signal	1: no				0: yes
	while(!(I2SR & 0x02));			 //Tx buff empty?		1: empty			0: full
	
	I2MCR = I2MCR | 0x08; 			 //Master mode: Receive mode 	 
//	I2MCR = I2MCR & 0x7F;			 //ack after receiving 8 bits
	I2MCR = I2MCR | 0x80;			 //no ack after receiving 8 bits
	I2MCR = I2MCR | 0x50;			 //Restart 
	while(!(I2SR & 0x01));			 //receive buff full?		 
	temp_data = I2DRR;				 //the data read from E2PROM 
	_nop_();
	_nop_();
	return temp_data;
}



/*
Discription:	Read some bytes in one page from E2PROM
Input:			Device's address, The starting address to be written(16 bits)
Output:			The datas from starting address 
*/
unsigned char 	Read_Page_From_E2PROM(unsigned char device_address, unsigned int word_address)
{
	unsigned char high_address, low_address;
	unsigned char temp_data;
	SCIIF = 0x00;					 //clear I2C flag register 
	high_address = (word_address & 0xFF00) >> 8;
	low_address = word_address & 0x00FF;
	I2ADR = device_address;			 //the device's address 
	I2DTR = high_address;			 //High address to be transfered 
	I2MCR = I2MCR | 0x07; 			 //Set I2C frequency   fsys/1024 
	I2MCR = I2MCR & 0xF7; 			 //Master mode: Send mode
	I2MCR = I2MCR | 0x10; 			 //I2C start
	while(I2SR & 0x08);				 //receive ack signal	1: no				0: yes
	while(!(I2SR & 0x02));			 //Tx buff empty?		1: empty			0: full
	
	I2DTR = low_address;			 //Low address to be transfered 
	while(I2SR & 0x08);				 //receive ack signal	1: no				0: yes
	while(!(I2SR & 0x02));			 //Tx buff empty?		1: empty			0: full
	
	I2MCR = I2MCR | 0x08; 			 //Master mode: Receive mode 	 
	I2MCR = I2MCR & 0x7F;			 //ack after receiving 8 bits
//	I2MCR = I2MCR | 0x80;			 //no ack after receiving 8 bits
	I2MCR = I2MCR | 0x50;			 //Restart 
	while(!(I2SR & 0x01));			 //receive buff full?		 
	temp_data = I2DRR;				 //the data read from E2PROM 
	_nop_();
	_nop_();
	return temp_data;
}

/*
Discription:	Read some bytes in one page from E2PROM
Input:			Device's address, The starting address to be written(16 bits)
Output:			The datas from starting address 
*/
unsigned char 	Read_Page_From_E2PROM_s(unsigned char device_address, unsigned int word_address)
{
	unsigned char high_address, low_address;
	unsigned char temp_data;
	SCIIF = 0x00;					 //clear I2C flag register 
	high_address = (word_address & 0xFF00) >> 8;
	low_address = word_address & 0x00FF;
	I2ADR = device_address;			 //the device's address 
	I2DTR = high_address;			 //High address to be transfered 
	I2MCR = I2MCR | 0x07; 			 //Set I2C frequency   fsys/1024 
	I2MCR = I2MCR & 0xF7; 			 //Master mode: Send mode
	I2MCR = I2MCR | 0x10; 			 //I2C start
	while(I2SR & 0x08);				 //receive ack signal	1: no				0: yes
	while(!(I2SR & 0x02));			 //Tx buff empty?		1: empty			0: full
	
	I2DTR = low_address;			 //Low address to be transfered 
	while(I2SR & 0x08);				 //receive ack signal	1: no				0: yes
	while(!(I2SR & 0x02));			 //Tx buff empty?		1: empty			0: full
	
	I2MCR = I2MCR | 0x08; 			 //Master mode: Receive mode 	 
	I2MCR = I2MCR & 0x7F;			 //ack after receiving 8 bits
//	I2MCR = I2MCR | 0x80;			 //no ack after receiving 8 bits
	I2MCR = I2MCR | 0x50;			 //Restart 
	while(!(I2SR & 0x01));			 //receive buff full?		 
	temp_data = I2DRR;				 //the data read from E2PROM 
	_nop_();
	_nop_();
	return temp_data;
}



/*
Discription:	Stop I2C transmission 
*/
void	I2C_Stop_s(void)
{
	I2MCR = I2MCR & 0xAF; 			 //I2C Stop 
}



/*
Discription:	Page Write to E2PROM(the address's low 5 bits are internally incremented)
Input:			Device's address, The starting address to be written(16 bits), The data to be written 
*/
void	Page_Write_To_E2PROM(unsigned char device_address, unsigned int start_address, unsigned char byte_data)
{
	unsigned char i;
	Write_Byte_To_E2PROM_s(device_address, start_address, byte_data);
	for(i=0; i<5; i++)
	{
		byte_data++;
		I2DTR = byte_data;
		while(I2SR & 0x08);			 //receive ack signal?	1: no		0: yes 
		while(!(I2SR & 0x02));		 //Tx buff empty?		1: empty	0: full
	}
}


/*
Discription:	Page Write to E2PROM(the address's low 5 bits are internally incremented)
Input:			Device's address, The starting address to be written(16 bits), The data to be written 
*/
void	Page_Write_To_E2PROM_s(unsigned char device_address, unsigned int start_address, unsigned char byte_data)
{
	unsigned char i;
	Write_Byte_To_E2PROM_s(device_address, start_address, byte_data);
	for(i=0; i<5; i++)
	{
		byte_data++;
		I2DTR = byte_data;
		while(I2SR & 0x08);			 //receive ack signal?	1: no		0: yes 
		while(!(I2SR & 0x02));		 //Tx buff empty?		1: empty	0: full
	}
}

/*
Discription:	Waiting for writting E2PROM 
*/
void	Delay(void)
{
	int i;
	for(i=0; i<2000; i++);
}


//--------------------------------------------------------------------------------------------------
// 函数名称: delay
// 入口参数: N
// 函数功能:延时子程序,实现(16*N+24)us的延时 
// 系统采用11.0592MHz的时钟时,延时满足要求,其它情况需要改动
//--------------------------------------------------------------------------------------------------
  void delay(unsigned  int N)  
  {
  int i; 
  for(i=0;i<N;i++);
  }
//----








/*
Discription:	I2C interrupt routing 
*/
void I2C_ROUTING() interrupt 11	      
{
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -