Add analogReadTemp() to get RP2040 core temp (#63)
This commit is contained in:
parent
d9a4bbd95c
commit
96e31c640e
3 changed files with 26 additions and 1 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
11
libraries/rp2040/examples/Temperature/Temperature.ino
Normal file
11
libraries/rp2040/examples/Temperature/Temperature.ino
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
/* Released into the public domain */
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(5000);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Serial.printf("Core temperature: %2.1fC\n", analogReadTemp());
|
||||
delay(1000);
|
||||
}
|
||||
Loading…
Reference in a new issue