How to 'flip' a port?

Hello,

I am trying to connect a 1284P to a T6963C display using this library: http://code.google.com/p/arduino-t6963c/; I have made changes to the header files so that all the pins pf PORTA drive the data bus and four pins on PORTC - PC2, PC3, PC4 and PC5 drive the control pins.

Here is the edit:

#if (defined(__AVR_ATmega1280__) || \
     defined(__AVR_ATmega1281__) || \
     defined(__AVR_ATmega2560__) || \
     defined(__AVR_ATmega2561__))      //--- Arduino Mega ---

// data port Arduino Mega
#define GLCD_DATA_PORT	 	PORTL
#define GLCD_DATA_PIN		PINL		//Arduino Mega Pins 49-42
#define GLCD_DATA_DDR		DDRL

// control port Arduino Mega
#define GLCD_CTRL_PORT		PORTC
#define GLCD_CTRL_PIN		PINC		//Arduino Mega Pins 37-34
#define GLCD_CTRL_DDR		DDRC

#elif defined(__AVR_ATmega1284P__)

// data port 1284P
#define GLCD_DATA_PORT 		PORTA
#define GLCD_DATA_PIN		PINA	//Arduino Pins 2-7
#define GLCD_DATA_DDR		DDRA
#define GLCD_DATA_MASK		0xFF

// control port 1284P
#define GLCD_CTRL_PORT		PORTC
#define GLCD_CTRL_PIN		PINC
#define GLCD_CTRL_DDR		DDRC

// control signals 1284P
#define GLCD_WR 	2
#define GLCD_RD			3
#define GLCD_CE			4  //Should be able to XNOR this with WR and RD
#define GLCD_CD			5
//#define GLCD_RESET  		4  //For some reason my LCD works with this pin resistored to +5
//#define GLCD_FS     		5  //Use hardware solution not pin.

#else// data port other Arduino
#define GLCD_DATA_PORT1 	PORTD
#define GLCD_DATA_PIN1		PIND	//Arduino Pins 2-7
#define GLCD_DATA_DDR1		DDRD
#define GLCD_DATA_SHIFT1	<<2
#define GLCD_DATA_RSHIFT1	>>2
#define GLCD_DATA_MASK1		0xFC

#define GLCD_DATA_PORT2 	PORTB
#define GLCD_DATA_PIN2		PINB	//Arduino Pins 8,9
#define GLCD_DATA_DDR2		DDRB
#define GLCD_DATA_SHIFT2	>>6
#define GLCD_DATA_RSHIFT2	<<6
#define GLCD_DATA_MASK2		0x03
// control port
#define GLCD_CTRL_PORT		PORTC
#define GLCD_CTRL_PIN		PINC
#define GLCD_CTRL_DDR		DDRC

// control signals
#define GLCD_WR 		0
#define GLCD_RD			1
#define GLCD_CE			2  //Should be able to XNOR this with WR and RD
#define GLCD_CD			3
//#define GLCD_RESET  		4  //For some reason my LCD works with this pin resistored to +5
//#define GLCD_FS     		5  //Use hardware solution not pin.
# endif

What this has done is that PortA.0 goes to DB0, PortA.1 goes to DB1 etc., till PortA.7 to DB7.

In a gross oversight on my part, I designed and fabricated a few PCBs with the inverted connection - i. e., PortA.0 to DB7, PortA.1 to DB6 etc., till PortA.7 to DB0.

Now, to my question: is there a way , for lack of a better word, 'to flip' PortA in code so as to have proper transmission of data?

I am sorry if I have not used the proper terminology.

Thanks,
Madhu.

You mean this:

?

A better tutorial is here. http://www.avrfreaks.net/index.php?name=pnphpbb2&file=viewtopic&t=37871 further down there are a couple of Macros for bit manipulation.

Hello,

Thanks for replying. I realise that there is bit manipulation involved; but I am unsure how to do it.

What I am trying to achieve is to 'turn the port over on its head'.

Let me explain - The library is configured to use PortA.0 for DB0 and my PCB has PortA.7 for DB0. I need guidance with the bit where we change the order.

Thanks,
Madhu.

Like:
ABCDEFGH
to
HGFEDCBA
?

WizenedEE,

Exactly.

Thanks,
Madhu.

Say you want to access bit BIT, if you use the bit manipulation with (7-BIT) instead of BIT, you should achieve that.

Bubulindo,

I am sorry, I do not follow.

Do you want to see how the data port is being used by the library?

Thanks,
Madhu.

Do you have 256 bytes of flash (program) memory spare? You could do a table lookup.

byte flip (byte a)
  {
  byte b;
  for (byte i = 0; i < 8; i++)
    {
    b <<= 1;
    if (a & 1)
      b |= 1;
    a >>= 1;
    }  
  return b;
  }  // end of flip

void setup () 
{
  Serial.begin (115200);
  Serial.println ("Start");
  Serial.println (flip (0x01), BIN);
  Serial.println (flip (0x02), BIN);
  Serial.println (flip (0x03), BIN);
  Serial.println (flip (0x40), BIN);
  Serial.println (flip (0xFF), BIN);
  Serial.println (flip (0xAE), BIN);
}

void loop () {}

Output:

Start
10000000
1000000
11000000
10
11111111
1110101

Mr. Gammon,

Has anyone told you lately that you are a godsend? Well, if not, please let me.

Your flip function performed flawlessly! Thank you ever so much!

Regards,
Madhu.