Want to light up a WS2812B whatever color I want but I failed

Chat about just about anything else
Forum rules
Do not post support questions here. Before you post read the forum rules. Topics in this forum are automatically closed 30 days after creation.
Locked
Tianshi

Want to light up a WS2812B whatever color I want but I failed

Post by Tianshi »

I'm trying to create project where I can light up WS2812B whatever color I want and specific RGB color code is delivered through UART. So when I receive the code I'm trying to send data to WS2812B but it seems to be working only every 2nd time when it actually shouldn't and it doesn't work at all when it should.

I'm using Atmel's Atmega328P-PU and its CPU speed is set to 8MHz. This code actually worked fine when I 1st wrote it but I had to change some stuff in UART section so had to compile and flush it again but this time it was done from Linux using avrdude command line tools instead of Windows 7(which was on my friends PC and got deleted).

This is data timing diagram for WS2812B
Image

Everything in code works fine except for sending '1' and '0' to LED and it happens in function set_leds_color. Weird thing is that if I leave the __builtin_avr_delay_cycles(); values as they were before my UART code updates (and as are shown in code) it always sets LED's color to white kind of like it would always send only '1' to led (255,255,255). Even if I set delay for '0' to __builtin_avr_delay_cycles(1) nothing changes - any color is displayed as white. Now the interesting part is that if I remove this delay for '0' so code become like this

Code: Select all

//send 0
else{              
   DATA_PORT = high_data;
    DATA_PORT = low_data;
}
When I set the same color multiple times in a row - LED actually sets my desired color 1 time but 2nd time sets white again. This goes on. I have no idea what the problem is. I tested that __builtin_avr_delay_cycles() works as expected and my CPU frequency is indeed 8Mhz. I also sent rgb_array[] values from mcu to my PC thought UART and they are fine as well. Can anybody give me some suggestions what might be wrong? Thanks.

Here is my final code

Code: Select all

#include <avr/io.h>
#include <util/delay.h>

#define DATA_PORT PORTD
#define DATA_PIN PD2
#define DATA_DDR DDRD

/*
UART stuff
*/

void fill_array(unsigned int* arr, unsigned int color)
{
    for(int i = 0; i < 8; ++i)
    arr[i] = color & (1 << (7 - i));
}

void set_color(unsigned int* arr, unsigned int red, unsigned int green, unsigned int blue)
{
    fill_array(arr, green);
    fill_array(&arr[8], red);
    fill_array(&arr[16], blue);
}

void set_leds_color(unsigned int red, unsigned int green, unsigned int blue)
{
    //prepare 24 color bits to send to LED
    unsigned int rgb_array[24];
    set_color(rgb_array, red, green, blue);

    int high_data = DATA_PORT | (1 << DATA_PIN);
    int low_data = DATA_PORT & (~(1 << DATA_PIN));

    for(int i = 0; i < 24; ++i){
        //send 1
        if(rgb_array[i]){   
            DATA_PORT = high_data;
            __builtin_avr_delay_cycles(6);    //1 clock cycle is 0.125us 
            DATA_PORT = low_data;
        }
        //send 0
        else{              
            DATA_PORT = high_data;
            __builtin_avr_delay_cycles(3);  //1 clock cycle is 0.125us 
            DATA_PORT = low_data;
        }
    }
}

int main(void)
{
    DATA_DDR = (1 << DATA_PIN);
    DATA_PORT &= ~(1 << DATA_PIN);

    initialize_UART();

    while(1)
    {
        int color_code[3];
        if(receive_color_from_uart(color_code))
        {
            set_leds_color(color_code[0], color_code[1], color_code[2]);
        }
    }

} 
Last edited by LockBot on Wed Dec 07, 2022 4:01 am, edited 3 times in total.
Reason: Topic automatically closed 30 days after creation. New replies are no longer allowed.
killer de bug

Re: Want to light up a WS2812B whatever color I want but I failed

Post by killer de bug »

You would have more chances to get an answer if you would register on the appropriate forum. :wink:
jerson
Level 3
Level 3
Posts: 181
Joined: Thu Dec 08, 2016 1:19 pm
Location: Bombay, India

Re: Want to light up a WS2812B whatever color I want but I failed

Post by jerson »

This seems to be arduino code, you will find more help on the arduino.cc forums.
Regards
Jerson
Locked

Return to “Open Chat”