下面是写数据 与读数据部分 void I2C_Send_n_Data(u16 addr,u8 *data,u8 len)//数据地址,数据指针,要写数据长度 { I2C_Cmd(I2C1,DISABLE); I2C_Cmd(I2C1,ENABLE); ScrollPage: I2C_AcknowledgeConfig(I2C1, ENABLE); while(((u16)(I2C1->SR2)&(u16)(0x0002))==0x0002); I2C_GenerateSTART(I2C1,ENABLE); while(!(((u16)(I2C1->SR1)&(u16)(0x0001)))); while(!((u16)(I2C1->SR2)&(u16)(0x0011))==0x0011); I2C_Send7bitAddress(I2C1,0xA0, I2C_Direction_Transmitter); delay(); while(!((u16)(I2C1->SR1)&(u16)(0x0082))==0x0082); while(!((u16)(I2C1->SR2)&(u16)(0x0007))==0x0007); I2C_SendData(I2C1, (addr>>8)&0xff); delay(); while(!((u16)(I2C1->SR1)&(u16)(0x0080))==0x0080); while(!((u16)(I2C1->SR2)&(u16)(0x0007))==0x0007); I2C_SendData(I2C1, addr&0xff); delay(); while(!((u16)(I2C1->SR1)&(u16)(0x0080))==0x0080); while(!((u16)(I2C1->SR2)&(u16)(0x0007))==0x0007); I2C_SendData(I2C1, *data++); delay(); while(!((u16)(I2C1->SR1)&(u16)(0x0080))==0x0080); while(!((u16)(I2C1->SR2)&(u16)(0x0007))==0x0007); addr++; len--; while(len) { if((addr%128)!=0)//由于一页写完需要产生一个停止信号,所以此处需要判断一页是否写完 { I2C_SendData(I2C1,*(data++)); delay(); while(!((u16)(I2C1->SR1)&(u16)(0x0080))==0x0080); while(!((u16)(I2C1->SR2)&(u16)(0x0007))==0x0007); addr++; len--; } else { I2C_GenerateSTOP(I2C1,ENABLE); goto ScrollPage; } } I2C_GenerateSTOP(I2C1,ENABLE); delay(); while(((u16)(I2C1->SR2)&(u16)(0x0002))==0x0002); } void I2C_Read_n_Data(u16 addr,u8 *data,u8 len) { u8 i; I2C_Cmd(I2C1,DISABLE); I2C_Cmd(I2C1,ENABLE); I2C_AcknowledgeConfig(I2C1, ENABLE); I2C_GenerateSTART(I2C1,ENABLE); delay(); while(!((u16)(I2C1->SR1)&(u16)(0x0001))==0x0001); while(!((u16)(I2C1->SR2)&(u16)(0x0011))==0x0011); I2C_Send7bitAddress(I2C1,0xA0, I2C_Direction_Transmitter); delay(); while(!((u16)(I2C1->SR1)&(u16)(0x0082))==0x0082); while(!((u16)(I2C1->SR2)&(u16)(0x0007))==0x0007); I2C_SendData(I2C1, (addr>>8)&0xff); delay(); while(!((u16)(I2C1->SR1)&(u16)(0x0080))==0x0080); while(!((u16)(I2C1->SR2)&(u16)(0x0007))==0x0007); I2C_SendData(I2C1, addr&0xff); delay(); while(!((u16)(I2C1->SR1)&(u16)(0x0080))==0x0080); while(!((u16)(I2C1->SR2)&(u16)(0x0007))==0x0007); I2C_GenerateSTART(I2C1,ENABLE); delay(); while(!((u16)(I2C1->SR1)&(u16)(0x0001))); while(!((u16)(I2C1->SR2)&(u16)(0x0011))==0x0011); I2C_AcknowledgeConfig(I2C1, DISABLE); I2C_Send7bitAddress(I2C1,0xA0, I2C_Direction_Receiver); delay(); while(!((u16)(I2C1->SR1)&(u16)(0x0082))==0x0082); while(!((u16)(I2C1->SR2)&(u16)(0x0007))==0x0007); if(len>1) { for(i=0;iSR1)&(u16)(0x0040))==0x0040); while(!((u16)(I2C1->SR2)&(u16)(0x0003))==0x0003);//主模式,总线忙,字节发送结束 delay(); while(!((u16)(I2C1->SR1)&(u16)(0x0004))==0x0004); *data=I2C_ReceiveData(I2C1);//读出DR寄存器中的值 I2C_AcknowledgeConfig(I2C1,ENABLE);//应答使能 data++; } } while(!((u16)(I2C1->SR1)&(u16)(0x0040))==0x0040); while(!((u16)(I2C1->SR2)&(u16)(0x0003))==0x0003);//主模式,总线忙,字节发送结束 delay(); while(!((u16)(I2C1->SR1)&(u16)(0x0004))==0x0004); *data=I2C_ReceiveData(I2C1);//读出DR寄存器中的值 I2C_AcknowledgeConfig(I2C1,DISABLE);//应答失能,无应答 I2C_GenerateSTOP(I2C1,ENABLE); delay(); while(((u16)(I2C1->SR2)&(u16)(0x0002))==0x0002);//等待总线通信结束} 其中我在串口中断中用到了这两个读写函数,假如我设置一次性让串口输出50个数据,这程序总是只有第一个数是正确的,另外49个数都是ff,本人菜鸟,请高手解答,一下是中断中应用部分,其中p和q是我所设置的两个指针,指向要存储的数据的,其中q是和c[j ]对应的 if(i>49) { i=0; I2C_Send_n_Data(0x00000000,p,50); delay(); I2C_Read_n_Data(0x00000000,q,50); for(j=0;jSR&0X0040));//等待发送完成 } } USART_ClearITPendingBit(USART2,USART_IT_RXNE); //清除中断标志位 |
RE:stm32I2C连续读写问题
2、I2C数据读取的时候,也必须是数组存放,不能是指针存放。因为你不知道指针会指到什么地址。
回复:stm32I2C连续读写问题