Unverified Commit 96e31c64 authored by Earle F. Philhower, III's avatar Earle F. Philhower, III Committed by GitHub

Add analogReadTemp() to get RP2040 core temp (#63)

parent d9a4bbd9
......@@ -63,6 +63,7 @@ PinStatus digitalRead(pin_size_t pinNumber);
// ADC
int analogRead(pin_size_t pinNumber);
float analogReadTemp(); // Returns core temp in Centigrade
// PWM
void analogWrite(pin_size_t pinNumber, int value);
......
......@@ -81,11 +81,11 @@ extern "C" void analogWrite(pin_size_t pin, int val) {
pwm_set_gpio_level(pin, val);
}
static bool adcInitted = false;
extern "C" int analogRead(pin_size_t pinNumber) {
if ((pinNumber < A0) || (pinNumber > A3)) {
return 0;
}
static bool adcInitted = false;
if (!adcInitted) {
adc_init();
}
......@@ -93,3 +93,16 @@ extern "C" int analogRead(pin_size_t pinNumber) {
adc_select_input(pinNumber - A0);
return adc_read();
}
extern "C" float analogReadTemp() {
if (!adcInitted) {
adc_init();
}
adc_set_temp_sensor_enabled(true);
delay(1); // Allow things to settle. Without this, readings can be erratic
adc_select_input(4); // Temperature sensor
int v = adc_read();
adc_set_temp_sensor_enabled(false);
float t = 27.0f - ((v * 3.3f / 4096.0f) - 0.706f) / 0.001721f; // From the datasheet
return t;
}
/* Released into the public domain */
void setup() {
Serial.begin(115200);
delay(5000);
}
void loop() {
Serial.printf("Core temperature: %2.1fC\n", analogReadTemp());
delay(1000);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment