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

How to read port pins in port(P0,1,2)?

I would like to call function like port(P0,1,2).i want to assign the values of 0 and 1 to P0.1 and P0.2 using that function prototype only...how can i assign the values to port pins.what are the steps i need to follow...

Thanks in advance

chinnaye

  • Well, guess what? You haven't told us what processor you have. Might not that be a rather important piece of information?

  • I am using 8051 family of micro controller..and code is

    void ping( unsigned char port ,pin1_number num1 ,pin2_number num2 );

    void main()
    { ping(P0,1,2);

    }
    void ping(unsigned char port ,pin1_number num1 ,pin2_number num2 )
    { unsigned char trig=0; unsigned char echo=1; unsigned char number1; unsigned char number2; unsigned char *signal1; unsigned char *signal2; switch(port) {

    case P0:number1=((port)|num1); //address of P0.1 number2=((port)|num2); //address of P0.2 break; case P1:number1=((port)|num1); number2=(port|num2); break; case P2:number1=(port|num1); number2=(port|num2); break; case P3:number1=(port|num1); number2=(port|num2); break; }

    signal1=&(number1); signal2=&(number2); *signal1=trig;
    *signal2=echo;

    }

    in this program i am getting the values of 0 & 1 to signal pointer.but i am not getting any change in port pins.how can i assign values to port pins.

    Thanks In advance.....

  • The fact of the matter is, SFRs can not be accessed indirectly via pointers. And ports P0, P1, P2, P3 are SFRs. Use a different approach.

  • Thanks for reply..i am unable to get it.can u explain how i need to start?

  • i did a study of self modifying code for my thesis. it is very complicated but it would be very excellent here.

  • You need to start by checking the processor instructions supported by the 8051 processor. Then you would see that not all address ranges can be accessed using an index, where you store the address in a register and perform an access relative to the contents of this register - the available instructions requires the offset to be hard-coded in the instruction.

    So you need to have self-modifying code (which isn't supported by C) to modify the instruction to point at a specific bit in the bit-addressable range. Or you need to perform and/or operation on bytes. Or you need to have a switch statement that contains a case: statement for every bit that the function needs to be able to operate on.

    The 8051 is not a general-purpose architecture, so you need specific knowledge about the architecture to understand the limitations imposed on C programs written for 8051 processors.

  • If i use switch statement it will become complexity.I don't want to use that one.I didn't another method also..can any tell me what is the flow of program?

  • Why is this too complex? Are you too lazy to implement it? Well, I'm stupid enough to show one possible solution here:

    typedef unsigned char uint8_t;
    typedef enum {PORT0, PORT1, PORT2, PORT3} port_t;
    
    void signal_port_out(port_t p, uint8_t pin_number, uint8_t value)
    {
        // Calculate byte mask with bit set at 'pin_number'
        uint8_t mask = (1 << pin_number);
    
        // add assertion here if required
        // assert(pin_number <= 7);
        // assert(value      <= 1);
    
        if (value)
        {
            switch (p)
            {
            case PORT0: P0 |= mask; break;
            case PORT1: P1 |= mask; break;
            case PORT2: P2 |= mask; break;
            case PORT3: P3 |= mask; break;
    //      default: add assertion here, if required.
            }
        }
        else
        {
            switch (p)
            {
            case PORT0: P0 &= ~mask; break;
            case PORT1: P1 &= ~mask; break;
            case PORT2: P2 &= ~mask; break;
            case PORT3: P3 &= ~mask; break;
    //      default: add assertion here, if required.
            }
        }
    }
    
    
    

  • never mind.

    check my code once

    typedef enum
    { int_pin0=0,
    int_pin1=1,
    int_pin2=2,
    int_pin3=3,
    int_pin4=4,
    int_pin5=5,
    int_pin6=6,
    int_pin7=7

    }pin1_number,pin2_number;

    void ping( unsigned char port ,pin1_number num1 ,pin2_number num2 );

    void main()
    { ping(P0,1,2);//1&2 are port pins...i.e P0_1,P0_2 like it can be changed..

    }

    remaining program which i was already post..i want to assign the values of 0 & 1 to the port pins..i didn't get it.

  • SFRs are NOT indirectly addressable

    thus if you want the port (number) as the entry to a function, the only way to do the function woill be to select the port by stacked if's or a switch statement.

    Erik

  • I want the port as the entry function as well as port pin numbers(two pins) also.Here i want to assign the values of 0 &1 to the two port pin numbers.that's it.

  • So - have you started coding yet? You have been given enough information to be able to write such a function.

  • I want the port as the entry function as well as port pin numbers(two pins) also.Here i want to assign the values of 0 &1 to the two port pin numbers.that's it.
    then do it.
    all possible ways to process has been described above.

    i did a study of self modifying code for my thesis
    you can't do that in a flash based controller.

    Erik

  • not true if you can;

    program the flash dynamically.

  • which you can't without spending an (in processor terms) enomous amount of time. Most apps would die if you blocked ints for that long

    Erik