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

📄 iic.c

📁 I2C协议的小程序
💻 C
字号:
#include "includes.h"
#include "iic.h"
#include "uart.h"

u8 hi2c2_11WriteByPolling(u8 vp_SlaveAddress, u16 vp_Length, u8* pp_Data)
{
  /***************************************/
  /* This function allows to write bytes */
  /* under polling method                */
  /***************************************/
  u8 vl_ReturnValue = HI2C_TRANSFER_OK;
  u8 vl_NbTry = 255;
  u16 i = vp_Length;

  /* Init I2C for Polling */
  /**********************************************************************/
  /* I2C configuration 							*/
  /*--------------------------------------------------------------------*/
  /* OPCLK =26 MHz and D0 = 2 (SICLK0 = OPCLCK/2) see CGUC in hm0init.c */
  /* Mode Standard  CR2--CR0 = 011 => 382 Kbit/s 			*/
  /* ENS1 = 1 I2C validation  						*/
  /* STA = 0 no generation of start condition to initialize I2c bus 	*/
  /* STO = 1 a stop condition to initialize I2c bus 			*/
  /* SI = 0 								*/
  /* AA = 0 no acknowledge generation to initialize I2c bus 		*/
  /**********************************************************************/
#if defined(I2C_LOW_SPEED)
  PCF508x_IICCON = ENS1 & ~STA | STO & ~SI & ~AA | CR20_7;
#else
  PCF508x_IICCON = ENS1 & ~STA | STO & ~SI & ~AA | CR20_3;
#endif

  /* Generation Start bit */
  /* Set bit STA and Initiate I2C with reset bit SI in SCON register */
  PCF508x_IICCON = (PCF508x_IICCON | STA) & ~SI ; 	       

  /**************************************/
  /* Wait for the IIC Acknowledge for   */
  /* Start condition                    */
  /**************************************/
  while(((PCF508x_IICCON & SI)!= SI)&&(vl_NbTry--));


  if (PCF508x_IICSTAT != 0x08)
    vl_ReturnValue = HI2C_TRANSFER_KO;

  /* Reset of bit STA */
  PCF508x_IICCON &= ~STA;

  if (vl_ReturnValue== HI2C_TRANSFER_OK)
  {

      /* Select Device */
      /* Transmit the address and the direction : write */
      PCF508x_IICDAT = vp_SlaveAddress | W;
  
      /* Initiate I2C with reset bit SI in SCON register*/
      PCF508x_IICCON &= ~SI;

      vl_NbTry = 255;
      /* Wait for state transition */
      while (((PCF508x_IICCON & SI)!= SI)&&(vl_NbTry--));
      
      if (PCF508x_IICSTAT != 0x18)
	vl_ReturnValue = HI2C_TRANSFER_KO;

    for(i=vp_Length;((i>0)&&(vl_ReturnValue == HI2C_TRANSFER_OK));i--)
	{
	  if (vl_ReturnValue == HI2C_TRANSFER_OK)
	    {      
	      /* Send Data value and the direction : write */
	      PCF508x_IICDAT = pp_Data[vp_Length-i];  

	      /* Initiate I2C with reset bit SI in SCON register*/
	      PCF508x_IICCON &= ~SI; 

	      vl_NbTry = 255;
	      /* Wait for state transition */
	      while (((PCF508x_IICCON & SI)!= SI)&&(vl_NbTry--));
              
	      if (PCF508x_IICSTAT != 0x28)
		vl_ReturnValue = HI2C_TRANSFER_KO;
	    }
	}

    if (vl_ReturnValue== HI2C_TRANSFER_OK) 
	{
	  /* Generation Stop bit */
	  /* Set bit STO and Initiate I2C with reset bit SI in SCON register*/
	  PCF508x_IICCON = (PCF508x_IICCON | STO) & ~SI; 
	}
  }
  /* Hold Stop Condition before placing the bus in high impeadence state */
  vl_NbTry = 255;

  while (vl_NbTry--) {
      /* PCF508x_IICCON; Prevent optimisation */ 
  }

  PCF508x_IICCON &= ~ENS1;
  PCF508x_IICCON = 00;

  return(vl_ReturnValue);
}

u8 hi2c2_12ReadByPolling(u8 vp_SlaveAddress, u8 vp_Address, u8* pp_Data)
{
  /***************************************/
  /* This function allows to read a byte */
  /* under polling method                */
  /***************************************/
  u8 vl_ReturnValue = HI2C_TRANSFER_OK;
  u16 vl_NbTry = 512;

  /* Init I2C for Polling */
  PCF508x_IICCON = ENS1 & ~STA | STO & ~SI & ~AA | CR20_3; 

  /* Generation Start bit */
  /* Set bit STA and Initiate I2C with reset bit SI in SCON register */
  PCF508x_IICCON = (PCF508x_IICCON | STA) & ~SI ;

  /* Wait for I2C end of START */
  while(((PCF508x_IICCON & SI)!= SI) && (vl_NbTry--));


  if (PCF508x_IICSTAT != HI2C_START_TRANSMIT)
  {  
     //MC_DEBUG_IIC("PCF508x_IICSTAT != HI2C_START_TRANSMIT PCF508x_IICSTAT = %#x\r\n",PCF508x_IICSTAT);
     vl_ReturnValue = HI2C_TRANSFER_KO;
  }
  /* Clear START condition */
  PCF508x_IICCON &= ~STA;

  if (vl_ReturnValue== HI2C_TRANSFER_OK)
  {
      /* Select Device */
      /* Transmit the address and the direction : write */
      PCF508x_IICDAT = vp_SlaveAddress | W;  
  
      /* reset of bit SI and STA */
      PCF508x_IICCON &= ~SI;
  
      vl_NbTry = 512;
      /* Wait for state transition */
      while (((PCF508x_IICCON & SI)!= SI) && (vl_NbTry--));
      
      if (PCF508x_IICSTAT != HI2C_SLA_T_ACK)
      {
          //MC_DEBUG_IIC("1->PCF508x_IICSTAT != HI2C_SLA_T_ACK PCF508x_IICSTAT = %#x\r\n",PCF508x_IICSTAT);
          vl_ReturnValue = HI2C_TRANSFER_KO;
      }
      if (vl_ReturnValue == HI2C_TRANSFER_OK)
      {      
	  /* Send register address to be read */
	  PCF508x_IICDAT = vp_Address;  

	  /* Initiate I2C with reset bit SI in SCON register*/
	  PCF508x_IICCON &= ~SI; 

	  vl_NbTry = 512;
	  /* Wait for state transition */
	  while (((PCF508x_IICCON & SI)!= SI) && (vl_NbTry--));
              
	       if (PCF508x_IICSTAT != HI2C_DATA_T_ACK)
	       {
	          //MC_DEBUG_IIC("2->PCF508x_IICSTAT != HI2C_SLA_T_ACK PCF508x_IICSTAT = %#x\r\n",PCF508x_IICSTAT);
	          vl_ReturnValue = HI2C_TRANSFER_KO;
	       }    
      }
  }

  /* Device address and register address have been send */
  /* Generate a start (with a stop) */
  
  if (vl_ReturnValue == HI2C_TRANSFER_OK)
  {
      PCF508x_IICCON = (PCF508x_IICCON | STO) & ~SI; 

      vl_NbTry = 512;
      /* Wait Stop Acknowledgement */
      while ((PCF508x_IICSTAT != HI2C_END_TRANSMIT) && (vl_NbTry--));
     
      /* if STOP did not worked, try anyway the following of transmission */
     
      vl_NbTry = 512;
      /* Generation Start bit (START, no STOP, reset AA and SI bits) */
      PCF508x_IICCON = (PCF508x_IICCON | STA) & ~SI & ~STO ;
     
      /**************************************/
      /* Wait for the IIC Acknowledge for   */
      /* Start condition                    */
     /**************************************/
      while(((PCF508x_IICCON & SI)!= SI) && (vl_NbTry--));

      if (PCF508x_IICSTAT != HI2C_START_TRANSMIT)
      {
         //MC_DEBUG_IIC("PCF508x_IICSTAT != HI2C_START_TRANSMIT PCF508x_IICSTAT = %#x\r\n",PCF508x_IICSTAT);
	     vl_ReturnValue = HI2C_TRANSFER_KO; 
      }
      /* Reset of bit STA */
      PCF508x_IICCON &= ~STA;
  
      if (vl_ReturnValue== HI2C_TRANSFER_OK)
      {
	  /* Select Device */
	  /* Transmit the address and the direction : read */
	  PCF508x_IICDAT = vp_SlaveAddress | R;
  
	  /* Initiate I2C with reset bit SI in SCON register*/
	  PCF508x_IICCON &= ~SI;
	  
	  vl_NbTry = 512;
	  /* Wait for state transition */
	  while (((PCF508x_IICCON & SI)!= SI) && (vl_NbTry--));
      
	  if (PCF508x_IICSTAT != HI2C_SLA_R_ACK)
	  {
	      //MC_DEBUG_IIC("PCF508x_IICSTAT != HI2C_SLA_R_ACK PCF508x_IICSTAT = %#x\r\n",PCF508x_IICSTAT);
	      vl_ReturnValue = HI2C_TRANSFER_KO;
      }
	  if (vl_ReturnValue == HI2C_TRANSFER_OK)
	  {      
	      /* At this point, slave has been addressed, and slave will send data */
	      /* As only one byte is to be send, master did not ACK it (AA bit) */
	      vl_NbTry = 512;
	      
	      PCF508x_IICCON &= ~SI;
	      PCF508x_IICCON &= ~AA;

	      /* Wait for state transition */
	      while (((PCF508x_IICCON & SI)!= SI) && (vl_NbTry--));

	      /* Read data in register */
	      pp_Data[0] = PCF508x_IICDAT;
          //MC_DEBUG_IIC("PCF508x_IICDAT = %#x\r\n",pp_Data[0]);
	      /* Initiate I2C with reset bit SI in SCON register*/
	      //MC_DEBUG_IIC("PCF508x_IICCON = %#x\r\n",PCF508x_IICCON);
	      
	      PCF508x_IICCON &= ~SI;
	      /* 由于只读一个字节所以直接设置停止标志 */
	      //vl_NbTry = 512;
	      /* Wait for state transition */
	      //while (((PCF508x_IICCON & SI)!= SI) && (vl_NbTry--));
	      
	      //MC_DEBUG_IIC("PCF508x_IICCON = %#x\r\n",PCF508x_IICCON);
	      
	      //if (PCF508x_IICSTAT != HI2C_SLA_R_NACK)
	      //{  
	       //  MC_DEBUG_IIC("PCF508x_IICSTAT != HI2C_SLA_R_NACK PCF508x_IICSTAT = %#x\r\n",PCF508x_IICSTAT);
		  //  vl_ReturnValue = HI2C_TRANSFER_KO;
		  //}   
	  }
	  if (vl_ReturnValue== HI2C_TRANSFER_OK) 
	  {
	      /* Generation Stop bit */
	      /* Set bit STO and Initiate I2C with reset bit SI in SCON register*/
	      PCF508x_IICCON = (PCF508x_IICCON | STO) & ~SI; 
	  }
      }
      vl_NbTry = 512;

      /* Wait Stop Acknowledgement */
      while ((PCF508x_IICSTAT != HI2C_END_TRANSMIT) && (vl_NbTry--));
      //MC_DEBUG_IIC("END PCF508x_IICSTAT = %#x\r\n",PCF508x_IICSTAT);
      PCF508x_IICCON &= ~ENS1;
      PCF508x_IICCON = 00;
  }  
  return(vl_ReturnValue);
}

u8 din2_31SetPMUWithPollingI2C(u8 vp_Address, u8 vp_Value)
{
 u8 vl_PMUState;
 u16 k;
 u8 a_data[2];

 a_data[0] = vp_Address;
 a_data[1] = vp_Value;//PCF50601_IIC_AD
 
 for (k= 0; ((vl_PMUState = hi2c2_11WriteByPolling(0x12,2,a_data))!= HI2C_TRANSFER_OK) && (k<=3); k++)
 {
     if ((vl_PMUState!=HI2C_TRANSFER_OK) || (k>2)) 
     {
        /* if PMU not responding, blocking defense */
        //MC_DIN_ERROR (BLOCKING, vl_PMUState);
        //MC_DEBUG_IIC("din2_31SetPMUWithPollingI2C ERROR\r\n");
        return FALSE;
     }
  }
  //MC_DEBUG_IIC("din2_31SetPMUWithPollingI2C vl_PMUState = %d\r\n",vl_PMUState);
  return TRUE;
}

u8 ReadPMUWithPollingI2C(u8 v_Register,u8 *vp_Value)
{
    if(hi2c2_12ReadByPolling(PCF50601_IIC_AD,v_Register,vp_Value) != HI2C_TRANSFER_OK)
       return FALSE;
    else 
       return TRUE;    
}


u8 PMU_ModifyBit(u8 v_Register,u8 v_num_bit,u8 v_SetBit)
{
    u8 ValRegister;
    
    if(hi2c2_12ReadByPolling(PCF50601_IIC_AD,v_Register,&ValRegister) != HI2C_TRANSFER_OK) 
    {
        //MC_DEBUG_IIC("PMU_ModifyBit ERROR\r\n");
        return FALSE;
    }
    //MC_DEBUG_IIC("PMU_ModifyBit ValRegister = %#x\r\n",ValRegister);
    if(v_SetBit == TRUE)
    {
        ValRegister |= v_num_bit;
    }
    else
    {
        ValRegister &= ~v_num_bit;
    }            
    if(din2_31SetPMUWithPollingI2C(v_Register,ValRegister) == FALSE)
    {
       return FALSE;
    }
    return TRUE;
}

u8 BackLight_ON(void)
{
   u8 a_BacklightReg[4];
   u16 k;
   u8 vl_PMUState;
   
   a_BacklightReg[0] = PCF50601_BBMLOW; 				
   /* BBMLOW Initialization */			
   a_BacklightReg[1] = 0x00;			
   /* BBMHIGH Initialization */			
   a_BacklightReg[2] = 0xFF;			
   /* BBMC Initialization */			
   a_BacklightReg[3] = (BBM_ACT_SET | BBM_SLP_SET);	
   for(k=0;((vl_PMUState = hi2c2_11WriteByPolling(PCF50601_IIC_AD,4,a_BacklightReg))!= HI2C_TRANSFER_OK)&&(k<3);k++)
   {
       if((vl_PMUState != HI2C_TRANSFER_OK) ||(k>2))
       {
           //MC_DEBUG_IIC("BackLight_ON ERROR\r\n");
           return FALSE;
       }
   }
   //MC_DEBUG_IIC("BackLight_ON vl_PMUState = %d\r\n",vl_PMUState);
   //PMU_ModifyBit(PCF50601_DVDD4,DPW_FSS_ON,TRUE);
   if(PMU_ModifyBit(DPW_BACKLIGHT_SUPPLY_ADDRESS,DPW_FSS_ON,TRUE) == FALSE)
   {
      return FALSE;
   }   
   return TRUE;        
}

u8 BackLight_OFF(void)
{
   u8 a_BacklightReg[4];
   u16 k;
   u8 vl_PMUState;
   
   a_BacklightReg[0] = PCF50601_BBMLOW; 				
   /* BBMLOW Initialization */			
   a_BacklightReg[1] = 0x00;			
   /* BBMHIGH Initialization */			
   a_BacklightReg[2] = 0xFF;			
   /* BBMC Initialization */			
   a_BacklightReg[3] = 0x00;	
   for(k=0;(vl_PMUState = hi2c2_11WriteByPolling(PCF50601_IIC_AD,4,a_BacklightReg)!= HI2C_TRANSFER_OK)&&(k<3);k++)
   {
       if((vl_PMUState != HI2C_TRANSFER_OK) ||(k>2))
       {
           return FALSE;
       }
   }
      
   //PMU_ModifyBit(PCF50601_DVDD4,DPW_FSS_ON,FALSE);
   if(PMU_ModifyBit(DPW_BACKLIGHT_SUPPLY_ADDRESS,DPW_FSS_ON,FALSE) == FALSE)
   {
       return FALSE;
   }        
   return TRUE;
}

⌨️ 快捷键说明

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