www.pudn.com > uCOSV262.rar > IIC.C


/************iic dirver for c8051f020 ************************ 
  	 
			system  clock  8M  
 
************************************************************/ 
#include "C8051F020.h" 
#include "IIC.h" 
 
static  void  GetBus ( void );  
static  void  Delay_IIC( void ); 
static void SendByte( unsigned char dataa ); 
 
static void Delay_IIC() 
{ 
   unsigned char  i=10; 
   while(i--); 
} 
static void Delay(unsigned char i) 
{ 
    unsigned char a; 
	while(i--) 
	for(a=0;a<255;a++); 
} 
static void GetBus() 
{   
   Delay(10); 
   while (BUSY==1) 
   { 
       STO = 1; 
       Delay(10); 
   } 
    
   SMB0CN=RELEASE_BUS_STA; 
   while(SI==0); 
   if (SMB0STA != 0X08) 
   { 
     STO = 1; 
	 SI = 0; 
	 return; 
   } 
   STA = 0; 
}  
 
static void SendByte( unsigned char dataa ) 
{   
   SMB0DAT=dataa; 
   SMB0CN=RELEASE_BUS_ACK; 
   while(SI==0); 
}                // waitting...... 
 
 
 
/* 
函数:SmbRec 
功能:I2C 总线综合接收函数,从从机接收多个字节的数据 
参数: 
slaveAddr:从机地址(7 位纯地址,不含读写位) 
subAddr:从机的子地址 
subMod:子地址模式,0-无子地址,1-单字节子地址, 
*recPtr:保存接收到的数据 
size:数据的字节数 
返回: 
1:接收成功 
0:在接收过程中出现异常 
*/ 
unsigned char SmbRec(unsigned char slaveAddr, 
           unsigned char *subAddress, 
           unsigned char subAddressType, 
           unsigned char size, 
           unsigned char *recPtr ) 
{	 
   unsigned char  counter; 
   Delay_IIC(); 
   GetBus(); 
   Delay_IIC(); 
   slaveAddr=slaveAddr<<1;//七位地址左移1位变为八位 
   SendByte(slaveAddr+0 ) ;  // send slaadd  + r 使地址为读地址 
   Delay_IIC(); 
   if(SMB0STA!=0X18) 
   { 
      STO = 1; 
      return(0); 
   } 
   if(subAddressType == ONE_BYTE_SUBA) 
	{ 
		SendByte(*subAddress); 
		if(SMB0STA != 0x28) 
		{ 
			STO = 1; 
			return 0; 
		} 
	} 
	if(subAddressType == TWO_BYTE_SUBA) 
	{ 
		SendByte(*subAddress); 
		if(SMB0STA != 0x28) 
		{ 
			STO = 1; 
			return 0; 
		} 
		subAddress++; 
		SendByte(*subAddress); 
		if(SMB0STA != 0x28) 
		{ 
			STO = 1; 
			return 0; 
		} 
	} 
   Delay_IIC(); 
   SMB0CN = RELEASE_BUS_STA;	   //从新发送STA  = 1 继续获得总线 
   while (SI == 0); 
   if (SMB0STA !=0X10) 
   { 
      STA = 0; 
      STO =1; 
	  return 0; 
	} 
	STA = 0; 
   Delay_IIC(); 
   SendByte(slaveAddr+1);		  //读地址 
   Delay_IIC(); 
   if (SMB0STA != 0x40) 
   { 
      SMB0CN = GENERATE_STOP; 
      return 0; 
   }    
   for(counter=0;counter<(size-1);counter++) 
   { 
      SMB0CN=RELEASE_BUS_ACK;                              //  set   ACK 
      while (SI==0); 
      if (SMB0STA!=0X50) 
      { 
         SMB0CN=GENERATE_STOP; 
         return(0); 
      } 
      *recPtr=SMB0DAT; 
      recPtr++; 
   } 
   SMB0CN=RELEASE_BUS_NACK;                                //  set     NACK 
   while(SI==0);          // receive the last byte data 
   *recPtr=SMB0DAT;	 
   SMB0CN=GENERATE_STOP; 
   Delay_IIC(); 
   return(1); 
} 
/* 
函数:SmbSend 
功能:I2C 总线综合发送函数,从主机发送多个字节的数据 
参数: 
slaveAddr:从机地址(7位纯地址,不含读写位) 
subAddr:从机的子地址 
subMod:子地址模式,0-无子地址,1-单字节子地址, 
*recPtr:发送的字节 
size:数据的字节数 
返回: 
0:接收成功 
1:在接收过程中出现异常 
*/ 
 
unsigned char SmbSend (unsigned char slaveAddr, unsigned char *subAddress, unsigned char subAddressType, unsigned char size, unsigned char *sendPtr)  
{ 
   unsigned char  counter;		 
   GetBus(); 
   Delay_IIC(); 
   slaveAddr=slaveAddr<<1; 
   SendByte(slaveAddr+0);//写信号时地址加零 
   if(SMB0STA!=0X18) 
   { 
      STO=1; 
      SI = 0; 
      return(0); 
   } 
   if(subAddressType == ONE_BYTE_SUBA) 
	{ 
		SendByte(*subAddress); 
		if(SMB0STA != 0x28) 
		{ 
			STO = 1; 
			return 0; 
		} 
	} 
	if(subAddressType == TWO_BYTE_SUBA) 
	{ 
		SendByte(*subAddress); 
		if(SMB0STA != 0x28) 
		{ 
			STO = 0; 
			return 0; 
		} 
		subAddress++; 
		SendByte(*subAddress); 
		if(SMB0STA != 0x28) 
		{ 
			STO = 1; 
			return 0; 
		} 
	} 
    for(counter=0;counter