我使用硬體I2C去做24C32燒寫 正在測試I2C READ BYTE跟WRITE BYTE時發現了一些問題如下, I2C READ如果加上回傳會出現讀出來的數據錯誤 WRITE程式如下 void I2C_EE_ByteWrite(uint8_t pBuffer,uint16_t WriteAddr) { uint8_t at24C02_to_16_addr; while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY)); I2C_GenerateSTART(I2C1,ENABLE); while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT)); #ifdef AT24C32_64 I2C_Send7bitAddress(I2C1,(uint8_t)EEPROM_ADDRESS,I2C_Direction_Transmitter);//EEPROM_ADDRESS while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); I2C_SendData(I2C1,(uint8_t)((WriteAddr&0xFF00)>>8)); //MSB while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTING)); // TXOK? I2C_SendData(I2C1,(uint8_t)(WriteAddr&0x00FF)); //LSB #else //AT24C02 to 16 at24C02_to_16_addr=(uint8_t)((WriteAddr&0x0E00)>>7); I2C_Send7bitAddress(I2C1,EEPROM_ADDRESS|at24C02_to_16_addr,I2C_Direction_Transmitter);//EEPROM_ADDRESS &MSB while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); I2C_SendData(I2C1,(uint8_t)(WriteAddr&0x00FF)); //LSB #endif while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTING)); // TXOK? I2C_SendData(I2C1,pBuffer); while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)); // TXOK? I2C_GenerateSTOP(I2C1,ENABLE); delay(EEPROM_DLY); } READ如下 uint8_t I2C_EE_ByteRead(uint16_t WriteAddr) { uint8_t at24C02_to_16_addr,read_data; while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY)); I2C_GenerateSTART(I2C1,ENABLE); while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT)); #ifdef AT24C32_64 I2C_Send7bitAddress(I2C1,(uint8_t)EEPROM_ADDRESS,I2C_Direction_Transmitter);//EEPROM_ADDRESS while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); I2C_ClearFlag(I2C1,I2C_FLAG_ADDR); I2C_SendData(I2C1,(uint8_t)((WriteAddr&0xFF00)>>8)); //MSB while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTING)); // TXOK? I2C_SendData(I2C1,(uint8_t)(WriteAddr&0x00FF)); //LSB while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTING)); // TXOK? I2C_GenerateSTART(I2C1,ENABLE); while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)); I2C_Send7bitAddress(I2C1,(uint8_t)EEPROM_ADDRESS,I2C_Direction_Receiver); //RECEIVER #else at24C02_to_16_addr=(uint8_t)((WriteAddr>>7)&0x0E); at24C02_to_16_addr=at24C02_to_16_addr|EEPROM_ADDRESS; I2C_Send7bitAddress(I2C1,at24C02_to_16_addr,I2C_Direction_Transmitter);//EEPROM_ADDRESS &MSB while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); I2C_SendData(I2C1,(uint8_t)(WriteAddr)); //LSB while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTING)); // TXOK? I2C_GenerateSTART(I2C1,ENABLE); while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)); I2C_Send7bitAddress(I2C1,(uint8_t)at24C02_to_16_addr,I2C_Direction_Receiver); //RECEIVER #endif while (!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)); //RX FLAG read_data=I2C_ReceiveData(I2C1); I2C_AcknowledgeConfig(I2C1,ENABLE); //NONE ACK I2C_GenerateSTOP(I2C1,ENABLE); //return read_data; } 拿掉return read_data;收下的資料居然正確,加上RETURN會不正確 麻煩幫小弟看一下,謝謝 |
从零开始操作STM8寄存器(风驰iCreate奉献)
【中文资料】初学STM8库函数的中文帮助软件
绝对经典的中文STM8学习手册,淘宝上学习板资料,友情大放送!
【原创教程】风驰iCreate独家开源STM8 27个例程和10多万字的pdf教程
STM8的LCD1602 4线驱动,为什么不工作
【精华资料】由零开始开发STM8
STM8S 的触摸库是如何在主程序中查询键的呢、
【精华资料】STM8的C语言编程1-14讲完整版
【精品教程】STM8系列单片机入门教程系列
STM8 第一次进中断不准【悬赏问答】
回复:STM8L I2C EEPROM燒寫問題
很怪的問題,都沒人想挑戰看看?
RE:STM8L I2C EEPROM燒寫問題
回复:STM8L I2C EEPROM燒寫問題
仿真一下在return以前, read_data的值是多少,return以后 read_data的值是多少。
RETURN之前,read_data就錯了,數據是161,拿掉return read_data;讀到的資料都是正確的
RE:STM8L I2C EEPROM燒寫問題
RE:STM8L I2C EEPROM燒寫問題
注意到你这个函数的返回值是VOID 应该没有返回值的
回复:STM8L I2C EEPROM燒寫問題
你的函数定义的是void型,所以不能加返回值
那是write,抱歉我沒加上分格線所以導致漏看,read function下面
回复:STM8L I2C EEPROM燒寫問題
void I2C_EE_ByteWrite(uint8_t pBuffer,uint16_t WriteAddr)
注意到你这个函数的返回值是VOID 应该没有返回值的
READ如下
uint8_t I2C_EE_ByteRead(uint16_t WriteAddr)
您看到write,write byte沒有返傳
謝謝回覆...希望問題能獲得解決
RE:STM8L I2C EEPROM燒寫問題
回复:STM8L I2C EEPROM燒寫問題
应该和return没有关系,你看一下I2C_ReceiveData返回的值的类型。
同型,都是uint8_t
謝謝回覆
RE:STM8L I2C EEPROM燒寫問題
RE:STM8L I2C EEPROM燒寫問題
回复:STM8L I2C EEPROM燒寫問題
還是要靠自己阿~~~