📄 dy4max6634.c
字号:
status = dy4I2cMasterRead(I2C_TEMP_SENS, I2C_TEMP1_ADDR + sensId, DY4THERMAL_THIGH_REG, &value[0], 2); dy4ThermalDelay(); timeoutValue++; } /* */ temp = ((((unsigned int)value[0]) << 8) | ((unsigned int)value[1])); if(( temp & 0x8000 ) != 0 ) { temp = ((temp & 0x7F80) >> DY4THERMAL_TEMP_SHIFT); temp = (0 - (256 - 1)) + temp; } else { temp = ((temp & 0x7F80) >> DY4THERMAL_TEMP_SHIFT); } *data = temp; DEBUGP(("DEB:dy4ThermalGetHighTemp:Read Thigh as %d for sensor %d\n", temp, sensId)); return( OK ); }/******************************************************************************** dy4ThermalSetHighTemp - Set the High temperature warning value** DESCRIPTION: Takes sensor ID and integer value for the maximum permitted temp* and sets this value into the register* INPUT:* sensor ID, integer value for maximum temperature** RETURN:* OK if successful or ERRROR if fails to write to the sensor register*******************************************************************************/STATUS dy4ThermalSetHighTemp ( DY4THERMAL_ID sensId, int temp ) { unsigned char data[2]; int value, sign = 0; volatile STATUS status = ERROR; unsigned int timeoutValue = 0; if( sensId >= DY4THERMAL_MAX ) { DEBUGEP(("ERR:dy4ThermalSetHighTemp:Error, invalid sensor id %d\n",sensId)); return( ERROR ); } DEBUGP(("DEB:dy4ThermalSetHighTemp:Setting Thigh to %d for sensor %d\n", temp, sensId)); if( temp < 0 ) { sign = 0x8000; } value = (((temp & 0xff) << DY4THERMAL_TEMP_SHIFT) | sign); data[1] = (value & 0xff); /* convert word to bytes */ data[0] = ((value >> 8) & 0xFF); /* convert word to bytes */ /* */ while (status != STAT_I2C_OK) { if(timeoutValue >= 10) { DEBUGEP(("ERR:dy4ThermalSetHighTemp:Error, could not write Thigh reg for sensor %d\n",sensId)); return( ERROR ); } status = dy4I2cMasterWrite(I2C_TEMP_SENS, I2C_TEMP1_ADDR + sensId, DY4THERMAL_THIGH_REG, &data[0], 2); dy4ThermalDelay(); timeoutValue++; } /* */ return( OK ); } /******************************************************************************** dy4ThermalGetLowTemp - Get the Low temperature warning value** DESCRIPTION: Takes sensor ID and pointer to storage location, retreives the* lowest permitted temperature value from the registers, stores at pointer* INPUT:* sensor ID, pointer to storage location** RETURN:* OK if successful or ERROR if fault occurs in reading of register*******************************************************************************/STATUS dy4ThermalGetLowTemp ( DY4THERMAL_ID sensId, int *data ) { unsigned char value[2]; int temp; volatile STATUS status = ERROR; unsigned int timeoutValue = 0; if( sensId >= DY4THERMAL_MAX ) { DEBUGEP(("ERR:dy4ThermalGetLowTemp:Error, invalid sensor id %d\n",sensId)); return( ERROR ); } /* */ while (status != STAT_I2C_OK) { if(timeoutValue >= 10) { DEBUGEP(("ERR:dy4ThermalGetLowTemp:Error, could not read Tlow reg for sensor %d\n",sensId)); return (ERROR); } status = dy4I2cMasterRead(I2C_TEMP_SENS, I2C_TEMP1_ADDR + sensId, DY4THERMAL_TLOW_REG, &value[0], 2); dy4ThermalDelay(); timeoutValue++; } /* */ temp = ((((unsigned int)value[0]) << 8) | ((unsigned int)value[1])); if(( temp & 0x8000 ) != 0 ) { temp = ((temp & 0x7F80) >> DY4THERMAL_TEMP_SHIFT); temp = (0 - (256 - 1)) + temp; } else { temp = ((temp & 0x7F80) >> DY4THERMAL_TEMP_SHIFT); } *data = temp; DEBUGP(("DEB:dy4ThermalGetLowTemp: Read Tlow as %d for sensor %d\n", temp, sensId)); return( OK ); }/******************************************************************************** dy4ThermalSetLowTemp - Set the Low temperature warning value** DESCRIPTION: Takes sensor ID and integer value of lowest permitted temperature* which is then stored into the sensor register* INPUT:* sensor ID, integer value of lowest permitted temperature** RETURN:* OK if successful or ERROR if fault occurs in writing to the register*******************************************************************************/STATUS dy4ThermalSetLowTemp ( DY4THERMAL_ID sensId, int temp ) { unsigned char data[2]; int value, sign = 0; volatile STATUS status = ERROR; unsigned int timeoutValue = 0; if( sensId >= DY4THERMAL_MAX ) { DEBUGEP(("ERR:dy4ThermalSetLowTemp:Error, invalid sensor id %d\n",sensId)); return( ERROR ); } DEBUGP(("DEB:dy4ThermalSetLowTemp: Setting Tlow to %d for sensor %d\n", temp, sensId)); if( temp < 0 ) { sign = 0x8000; } value = (((temp & 0xff) << DY4THERMAL_TEMP_SHIFT) | sign); data[1] = (value & 0xff); /* convert word to bytes */ data[0] = ((value >> 8) & 0xFF); /* convert word to bytes */ /* */ while (status != STAT_I2C_OK) { if(timeoutValue >= 10) { DEBUGEP(("ERR:dy4ThermalSetLowTemp:Error, could not write Tlow reg for sensor %d\n",sensId)); return( ERROR ); } status = dy4I2cMasterWrite(I2C_TEMP_SENS, I2C_TEMP1_ADDR + sensId, DY4THERMAL_TLOW_REG, &data[0], 2); dy4ThermalDelay(); timeoutValue++; } /* */ return( OK ); } /******************************************************************************** dy4ThermalGetHystTemp - Get the Hysterisis temperature warning value** DESCRIPTION: Takes sensor ID and pointer to storage location and retreives the* hysterisis temperature warning value stored in the sensor's register* INPUT:* sensor ID, pointer to storage location** RETURN:* OK if successful or ERROR if fault occurs in reading of sensor register*******************************************************************************/STATUS dy4ThermalGetHystTemp ( DY4THERMAL_ID sensId, int *data ) { unsigned char value[2]; int temp; volatile STATUS status = ERROR; unsigned int timeoutValue = 0; if( sensId >= DY4THERMAL_MAX ) { DEBUGEP(("ERR:dy4ThermalGetHystTemp:Error, invalid sensor id %d\n",sensId)); return( ERROR ); } /* */ while (status != STAT_I2C_OK) { if(timeoutValue >= 10) { DEBUGEP(("ERR:dy4ThermalGetHystTemp:Error, could not read Thyst reg for sensor %d\n",sensId)); return( ERROR ); } status = dy4I2cMasterRead(I2C_TEMP_SENS, I2C_TEMP1_ADDR + sensId, DY4THERMAL_THYST_REG, &value[0], 2); dy4ThermalDelay(); timeoutValue++; } /* */ temp = ((((unsigned int)value[0]) << 8) | ((unsigned int)value[1])); if(( temp & 0x8000 ) != 0 ) { temp = ((temp & 0x7F80) >> DY4THERMAL_TEMP_SHIFT); temp = (0 - (256 - 1)) + temp; } else { temp = ((temp & 0x7F80) >> DY4THERMAL_TEMP_SHIFT); } *data = temp; DEBUGP(("DEB:dy4ThermalGetHystTemp:Read Thyst as %d for sensor %d\n", temp, sensId)); return( OK ); }/******************************************************************************** dy4ThermalSetHystTemp - Set the Hysterisis temperature warning value** DESCRIPTION: Takes sensor ID and integer value of maximum permitted hysterisis* temperature value* INPUT:* sensor ID, integer value of maximum temperature permitted** RETURN:* OK if successful or ERROR if fault occurs in reading of sensor register*******************************************************************************/STATUS dy4ThermalSetHystTemp ( DY4THERMAL_ID sensId, int temp ) { unsigned char data[2]; int value, sign = 0; volatile STATUS status = ERROR; unsigned int timeoutValue = 0; if( sensId >= DY4THERMAL_MAX ) { DEBUGEP(("ERR:dy4ThermalSetHystTemp:Error, invalid sensor id %d\n",sensId)); return( ERROR ); } DEBUGP(("DEB:dy4ThermalSetHystTemp:Setting Thyst to %d for sensor %d\n", temp, sensId)); if( temp < 0 ) { sign = 0x8000; } value = (((temp & 0xff) << DY4THERMAL_TEMP_SHIFT) | sign); data[1] = (value & 0xff); /* convert word to bytes */ data[0] = ((value >> 8) & 0xFF); /* convert word to bytes */ /* */ while (status != STAT_I2C_OK) { if(timeoutValue >= 10) { DEBUGEP(("ERR:dy4ThermalSetHystTemp:Error, could not write Thyst reg for sensor %d\n",sensId)); return( ERROR ); } status = dy4I2cMasterWrite(I2C_TEMP_SENS, I2C_TEMP1_ADDR + sensId, DY4THERMAL_THYST_REG, &data[0], 2); dy4ThermalDelay(); timeoutValue++; } /* */ return( OK ); }/******************************************************************************** dy4ThermalConvertTemp - Convert reading data to Celsius or Fahrenheit unit ** DESCRIPTION: Takes temperature sensor unit ( C or F) and the temperature value* and converts it to the unit type passed* INPUT:* temperature unit desired, data to be converted** RETURN:* converted temperature value*******************************************************************************/LOCAL int dy4ThermalConvertTemp ( DY4THERMAL_UNIT unit, int data ) { int value, sign; /* * The data format of the 16 bit temp reg is: * MSB sign bit (above or below 0C) * bits 14-3 temp data, 8 bits integer, 4 fixed point bits. * bit 2 reserved * bit 1 T-High status * bit 0 T-Low status */ sign = data & 0x8000; value = (data & 0x7ff8) >> 3; if( sign != 0 ) { value = (0 - (4096 - 1)) + value; } /* convert reading to proper temperature unit */ if (unit == DY4THERMAL_FAHRENHEIT) { /* Fahrenheit, scale offset value for fixed point math */ value = ((value * 9) / 5) + (32 * 16); value /= 16; /* convert to integer */ return value; } else { value /= 16; /* convert to integer */ return value; } }/******************************************************************************** dy4ThermalGetTemp - Get temperature** DESCRIPTION: Takes sensor ID and temp unit ( C or F) and returns the temperature* after being converted to the passed unit type* INPUT:* sensor ID, temperature unit** RETURN:* converted temp value (in unit specified by input)*******************************************************************************/UINT32 dy4ThermalGetTemp ( DY4THERMAL_ID sensId, DY4THERMAL_UNIT unit ) { unsigned char value[2]; volatile STATUS status = ERROR; unsigned int timeoutValue = 0; if( sensId >= DY4THERMAL_MAX ) { DEBUGEP(("ERR:dy4ThermalReadTemp:Error, invalid sensor id %d\n",sensId)); return (UINT32) ERROR; } /* */ while (status != STAT_I2C_OK) { if(timeoutValue >= 10) { DEBUGEP(("ERR:dy4ThermalReadTemp:Error, could not read temp sensor %d\n",sensId)); return (UINT32) ERROR; } status = dy4I2cMasterRead(I2C_TEMP_SENS, I2C_TEMP1_ADDR + sensId, DY4THERMAL_TEMP_REG, &value[0], 2); dy4ThermalDelay(); timeoutValue++; } /* */ return( dy4ThermalConvertTemp(unit, (((unsigned int)value[0]) << 8) | ((unsigned int)value[1]) )); }/******************************************************************************** dy4ThermalReadTemp - Read temperature ** DESCRIPTION: Takes sensor ID, temp unit, and pointer to storage location and* retreives temperature, converts to desired units, and stores at pointer* INPUT:* sensor ID, temp unit, pointer to storage location** RETURN:* OK if successfull or ERROR if fault occurs in reading of register*******************************************************************************/STATUS dy4ThermalReadTemp ( DY4THERMAL_ID sensId, DY4THERMAL_UNIT unit, int *data ) { unsigned char value[2]; volatile STATUS status = ERROR; unsigned int timeoutValue = 0; if( sensId >= DY4THERMAL_MAX ) { DEBUGEP(("ERR:dy4ThermalReadTemp:Error, invalid sensor id %d\n",sensId)); return( ERROR ); } /* */ while (status != STAT_I2C_OK) { if(timeoutValue >= 10) { DEBUGEP(("ERR:dy4ThermalReadTemp:Error, could not read temp sensor %d\n",sensId)); return( ERROR ); } status = dy4I2cMasterRead(I2C_TEMP_SENS, I2C_TEMP1_ADDR + sensId, DY4THERMAL_TEMP_REG, &value[0], 2); dy4ThermalDelay(); timeoutValue++; } /* */ *data = dy4ThermalConvertTemp(unit, (((unsigned int)value[0]) << 8) | ((unsigned int)value[1]) ); return( OK ); }/* END OF FILE */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -