你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

查看: 4767|回复: 1

对24C02当前地址读操作

[复制链接]

1

主题

0

回帖

0

蝴蝶豆

新手上路

最后登录
1970-1-1
发表于 2010-8-17 20:49:18 | 显示全部楼层 |阅读模式
大家好,想问下那个对IIC写了之后,立即读数据,读到的是下个地址里面的东西,我知道这个是因为其内部存在一个可以加一计数的地址计数器,写操作之后加入延时,再进行读操作,就可以读到当前地址里面的东西了,可以不用延时解决这个问题吗?
下面这个是我们开发板上面的程序,是延时40000后再读操作,可以正确读到地址里面的东西,去掉延时读到的下个地址里面的东西,
怎么样可以不用延时,又可以读到要求的地址里面的东西啊

#include
#include    
#include "STM32_Init.h"     
#include "TFT200.h"
#define LED             ( 1 APB2ENR |= (1SR1;
 //if (s & (I2C_FLAG_TIMEOUT | I2C_FLAG_PECERR | I2C_FLAG_OVR | I2C_FLAG_AF | I2C_FLAG_ARLO | I2C_FLAG_BERR | I2C_FLAG_STOPF));
    if (s & event) return s;
   }
  return 0;
  }
void sendStart(void) {
 I2C_GenerateSTART(I2C1, ENABLE);       // send start when bus becomes available
 waitForEvent(I2C_FLAG_SB);
 I2C1->DR = I2C_OwnAddr;          // send slave address for transmission
 waitForEvent(I2C_FLAG_ADDR);
 I2C1->SR2;            // clear event
  }
void sendData(u8 data){
  //waitForEvent(I2C_FLAG_TXE);
  waitForEvent(I2C_FLAG_BTF);
  I2C1->DR = data;           // send byte
  }
void sendStop(void) {
  waitForEvent(I2C_FLAG_TXE);
  I2C_GenerateSTOP(I2C1, ENABLE);       // send stop after current byte
  }
u8 receiveByte(void) {
  I2C_GenerateSTART(I2C1, ENABLE);       // send start when bus becomes available
  waitForEvent(I2C_FLAG_SB);
  I2C_AcknowledgeConfig(I2C1, DISABLE);     // only one byte will be read
  I2C1->DR = I2C_OwnAddr | 0x01;        // send slave address for reception
  waitForEvent(I2C_FLAG_ADDR);
  I2C1->SR2;            // clear event
  waitForEvent(I2C_FLAG_RXNE);
  I2C_GenerateSTOP(I2C1, ENABLE);       // send stop after current byte
  return I2C1->DR;           // receive byte
  }
void WriteByte(u16 addr, u8 data) {
  /* Enable I2C1 acknowledgement if it is already disabled by other function */
  //I2C_AcknowledgeConfig(I2C1, ENABLE);
  //I2C_AcknowledgeConfig(I2C1, DISABLE);     // only one byte will be read
  sendStart();
  sendData( addr & 0xFF );
  //I2C_AcknowledgeConfig(I2C1, DISABLE);     // only one byte will be read
  sendData( data );
  waitForEvent(I2C_FLAG_BTF);
  sendStop();
  }
u8 ReadByte(u16 addr) {
  /* Enable I2C1 acknowledgement if it is already disabled by other function */
  //I2C_AcknowledgeConfig(I2C1, ENABLE);
  sendStart();
  sendData( addr & 0xFF );
  //sendStart();
  //sendStop();
  return receiveByte();
  }
/*----------------------------------------------------------*\
 |  Delay                                                   |
 |  延时 Inserts a delay time.                              |
 |  nCount: 延时时间                                        |
 |  nCount: specifies the delay time length.                |
\*----------------------------------------------------------*/
void Delay(vu32 nCount) {
  for(; nCount != 0; nCount--);
  }
int main (void) {
  char s[20];
  stm32_Init ();                               
  LCD_Init();
  LCD_Clear_Screen(Blue);
  LCD_PutString(58,20,"RC-M3 开发板",Cyan,Blue);
  I2C_Initialisation();
  LCD_PutString(70,60,"Write:0x5A",Red,Yellow);
  WriteByte(0x00, 0x5A);
  Delay(40000);                                                                                                                              //去掉该行后读到的数据时0X01里面的数据,怎么不用延时,又可以解决问题啊
  sprintf(s, "Read:0x%2X", ReadByte(0x00)); //字符转换
  LCD_PutString(70,100,s,Red,Yellow);
  for(;;) {
    GPIOB->ODR &= ~LED;                         // switch on LED
 Delay(2000000);
    GPIOB->ODR |=  LED;                         // switch off LED
 Delay(2000000);
    }
  }
/*******************************************************************************
* Function Name  : assert_failed
* Description    : Reports the name of the source file and the source line number
*                  where the assert error has occurred.
* Input          : - file: pointer to the source file name
*                  - line: assert error line source number
* Output         : None
* Return         : None
*******************************************************************************/
void assert_failed(u8* file, u32 line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* Infinite loop */
  while (1)
  {
  }
}
<
回复

使用道具 举报

134

主题

4489

回帖

239

蝴蝶豆

版主

最后登录
2020-12-9
发表于 2010-8-18 14:47:18 | 显示全部楼层

RE:对24C02当前地址读操作

可以使用中断,当读取到数据以后,发送ACK给E2,然后等待下一个数据的到达。
回复 支持 反对

使用道具 举报

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版