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

📄 i2c_slave.c

📁 STM32F10xx的LCD驱动源码
💻 C
字号:
/**************************************************************************************
  File Name:I2C_Slave->c
DEscription:
***************************************************************************************/
/***********************************include files*************************************/




#include  "I2C.h"
/********************************defined macros***************************************/
        

#define I2C2                ((I2C_TypeDef *) I2C2_BASE)




/*************************************************************************************/
/************************************************************************************8
Function Name:I2C_SlaveIni( )
     Function:Initiate the I2C controllor working in slave mode ->
    Parameter:none
       Return:none
         Note:
*************************************************************************************/
void I2C_SlaveIni(void)
{  
  I2C2->CR2     &=0x0000;
  I2C2->OAR1    &=0x0000; 
  I2C2->OAR2    &=0x0000;
  I2C2->CR1     &=0x0000;
  
  
   I2C2->CR2     =0x0024;       //The peripheral`s input clock is 36 MHz 
   I2C2->OAR1   |=0x0002;       //Slave node address1 set as  1
   I2C2->OAR2   |=0x0005;       //Slave node address2 set as  2 wiht slave node address2 enabled->
   I2C1->CR1    |=0x0401;       //Set ACK (Acknowledge returned after a byte is received (matched address or data)).Set PE bit to enable this peripheral.
}
/************************************************************************************8
Function Name:I2C_Slave_ByteTransmit( )
     Function:To transmit a byte as a slave->
    Parameter:u8 Data:The byte to be transmit->
       Return:none
         Note:
*************************************************************************************/
void I2C_Slave_ByteTransmit(u8 Data)
{
// while(!(I2C2->SR1&0x0002)) ;                   //Wait till the ADDR bit set->Clear the ADDR bit by read SR1 followed by SR2 reading->
     if(I2C2->SR2&0x0004)
	  {
	    I2C2->DR=Data;
	    while (!( I2C2->SR1&0x0080));        //Wait till TxE   set indicates that the byte sent finisheed successfully->
	  }
	
}
/************************************************************************************8
Function Name:I2C_Slave_BlockTransmit( )
     Function:To transmit a block as a slave ->
    Parameter:u8  *Data:The address of the first byte of the block to be transmit->
              u8  Total:The size of the block in byte,
       Return:none
         Note:
*************************************************************************************/
void I2C_Slave_BlockTransmit(u8 *Data ,u8 Total)
{
	while(!(I2C2->SR1&0x0002)) ;            //Wait till the ADDR bit set->Clear the ADDR bit by read SR1 followed by SR2 reading->
	I2C2->SR2=I2C2->SR2;
	
	
        if(I2C2->SR2&0x0004)
	   {
	      while(!(I2C2->SR1&0x0010))          //Send data till the STOP condition be detected->
		{
		  I2C2->DR=*Data;                   //Write the current byte to DR to be ready for transmittion->
		  Data++;                           //Point to the next byte->
		  Total--;                          //Decriment the remain number of bytes by 1->
		  while (!( I2C2->SR1&0x0080));    //Wait till TxE set indicates that the byte sent finisheed successfully->
		 //if(Total==0) return ERR_Flag ;
		}
	  }
}
/************************************************************************************8
Function Name:I2C_Slave_BlockReception( )
     Function:To recive a block of data as a slave->
    Parameter:u8 *Buffer:The address of the first byte of the recived data buffer->
       Return:none
         Note:
*************************************************************************************/
u8 I2C_Slave_BlockReception(u8 *Buffer )
{
	u8  Total=0;
	
	while(!(I2C2->SR1&0x0002)) ;                                  //Wait till the ADDR bit set->Clear the ADDR bit by read SR1 followed by SR2 reading->
	//I2C2->SR2=I2C2->SR2;
	if(!(I2C2->SR2&0x0004))                                       //The peripheral is in slave recive mode->
		{
		   while(!(I2C2->SR1&0x0010))                         //The bus is not in stop condition->
		      {
			   while(!( I2C2->SR1&0x0040));               //Wait till a byte recipt succesfully(RxNE set)->
			   *Buffer=I2C2->DR;
			    Buffer++;                                //Point to next byte and ready for reciption of another byte ->
			    Total++;                                 //Incriment the recived bytes number by 1->
                            if(( ( I2C2->SR1&0x0010)))  
                            {
                              I2C2->SR1&=0xFFFE;
                              return Total;
                            }
		      }
		 }
		 
}
/************************************************************************************8
Function Name:I2C_Slave_ByteReception()
     Function:To recive a byte as a slave->
    Parameter:none
       Return:u8 Data :The recived byte->
         Note:
*************************************************************************************/
u8 I2C_Slave_ByteReception(void)
{
	
  //while(!(I2C2->SR1&0x0002)) ;                //Wait till the ADDR bit set->Clear the ADDR bit by read SR1 followed by SR2 reading.
   if(!(I2C2->SR2&0x0004))                     //The peripheral is in slave recive mode.
      {
        while(!( I2C2->SR1&0x0040));           //Wait till a byte recipt succesfully->
        return I2C2->DR;		                       //Read the recived byte and clear the RxNE byte->
     
      }
}

⌨️ 快捷键说明

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