Skip to main content
Graduate II
November 20, 2024
Question

control algorithm for LED shift register

  • November 20, 2024
  • 8 replies
  • 8482 views

Friends, which algorithm do you use to control the LEDs in this structure? I need your ideas. I have prepared a table of the LEDs' turn on order.

ttaa

 

    This topic has been closed for replies.

    8 replies

    Technical Moderator
    November 20, 2024

    Hello,

    Not sure what do you mean by


    @XooM wrote:

    Friends, which algorithm do you use to control the LEDs in this structure? 

     

    Define a lookup table where you put your pattern.

    const uint16_t tab[] = {0x8080, 0x8040, ....};

    Then use this table to shift out the bits to the shift register.

    XooMAuthor
    Graduate II
    November 20, 2024

    Have you examined the electronic circuit?

    for example, what should be the algorithm to turn on led1 led2 led3 and led4?

    Because when led1 led2 led3 is active, the (2nd) 74hc4094 integrated circuit on the right has to give cathode information to all leds.
    If you turn on led4 while led1 led2 led3 is on, how will you ensure that led5 and led6 remain off?

    Super User
    November 20, 2024

    @XooM wrote:

    what should be the algorithm to turn on led1 led2 led3 and led4?


    That's what the table you posted tells you - it tells you what values to write to get each LED illuminated.

    You said you already had this working:

    https://community.st.com/t5/stm32-mcus-embedded-software/scan-inputs-and-drive-outputs-after-delay/m-p/741955/highlight/true#M56925:~:text=I%20said%20from%20the%20beginning%20that%20I%20can%20activate/pass%20the%20LEDs

     

    Super User
    November 20, 2024

    I thought you said you had the LEDs working already?

    Here: https://community.st.com/t5/stm32-mcus-embedded-software/scan-inputs-and-drive-outputs-after-delay/m-p/741955/highlight/true#M56925

    Surely, the table you've provided tells you what to do - load the appropriate values into U1 and U2 ?

    XooMAuthor
    Graduate II
    November 20, 2024

    Yes, I am doing this, I am sending the information of the LEDs that need to be lit with 250us. But the LEDs are dimly lit and there are very, very small traces of the LEDs that should not be lit.
    I wanted to get your opinions on whether I need to change the control method.
    Otherwise, I can control the LEDs, yes.

    Super User
    November 20, 2024

    @XooM wrote:

     I am sending the information of the LEDs that need to be lit with 250us. But the LEDs are dimly lit and there are very, very small traces of the LEDs that should not be lit..


    So why didn't you say that, then?

    What do you mean by, "sending the information of the LEDs that need to be lit with 250us" ?

    You should know by know that you need to provide a complete and clear description of what's going on - don't leave us guessing, and having to pull the information out of you bit-by-bit!

    Show a minimum but complete code example.

     

    PS:

    It's already been noted that there are no current-limiting resistors in your schematic.

    Is that just an omission from the schematic, or are they really not there in the actual hardware?

    Graduate II
    November 20, 2024

    A lot has already been said in this topic. I'd like to ask a few questions:

    1. What is the purpose of this project? (What are the requirements?)
    2. What are the LEDs connected to? 7 segment displays?
    3. Does each LED have its own current limiting resistor or are those shared by a group of 3?
    4. what strobe rate do you want?
    5. Are glitches in strobing allowed or do you not want LEDS to glitch momentarily?
    6. I see data of second shift unconnected that is not good. How do you intend to control that?

    The LED's need to be controlled via strobing if you want to allow all LEDs to be controlled individually (due to persistence of vision at sufficient strobing rates). Otherwise you cannot get all combinations working.

    In this case you have 8 groups of 3 (or 3 groups of 8 depending on how you see it)

    So I would implement the algorithm the following way:

    • set LED selector to 100
    • set group selectors to 1 for each group that has first LED on
    • delay T/3
    • set LED selector to 010
    • set group selectors to 1 for each group that has second LED on
    • delay T/3
    • set LED selector to 001
    • set group selectors to 1 for each group that has third LED on
    • delay T/3
    • repeat

    If you don't want any glitch you need write zeros to one of the shift registers prior to changing (dead time) the other or daisy chain them and Latch them synchronously (no overlap).

    The LEDs that need to be on are only on 1/3 of the time. For instance for 333us if you want a strobing rate of 1kHz. So you need to increase the current. If 10mA is the desired continuous current you need to use 30mA. But check the datasheet for maximum pulsed current for the LEDs.

    You can use a loop and simple bitshifting to check which group has an LED on. You can store all LEDs in a single uint32_t led_states . group_state[x] = led_states & 1<<(x*3+led_shift)

     

     

    Super User
    November 20, 2024

    @unsigned_char_array wrote:

    In this case you have 8 groups of 3 (or 3 groups of 8 depending on how you see it)


    I don't think that's true?

    I think one 11-bit pattern can be used to turn on any number of LEDs - including all or none?

     

    @XooM show the full schematic - transistor, all resistors, etc - for one group of three LEDs

    Or confirm if @mƎALLEm guessed correctly with this:

    AndrewNeil_0-1732114228000.png

    Is there really only one R1 per group of 3, or is there a separate series resistor for each LED ?

     

    Graduate II
    November 20, 2024

    @Andrew Neil wrote:

    @unsigned_char_array wrote:

    In this case you have 8 groups of 3 (or 3 groups of 8 depending on how you see it)


    I don't think that's true?

    I think one 11-bit pattern can be used to turn on any number of LEDs - including all or none?


    The groups are connected in parallel. So you cannot have:

     

    LED0: 1
    LED1: 0
    LED2: 0
    LED3: 0
    LED4: 0
    LED5: 1

     

    simultaneously.
    Because group 0 has LED0 on and group1 has LED3 off and group0 has LED2 off and group1 has LED5 on. Since both groups have active LEDs they need to be selected.
    With strobing it is possible because the eyes see fast pulsed LEDs as on. Either in 8 steps of 3 or 3 steps of 8. The latter is quicker and requires less peak current.

    Super User
    November 20, 2024

    @XooM What is driving you to use this board - which, apparently, has inadequate documentation and no support??

    XooMAuthor
    Graduate II
    November 20, 2024

    Friends, I will give you the document you want in a moment, but I do not understand how this forum works. In the country where I speak my own language, we can understand what we mean very quickly without giving too much detail. Here, people either have poor electronics knowledge or want to know everything.
    I just made a simplified drawing to ask for help from the algorithm.
    The missing parts are not relevant to the question I asked.
    I assumed that everything was complete and wanted to talk about the algorithm. There is no deficiency or error in the design of the circuit. The circuit is an actively working circuit.
    Since every programmer has a different perspective, I asked to learn your perspective on the event.
    I will explain the missing parts of the circuit and my own coding in a way you can understand.
    I will share it in 30 minutes.

    Super User
    November 20, 2024

    @XooM wrote:

    The missing parts are not relevant to the question I asked.


    Again, we had no way to know whether the missing parts were simply omitted from the diagram for clarity, or were an actual design error.

     


    @XooM wrote:

    Friends, I will give you the document you want in a moment, but I do not understand how this forum works. In the country where I speak my own language, we can understand what we mean very quickly without giving too much detail. .


    I think the real difference is between when you're discussing with people face-to-face, and when using a forum.

    When you discuss face-to-face, people can actually see what you're talking about - it's there in the room with them.

    So they have a lot more information & context than just what you say.

    When you post on a forum, we can see nothing other than what you put in your posts.

    We don't get any of that non-verbal context.

    That's why you need to give a lot more detail.

    And explain what you've omitted - and why.

    Super User
    November 20, 2024

    I made a spreadsheet to investigate possible combinations of LEDs: 

    AndrewNeil_0-1732120072724.png

     

    XooMAuthor
    Graduate II
    November 20, 2024

     

     

     

    #define SER_PIN GPIO_PIN_4 // SER_PIN
    #define SER_PORT GPIOB
    
    #define SRCLK_PIN GPIO_PIN_12 // SRCLK_PIN
    #define SRCLK_PORT GPIOA
    
    #define RCLK_PIN GPIO_PIN_5 // RCLK_PIN
    #define RCLK_PORT GPIOB
    
    
    
    void HC4094write()
    {
    	 HAL_GPIO_WritePin(RCLK_PORT, RCLK_PIN, GPIO_PIN_RESET);
    
     for(int i=0; i<16; i++)
     {
     if(currentVal & (1<<i))
     {
     HAL_GPIO_WritePin(SER_PORT, SER_PIN, GPIO_PIN_SET);
     }
     else
     {
     HAL_GPIO_WritePin(SER_PORT, SER_PIN, GPIO_PIN_RESET);
     }
     HAL_GPIO_WritePin(SRCLK_PORT, SRCLK_PIN, GPIO_PIN_RESET);
     HAL_GPIO_WritePin(SRCLK_PORT, SRCLK_PIN, GPIO_PIN_SET);
     }
     //HAL_GPIO_WritePin(RCLK_PORT, RCLK_PIN, GPIO_PIN_RESET);
     HAL_GPIO_WritePin(RCLK_PORT, RCLK_PIN, GPIO_PIN_SET);
    }
    void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
    {
    if(htim->Instance==TIM17) // 250us
    {
    if(input1==0)
    {
    currentVal=0b1000000010000000; //led1
    HC4094write();
    }
    if(input2==0)
    {
    currentVal=0b1000000001000000; // led2
    HC4094write();
    }
    if(input3==0)
    {
    currentVal=0b1000000000100000; //led3
    HC4094write();
    }
    if(input4==0)
    {
    currentVal=0b0100000010000000; //led4
    HC4094write();
    }
    
    }
    }

     

     

     

     

    Graduate II
    November 20, 2024

    No your code isnt ok , because led light if more as one input active only some us . if Only one input is active led light relative ok only one data is send to one led and repeat in time input is selected.

    But i mean your real situation not guarantie only one input selected ?

    TRY in timer only my code and set values in main based for all 24 leds.

    XooMAuthor
    Graduate II
    November 23, 2024

    Thank you.
    I can't understand your codes.
    I think I need to work a little.
    I can't understand some topics and what some commands do, I guess I can't think as complicated as you think.

    I have to work hard.
    I guess I have no choice but to use the commands I control.
    I have to work very, very hard. You are all good programmers.
    I am not at your level yet.

    Super User
    November 23, 2024
    Super User
    November 25, 2024

    But those all have just one LED connected to each shift-register bit - none of this multiplexed group stuff.