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

📄 24cxx.c

📁 DSP部分功能测试 串口
💻 C
📖 第 1 页 / 共 2 页
字号:
	SCL_H;
	delay( T_EEDLY );
	SDADIR_I;
	delay( T_EEDLY );
	SCL_L;
	k = PB & 0x01;
	return(k);
}


//---------------------------------------//
//---read from IIC-----------------------//
//---------------------------------------//
int II2CRecByte()
{
	int k = 0;
	int flag;
	int sz;
	
	SCL_L;
	SDADIR_I;
	for(flag = 0x0080;flag != 0x00; flag = flag/2)
	{
		delay(2* T_EEDLY );
		SCL_H;
		delay( T_EEDLY );
		sz = PB & 0x0001;
		if(sz == 0x0001)
			k = k | flag;
		delay( T_EEDLY );
		SCL_L;
	}
	return(k);
}

//-----------------------------------//
//------Ack IIC----------------------//
//-----------------------------------//
void II2CAck()
{
	SCL_L;
	delay( T_EEDLY );
	SDADIR_O;
	delay(0);
	SDA_L;
	delay( T_EEDLY );
	SCL_H;
	delay(2* T_EEDLY );
	SCL_L;

}

//---------------------------------------//
//-----no ack to IIC---------------------//
//---------------------------------------//
void II2CNoAck()
{
	SCL_L;
	delay( T_EEDLY );
	SDADIR_O;
	delay(0);
	SDA_H;
	delay( T_EEDLY );
	SCL_H;
	delay(2* T_EEDLY );
	SCL_L;

}
//---------------------------------------//
//----------Stop IIC--------------------//
//--------------------------------------//
void II2CStop()
{

	SCL_L;
	SDADIR_O;
	delay( T_EEDLY );
	SDA_L;
	delay( T_EEDLY );
	SCL_H;
	delay( T_EEDLY );
	SDA_H;
	delay( T_EEDLY );
}


#ifdef C16_24
//-------------------------------------//
//-------read array from IIC----------//
//------------------------------------//

int read1(array,control,n)
int *array,control,n;
{
	int dat;
	I2CStart();
	I2CSendByte(control);
	dat = I2CRecAck();
	if(dat == 0)
	{
		for(;n != 1;n--,array++)
		{
			dat = I2CRecByte();
			I2CAck();
			*array = dat;
		}
		dat = I2CRecByte();
		I2CNoAck();
		*array = dat;
		dat = 0x00;
	}
	return(dat);
}


//------------------------------------------------//
//-----write to IIC-------------------------------//
//------------------------------------------------//

int write1(array,control,n)
int *array,control,n;
{	
	int dat;
	//WP_Enable;
	for(;n != 0;n--,array++)
	{
		I2CSendByte(*array);
		dat = I2CRecAck();
		if(dat == 1)
		{
			break;
		}
	}
	//WP_Protect;
	return(dat);
}





//-------------------------------------//
//-------read array from IIC----------//
//------------------------------------//
 
int read(array,adress,control,n)  // 读数据子程序AT24C01-16	 //
int *array,adress,control,n;
{ 
	int dat,buf;
 	I2CStop();
  	I2CStart();               // 设置I2C总线的开始状态 //
  	buf = control & 0xFFFE;
  	I2CSendByte(buf);            // 送出控制字节(R/W=0) //
  	dat=I2CRecAck();           // 检查应答位// 
  	I2CSendByte(adress);        // 送出地址字节 //
  	dat=I2CRecAck();           // 检查应答位// 
  	I2CStart();               // 再设置I2C总线的开始状态 //
  	I2CSendByte(control);        // 送出控制字节(R/W=1) //
	dat=I2CRecAck();           // 检查应答位// 
  	if (dat == 0)
  	{
    	for (;n != 1;n--) 
    	{
      		int i;
      		dat = I2CRecByte();        // 读取一个字节的数据 //
      		I2CAck();               //产生应答信号 //
      		i = (dat<<8) & 0xFF00;      // 移到高8位 //
      		dat = I2CRecByte();       // 读取一个字节的数据 //
      		I2CAck();               // 产生应答信号 //
      		dat =  dat &  0x00FF;
      		dat = dat | i;
      		*array = dat;            // 把读取的数据存入数组 //
      		array++;
    	}
    	dat=I2CRecByte();          // 接收最后一个高字节的数据 //
    	I2CAck();                 // 产生应答信号 //
    	*array  = (dat<<8) & 0xFF00;
    	dat=I2CRecByte();          // 接收最后一个字节的数据 //
    	I2CNoAck();               // 不产生应答信号 //
    	dat = dat & 0x00FF;
    	*array = *array | dat;     // 把读取的数据存入数组 //
    	dat = 0x00;              // dat值赋0,表示前面的操作成功 //
  	}
  	return(dat);
}

//------------------------------------------------//
//-----write to IIC-------------------------------//
//------------------------------------------------//
int write(array,adress,control,n)    //写数据子程序,不超过8个字//
int *array,adress,control,n;
{
	int dat,buf;
  	//WP_Enable   // 写操作开始,打开写保护 //
  	I2CStart();
  	dat = control;
  	I2CSendByte(dat);               // 送出控制字节(R/W=0) //
  	dat = I2CRecAck();
  	I2CSendByte(adress);           // 送出地址字节 //
  	dat = I2CRecAck();
  	if ( dat == 0)
  	{
   		for(;n != 0;n--,array++) 
    	{ 
     		buf = (*array);
      		buf = (buf & 0xFF00) >> 8;
      		I2CSendByte(buf);            // 写出一个字节数据 //
      		dat = I2CRecAck();
      		if(dat == 1) break;        // 如果无应答位,则写数据失败,终止写操作 //
      		buf = *array;
      		buf = buf & 0x00FF;    
      		I2CSendByte(buf);            // 写出一个字节数据//
      		dat = I2CRecAck();
      		if(dat == 1) break;        // 如果无应答位,则写数据失败,终止写操作 //
    	}
  	}
  //WP_PROTECT//     // 写操作完成,设置写保护:WP=1 //
	return(dat);
}




//------------------------------------------------------//
//---General W/R 24LC256 IIC----------------------------//
//---input:data address,24LC256ADD,control word,nbytes--//
//------------------------------------------------------//

int EEPROM1(int *array,int addr,int control,int n)
//int *array,address,control,n;
{
	int dat;
	I2CStop();
	I2CStart();
	dat = control & 0x0fffe;
	I2CSendByte(dat);
	dat = I2CRecAck();
	if(dat == 0)
	{
	//	dat = address & 0x0ff00;
	//	dat = dat>>8;
	//	I2CSendByte(dat);
	//	dat = I2CRecAck();
		if(dat == 0)
		{
			dat = addr & 0x00ff;
			I2CSendByte(dat);
			dat = I2CRecAck();
			if(dat == 0)
			{
				dat = control & 0x0001;
				switch(dat)
				{
					case 1: dat = read1(array,control,n);break;
					case 0: dat = write1(array,control,n);break;
				}
			}
		}
	}
	I2CStop();
	return(dat);
}
/**/







//--------------------------------------------------------------//
//-通用读写24C16子程序,入口参数为输入/输出缓冲区首地址array,--//
//-24C16的块内地址adress,控制字节control,需要读写的字数n ---//
int EEPROM(int *array,int adress,int control,int n)
//int    *array,adress,control,n;  
{
	int  dat;
	dat = control & 0x0001;
 	switch (dat)  
 	{
   		case 1 :
   		{
     		dat = read(array,adress,control,n);    /* 调用读数据子程序 */
     		break;  
   		} 
   		case 0 :
   		{
     		dat = write(array,adress,control,n);   /* 调用写数据子程序*/
     		break;
   		}      
  	}  
   	I2CStop();                                /* 停止IIC总线*/
   	return(dat);                        /* 返回一个状态字,为0表示操作是否成功 */
}
#endif

#ifdef LC256_24


//-------------------------------------//
//-------read array from IIC----------//
//------------------------------------//
 
int read_LC256(int *array,int adress,int control,int n)  // 读数据映绦

⌨️ 快捷键说明

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