void I2C_write(u8 Addr, u8 *WriteData, u8 length){ /* Send STRAT condition */ I2C_GenerateSTART(ENABLE); /* Test on EV5 and clear it */ while(!I2C_CheckEvent(I2C_EVENT_MASTER_START_SENT)); /* Send EEPROM address for write */ I2C_Send7bitAddress(Addr, I2C_DIRECTION_TX); /* Test on EV6 and clear it */ while(!I2C_CheckEvent(I2C_EVENT_MASTER_ADDRESS_ACKED)); I2C_ClearFlag(I2C_FLAG_ADDRESSSENTMATCHED);// must add for(int i = 0; iSR3&0x02); I2C->CR2 |= 0x01; while(!(I2C->SR1 & 0x01)); I2C->DR = ReadAddr; while(!(I2C->SR1 & 0x02)); I2C->SR1; I2C->SR3; while(NumByteToRead) { if(NumByteToRead == 1) { I2C->CR2 &= ~0x04; I2C->CR2 |= 0x02; } if(I2C->SR1 & 0x40) { *pBuffer=I2C->DR; pBuffer++; NumByteToRead--; } } I2C->CR2 |= 0x04;}
有其他选择千万不要选择STM8,太难用了。