LED matrix incorrectly displays data from structures

62 views Asked by At

I just started to work with LED matrix (16*16) and attiny85.

The current purpose is to switch on a led on each row, where led number is the number of row (I know that led strip is like a snake, it does not matter for now).

So, I written an byte matrix[16][16] and manually put a digit into target cells. It worked well. After that I replace byte matrix[16][16] into a rgb matrix[16][16] where struct rgb {byte r, byte g, byte b} and it doesn`t work correctly (see photos below).

The LED functions:

#define LED PB0
#define byte unsigned char

struct rgb {
    byte r;
    byte g;
    byte b;
};

// @see https://agelectronica.lat/pdfs/textos/L/LDWS2812.PDF
// HIGH 0.8mks +/- 150ns and 0.45mks +/- 150ns
// LOW 0.4mks +/- 150ns and 0.85mks +/- 150ns
// 1s/8000000 = 125ns for 1 tact

void setBitHigh(byte pin) {
    PORTB |= _BV(pin); // 1 tactDuration = 125ns
    asm("nop");
    asm("nop");
    asm("nop");
    asm("nop");
    asm("nop"); // 0.75mks

    PORTB &= ~_BV(pin); // 1 tactDuration
    asm("nop");
    asm("nop");
    asm("nop"); // 0.5mks
}

void setBitLow(byte pin) {
    PORTB |= _BV(pin); // 1 tactDuration
    asm("nop");
    asm("nop"); // 0.375mks

    PORTB &= ~_BV(pin); // 1 tactDuration
    asm("nop");
    asm("nop");
    asm("nop");
    asm("nop");
    asm("nop");
    asm("nop"); // 0.875mks
}

void trueByte(byte pin, byte intensity) {
    for (int i = 7; i >= 0; i--) {
        intensity & _BV(i) ? setBitHigh(pin) : setBitLow(pin);
    }
}
void falseByte(byte pin) {
    for (int i = 0; i < 8; i++) {
        setBitLow(pin);
    }
}

void setPixel(byte pin, rgb color) {
    DDRB |= _BV(pin);

    color.g > 0 ? trueByte(pin, color.g) : falseByte(pin);
    color.r > 0 ? trueByte(pin, color.r) : falseByte(pin);
    color.b > 0 ? trueByte(pin, color.b) : falseByte(pin);
}

working well code:

void display(byte matrix[WIDTH][HEIGHT]) {
    for (byte rowIdx = 0; rowIdx < WIDTH; rowIdx++) {
        for (byte cellIdx = 0; cellIdx < HEIGHT; cellIdx++) {
            setPixel(LED, {matrix[rowIdx][cellIdx], 0, 0});
        }
    }
}

byte m[WIDTH][HEIGHT] = {
    { 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    { 0, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    { 0, 0, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
...
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0},
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15}
};

int main() {
    display(m);
}

result: enter image description here

NOT working well code:

void display(rgb matrix[WIDTH][HEIGHT]) {
    for (byte rowIdx = 0; rowIdx < WIDTH; rowIdx++) {
        for (byte cellIdx = 0; cellIdx < HEIGHT; cellIdx++) {
            setPixel(LED, matrix[rowIdx][cellIdx]);
        }
    }
}

rgb m[WIDTH][HEIGHT] = {
    {
            {15, 0, 0},
            {0, 0, 0},
            {0, 0, 0},
            {0, 0, 0},
            {0, 0, 0},
            {0, 0, 0},
            {0, 0, 0},
            {0, 0, 0},
            {0, 0, 0},
            {0, 0, 0},
            {0, 0, 0},
            {0, 0, 0},
            {0, 0, 0},
            {0, 0, 0},
            {0, 0, 0},
            {0, 0, 0}
    },
...
};


int main() {
    display(m);
}

result: enter image description here

I will be glad to any advice...

0

There are 0 answers