各位大侠,我用STM8S103做从机,用的硬件I2C,根据官网的I2C进行修改
里面 有些地方不理解,希望得到帮助
#define MAX_BUFFER 32
u8 u8_My_Buffer[MAX_BUFFER];
u8 *u8_MyBuffp;
u8 MessageBegin;
void I2C_byte_received(u8 u8_RxData) //接收数据的子函数
{
if (MessageBegin == TRUE && u8_RxData < MAX_BUFFER)
{ //从机地址匹配加上 u8_RxData <32,这里的 u8_RxData是从I2C->DR得到的数据
u8_MyBuffp= &u8_My_Buffer[u8_RxData]; //u8_RxData地址放到 u8_u8_MyBuffp中
MessageBegin = FALSE;
}
else if(u8_MyBuffp < &u8_My_Buffer[MAX_BUFFER]) // 这里的else if 是怎么回事?有点不懂啊
*(u8_MyBuffp++) = u8_RxData;
}
u8 I2C_byte_write(void)
{
if (u8_MyBuffp < &u8_My_Buffer[MAX_BUFFER]) //写的数据都放到u8_MyBuffp中?然后地址依次后移??
return *(u8_MyBuffp++);
else
return 0x00;
/* More bytes received ? */
if ((sr1 & (I2C_SR1_RXNE | I2C_SR1_BTF)) == (I2C_SR1_RXNE | I2C_SR1_BTF))
{
I2C_byte_received(I2C->DR);
}
/* Byte received ? */
if (sr1 & I2C_SR1_RXNE)
{
I2C_byte_received(I2C->DR);
}
/* More bytes to transmit ? */
if ((sr1 & (I2C_SR1_TXE | I2C_SR1_BTF)) == (I2C_SR1_TXE | I2C_SR1_BTF))
{
I2C->DR = I2C_byte_write();
}
/* Byte to transmit ? */
if (sr1 & I2C_SR1_TXE)
{
I2C->DR = I2C_byte_write();
}
GPIOD->ODR^=1;
}
还有上面的 接收/发送数据 ,那个更多的数据/接收 是怎么回事呢?不理解??本人菜鸟,刚接触,望各位大神多多指点,不吝赐教
} |