初中生
最后登录1970-1-1
在线时间 小时
注册时间2024-7-18
|
有没有老哥帮小老弟看下呀
目的:想通过调用test02()函数,看看能否在上位机中正确输出数据,以此来判断I2C_BufferWrite和I2C_BufferRead函数是否正常;
结果:上位机中出现如下结果
- //在主函数中调用该函数
- void test02(void)
- {
- uint16_t tem=18;
- for(i=0; i<tem; i++){
- I2c_Buf_Write[i] = i;//程序给这里打了个断点,为啥
- }
-
- I2C_BufferWrite(8, I2c_Buf_Write, tem);
- I2C_BufferRead(8, I2c_Buf_Read, tem);
-
- printf("这是一个测试\n");
- for(i=0;i<tem;i++){
- printf("%d ",I2c_Buf_Read[i]);
- }
- }
- //
- //连续写入
- void I2C_BufferWrite(uint8_t WriteAddr, uint8_t* pBuffer, uint16_t numByte2Write){
- uint8_t i;
- uint8_t Addr = 0, count = 0, NumOfPage = 0, NumOfSingle = 0;
-
- Addr = WriteAddr % PageSize; //相对于某一页来说,要写入的位置0-7
- count = PageSize - Addr; //用于补齐未对齐页
- NumOfPage = numByte2Write / PageSize;//可写入多少页
- NumOfSingle = numByte2Write % PageSize;//最后一页写入的字节数
-
- //页写入对齐
- if(Addr == 0){
- //NumOfPage<PageSize
- if(NumOfPage==0){
- PageWrite(WriteAddr, pBuffer, numByte2Write);
- EEPROM_WaitForWriteEnd();
- }
- //NumOfPage>PageSize
- else{
- while(NumOfPage--){
- PageWrite(WriteAddr, pBuffer, PageSize);
- EEPROM_WaitForWriteEnd();
- WriteAddr += PageSize;
- pBuffer += PageSize;
- }
- if(NumOfSingle){
- PageWrite(WriteAddr, pBuffer, NumOfSingle);
- EEPROM_WaitForWriteEnd();
- }
- }
- }
- //未对齐
- else{
- }
- }
- //页写入,每页字节数不能超过8字节
- uint8_t PageWrite(uint8_t WriteAddr, uint8_t* pBuffer, uint16_t numByte2Write){
- uint8_t i;
-
- //等待总线空闲(这里需要搞个死循环吗)
- while(!I2C_GetFlagStatus(EEPROM_I2Cx, I2C_FLAG_BUSY))
- I2C_GenerateSTART(EEPROM_I2Cx, ENABLE);//产生起始信号
-
- while(!I2C_CheckEvent(EEPROM_I2Cx, I2C_EVENT_MASTER_MODE_SELECT)){//等待EV5事件
- if((Timeout--)==0)
- return I2C_TIMEOUT_UserCallback(4);
- }
-
- I2C_Send7bitAddress(EEPROM_I2Cx, EEPROM_I2C_ADDRESS, I2C_Direction_Transmitter);//发送从机地址
-
- while(!I2C_CheckEvent(EEPROM_I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)){//等待EV6事件
- if((Timeout--)==0)
- return I2C_TIMEOUT_UserCallback(5);
- }
-
- I2C_SendData(EEPROM_I2Cx, WriteAddr);//发送要写入的地址
-
- while(!I2C_CheckEvent(EEPROM_I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED)){
- if((Timeout--)==0)
- return I2C_TIMEOUT_UserCallback(6);
- }
-
- for(i=0; i < numByte2Write; i++){
- I2C_SendData(EEPROM_I2Cx, pBuffer[i]);//发送一字节数据
-
- while(!I2C_CheckEvent(EEPROM_I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED)){
- if((Timeout--)==0)
- return I2C_TIMEOUT_UserCallback(7);
- }
- }
-
- I2C_GenerateSTOP(EEPROM_I2Cx, ENABLE);//发送终止信号
- return 1;
- }
复制代码
|
-
|