104 lines
3.3 KiB
ObjectPascal
104 lines
3.3 KiB
ObjectPascal
unit uTempCalc_Custom;
|
|
|
|
interface
|
|
|
|
type
|
|
TAdcToTemperature = class
|
|
private
|
|
FAdcResolution: Integer; // 767
|
|
FReferenceVoltage: Double; // 3.74
|
|
FSensorMaxVoltage: Double; // 5.0
|
|
FTempMax: Double; // 850¡ÆC
|
|
|
|
// Getter and Setter methods for properties
|
|
function GetAdcResolution: Integer;
|
|
procedure SetAdcResolution(const Value: Integer);
|
|
function GetTempMax: Double;
|
|
procedure SetTempMax(const Value: Double);
|
|
|
|
public
|
|
constructor Create(AAdcRes: Integer = 767;
|
|
ARefVolt: Double = 3.74;
|
|
ASensorMaxVolt: Double = 5.0;
|
|
ATempMax: Double = 850.0);
|
|
|
|
// Public properties to allow changing these values
|
|
property AdcResolution: Integer read GetAdcResolution write SetAdcResolution;
|
|
property TempMax: Double read GetTempMax write SetTempMax;
|
|
// The other parameters remain fixed via the constructor
|
|
property ReferenceVoltage: Double read FReferenceVoltage;
|
|
property SensorMaxVoltage: Double read FSensorMaxVoltage;
|
|
|
|
function AdcToVoltage(AAdcValue: Integer): Double;
|
|
function VoltageToSensorVoltage(AVoltage: Double): Double;
|
|
function SensorVoltageToTemperature(AVoltage: Double): Double;
|
|
function AdcToTemperature(AAdcValue: Integer): Double;
|
|
end;
|
|
|
|
implementation
|
|
|
|
{ TAdcToTemperature }
|
|
|
|
// --- Property Getter/Setter Implementations ---
|
|
|
|
function TAdcToTemperature.GetAdcResolution: Integer;
|
|
begin
|
|
Result := FAdcResolution;
|
|
end;
|
|
procedure TAdcToTemperature.SetAdcResolution(const Value: Integer);
|
|
begin
|
|
// Basic validation to prevent division by zero
|
|
if Value > 0 then
|
|
FAdcResolution := Value
|
|
else
|
|
FAdcResolution := 1; // Set a safe minimum or raise an exception in a real app
|
|
end;
|
|
function TAdcToTemperature.GetTempMax: Double;
|
|
begin
|
|
Result := FTempMax;
|
|
end;
|
|
procedure TAdcToTemperature.SetTempMax(const Value: Double);
|
|
begin
|
|
// Allow any double value for the maximum temperature
|
|
FTempMax := Value;
|
|
end;
|
|
|
|
constructor TAdcToTemperature.Create(AAdcRes: Integer; ARefVolt, ASensorMaxVolt, ATempMax: Double);
|
|
begin
|
|
// Use the Setters for initialization to apply any validation
|
|
SetAdcResolution(AAdcRes);
|
|
SetTempMax(ATempMax);
|
|
FReferenceVoltage := ARefVolt;
|
|
FSensorMaxVoltage := ASensorMaxVolt;
|
|
end;
|
|
|
|
function TAdcToTemperature.AdcToVoltage(AAdcValue: Integer): Double;
|
|
begin
|
|
// ADC -> Measured Voltage
|
|
// FAdcResolution is now accessed via the field, or it could use the property Read
|
|
Result := (AAdcValue / FAdcResolution) * FReferenceVoltage;
|
|
end;
|
|
function TAdcToTemperature.VoltageToSensorVoltage(AVoltage: Double): Double;
|
|
begin
|
|
// Scales the measured voltage (within Vref) to the full sensor range (VsensorMax)
|
|
Result := (AVoltage / FReferenceVoltage) * FSensorMaxVoltage;
|
|
end;
|
|
function TAdcToTemperature.SensorVoltageToTemperature(AVoltage: Double): Double;
|
|
begin
|
|
// Clips voltage to the sensor range and scales to the max temperature
|
|
if AVoltage < 0 then AVoltage := 0;
|
|
if AVoltage > FSensorMaxVoltage then AVoltage := FSensorMaxVoltage;
|
|
// FTempMax is now accessed via the field
|
|
Result := (AVoltage / FSensorMaxVoltage) * FTempMax;
|
|
end;
|
|
function TAdcToTemperature.AdcToTemperature(AAdcValue: Integer): Double;
|
|
var
|
|
vAdc, vSensor: Double;
|
|
begin
|
|
vAdc := AdcToVoltage(AAdcValue);
|
|
vSensor := VoltageToSensorVoltage(vAdc);
|
|
Result := SensorVoltageToTemperature(vSensor);
|
|
end;
|
|
end.
|
|
|