analogRead resets pinMode to "input"

Posted on
  • I'm trying to set-up an input_pullup and each time I do an analogRead on it, the pinMode resets from "input_pullup" to standard "input", I'm not sure this has always been to correct behaviour. I'm using the original Espruino board on latest version 1v75.

  • In source, this is clearly done:

    
    JsVarFloat jshPinAnalog(Pin pin) {
      if (pin >= JSH_PIN_COUNT /* inc PIN_UNDEFINED */ || pinInfo[pin].analog==JSH_ANALOG_NONE) {
        jshPrintCapablePins(pin, "Analog Input", 0,0,0,0, true);
        return 0;
      }
      **jshSetPinStateIsManual(pin, false);
      jshPinSetState(pin, JSHPINSTATE_ADC_IN);**
    
      return jshAnalogRead(pinInfo[pin].analog, false) / (JsVarFloat)65535;
    }
    
    

    Now, per datasheet, we can't use a pin's Alternate Functions as input unless the pin mode is one of the input modes:

    For alternate function inputs, the port must be configured in Input mode (floating, pullup
    or pull-down) and the input pin must be driven externally.

    There should probably be a conditional there - if pinMode is in manual mode, only un-manualize it and set it to input if it's not currently configured as input_pullup/input_pulldown.

    On the other hand, it's not much of a datasheet - more like a datatome of 700+ pages, so there may be some reason that you can't use the ADC with a pullup/down.

    That all said... according to the datasheet, the tolerances on the internal pullup/downs are wide enough to drive a truck through (nominal 40k ohm, but maybe as low as 30k or as high as 50k), so I question what one might be doing where the pullup would be acceptable in place of an external pullup. Unless it's a low impedance (so it effectively ignores the pullup) input that's intermittently connected, so you're trying to keep it from floating....

  • Notice that too... before first analogRead(), the pin was not floating because initialized as input with pulled-down. Afterwards, it jumped all over the place... Since I needed a digitalRead() for good and the analogRead() only temporarily to figure out some resistor network, I did a pull-down with the digitalRead(). The digitalRead() 's pull-down will stay.

  • Thanks for letting me know - if the pin state was manually set then yes, it probably shouldn't overwrite it. I'll push a change to this now so you can play around.

    However I'm not 100% sure if you can actually do what you want. Even if it looks like you can from the circuit diagram, section 9.1 in the F103 datasheet implies that you can't (see below). It gets even more strange when you look at the F401 too.


    1 Attachment

    • Screenshot from 2015-03-23 10:25:51.png
  • Ok, it works! In fact I think that due to a copy/paste bug it was always going into the 'input floating' mode for analog reads anyway.

    My guess is that the special 'analog' mode mentioned in the table actually just powers down/disables the schmitt trigger somehow. All the power saving notes say that you should switch pins to the analog state at startup, as it uses less power than 'input floating'.


    1 Attachment

    • Screenshot from 2015-03-23 10:44:26.png
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

analogRead resets pinMode to "input"

Posted by Avatar for TiCPU @TiCPU

Actions