Skip to content

Commit f853232

Browse files
authored
Handle PADC_VREF mapping in analogInputToPinName
Add conditional check for PADC_VREF in analogInputToPinName function. Silly workaround in analogInputToPinName(), since PADC_VREF mapped to PA_1 instead of just PADC_VREF. Don't know why the code is this convoluted, so this is just a quick fix that works on the CH32V003. Note: this change was only tested with CH32V003. It requires ADC to be enabled in .../variants/CH32V00x/CH32V003F4/variant_CH32V003F4.h: #define ADC_MODULE_ENABLED Example usage: uint32_t _uVref=1200L; // Vref 1.17V-1.23V (avg 1.20V) according CH32V003 datasheet uint32_t getVCC(uint32_t uMultiplier=100) { // return VCC in mV, based on internal 1.2V reference voltage and the supplied multiplier // multiplier is a percentage (100%=100), to be used for calibration uint32_t uVal=analogRead(PADC_VREF); // Note: would be better to read an average after ADC gives stable readings uVal=(1024L*_uVref)/uVal; // assume 1024 is full VCC, this applies to 10-bit ADC resolution // NOTE: 1024 works for CH32V003, others may need different values return((uVal * uMultiplier)/100); } openwch/arduino_core_ch32#156
1 parent 4151b08 commit f853232

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

cores/arduino/pins_arduino.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ WEAK uint32_t pinNametoDigitalPin(PinName p)
3939
PinName analogInputToPinName(uint32_t pin)
4040
{
4141
PinName pn = digitalPinToPinName(analogInputToDigitalPin(pin));
42+
// MMOLE 240330: Don't know what's going on, but PADC_VREF maps to PA_1 instead of just PADC_VREF
43+
// --->>>
44+
#ifdef ADC_MODULE_ENABLED
45+
if(pin==PADC_VREF)
46+
return(PADC_VREF);
47+
#endif
48+
// <<<---
4249
if (pn == NC) {
4350
switch (pin) {
4451
#if defined(ADC_CHANNEL_TEMPSENSOR) || defined(ADC_CHANNEL_TEMPSENSOR_ADC1)

0 commit comments

Comments
 (0)