# changing pin orders in a #define varaible?

This is a discussion on changing pin orders in a #define varaible? within the C Programming forums, part of the General Programming Boards category; Hi, Im trying to write a simple bit of C to multiplex some 7seg displays. For practical purposes on the ...

1. ## changing pin orders in a #define varaible?

Hi,
Im trying to write a simple bit of C to multiplex some 7seg displays. For practical purposes on the layout it made sense to muddle up the order of some pins (ie abcdefg corresponds to PA0,PA1,PA4,PB0 etc..)

I was just wondering how you might #define a variable, say UNITS, that has these pins rearranged in an order you want! Sort of like a virtual PORT...

Thanks

2. Sorry I wasn't maybe clear enough, I wanted to define all of these into a single varaible in a logical order so I can have something like:
<code>
UNITS = EIGHT;
TENS = ONE;
</code>
where EIGHT would be predefined as 0x7F for example.

OR is there a better way to do this!?

3. <code>
UNITS = EIGHT;
TENS = ONE;
</code>
So you mean like this?
Code:
```#define EIGHT 0x7F  //I don't understand your choice of number
#define ONE    0xFE  //so I guessed that the bits are 1-8, the 0 bit is the name

int UNITS, TENS, VALUE;
UNITS = EIGHT;
TENS = ONE;
/*Now we combine the two? Units + tens? I have no idea what you want*/
VALUE = UNITS + (TENS << 16); //so now VALUE = 0xFE7F```

4. Ok so what I want to do is make a variable UNITS that combines a random selection of pins into one kind of vritual port where bit0 will be PA3, bit1 will be PA5, bit 2 will be another random pin and so on.

Normally I would just do PORTA = 0xFF or whatever but the pins are all muddled up so I want to correct for this.

Does this make sense?

5. So something like this, then?
Code:
```#define PA3 0x01
#define PA5 0x02
/* I'm guessing these are your pins 1 through 7, so then later you might have: */
#define EIGHT (PA1 | PA2 | PA3 | PA4 | PA5 | PA6 | PA7)
/* This would set all seven pins to on, which you want if you display an eight. */```

I just want to create a variable called MYPORT where

bit0 will be PA4
bit1 will be PB0
bit2 will be PA7
etc...

Thanks again!

7. I think you think PA4, PB0, and PA7 have some intrinsic value. If that's true, then you might be able to get somewhere. And do you mean you want the names bit0, bit1, in your code?

8. I mean bit0 of MYPORT will represent PA4 and bit1 of MYPORT will represent PB5 etc.

9. Originally Posted by graeme
I mean bit0 of MYPORT will represent PA4 and bit1 of MYPORT will represent PB5 etc.
So that's where we started then, isn't it?
Code:
```#define PA4bit 0x01
#define PB5bit 0x02
.
.
.
int MYPORT = 0x37;
if (MYPORT & PA4bit) {
/* do what needs to be done to turn on PA4 */
}```

10. Originally Posted by graeme
I mean bit0 of MYPORT will represent PA4 and bit1 of MYPORT will represent PB5 etc.
Sounds like you need an enum type variable unless you have to do it using #define, as in
Code:
`enum SegDisp {PA4, PB5, PA7, ... } MYPORT;  /* now PA4=0, PB5=1, PA7=2, so on and so forth */`

11. Originally Posted by itCbitC
Sounds like you need an enum type variable unless you have to do it using #define, as in
Code:
`enum SegDisp {PA4, PB5, PA7, ... } MYPORT;  /* now PA4=0, PB5=1, PA7=2, so on and so forth */`
Does this mean that now I could set
PA4 HIGH
PB5 LOW
PA7 HIGH
by using enum and:

Code:
`MYPORT = 0b00000101;`
If so, thanks very much!!!!

12. OMG I'm totally confused now.
Precisely clarify what you mean.

13. Originally Posted by graeme
Does this mean that now I could set
PA4 HIGH
PB5 LOW
PA7 HIGH
by using enum and:

Code:
`MYPORT = 0b00000101;`
If so, thanks very much!!!!
I'm pretty sure that's a no. You can do that with the defines I had up a bit:
Code:
`MYPORT = PA4 | PB5 | PA7; /*myport now has three bits set */`
(EDIT: And if you don't like |, you can use + to get the same effect (in this case).)

14. Originally Posted by tabstop
I'm pretty sure that's a no. You can do that with the defines I had up a bit:
Code:
`MYPORT = PA4 | PB5 | PA7; /*myport now has three bits set */`
(EDIT: And if you don't like |, you can use + to get the same effect (in this case).)
Thanks, but I'm not wanting to actually set the bits in this expression, just rearrange the ports into another order so I can use

Code:
```MYPORT = EIGHT;
MYPORT2 = NINE;
MYPORT3 = SIX;```
where each MYPORTx is a combination of various ports.

I could define different EIGHT,SIX's etc for each port but thats what I'm trying to work around. Am I making any sense yet??

Thanks for the help too though guys!

15. OK...

I want to be able to access a variety of ports at once from just one variable...

So I could set or clear PA3, PB3, PC2, PD5, PA4, PB5, PC6, PD7

by doing something like:
Code:
`MYPORT = 0b10001001;`
Hopefully this makes sense.

Thanks again!!

Page 1 of 2 12 Last