在线时间0 小时
UID209567
ST金币0
蝴蝶豆0
注册时间2010-8-17
新手上路
- 最后登录
- 1970-1-1
|
a0a.1 0b0c
大家好,想问下那个对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)
{
}
} |
|