码迷,mamicode.com
首页 > 其他好文 > 详细

STM8 中I2C读写函数。

时间:2016-01-14 22:22:08      阅读:771      评论:0      收藏:0      [点我收藏+]

标签:

void I2C_write(u8 Addr, u8 *WriteData, u8 length)
{
  /* Send STRAT condition */
  I2C_GenerateSTART(ENABLE);
  /* Test on EV5 and clear it */
  while(!I2C_CheckEvent(I2C_EVENT_MASTER_START_SENT)); 
  /* Send EEPROM address for write */
  I2C_Send7bitAddress(Addr, I2C_DIRECTION_TX);  
  /* Test on EV6 and clear it */
  while(!I2C_CheckEvent(I2C_EVENT_MASTER_ADDRESS_ACKED));
   I2C_ClearFlag(I2C_FLAG_ADDRESSSENTMATCHED);// must add   
  for(int i = 0; i<length; i++)
  {
    I2C_SendData(WriteData[i]); 
    /* Test on EV8 and clear it */
    while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTING));    
  }  
  /* Send STOP condition */
  I2C_GenerateSTOP(ENABLE);  
}
void I2C_Read(u8* pBuffer, u8 ReadAddr, u8 NumByteToRead) { while(I2C->SR3&0x02); I2C->CR2 |= 0x01; while(!(I2C->SR1 & 0x01)); I2C->DR = ReadAddr; while(!(I2C->SR1 & 0x02)); I2C->SR1; I2C->SR3; while(NumByteToRead) { if(NumByteToRead == 1) { I2C->CR2 &= ~0x04; I2C->CR2 |= 0x02; } if(I2C->SR1 & 0x40) { *pBuffer=I2C->DR; pBuffer++; NumByteToRead--; } } I2C->CR2 |= 0x04; }

有其他选择千万不要选择STM8,太难用了。

STM8 中I2C读写函数。

标签:

原文地址:http://www.cnblogs.com/cjyszbd/p/5131701.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!