-->
Page 1 of 1

getPinMode

PostPosted: Sat Nov 07, 2015 9:33 am
by ivan82
Is it possible to extract what the pin mode has ben set to?

Tried this but this but I couldn't make it work.
http://arduino.stackexchange.com/questions/13165/how-to-read-pinmode-for-digital-pin

Code: Select allif ( DDRD & _BV(4) ) {   // Check bit #4 of the data direction register for port D
    // If we get here, then Arduino digital pin #4 was in output mode
} else {
    // If we get here, then Arduino digital pin #4 was in input mode
}

error: 'DDRD' was not declared in this scope




Code: Select alluint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
volatile uint8_t *reg = portModeRegister(port);

if (*reg & bit) {
    // It's an output
} else {
    // It's an input
}



Tried the code above, but got an exception @ portModeRegister
error: cannot convert 'volatile uint32_t* {aka volatile unsigned int*}' to 'volatile uint8_t* {aka volatile unsigned char*}' in initialization
#define portModeRegister(port) ((volatile uint32_t*) GPE)


(... , changed the reg type to uint32_t got an stack trace in serial monitor.
Exception (28):
epc1=0x40202994 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

ctx: cont
sp: 3ffeb240 end: 3ffeb400 offset: 01a0

>>>stack>>>
3ffeb3e0: 3fffdc20 00000000 3ffeb424 40201c63
3ffeb3f0: 00000000 00000000 3ffea3e0 40100378
<<<stack<<<