STM32 IIC 通信 库函数实现

reading stucture               
 device address read commandwrite address to board  device addressread data from board  
start0xb0ack0x01ackaddress highackaddress lowackstart0xb1ackdata highackdata lownackstop
example to read   address 0x1234 and result 0x789A            
start0xb0ack0x01ack0x12ack0x34ackstart0xb1ack0x78ack0x9anackstop
                 
                 
                 
                 
writing   stucture               
 device address write commandwrite address to boardread data from board     
start0xb0ack0x81ackaddress highackaddress lowackdata highackdata lowackstop   
example to write   address 0x5678 with data 0xcdef            
start0xb0ack0x81ack0x56ack0x78ack0xcdack0xefackstop0x9a 

 

 

Read Register
void Read_Reg(u8 *DataBuff,u8 ByteQuantity,uint16_t RegAddress,u8 SlaveAddress)
{
     //这句去掉也正常
     while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY)); // Added by Najoua 27/08/2008

    /* Send START condition */     
     I2C_GenerateSTART(I2C1, ENABLE);
     while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
     /* 发送器件地址(写)*/
     I2C_Send7bitAddress(I2C1, SlaveAddress, I2C_Direction_Transmitter);
     while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)); /* EV7 */

     //这句去掉也正常
     /* Clear EV6 by setting again the PE bit */
     I2C_Cmd(I2C1, ENABLE);

    I2C_SendData(I2C1,01);
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
    I2C_SendData(I2C1,RegAddress>>8);
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
    I2C_SendData(I2C1,RegAddress);
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
    I2C_GenerateSTART(I2C1, ENABLE);
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
     /* 发送器件地址(写)*/
    I2C_Send7bitAddress(I2C1, SlaveAddress, I2C_Direction_Receiver);
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)); /* EV7 */

    while (ByteQuantity)
    {
        if(ByteQuantity==1)
        {
             I2C_AcknowledgeConfig(I2C1, DISABLE);    //最后一位后要关闭应答的
            I2C_GenerateSTOP(I2C1, ENABLE);            //发送停止位
        }
        
        while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)); /* EV7 */
        *DataBuff = I2C_ReceiveData(I2C1);
        DataBuff++;
        /* Decrement the read bytes counter */
        ByteQuantity--;
    }
    //再次允许应答模式
    I2C_AcknowledgeConfig(I2C1, ENABLE);
    //errorflag=0;
    //break;
    
}
Write Register
void Write_Reg(uint16_t WriteValue,uint16_t RegAddress,u8 SlaveAddress)
{


  while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY)); // Added by Najoua 27/08/2008
    
  /* Send START condition */
  I2C_GenerateSTART(I2C1, ENABLE);
  
  /* Test on EV5 and clear it */
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)); 
  
  /* Send EEPROM address for write */
  I2C_Send7bitAddress(I2C1, SlaveAddress, I2C_Direction_Transmitter);
  
  /* Test on EV6 and clear it */
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));  

      I2C_SendData(I2C1,0x81);
   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
    I2C_SendData(I2C1,RegAddress>>8);
   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
    I2C_SendData(I2C1,RegAddress);
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

    I2C_SendData(I2C1,WriteValue>>8);
   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
    I2C_SendData(I2C1,WriteValue);
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

  /* Send STOP condition */
  I2C_GenerateSTOP(I2C1, ENABLE);
}
I2C_Send7bitAddress
/**
  * @brief  Transmits the address byte to select the slave device.
  * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  * @param  Address: specifies the slave address which will be transmitted
  * @param  I2C_Direction: specifies whether the I2C device will be a
  *   Transmitter or a Receiver. This parameter can be one of the following values
  *     @arg I2C_Direction_Transmitter: Transmitter mode
  *     @arg I2C_Direction_Receiver: Receiver mode
  * @retval None.
  */
void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, uint8_t Address, uint8_t I2C_Direction)
{
  /* Check the parameters */
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  assert_param(IS_I2C_DIRECTION(I2C_Direction));
  /* Test on the direction to set/reset the read/write bit */
  if (I2C_Direction != I2C_Direction_Transmitter)
  {
    /* Set the address bit0 for read */
    Address |= OAR1_ADD0_Set;
  }
  else
  {
    /* Reset the address bit0 for write */
    Address &= OAR1_ADD0_Reset;
  }
  /* Send the address */
  I2Cx->DR = Address;
}

#define  I2C_Direction_Transmitter      ((uint8_t)0x00)
#define  I2C_Direction_Receiver         ((uint8_t)0x01)

7位模式下,地址字节最低位若是0则说明主机要进入发送模式,若是1则是接收模式

转载于:https://www.cnblogs.com/wwjdwy/archive/2013/03/06/2945742.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值