标签:
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; i<length; i++) { I2C_SendData(WriteData[i]); /* Test on EV8 and clear it */ while (!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTING)); } /* Send STOP condition */ I2C_GenerateSTOP(ENABLE);
} void I2C_Read(u8* pBuffer, u8 ReadAddr, u8 NumByteToRead) { while(I2C->SR3&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,太难用了。
标签:
原文地址:http://www.cnblogs.com/cjyszbd/p/5131701.html