刚从别的行业转到这个行业做软件,除了有点乐趣外,感觉在这个行业混好难
才干着行3个月, 代码写的垃圾 凑活看吧,不喜欢的 不要喷啊,我就是刚开始找这方面资料的时候一点也没找到
这个是 stm8l052r8 的硬件i2c 和 温度芯片TMP102 通讯 (写温度转换操作 和 读温度数据)一部分寄存器的还没找到库函数代码 就用寄存器代替了
过程中遇到的问题: 初始化后 最后读完数据 如果不写 I2C_Cmd(I2C1,DISABLE) 不能连续读 寄存器显示 好多寄存器没完全复位
有人能帮忙改正下 下面的寄存器 改成那个库函数 或者交给下方法也行
void InitIIC(void)
{
CLK_PeripheralClockConfig(CLK_Peripheral_I2C1,ENABLE );
I2C_DeInit(I2C1);
I2C_Init(I2C1,100000,0x30,I2C_Mode_I2C,I2C_DutyCycle_2,I2C_Ack_Enable,I2C_AcknowledgedAddress_7bit);
I2C_Cmd(I2C1,ENABLE);
}
state_tmp102 tmp102_tmpConversion()
{
unsigned int x = 0xffff;
unsigned char temp;
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY))
{
x -- ;
if(x <= 1)
return ERROR_TMP102;
}
I2C_GenerateSTART(I2C1,ENABLE); //起始位
x = 0xffff;
while(!I2C_GetFlagStatus(I2C1,I2C_FLAG_SB))
{
x -- ;
if(x <= 1)
return ERROR_TMP102;
}
I2C_Send7bitAddress(I2C1,0x90,I2C_Direction_Transmitter); //从地址 0x90
x = 0xffff;
while(!I2C_GetFlagStatus(I2C1,I2C_FLAG_ADDR)) //地址发送完成
{
x -- ;
if(x <= 1)
return ERROR_TMP102;
}
temp = (unsigned char )I2C1->SR1;
temp = (unsigned char )I2C1->SR3;
I2C_SendData(I2C1,0x01); //发送 指针 0x01
x = 0xffff;
while(!I2C_GetFlagStatus(I2C1,I2C_FLAG_TXE))
{
x -- ;
if(x <= 1)
return ERROR_TMP102;
}
I2C_SendData(I2C1,0xe0); //配置寄存器
x = 0xffff;
while(!I2C_GetFlagStatus(I2C1,I2C_FLAG_TXE))
{
x -- ;
if(x <= 1)
return ERROR_TMP102;
}
I2C_SendData(I2C1,0xa0);
x = 0xffff;
while(!I2C_GetFlagStatus(I2C1,I2C_FLAG_TXE))
{
x -- ;
if(x <= 1)
return ERROR_TMP102;
}
I2C_GenerateSTOP(I2C1,ENABLE);
return SUCCESS_TMP102;
}
state_tmp102 tmp102_read(unsigned int *byte)
{
if(tmp102_tmpConversion() == ERROR_TMP102)
{
return ERROR_TMP102;
}
unsigned int x = 0xffff;
while(x --);
unsigned int byte1 = 0,byte2 = 0;
unsigned char temp;
x = 0xffff;
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY))
{
x -- ;
if(x <= 1)
return ERROR_TMP102;
}
I2C_GenerateSTART(I2C1,ENABLE); //起始位
x = 0xffff;
while(!I2C_GetFlagStatus(I2C1,I2C_FLAG_SB))
{
x -- ;
if(x <= 1)
return ERROR_TMP102;
}
I2C_Send7bitAddress(I2C1,0x90,I2C_Direction_Transmitter); //从地址 0x90
x= 0xffff;
while(!I2C_GetFlagStatus(I2C1,I2C_FLAG_ADDR)) //地址发送完成
{
x -- ;
if(x <= 1)
return ERROR_TMP102;
}
temp = (unsigned char )I2C1->SR1;
temp = (unsigned char )I2C1->SR3;
I2C_SendData(I2C1,0x00); //发送 指针 0x00
x = 0xffff;
while(!I2C_GetFlagStatus(I2C1,I2C_FLAG_TXE))
{
x -- ;
if(x <= 1)
return ERROR_TMP102;
}
I2C_GenerateSTOP(I2C1,ENABLE);
x = 0xffff;
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY))
{
x -- ;
if(x <= 1)
return ERROR_TMP102;
}
I2C_GenerateSTART(I2C1,ENABLE); //起始位
x = 0xffff;
while(!I2C_GetFlagStatus(I2C1,I2C_FLAG_SB))
{
x -- ;
if(x <= 1)
return ERROR_TMP102;
}
I2C_Send7bitAddress(I2C1,0x91,I2C_Direction_Receiver);
x= 0xffff;
while(!I2C_GetFlagStatus(I2C1,I2C_FLAG_ADDR)); //地址发送完成
{
x -- ;
if(x <= 1)
return ERROR_TMP102;
}
temp = (unsigned char )I2C1->SR1;
temp = (unsigned char )I2C1->SR3;
x = 0xffff;
while(!I2C_GetFlagStatus(I2C1,I2C_FLAG_RXNE))
{
x -- ;
if(x <= 1)
return ERROR_TMP102;
}
byte1 = (unsigned char)I2C_ReceiveData(I2C1);
x = 0xffff;
while(!I2C_GetFlagStatus(I2C1,I2C_FLAG_RXNE))
{
x -- ;
if(x <= 1)
return ERROR_TMP102;
}
byte2 = (unsigned char)I2C_ReceiveData(I2C1);
I2C_AcknowledgeConfig(I2C1,DISABLE);
I2C_GenerateSTOP(I2C1,ENABLE);
*byte = (byte1 << 8) | byte2;
*byte = *byte >>4;
return SUCCESS_TMP102;
}
|
|