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. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    8

    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
    Last edited by graeme; 03-31-2010 at 07:45 AM.

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    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!?
    Last edited by graeme; 03-31-2010 at 08:06 AM.

  3. #3
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    <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
    Last edited by bernt; 03-31-2010 at 08:41 AM. Reason: Mathematical error
    Consider this post signed

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    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. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. */

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    Ok, I realise Im not being very clear so how about this more generally.

    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!
    Last edited by graeme; 03-31-2010 at 12:17 PM.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    I mean bit0 of MYPORT will represent PA4 and bit1 of MYPORT will represent PB5 etc.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by graeme View Post
    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. #10
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by graeme View Post
    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 */
    Last edited by itCbitC; 03-31-2010 at 01:25 PM.

  11. #11
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    Quote Originally Posted by itCbitC View Post
    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. #12
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    OMG I'm totally confused now.
    Precisely clarify what you mean.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by graeme View Post
    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).)
    Last edited by tabstop; 03-31-2010 at 01:41 PM.

  14. #14
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    Quote Originally Posted by tabstop View Post
    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. #15
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    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 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Buidl Library with ./configure script
    By Jardon in forum C Programming
    Replies: 6
    Last Post: 07-24-2009, 09:36 AM
  2. Pointer within a Struct
    By Bladactania in forum C Programming
    Replies: 11
    Last Post: 04-03-2009, 10:20 PM
  3. Why?!?
    By p3rry in forum C Programming
    Replies: 3
    Last Post: 01-08-2009, 11:52 AM
  4. size of an integer pointer
    By onebrother in forum C Programming
    Replies: 5
    Last Post: 07-09-2008, 11:49 AM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21