This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TMS320F280049C: Blink the WS2812B neo pixel leds using tms320f280049c

Part Number: TMS320F280049C

Tool/software:

I am using TMS320F280049C microcontroller and i want to use the Ws2812B neo pixel led's to light up in my desired colour and in my code the it turns on all the  number of mentioned led's white and not my desired colour. I will attach my code. Please help me resolve this issue as soon as possible. Thank You.

#include "device.h"
#include "gpio.h"

#define NEOPIXEL_PIN  4
#define NUM_LEDS      2               // Number of LEDs
#define BLINK_DELAY   500000          // 500ms delay

// GRB color values (0-255)
#define RED           0, 255, 0       // Green component for red color
#define GREEN         255, 0, 0       // Red component for green color
#define BLUE          0, 0, 255       // Blue component

// System clock frequency in MHz for cycle calculations
#define SYS_FREQ_MHZ (DEVICE_SYSCLK_FREQ / 1000000)

// Timing parameters (in microseconds)
#define T0H           0.4f            // High time for '0' bit
#define T1H           0.8f            // High time for '1' bit
#define T0L           0.85f           // Low time for '0' bit
#define T1L           0.45f           // Low time for '1' bit

// Convert timing to CPU cycles
#define CYCLES_PER_US SYS_FREQ_MHZ
#define T0H_CYCLES   (uint32_t)(T0H * CYCLES_PER_US)
#define T1H_CYCLES   (uint32_t)(T1H * CYCLES_PER_US)
#define T0L_CYCLES   (uint32_t)(T0L * CYCLES_PER_US)
#define T1L_CYCLES   (uint32_t)(T1L * CYCLES_PER_US)

void delay_cycles(uint32_t cycles) {
    while (cycles--) {
        __asm(" NOP");
    }
}

void send_neopixel_byte(uint8_t byte, uint16_t pin) {
    for (int i = 7; i >= 0; i--) {
        uint8_t bit_val = (byte >> i) & 1;
        
        GPIO_writePin(pin, 1);                // Signal start of bit
        
        if (bit_val) {
            delay_cycles(T1H_CYCLES - 2);     // High pulse for '1'
            GPIO_writePin(pin, 0);
            delay_cycles(T1L_CYCLES - 2);     // Low period for '1'
        } else {
            delay_cycles(T0H_CYCLES - 2);     // High pulse for '0'
            GPIO_writePin(pin, 0);
            delay_cycles(T0L_CYCLES - 2);     // Low period for '0'
        }
    }
}

void set_all_leds(uint8_t green, uint8_t red, uint8_t blue) {
    for (int j = 0; j < NUM_LEDS; j++) {
        send_neopixel_byte(green, NEOPIXEL_PIN);  // GRB order
        send_neopixel_byte(red, NEOPIXEL_PIN);
        send_neopixel_byte(blue, NEOPIXEL_PIN);
    }
    GPIO_writePin(NEOPIXEL_PIN, 0);   // Reset signal
    delay_us(60);                      // Hold low for 60µs
}

void main(void) {
    Device_init();
    Device_initGPIO();
    GPIO_setDirectionMode(NEOPIXEL_PIN, GPIO_DIR_MODE_OUT);

    while (1) {
        set_all_leds(GREEN);           // Green component for red color
        delay_us(BLINK_DELAY);
        set_all_leds(0, 0, 0);         // Turn off LEDs
        delay_us(BLINK_DELAY);
    }
}
 

  • Hello,

    Please allow another day for me to review this query. Thanks for your patience!

    Best Regards,

    Allison

  • ok fine. please give me the full code that turns on the led with any colour other than white so that i can verify that with mine and also the problem I am facing is with the timing issue I think. It will be very helpful for me if you give me the full code. Thank you for your quick response.

  • Hello,

    I unfortunately cannot help on the WS2812B side as my expertise is only regarding the TMS320F280049C device. Have you already scoped the GPIOs and ensure your outputs are as expected? I am not versed in this particular setup, so I cannot provide a code solution for you. However, I can provide guidance to try to help debug the issue. If you have any specific targeted questions regarding the F280049C device (configurations, etc.), I will be able to help provide further guidance there. 

    Best Regards,

    Allison

  • #include "driverlib.h"
    #include "device.h"
    #include "gpio.h"
    
    #define NEOPIXEL_PIN  4
    #define NUM_LEDS      2
    #define BLINK_DELAY   500000
    
    
    #define RED           0, 255, 0
    #define GREEN         25, 10, 20
    #define BLUE          69, 98, 25
    
    //#define SYS_FREQ_MHZ (DEVICE_SYSCLK_FREQ/10000000)
    
    #define T0H           0.4f
    #define T1H           0.8f
    #define T0L           0.85f
    #define T1L           0.45f
    int i,j;
    
    #define T0H_CYCLES   (uint32_t)((uint64_t)DEVICE_SYSCLK_FREQ * 0.4 / 10000000)
    #define T1H_CYCLES   (uint32_t)((uint64_t)DEVICE_SYSCLK_FREQ * 0.8 / 10000000)
    #define T0L_CYCLES   (uint32_t)((uint64_t)DEVICE_SYSCLK_FREQ * 0.85 / 10000000)
    #define T1L_CYCLES   (uint32_t)((uint64_t)DEVICE_SYSCLK_FREQ * 0.45 / 10000000)
    
    
    
    void send_neopixel_byte(uint8_t byte, uint16_t pin) {
        for ( i = 7; i >= 0; i--) {
            uint8_t bit_val = (byte >> i) & 1;
            if (bit_val) {
                GPIO_writePin(pin, 1);
                DEVICE_DELAY_US(0.8);
                GPIO_writePin(pin, 0);
                DEVICE_DELAY_US(0.45);
            } else {
                GPIO_writePin(pin, 1);
                DEVICE_DELAY_US(0.4);
                GPIO_writePin(pin, 0);
                DEVICE_DELAY_US(0.85);
            }
        }
    }
    
    void set_all_leds(uint8_t green, uint8_t red, uint8_t blue) {
        for ( j = 0; j < NUM_LEDS; j++) {
            send_neopixel_byte(green, NEOPIXEL_PIN);
            send_neopixel_byte(red, NEOPIXEL_PIN);
            send_neopixel_byte(blue, NEOPIXEL_PIN);
    
        }
        GPIO_writePin(NEOPIXEL_PIN, 0);
        DEVICE_DELAY_US(50);
    }
    
    void main(void) {
        Device_init();
        Device_initGPIO();
        GPIO_setDirectionMode(NEOPIXEL_PIN, GPIO_DIR_MODE_OUT);
    
        while (1) {
            set_all_leds(RED);
            DEVICE_DELAY_US(BLINK_DELAY);
            set_all_leds(0, 0, 0);
            DEVICE_DELAY_US(BLINK_DELAY);
        }
    }
    

    That's alright. Can you add someone who has some expertise in the WS2812B neopixel led's so that I can verify with them and i am pretty sure that i have timing issue. If possible please check on that. I have added my modified code but still the same issue only turning led's in  white colour.  Thank you so much.

  • Hello,

    The WS2812B is not a TI-owned device, so we cannot support inquiries specific to this. You will have to reach out to that device group for support.

    From a quick search, it does seem like timing requirements are quite strict to support the LED. Hence, I would advise you use NOPs instead of DEVICE_DELAY_US(x). The DEVICE_DELAY_US(x) is used to achieve a delay on the order of microseconds as defined in the device.h file:

    I would also double check your FOR loop and make sure "i" is designated as a signed integer type to satisfy your loop condition.

    Best Regards,

    Allison