1
votes

I am working on LED Tower that contains 15 layers, where each of them contains 4 bytes (32 LEDs). I would like to be able to shift a byte from right to left. However there is an issue with multiple bytes, can't figure out how to continously transition the shifting.

Additional information:

void Invert_Display(void){
for (int y = 0; y < LAYERS; y++){
    for (int x = 0; x < BYTES; x++){
        LED_Buffer[y][x] ^= (0b11111111);
    }
}
Update_Display();

Where UpdateDisplay function is as follows:

void Update_Display(void){

    while(!TRMT);           // Wait until transmission register is empty

    for (int y = 0; y < LAYERS; y++){
        for (int x = 0; x < BYTES; x++){
        TXREG = LED_Buffer[y][x];
        while (!TRMT);
        }
    }

    LE = 1;                 // Data is loaded to the Output latch
    NOP();              
    LE = 0;                 // Data is latched into the Output latch

The expected outcome is attached below.enter image description here

1
how long shift 1 bit or 8 bits?0___________
Well ideally i want to shift through all bytes (all LEDs) per each layer.RytisBe
Can you pack your 4 bytes into one big uint32_t? If yes, then all you need to do is perform a 1 bit left shift: my_uint32_variable <<= 1; — or my_uint32_variable *= 2; for that matter.hidefromkgb
I have to specify which LED_Buffer[y][x] I am using to shift the values in. Say I wanted to shift 0xAA value starting from LED_Buffer[0][0] and finishing outside of LED_Buffer[0][3], so it sweeps through entire display. That is my end goal. * I added some code to give a better idea how it is done. I am using PIC16F1829 microcontroller to develop this.RytisBe
I guess the main problem is that I am updating the display using bytes whereas shifting is usually done in terms of single bits, correct?RytisBe

1 Answers

1
votes

The following code will shift an array of bytes to the left. The number of bits to shift must be between 1 and 7. Shifting by more than 7 would require additional code.

void shiftArrayLeft(unsigned char array[], int length, int shift) // 1 <= shift <= 7
{
    unsigned char carry = 0;                        // no carry into the first byte
    for (int i = length-1; i >= 0; i--)
    {
        unsigned char temp = array[i];              // save the value
        array[i] = (array[i] << shift) | carry;     // update the array element
        carry = temp >> (8 - shift);                // compute the new carry
    }
}

It works by storing the old value from the array. Then update the current array element by shifting it and logical-OR a carry from the previous byte. Then compute the new carry (the upper bits of the original value).

The function can be called with code like this

unsigned char array[] = { 0x00, 0x00, 0x00, 0xAA };
int length = sizeof(array) / sizeof(array[0]);
shiftArrayLeft(array, length, 1);

which will change the array to { 0x00, 0x00, 0x01, 0x54 }