我有些代码,可以读LM75,仅供参考(不保证能行):
#define LM75_Addr 0x90 // 7-bit address of LM75: left align to MSbit
#define LM75_Temp_Reg 0x00 // Temperature Register of LM75
#define LM75_Conf_Reg 0x01 // Configuration Register of LM75
#define LM75_Thyst_Reg 0x02 // Temperature Register of LM75
#define LM75_Tos_Reg 0x03 // Over-temp Shutdown threshold Register of LM75
#define Start_Bit 0x01
#define Address_Sent 0x02
#define BTF_Bit 0x04
#define Rx_Not_Empty 0x40
#define Ack_Failure 0x0400
#define Time_Out 0x4000
//-----------------------------------------------------------------
// I2C_LM75(I2C1 of 1~2) initialization
//-----------------------------------------------------------------
void I2C_LM75_Init(void)
{
I2C_InitTypeDef I2C_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
// Enable I2C1 and GPIOB & AFEN(Alternate Function) clocks
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
// Reset I2C1 IP
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
// Release reset signal of I2C1 IP
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);
// Set PB6,7 as OD AF - I2C1_SCL, I2C1_SDA
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; // standard: 100k, fast: 400k
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// Set PB5 as PU in - TemperatureSensor_INT
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// I2C1 configuration
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_ClockSpeed = 50000; // 50k baud
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x00; // 7-bit own-address: right align to LSbit
I2C_InitStructure.I2C_Ack = I2C_Ack_Disable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_Init(I2C1, &I2C_InitStructure);
// Enable I2C1
I2C_Cmd(I2C1, ENABLE);
}
//-----------------------------------------------------------------
// Read Tos register of LM75
//-----------------------------------------------------------------
u16 I2C_LM75_Tos_Read(void)
{
u16 u;
u = 0xffff; // init an invalid value
// Generate START condition
I2C_GenerateSTART(I2C1, ENABLE);
while( !(I2C_ReadRegister(I2C1, I2C_Register_SR1) & Start_Bit) )
;
// Send 7bit Address
I2C_Send7bitAddress(I2C1, LM75_Addr, I2C_Direction_Transmitter);
while( !(I2C_ReadRegister(I2C1, I2C_Register_SR1) & Address_Sent) )
{
if( I2C_ReadRegister(I2C1, I2C_Register_SR1) & Ack_Failure )
{
I2C_GenerateSTOP(I2C1, ENABLE);
return u;
}
}
// Read SR2 to clear ADDR bit in SR1
I2C_ReadRegister(I2C1, I2C_Register_SR2);
// Send data: pointer (Tos_Reg)
I2C_SendData(I2C1, LM75_Tos_Reg);
while( !(I2C_ReadRegister(I2C1, I2C_Register_SR1) & BTF_Bit) )
{
if( I2C_ReadRegister(I2C1, I2C_Register_SR1) & (Time_Out | Ack_Failure) )
{
I2C_GenerateSTOP(I2C1, ENABLE);
return u;
}
}
// Generate STOP condition
I2C_GenerateSTOP(I2C1, ENABLE);
// Generate START condition
I2C_GenerateSTART(I2C1, ENABLE);
while( !(I2C_ReadRegister(I2C1, I2C_Register_SR1) & Start_Bit) )
;
// Send 7bit Address
I2C_Send7bitAddress(I2C1, LM75_Addr, I2C_Direction_Receiver);
while( !(I2C_ReadRegister(I2C1, I2C_Register_SR1) & Address_Sent) )
{
if( I2C_ReadRegister(I2C1, I2C_Register_SR1) & Ack_Failure )
{
I2C_GenerateSTOP(I2C1, ENABLE);
return u;
}
}
// Read SR2 to clear ADDR bit in SR1
I2C_ReadRegister(I2C1, I2C_Register_SR2);
// Receive data: high byte (Tos_Reg) and ACK
I2C_AcknowledgeConfig(I2C1, ENABLE);
while( !(I2C_ReadRegister(I2C1, I2C_Register_SR1) & Rx_Not_Empty) )
;
u = I2C_ReceiveData(I2C1) << 8;
// Receive data: low byte (Tos_Reg) and NAK
I2C_AcknowledgeConfig(I2C1, DISABLE);
while( !(I2C_ReadRegister(I2C1, I2C_Register_SR1) & Rx_Not_Empty) )
;
u |= I2C_ReceiveData(I2C1);
I2C_GenerateSTOP(I2C1, ENABLE);
// return correct result
return u;
}
//-----------------------------------------------------------------
// Read Temperature register of LM75
//-----------------------------------------------------------------
u16 I2C_LM75_Temp_Read(void)
{
u16 u;
u = 0xffff; // init an invalid value
// Generate START condition
I2C_GenerateSTART(I2C1, ENABLE);
while( !(I2C_ReadRegister(I2C1, I2C_Register_SR1) & Start_Bit) )
;
// Send 7bit Address
I2C_Send7bitAddress(I2C1, LM75_Addr, I2C_Direction_Transmitter);
while( !(I2C_ReadRegister(I2C1, I2C_Register_SR1) & Address_Sent) )
;
// Read SR2 to clear ADDR bit in SR1
I2C_ReadRegister(I2C1, I2C_Register_SR2);
// Send data: pointer (Temp_Reg)
I2C_SendData(I2C1, LM75_Temp_Reg);
while( !(I2C_ReadRegister(I2C1, I2C_Register_SR1) & BTF_Bit) )
;
// Generate STOP condition
I2C_GenerateSTOP(I2C1, ENABLE);
// Generate START condition
I2C_GenerateSTART(I2C1, ENABLE);
while( !(I2C_ReadRegister(I2C1, I2C_Register_SR1) & Start_Bit) )
;
// Send 7bit Address
I2C_Send7bitAddress(I2C1, LM75_Addr, I2C_Direction_Receiver);
while( !(I2C_ReadRegister(I2C1, I2C_Register_SR1) & Address_Sent) )
;
// Read SR2 to clear ADDR bit in SR1
I2C_ReadRegister(I2C1, I2C_Register_SR2);
// Receive data: high byte (Temp_Reg) and ACK
I2C_AcknowledgeConfig(I2C1, ENABLE);
while( !(I2C_ReadRegister(I2C1, I2C_Register_SR1) & Rx_Not_Empty) )
;
u = I2C_ReceiveData(I2C1) << 8;
// Receive data: low byte (Temp_Reg) and NAK
I2C_AcknowledgeConfig(I2C1, DISABLE);
while( !(I2C_ReadRegister(I2C1, I2C_Register_SR1) & Rx_Not_Empty) )
;
u |= I2C_ReceiveData(I2C1);
I2C_GenerateSTOP(I2C1, ENABLE);
// return correct result - Temp_Reg low 5 bits have no meaning
return (u>>5);
}