Restructering of bitshift-function

Hi,

i built a electronic "sculpture" similar to this ones https://www.edn.com/jim-williams-the-light-side-and-classic-electronics-art-sculptures/

For one of the parts I used this schematic http://www.fartmagnet.co.uk/warmcat/wp-content/uploads/2014/10/WarmCatDisplay3.jpg

Unfortunately I did the wiring on the display like
0,1,2,3,4,5,6
but it should be 0,1,2,3,6,5,4

I'm looking now for a software solution to fix that and guess I need to modify the last bits of the Shiftout-function

void shiftOut( uint32_t ulDataPin, uint32_t ulClockPin, uint32_t ulBitOrder, uint32_t ulVal )
{
  uint8_t i ;

  for ( i=0 ; i < 8 ; i++ )
  {
    if ( ulBitOrder == LSBFIRST )
    {
      digitalWrite( ulDataPin, !!(ulVal & (1 << i)) ) ;
    }
    else
    {
      digitalWrite( ulDataPin, !!(ulVal & (1 << (7 - i))) ) ;
    }

    digitalWrite( ulClockPin, HIGH ) ;
    digitalWrite( ulClockPin, LOW ) ;
  }
}

Is there any smart solution to this?

A lookup table?

Swap wires on the display.

Hello

Look here : https://www.geeksforgeeks.org/how-to-swap-two-bits-in-a-given-integer/

Then you can use something like
shiftOut( dataPin, clockPin, bitOrder, swapBits( value, 0, 2 ) );

There is a built in function called __builtin_avr_insert_bits which will rearrange the bits for you.

A brief example:

// from avrfreaks.net https://www.avrfreaks.net/comment/204080#comment-204080
// post #17

// https://gcc.gnu.org/onlinedocs/gcc/AVR-Built-in-Functions.html

void setup() {
  Serial.begin(115200);
  delay(100);
  uint8_t argument = 0x8f;
  
  Serial.println("\n\n");
  Serial.print(argument, BIN);
  Serial.print("\t");
  Serial.println(argument, HEX);
  Serial.println("swapped nybbles");

  // use AVR-provided function to reverse bits
  // Example: if map = 0xffff0123, upper four bits are unchanged,
  // bit order of lower four bits is reversed.
  // 2nd and 3rd function parameters can be different variables
  //  argument = __builtin_avr_insert_bits (0xffff0123, argument, argument);
  //  Serial.println(argument, BIN);

  // swap nybbles using 'insert_bits'
  argument = __builtin_avr_insert_bits (0x32107654, argument, argument);

  Serial.print(argument, BIN);
  Serial.print("\t");
  Serial.print(argument, HEX);
}

void loop() {}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.