Using bits of different ports as if they were the same byte

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Using bits of different ports as if they were the same byte

467 Views
DarthGremlin
Contributor I

Hello,

 

I am a Computer Engineering Student and I am working with an MC9S12C32.  I have made a keypad interface using 7 bits of port T.  But now I am making a more complicated project that requires use of the keypad but I will need to move it off of port T, so that I can use the input capture pins for other things.  Due to the limited number of pins that I have access to I am going to need to split this up amongst several different pins of different ports.  I am wondering if there is anyway that I could make a header file similar to the mc9s12c32.h file that is included with code Warrior to define a new byte of memory that is not actually the same byte in the hardware.  That way I can reuse code that has been tested and proven and simply change the calls to Port T to something else.  Is this possible?

Labels (1)
0 Kudos
1 Reply

386 Views
kef
Specialist I

If you are only reading from keypad pins, then you can create preprocessor macro, that will combine few pins from different ports to one "byte". Like this

// bits 0-1 are bits 3,4 from PORTA// ( (PORTA & ((1<<3)|(1<<4)) ) >> (3-0) )// bit2  is bit 0 from PORTB// ( (PORTB & (1<<0)) << (2-0) )// bits 34567 are bit 23456 from PORTE// ( (PORTE & ((1<<2)|(1<<3)|(1<<4)|(1<<5)|(1<<6)) ) << (3-2) )#define KEYPADPORT (     ( (PORTA & ((1<<3)|(1<<4)) ) >> (3-0) )                \                      |  ( (PORTB & (1<<0)) << (2-0) )                          \                      |  ( (PORTE & ((1<<2)|(1<<3)|(1<<4)|(1<<5)|(1<<6)) ) << (3-2) )  )

 Instead reading PTT you read KEYPADPORT.

 

Of course you can't write access KEYPADPORT, you need to code some function that would take byte and write all bits back to different ports.

 

0 Kudos