Skip to content

Add getTemperature() to support internal temp sensor #159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions cores/arduino/ard_sup/analog/ap3_analog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,35 @@ uint16_t analogRead(uint8_t pinNumber)
}
}

//Returns the internal temperature of the Apollo3
float getTemperature()
{
const float fReferenceVoltage = 2.0;
float fADCTempDegreesC = 0.0;

uint16_t internalTemp = analogRead(ADC_INTERNAL_TEMP); //Read internal temp sensor channel

//
// Convert and scale the temperature.
// Temperatures are in Fahrenheit range -40 to 225 degrees.
// Voltage range is 0.825V to 1.283V
// First get the ADC voltage corresponding to temperature.
//
float fADCTempVolts = ((float)internalTemp) * fReferenceVoltage / ((float)(pow(2, _analogBits)));

float fVT[3];
fVT[0] = fADCTempVolts;
fVT[1] = 0.0f;
fVT[2] = -123.456;
uint32_t ui32Retval = am_hal_adc_control(g_ADCHandle, AM_HAL_ADC_REQ_TEMP_CELSIUS_GET, fVT);
if (ui32Retval == AM_HAL_STATUS_SUCCESS)
{
fADCTempDegreesC = fVT[1]; // Get the temperature
}

return (fADCTempDegreesC);
}

//Power down ADC. Comes from adc_lpmode2.c example from Ambiq SDK
bool power_adc_disable()
{
Expand Down
1 change: 1 addition & 0 deletions cores/arduino/ard_sup/ap3_analog.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ ap3_err_t ap3_change_channel(ap3_gpio_pad_t padNumber);
bool power_adc_disable();
uint16_t analogRead(uint8_t pinNumber);
ap3_err_t analogReadResolution(uint8_t bits);
float getTemperature();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like getTemperature() is just a bit too generic. What am I getting the temperature of? Should I be passing in an argument that is a value to convert to temperature?

How about something like getInternalTempC() or getChipTempC()?


ap3_err_t ap3_pwm_output(uint8_t pin, uint32_t th, uint32_t fw, uint32_t clk);
ap3_err_t analogWriteResolution(uint8_t res);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,8 @@ void loop()
Serial.print(vcc, 2);
Serial.print("V");

int internalTempVoltage = analogRead(ADC_INTERNAL_TEMP); //Read internal temp sensor. 3.8mV/C, +/-3C
double internalTemp = internalTempVoltage * vcc / 16384.0; //Convert internal temp reading to voltage
internalTemp /= 0.0038; //Convert voltage to degrees C
Serial.print("\tinternalTemp: ");
Serial.print(internalTemp, 2);
Serial.print(getTemperature(), 2);

int vss = analogRead(ADC_INTERNAL_VSS); //Read internal VSS (should be 0)
Serial.print("\tvss: ");
Expand Down