Runtime pin reconfiguration?

10 Aug 2011

In my design I've attached an analogue multiplexor to p18 of my mbed. The idea is to switch p18 between adc and dac during program operation. Is this actually possible? To clarify: I want to change a pin between different types of I/O, without rebooting the mbed. More specifically I want to change p18 between DigitalIn DigitalOut, AnalogueIn and AnalogueOut. I know ideally I would use different pins, but that is impossible in this case. Can I do this, and if so, how?

10 Aug 2011

Yes you can. You just need to use dynamic allocation of objects instead of static. An easy way to do it is to limit the scope of your class variables. E.g.:

float doADC()
{
  AnalogIn ain(p18);
  return ain;
}// ain is destroyed after leaving the function

void doDAC(float val)
{
  AnalogOut aout(p18);
  aout = val;
}// aout is destroyed after leaving the function
10 Aug 2011

Great! Thanks!

14 Nov 2012

what about run-time resetting of the pins? For example, having p18 go between ADC to it original state before it was initialized. How would I go about implementing that?