Skip to content

Commit 870e83d

Browse files
Nathan Seidlenseidle
Nathan Seidle
authored andcommitted
Add getTemperature() function. Update example 4 with new temp read function.
1 parent ce23bcd commit 870e83d

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

cores/arduino/ard_sup/analog/ap3_analog.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,35 @@ uint16_t analogRead(uint8_t pinNumber)
223223
}
224224
}
225225

226+
//Returns the internal temperature of the Apollo3
227+
float getTemperature()
228+
{
229+
const float fReferenceVoltage = 2.0;
230+
float fADCTempDegreesC = 0.0;
231+
232+
uint16_t internalTemp = analogRead(ADC_INTERNAL_TEMP); //Read internal temp sensor channel
233+
234+
//
235+
// Convert and scale the temperature.
236+
// Temperatures are in Fahrenheit range -40 to 225 degrees.
237+
// Voltage range is 0.825V to 1.283V
238+
// First get the ADC voltage corresponding to temperature.
239+
//
240+
float fADCTempVolts = ((float)internalTemp) * fReferenceVoltage / ((float)(pow(2, _analogBits)));
241+
242+
float fVT[3];
243+
fVT[0] = fADCTempVolts;
244+
fVT[1] = 0.0f;
245+
fVT[2] = -123.456;
246+
uint32_t ui32Retval = am_hal_adc_control(g_ADCHandle, AM_HAL_ADC_REQ_TEMP_CELSIUS_GET, fVT);
247+
if (ui32Retval == AM_HAL_STATUS_SUCCESS)
248+
{
249+
fADCTempDegreesC = fVT[1]; // Get the temperature
250+
}
251+
252+
return (fADCTempDegreesC);
253+
}
254+
226255
//Power down ADC. Comes from adc_lpmode2.c example from Ambiq SDK
227256
bool power_adc_disable()
228257
{

cores/arduino/ard_sup/ap3_analog.h

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ ap3_err_t ap3_change_channel(ap3_gpio_pad_t padNumber);
4444
bool power_adc_disable();
4545
uint16_t analogRead(uint8_t pinNumber);
4646
ap3_err_t analogReadResolution(uint8_t bits);
47+
float getTemperature();
4748

4849
ap3_err_t ap3_pwm_output(uint8_t pin, uint32_t th, uint32_t fw, uint32_t clk);
4950
ap3_err_t analogWriteResolution(uint8_t res);

libraries/Examples/examples/Example4_analogRead/Example4_analogRead.ino

+1-4
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,8 @@ void loop()
5252
Serial.print(vcc, 2);
5353
Serial.print("V");
5454

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

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

0 commit comments

Comments
 (0)