Question

I want to define pins and ports in an array for example something like this.

char a[3]={PINA.0,PINB.2,PORTC.4};

/***********************************/
if(a[0]==1) // equal to (PINA.0==1)
//do something

a[2]=1; // equal to PORTC.4=1;

I know this codes are not correct and want your help to write the correct one.

My compiler is CodeVision.

Était-ce utile?

La solution 3

I wrote my PIN codes in pin() function:

int pin(int c){
bit a;
switch(c){
case 0:
a=PINA.0;
break;

case 1:
a=PINA.1;
break;

case 2:
a=PINA.2;
break;

case 3:
a=PINA.3;
break;

case 4:
a=PINA.4;
break;

case 5:
a=PINA.5;
break;

case 6:
a=PINA.6;
break;

case 7:
a=PINA.7;
break;
}
return a;
}

i=pin(0) equals to i=PINA.0

Autres conseils

PINx.<PIN_number> is a compiler extension and not standard. It's usually used in compilers for embedded systems. Also, C doesn't allow to initialize arrays/structs with variables

If you need to store it in an array and get the value later, explicitly assign the elements

char a[3];
a[0] = PINA.0;
a[1] = PINB.2;
a[2] = PORTC.4;  // why the above is PIN and below is PORT?
// This makes zero sense. PIN cannot have multiple bits

or

char a[3];
a[0] = PINA & 0x01;
a[1] = (PINB >> 2) & 0x01;
a[2] = (PORTC >> 4) & 0x01;

Edit:

It turns out you want to access pins as index. In that case you'll need a getter and a setter instead of array. If the pin indices are random then use a switch

void setpin(char pin, bit value)
{
    switch (pin)
    {
    case 0:
        PINA.0 = value;
        break;
    case 1:
        PINB.2 = value;
        break;
    case 2:
        PORTC.4 = value;
        break;
    }
}

bit getpin(char pin)
{
    switch (pin)
    {
    case 0:
        return PINA.0;
    case 1:
        return PINB.2;
    case 2:
        return PORTC.4;
    default:
        return 0;
    }
}

If the indices are linear and you don't need to use a runtime index then it may be possible to make a simple macro to access the pins like this:

#define PIN_A(X) (PINA.X)
PIN_A(0) = 1;
PIN_A(4) = PIN_A(2);
int b = PIN_A(6);

#define PIN_X0(X) (PIN##X.0)
PIN_X0(A) = 0;
PIN_X0(B) = PIN_X0(C);
int c = PIN_X0(D);

As you can see, PIN_A(X) now can replace both the get and set functions above

If the compiler lets you to access the pins with PINA.0 then the PINA might be a bitfield. Bitfields does not have addresses. With this statement: char a[3]={PINA.0,PINB.2,PORTC.4}; you are storing pin values in the array. So, later when you write a[2]=1 you write 1 to the copied register value, not to the actual register. If you want to have right results you should work directly with the registers, or macros.

//setting PINA.2 = 1
PINA |= 1<< 2;

you can also define a macro like

#define SET_BIT(PORT_NR,BIT) (PORT_NR |= (1<<BIT)) 
#define CLEAR_BIT(PORT_NR,BIT) (PORT_NR &= ~(1<<BIT))

and call it:

SET_BIT(PINA,2);
CLEAR_BIT(PINA,2);

However , if you still want to keep them into an array you might be able to store pointer to functions in the array and then call them.

void setBit(struct port_operation* a,unsigned char bit){
    return a->_port |= (1<<bit);
}
void clearBit(struct port_operation* a,unsigned char bit){
    return a->_port &= ~(1<<bit);
}


typedef struct _port_operation{
    unsigned char _port;
    void(*set)(struct _port_operation* ,unsigned char);
    void(*clear)(struct _port_operation* ,unsigned char);
}port_operation;

port_operation port_op_a;
port_op_a._port = PORTA;
port_op_a.set = setBit;
port_op_a.clear = clearBit;

port_operation port_op_b = port_op_a;
port_op_b._port = PORTB;

port_operation[2] = {port_op_a,port_op_b};

//set 1 to PORTA bit 1
port_operation[0].set(port_operation[0],1);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top