打印

STM32 I2C的问题

[复制链接]
2277|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
jjl3|  楼主 | 2009-9-24 21:44 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
void I2C_EE_ByteWrite(unsigned short int WriteAddr, unsigned char pBuffer)
{
  /* Send STRAT condition */
  I2C1_CR1             |= 0x0100;
  
  /* Test on EV5 and clear it */
  while(!(I2C1_SR1 & 0x0001));  
  /* Send EEPROM address for write */
  I2C1_DR               = I2C_SLAVE_ADDRESS7_Write;
  
  /* Test on EV6 and clear it */
  while((!(I2C1_SR1 & 0x0002)) || (I2C1_SR2 & 0x0000));

  /* Send the EEPROM's internal address to write to : MSB of the address first */
  I2C1_DR               = (unsigned char)((WriteAddr & 0xFF00) >> 8);
  
  /* Test on EV8 and clear it */
  while(!(I2C1_SR1 & 0x0080) || (I2C1_SR2 & 0x0000));
  
  /* Send the EEPROM's internal address to write to : LSB of the address */
  I2C1_DR               = (unsigned char)(WriteAddr & 0x00FF);  
  
  /* Test on EV8 and clear it */
  while(!(I2C1_SR1 & 0x0080));
  /* Send the byte to be written */
  I2C1_DR               = pBuffer;
   
  /* Test on EV8 and clear it */
  while(!(I2C1_SR1 & 0x0080));
  
  /* Send STOP condition */
  I2C1_CR1             |= 0x0200;
}
unsigned char I2C_EE_ByteRead(unsigned short int ReadAddr)
{  
    /* While the bus is busy */
  while(I2C1_SR2 & 0x0002);
  
  /* Send START condition */
  I2C1_CR1             |= 0x0100;
  
  /* Test on EV5 and clear it */
  while(!(I2C1_SR1 & 0x0001));
   
  /* Send EEPROM address for write */
  I2C1_DR               = I2C_SLAVE_ADDRESS7_Write;
  /* Test on EV6 and clear it */
  while(!(I2C1_SR1 & 0x0002) || (I2C1_SR2 & 0x0000));
  
  /* Clear EV6 by setting again the PE bit */
  I2C1_CR1             |= 0x0001;
  /* Send the EEPROM's internal address to read from: MSB of the address first */
  I2C1_DR               = (unsigned char)((ReadAddr & 0xFF00) >> 8);
  
  /* Test on EV8 and clear it */
  while(!(I2C1_SR1 & 0x0080));
  /* Send the EEPROM's internal address to read from: LSB of the address */
  I2C1_DR               = (unsigned char)(ReadAddr & 0x00FF);   
  
  /* Test on EV8 and clear it */
  while(!(I2C1_SR1 & 0x0080));
  
  /* Send STRAT condition a second time */  
  I2C1_CR1             |= 0x0100;
  
  /* Test on EV5 and clear it */
  while(!(I2C1_SR1 & 0x0001));
  
  /* Send EEPROM address for read */
  I2C1_DR               = I2C_SLAVE_ADDRESS7_Read;
  
  /* Test on EV6 and clear it */
  while(!(I2C1_SR1 & 0x0002) || (I2C1_SR2 & 0x0000));
  
  /* While there is data to be read */
  /* Disable Acknowledgement */
  I2C1_CR1             &= 0xFBFF;
      
  /* Send STOP Condition */
  I2C1_CR1             |= 0x0200;
  return I2C1_DR;
}

在调试时,单步运行这两个函数可以运行通过,但run起来就老是死在某哦个while处,请大家帮忙看看这两个函数有什么问题?
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

43

主题

265

帖子

2

粉丝