给你贴一个我读取数字温度传感器ADT7410的程序
/* init i2c */
void Bsp_IIC_Config(void)
{
GPIO_Init(GPIOE,
GPIO_PIN_1|GPIO_PIN_2,
GPIO_MODE_OUT_PP_HIGH_SLOW); //push-pull high output.2MHz.
CLK->PCKENR1 |= 0x01; //open i2c clk.
I2C_DeInit();
I2C_Init(100000, //100khz.
0xa0, //address.
I2C_DUTYCYCLE_2, //50% duty cycle.
I2C_ACK_CURR,
I2C_ADDMODE_7BIT,
16); //16MHz input clock.
}
void IIC_TEMP(void)
{
uint16_t Timeout;
//While the bus is busy,wait it.
Timeout = 0x1000;
while(I2C_GetFlagStatus( I2C_FLAG_BUSBUSY))
{
if((Timeout--) == 0)
{
nop();
}
}
//Send START condition.
I2C_GenerateSTART(ENABLE);
// Test on EV5 and clear it (cleared by reading SR1 then writing to DR)
Timeout = 0x1000;
while(!I2C_CheckEvent( I2C_EVENT_MASTER_MODE_SELECT))
{
if((Timeout--) == 0)
{
nop();
}
}
// Send sensor address for write.
I2C_Send7bitAddress( (uint8_t)0x90, I2C_DIRECTION_TX);
// Test on EV6 and clear it.
Timeout = 0x1000;
while(!I2C_CheckEvent( I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
{
if((Timeout--) == 0)
{
nop();
}
}
//Send the register address.
I2C_SendData((uint8_t)0x00);
// Test on EV8 and clear it.
Timeout = 0x1000;
while(!I2C_CheckEvent( I2C_EVENT_MASTER_BYTE_TRANSMITTED))
{
if((Timeout--) == 0)
{
nop();
}
}
// Send START condition.
I2C_GenerateSTART(ENABLE);
// Test on EV5 and clear it (cleared by reading SR1 then writing to DR).
Timeout = 0x1000;
while(!I2C_CheckEvent( I2C_EVENT_MASTER_MODE_SELECT))
{
if((Timeout--) == 0)
{
nop();
}
}
// Send sensor address for read
I2C_Send7bitAddress((uint8_t)0x90, I2C_DIRECTION_RX);
// Test on EV6 and clear it.
Timeout = 0x1000;
while(!I2C_CheckEvent(I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
{
if((Timeout--) == 0)
{
nop();
}
}
// Test on EV7 and clear it.
Timeout = 0x1000;
while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_RECEIVED))
{
if((Timeout--) == 0)
{
nop();
}
}
// Read temperature high byte from the sensor.
CanTX[1] = I2C_ReceiveData();
// Disable Acknowledgement
I2C_AcknowledgeConfig(I2C_ACK_NONE);
IIC_Delay(1); //at least delay 9*10us.
// Read temperature low byte from the sensor.
CanTX[2] = I2C_ReceiveData();
// Program the STOP
I2C_GenerateSTOP(ENABLE);
// eable Acknowledgement
I2C_AcknowledgeConfig(I2C_ACK_CURR);
CanTX[0] = 0x35;
CAN_Transmit(0x55a,
CAN_Id_Standard,
CAN_RTR_Data,
3,
CanTX);
}
/* delay 0.2ms*times */
void IIC_Delay(uint8_t times)
{
volatile uint8_t dly;
while(times--)
{
for(dly=220; dly>0; dly--);
for(dly=220; dly>0; dly--);
for(dly=220; dly>0; dly--);
}
}
|