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

Chat about just about anything else
Post Reply
Tianshi
Level 1
Level 1
Posts: 1
Joined: Fri Mar 09, 2018 2:02 am

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

Post by Tianshi » Fri Mar 09, 2018 2:26 am

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 killer de bug on Sat Mar 10, 2018 4:32 am, edited 2 times in total.
Reason: moved to openchat section as this is not about Linux. Removed the useless link.

User avatar
killer de bug
Level 14
Level 14
Posts: 5390
Joined: Tue Jul 08, 2008 1:49 pm
Location: Graz, Austria

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

Post by killer de bug » Fri Mar 09, 2018 3:03 am

You would have more chances to get an answer if you would register on the appropriate forum. :wink:
Image
If it ain't broke, fix it until it is.

jerson
Level 3
Level 3
Posts: 140
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 » Fri Mar 09, 2018 3:09 am

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

Post Reply

Return to “Open chat”