STM32cubeMX工程软件模拟IIC


//simulate I2C 

/**
 * @brief IIC延时
 * @param  1us
 * @return 无
 */
void IIC_Delay(uint8_t time)
{
    uint32_t i = time * 33; 

    while (i--)
    {
        ;
    }
}

/**
 * @brief IIC初始化
 * @param  无
 * @return 无
 */
void Soft_IIC_Init(uint8_t module)
{

    IIC_SDA_H();
    IIC_SCL_H();


}

/**
 * @brief SDA引脚设置输出模式
 * @param  无
 * @return 无
 */
void Soft_IIC_Output(uint8_t module)
{

    GPIO_InitTypeDef SOFT_IIC_GPIO_STRUCT;
    SOFT_IIC_GPIO_STRUCT.Mode = GPIO_MODE_OUTPUT_PP;
    SOFT_IIC_GPIO_STRUCT.Pin = IIC_SDA_PIN;
    SOFT_IIC_GPIO_STRUCT.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

    HAL_GPIO_Init(IIC_SDA_PORT, &SOFT_IIC_GPIO_STRUCT);

}

/**
 * @brief SDA引脚设置输入模式
 * @param  无
 * @return 无
 */
void Soft_IIC_Input(uint8_t module)
{

    GPIO_InitTypeDef SOFT_IIC_GPIO_STRUCT;
    SOFT_IIC_GPIO_STRUCT.Mode = GPIO_MODE_INPUT;
    SOFT_IIC_GPIO_STRUCT.Pin = IIC_SDA_PIN;
    SOFT_IIC_GPIO_STRUCT.Speed = GPIO_SPEED_FREQ_VERY_HIGH;	

    HAL_GPIO_Init(IIC_SDA_PORT, &SOFT_IIC_GPIO_STRUCT);

}

/**
 * @brief IIC起始信号
 * @param  无
 * @return 无
 */
void Soft_IIC_Start(uint8_t module)
{

    Soft_IIC_Output(module);
    IIC_SDA_H();
    IIC_SCL_H();
    IIC_Delay(IIC_DELAY_TIME);
    IIC_SDA_L();
    IIC_Delay(IIC_DELAY_TIME);
    IIC_SCL_L();

}

/**
 * @brief IIC停止信号
 * @param  无
 * @return 无
 */
void Soft_IIC_Stop(uint8_t module)
{

    Soft_IIC_Output(module);
    IIC_SDA_L();
    IIC_SCL_H();
    IIC_Delay(IIC_DELAY_TIME);
    IIC_SDA_H();
    IIC_Delay(IIC_DELAY_TIME);
}

/**
 * @brief IIC应答信号
 * @param  无
 * @return 无
 */
void Soft_IIC_ACK(uint8_t module)
{

    Soft_IIC_Output(module);
    IIC_SCL_L();
    IIC_SDA_L();
    IIC_Delay(IIC_DELAY_TIME);
    IIC_SCL_H();
    IIC_Delay(IIC_DELAY_TIME);
    IIC_SCL_L();
}

/**
 * @brief IIC无应答信号
 * @param  无
 * @return 无
 */
void Soft_IIC_NACK(uint8_t module)
{

    Soft_IIC_Output(module);
    IIC_SCL_L();
    IIC_SDA_H();
    IIC_Delay(IIC_DELAY_TIME);
    IIC_SCL_H();
    IIC_Delay(IIC_DELAY_TIME);
	IIC_SCL_L();
}

/**
 * @brief IIC等待应答信号
 * @param  无
 * @return 0无应答  1有应答
 */
uint8_t Soft_IIC_Wait_ACK(uint8_t module)
{
    uint32_t wait;

      Soft_IIC_Output(module);
      IIC_SDA_H();
      Soft_IIC_Input(module);
      IIC_SCL_H();
      IIC_Delay(IIC_DELAY_TIME);
      while (HAL_GPIO_ReadPin(IIC_SDA_PORT, IIC_SDA_PIN))
      {
          wait++;
          if (wait > 2000)
          {
              Soft_IIC_Stop(module);
              return 0;
          }
      }
      IIC_SCL_L();
      return 1;
}

/**
 * @brief IIC写数据1
 * @param  无
 * @return 无
 */
void Soft_IIC_Write_High(uint8_t module)
{

    IIC_SCL_L();
    IIC_SDA_H();
    IIC_Delay(IIC_DELAY_TIME);
    IIC_SCL_H();
    IIC_Delay(IIC_DELAY_TIME);
    IIC_SCL_L();
}

/**
 * @brief IIC写数据0
 * @param  无
 * @return 无
 */
void Soft_IIC_Write_Low(uint8_t module)
{

    IIC_SCL_L();
    IIC_SDA_L();
    IIC_Delay(IIC_DELAY_TIME);
    IIC_SCL_H();
    IIC_Delay(IIC_DELAY_TIME);
    IIC_SCL_L();
}

/**
 * @brief IIC写入单个数据
 * @param  无
 * @return 应答信号, 0无应答 1有应答
 */
uint8_t Soft_IIC_Write_Byte(uint8_t module,uint8_t Byte)
{
    uint8_t i;
   

      Soft_IIC_Output(module);
      for (i = 0x80; i != 0; i >>= 1)
      {
          if (Byte & i)
          {
              Soft_IIC_Write_High(module);
          }
          else
          {
              Soft_IIC_Write_Low(module);
          }
      }
      return (Soft_IIC_Wait_ACK(module));
}

/**
 * @brief IIC读一个数据
 * @param  ACK:应答 NACK:不应答
 * @return 返回读到的数据
 */
uint8_t Soft_IIC_Recv_Byte(uint8_t module,ACK_STATUS ack_sta)
{
	
    uint8_t data = 0, i;

      Soft_IIC_Input(module);
		
      for (i = 0x80; i != 0; i >>= 1)
      {
          IIC_SCL_L();
          IIC_Delay(IIC_DELAY_TIME);
          IIC_SCL_H();
          IIC_Delay(IIC_DELAY_TIME);
		  if (HAL_GPIO_ReadPin(IIC_SDA_PORT, IIC_SDA_PIN) == 1)
          {
              data |= i;
          }

      }
		
	  IIC_SCL_L();

      if (ack_sta == ACK)
      {
          Soft_IIC_ACK(module);
      }
      else
      {
          Soft_IIC_NACK(module);
      }
      return data;

}


/**
 * @brief IIC写入多个数据
 * @param  
 * @return 
 */

void Soft_I2C_Mem_Write(uint8_t module, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
{

    //start
    Soft_IIC_Start(module);
    
    //send slave addr
    Soft_IIC_Write_Byte(module,DevAddress);

    //send register addr
    Soft_IIC_Write_Byte(module,MemAddress);

    //send data
    for(uint8_t i=0;i

头文件.h


typedef enum{
    NACK = 0,
    ACK  = 1
}ACK_STATUS;

#define IIC_DELAY_TIME 5		

#define IIC_SCL_PIN  I2C_CLK5_Pin
#define IIC_SCL_PORT I2C_CLK5_GPIO_Port
#define IIC_SDA_PIN  I2C_SDA5_Pin
#define IIC_SDA_PORT I2C_SDA5_GPIO_Port




#define IIC_SCL_H()     HAL_GPIO_WritePin(IIC_SCL_PORT,IIC_SCL_PIN,GPIO_PIN_SET)
#define IIC_SCL_L()     HAL_GPIO_WritePin(IIC_SCL_PORT,IIC_SCL_PIN,GPIO_PIN_RESET)
#define IIC_SDA_H()     HAL_GPIO_WritePin(IIC_SDA_PORT,IIC_SDA_PIN,GPIO_PIN_SET)
#define IIC_SDA_L()     HAL_GPIO_WritePin(IIC_SDA_PORT,IIC_SDA_PIN,GPIO_PIN_RESET)



void IIC_Delay(uint8_t time);
void Soft_IIC_Init(uint8_t module);
void Soft_IIC_Output(uint8_t module);
void Soft_IIC_Input(uint8_t module);
void Soft_IIC_Start(uint8_t module);
void Soft_IIC_Stop(uint8_t module);
void Soft_IIC_ACK(uint8_t module);
void Soft_IIC_NACK(uint8_t module);
uint8_t Soft_IIC_Wait_ACK(uint8_t module);
void Soft_IIC_Write_High(uint8_t module);
void Soft_IIC_Write_Low(uint8_t module);
uint8_t Soft_IIC_Write_Byte(uint8_t module,uint8_t Byte);
uint8_t Soft_IIC_Recv_Byte(uint8_t module,ACK_STATUS ack_sta);
void Soft_I2C_Mem_Write(uint8_t module, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout);
void Soft_I2C_Mem_Read(uint8_t module, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout);

你可能感兴趣的:(stm32,嵌入式硬件,单片机)