Electronics > Beginners

WSD2811 led time code in C, optimize further

(1/2) > >>

Vindhyachal.takniki:
1. I am using WSD2811 to run led in series. I am using C-code for programming. My below code is working by carefully adjusting some of NOP instructions.

2. 100 leds are working fine on WSD2811, my code is in C, so no issues. Dont want to write in assembly.

3. Below is my code, in which I check each bit of red/green/blue by if/else, then make pin high/low

4. Want to know if there any other way I can optimize code in c language or better way to do that in C-language.

5. Please dont suggest to write in assembly or if code is already working why to change.
 
I want to write in C only, & check if there any way I can further optimize code in C language.

Again by current code, my leds are working fine



--- Code: ---static void ws2811_rgb_write(uint8_t red, uint8_t green , uint8_t blue) 
{
//R1
    if(red & 0x80U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    } 
///   
   
//R2
    if(red & 0x40U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
/// 

//R3
    if(red & 0x20U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
///     
   
//R4
    if(red & 0x10U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
/// 

//R5
    if(red & 0x08U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
///   

//R6
    if(red & 0x04U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }   
///     

//R7
    if(red & 0x02U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
///   

//R8
    if(red & 0x01U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
///   


   
   
//R1
    if(green & 0x80U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    } 
///   
   
//R2
    if(green & 0x40U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
/// 

//R3
    if(green & 0x20U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }   
///     
   
//R4
    if(green & 0x10U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }       
/// 

//R5
    if(green & 0x08U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
///   

//R6
    if(green & 0x04U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
///     

//R7
    if(green & 0x02U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
///   

//R8
    if(green & 0x01U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
///       

   
   
   
//R1
    if(blue & 0x80U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    } 
///   
   
//R2
    if(blue & 0x40U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
/// 

//R3
    if(blue & 0x20U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
///     
   
//R4
    if(blue & 0x10U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
/// 

//R5
    if(blue & 0x08U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
///   

//R6
    if(blue & 0x04U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
///     

//R7
    if(blue & 0x02U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
///   

//R8
    if(blue & 0x01U)
    {
        LED_PIN_HIGH();
    } 
    else
    {
        LED_PIN_LOW();
    }     
///       
   
     
 
}

--- End code ---

garethw:
I believe that apart from purchasing a more efficient compiler the only alternative is what you’ve told us not to tell you. ;)


Sent from my iPhone using Tapatalk

cgroen:
Get a CPU that will allow you to handle some of that timing direct in hardware....
I use a LPC1549 that uses one of its SCT modules to handle this. I do all in C and have no problem with timing ;)

garethw:
What is it that isn’t working? You said it works with 100 leds. Why try to optimise if it already works.
I’m not the best person to comment as I really like assembly and only use C for quick jobs if I’m using an unfamiliar mcu.


Sent from my iPhone using Tapatalk

Kjelt:
Easy optimization is a for loop per led  color databyte where you just shift left and use the same bitmask 0x80.
Code looks cleaner that way and others can easier understand the code IMO.

BTW working with NOPs for bitbanging is a last resort if you really need the extreme speed.
It will only work on that uC , others it might fail due to timing issues. Cleaner to use a timer peripheral for the exact timing, but as said this might be impossible in some extreme cases.

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod