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

Parents
  • 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.
            }
        }
    }
    
    
    

Reply
  • 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.
            }
        }
    }
    
    
    

Children