/* While the bus is busy */
//while(I2C_GetFlagStatus(I2C_FLAG_BUSBUSY));
while(I2C_SR3 & 0x02); //总线空闲
/* Generate start & wait event detection */
// I2C_GenerateSTART(ENABLE);
I2C_CR2 |= 0x01; //产生起始位
/* Test on EV5 and clear it */
//while(!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));
while(!(I2C_SR1 & 0x01)); //起始位发送完成
/* Send slave Address in write direction & wait detection event */
// I2C_Send7bitAddress(EEPROM_ADDRESS, I2C_DIRECTION_TX);
I2C_DR = EEPROM_ADDRESS;
place=1;
//while(!I2C_GetFlagStatus(I2C_FLAG_ADDRESSSENTMATCHED));
//I2C_ClearFlag(I2C_FLAG_ADDRESSSENTMATCHED);
while(!(I2C_SR1&0x02));
temp = I2C_SR1;
temp = I2C_SR3;
place=2;
/* Test on EV6 and clear it */
//while(!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
//I2C_ClearFlag(I2C_FLAG_ADDRESSSENTMATCHED);
/* Send Address of first byte to be read & wait event detection */
// I2C_SendData((u8)(ReadAddr >> 8)); /* MSB */
I2C_DR = ((u8)(ReadAddr >> 8));
/* Test on EV8 and clear it */
// while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED));
while(!(I2C_SR1 & 0x84));
// I2C_SendData((u8)(ReadAddr)); /* LSB */
I2C_DR = ((u8)(ReadAddr));
/* Test on EV8 and clear it */
// while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED));
while(!(I2C_SR1 & 0x84));
/* Send STRAT condition a second time */
// I2C_GenerateSTART(ENABLE);
I2C_CR2 |= 0x01; //产生起始位
/* Test on EV5 and clear it */
// while(!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));
while(!(I2C_SR1 & 0x01)); //起始位发送完成
/* Send slave Address in read direction & wait event */
// I2C_Send7bitAddress(EEPROM_ADDRESS, I2C_DIRECTION_RX);
I2C_DR = EEPROM_READ_ADDRESS;
//while(!I2C_GetFlagStatus(I2C_FLAG_ADDRESSSENTMATCHED));
//I2C_ClearFlag(I2C_FLAG_ADDRESSSENTMATCHED);
while(!(I2C_SR1&0x02));
temp = I2C_SR1;
temp = I2C_SR3;
/* Test on EV6 and clear it */
// while(!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
//I2C_ClearFlag(I2C_FLAG_ADDRESSSENTMATCHED);
/* While there is data to be read */
while(NumByteToRead)
{
numb=NumByteToRead;
if(NumByteToRead == 1)
{
numb=NumByteToRead;
NumByteToRead--;
/* Disable Acknowledgement */
//I2C_AcknowledgeConfig(I2C_ACK_NONE);
I2C_CR2 &=~0x04;
/* Send STOP Condition */
//I2C_GenerateSTOP(ENABLE);
I2C_CR2 |=0x02;
I2C_CR2 &=0xf7;
while(!(I2C_SR1&0x40));
*pBuffer = I2C_DR;
I2C_CR2 |=0x04;
}
//* Test on EV7 and clear it */
//if(I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_RECEIVED))
if(NumByteToRead > 1)
{
//I2C_CR2 |=0x04;
I2C_CR2 &=0xfd;
I2C_CR2 |=0x08;
/* Read a byte from the EEPROM */
//*pBuffer = I2C_ReceiveData();
while(!(I2C_SR1&0x40));
temp = I2C_SR1;
*pBuffer = I2C_DR;
I2C_CR2 |=0x04;
/* Point to the next location where the byte read will be saved */
pBuffer++;
/* Decrement the read bytes counter */
NumByteToRead--;
RE:STM8S硬件I2C读数据有问题
{
unsigned char temp;
numb=NumByteToRead;
/* While the bus is busy */
//while(I2C_GetFlagStatus(I2C_FLAG_BUSBUSY));
while(I2C_SR3 & 0x02); //总线空闲
/* Generate start & wait event detection */
// I2C_GenerateSTART(ENABLE);
I2C_CR2 |= 0x01; //产生起始位
/* Test on EV5 and clear it */
//while(!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));
while(!(I2C_SR1 & 0x01)); //起始位发送完成
/* Send slave Address in write direction & wait detection event */
// I2C_Send7bitAddress(EEPROM_ADDRESS, I2C_DIRECTION_TX);
I2C_DR = EEPROM_ADDRESS;
place=1;
//while(!I2C_GetFlagStatus(I2C_FLAG_ADDRESSSENTMATCHED));
//I2C_ClearFlag(I2C_FLAG_ADDRESSSENTMATCHED);
while(!(I2C_SR1&0x02));
temp = I2C_SR1;
temp = I2C_SR3;
place=2;
/* Test on EV6 and clear it */
//while(!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
//I2C_ClearFlag(I2C_FLAG_ADDRESSSENTMATCHED);
/* Send Address of first byte to be read & wait event detection */
// I2C_SendData((u8)(ReadAddr >> 8)); /* MSB */
I2C_DR = ((u8)(ReadAddr >> 8));
/* Test on EV8 and clear it */
// while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED));
while(!(I2C_SR1 & 0x84));
// I2C_SendData((u8)(ReadAddr)); /* LSB */
I2C_DR = ((u8)(ReadAddr));
/* Test on EV8 and clear it */
// while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED));
while(!(I2C_SR1 & 0x84));
/* Send STRAT condition a second time */
// I2C_GenerateSTART(ENABLE);
I2C_CR2 |= 0x01; //产生起始位
/* Test on EV5 and clear it */
// while(!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));
while(!(I2C_SR1 & 0x01)); //起始位发送完成
/* Send slave Address in read direction & wait event */
// I2C_Send7bitAddress(EEPROM_ADDRESS, I2C_DIRECTION_RX);
I2C_DR = EEPROM_READ_ADDRESS;
//while(!I2C_GetFlagStatus(I2C_FLAG_ADDRESSSENTMATCHED));
//I2C_ClearFlag(I2C_FLAG_ADDRESSSENTMATCHED);
while(!(I2C_SR1&0x02));
temp = I2C_SR1;
temp = I2C_SR3;
/* Test on EV6 and clear it */
// while(!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
//I2C_ClearFlag(I2C_FLAG_ADDRESSSENTMATCHED);
/* While there is data to be read */
while(NumByteToRead)
{
numb=NumByteToRead;
if(NumByteToRead == 1)
{
numb=NumByteToRead;
NumByteToRead--;
/* Disable Acknowledgement */
//I2C_AcknowledgeConfig(I2C_ACK_NONE);
I2C_CR2 &=~0x04;
/* Send STOP Condition */
//I2C_GenerateSTOP(ENABLE);
I2C_CR2 |=0x02;
I2C_CR2 &=0xf7;
while(!(I2C_SR1&0x40));
*pBuffer = I2C_DR;
I2C_CR2 |=0x04;
}
//* Test on EV7 and clear it */
//if(I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_RECEIVED))
if(NumByteToRead > 1)
{
//I2C_CR2 |=0x04;
I2C_CR2 &=0xfd;
I2C_CR2 |=0x08;
/* Read a byte from the EEPROM */
//*pBuffer = I2C_ReceiveData();
while(!(I2C_SR1&0x40));
temp = I2C_SR1;
*pBuffer = I2C_DR;
I2C_CR2 |=0x04;
/* Point to the next location where the byte read will be saved */
pBuffer++;
/* Decrement the read bytes counter */
NumByteToRead--;
}
RE:STM8S硬件I2C读数据有问题
回复:STM8S硬件I2C读数据有问题
RE:STM8S硬件I2C读数据有问题
I2C_CR2 &=0xfd;I2C_CR2 &= ~0x08;,结帖