This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

passing a port/pin to a function

hi,

i have a question regarding passing a port/pin to a function.

i have written a piece of code as below

#define LED_PIN P1_0 // bit LED_PIN = P1_0 also does not work

void blink(bit pin, int delayTime, int blinkCount)
{ while( --blinkCount ) { pin = !pin ; delay(delayTime) ; // function to generate delay...this works fine }
}

void main()
{ blink( LED_PIN, 1000, 10 ) ;
}

this also does not work if i code the blink function as a macro.

is there any way i can pass the address/pin name to the function? i am using keil compiler for 89C51 MCU.

Parents
  • hi Erik,

    Thanks for your reply.

    i like your solution and i will use it.

    Just a thought instead of passing the mask value I will calculate the mask in the function itself.

    something like

    #define LED_PIN 2,3 //similar to P2_3

    void setpin(int port, int pin)
    { switch( port ) { case 2 : P2 |= (0x01 << pin) ; break ; }
    }

    void main()
    { setpin(LED_PIN) ;
    }

    i havent compiled and tested this code yet but i think it will work.

    thanks!

Reply
  • hi Erik,

    Thanks for your reply.

    i like your solution and i will use it.

    Just a thought instead of passing the mask value I will calculate the mask in the function itself.

    something like

    #define LED_PIN 2,3 //similar to P2_3

    void setpin(int port, int pin)
    { switch( port ) { case 2 : P2 |= (0x01 << pin) ; break ; }
    }

    void main()
    { setpin(LED_PIN) ;
    }

    i havent compiled and tested this code yet but i think it will work.

    thanks!

Children