Menu

APA102 addressable LED program.

mkstevo
2022-07-04
2022-07-06
  • mkstevo

    mkstevo - 2022-07-04

    I had a need to test some displays that use the APA102 addressable LEDs. These have four wire connections, 5V, Clock, Data and Ground (0V).

    The data format is one with a requirement where a start frame is sent, a number of 'LED' frames (to suit the number of LEDs in the display) followed by an end frame.

    There are some confusing datasheets out on the 'net and it took me a while to get my head around the data formatting needed to illuminate the LEDs.

    The program initially clears the display, sets all LEDs to Red, then Green, then Blue. It then lights each LED with a random colour before the pattern repeats, but more slowly, due to the RedDisplay(), GreenDisplay() and BlueDisplay() routines being called with their SlowD parameter set to 1 which incorporates longer delays between each LEDs data frame.

    Not very exciting, but it did at least allow me to test the display matrixes I had, and prove which were working and which were not.

    I couldn't find any programs for this display in BASIC so thought I'd post it in the hope that someone might be able to make use of it.

    #Option Explicit
    'Quick and dirty program for the APA102 addressable LED's with
    '5V, Clock, Data and Ground connections.
    
    'The data format is:
    'Start frame
    '32 bits at Low
    '
    'Frame for each LED
    '3 bits at High.
    '5 bits of global brightness data
    '8 bits Blue data
    '8 bits Green data
    '8 bits Red data
    '
    'End frame
    '32 bits at high
    
    'It was suggested that after 64 LED frames an 'End frame' should be sent
    'before sending the next LED frames.
    '
    'I found that didn't seem to be required so send a set of 113 LED frames.
    '
    'Although the LED array I had only 112 LEDs were fitted. I had to send 113
    'LED frames to ensure the final LED was correctly illuminated.
    'Without that last frame the final LED could be left fully lit?
    
    #Chip 16F15313, 32
    #Config CP=On
    'Read protected
    
    
    #Define D_Clk1        PortA.5 'Pin 2
    #Define D_Data        PortA.4 'Pin 3
    
    
    #Define Elements 113
    
    Dir     D_Clk1        Out
    Dir     D_Data        Out
    
    Dim RandData As Byte
    
    Let D_Clk1 = 0
    
    ClearDisplay(0)
    RedDisplay(0)
    Wait 500 mS
    GreendDisplay(0)
    Wait 500 mS
    BlueDisplay(0)
    Wait 500 mS
    
    Do
      RandomDisplay
      RedDisplay(1)
      GreendDisplay(1)
      BlueDisplay(1)
    Loop
    
    Sub RandomDisplay
      'Send Start Frame
      Let D_Data = 0
      Repeat 32
        ClockOut
      End Repeat
    
      Repeat Elements
        'Send Global frame
        Let D_Data = 1
        ClockOut
    
        Let D_Data = 1
        ClockOut
    
        Let D_Data = 1
        ClockOut
    
        'Send brightness frame
        Let D_Data = 0
        ClockOut
    
        Let D_Data = 0
        ClockOut
    
        Let D_Data = 0
        ClockOut
    
        Let D_Data = 0
        ClockOut
    
        Let D_Data = 1
        ClockOut
    
    
        'Send Blue frame
        Let RandData = Random
        Let D_Data = RandData.0
        ClockOut
    
        Let D_Data = RandData.1
        ClockOut
    
        Let D_Data = RandData.2
        ClockOut
    
        Let D_Data = RandData.3
        ClockOut
    
        Let D_Data = RandData.4
        ClockOut
    
        Let D_Data = RandData.5
        ClockOut
    
        Let D_Data = RandData.6
        ClockOut
    
        Let D_Data = RandData.7
        ClockOut
    
    
        'Send Red frame
        Let RandData = Random
        Let D_Data = RandData.0
        ClockOut
    
        Let D_Data = RandData.1
        ClockOut
    
        Let D_Data = RandData.2
        ClockOut
    
        Let D_Data = RandData.3
        ClockOut
    
        Let D_Data = RandData.4
        ClockOut
    
        Let D_Data = RandData.5
        ClockOut
    
        Let D_Data = RandData.6
        ClockOut
    
        Let D_Data = RandData.7
        ClockOut
    
        'Send Green frame
        Let RandData = Random
        Let D_Data = RandData.0
        ClockOut
    
        Let D_Data = RandData.1
        ClockOut
    
        Let D_Data = RandData.2
        ClockOut
    
        Let D_Data = RandData.3
        ClockOut
    
        Let D_Data = RandData.4
        ClockOut
    
        Let D_Data = RandData.5
        ClockOut
    
        Let D_Data = RandData.6
        ClockOut
    
        Let D_Data = RandData.7
        ClockOut
    
      End Repeat
    
      'Send End Frame
      Let D_Data = 1
      Repeat 32
        ClockOut
      End Repeat
    
      Let D_Data = 0
    
    End Sub
    
    Sub ClockOut
        Let D_Clk1 = 1
        Wait 500 uS
        Let D_Clk1 = 0
        Wait 500 uS
    End Sub
    
    Sub KwikOut
        Let D_Clk1 = 1
        'Wait 1 uS
        Let D_Clk1 = 0
        'Wait 1 uS
    End Sub
    
    Sub ClearDisplay(SlowD As Byte)
    If SlowD = 0 Then
      'Send Start Frame
      Let D_Data = 0
      Repeat 32
        KwikOut
      End Repeat
    
      Repeat Elements
        Let D_Data = 1
        Repeat 3
          KwikOut
        End Repeat
    
        Let D_Data = 0
        'five brightness bits
        Repeat 5
          KwikOut
        End Repeat
        '24 RGB bits (8 x Red, 8 x Green, 8 x Blue)
        Repeat 24
          KwikOut
        End Repeat
    
      End Repeat
    
      'Send End Frame
      Let D_Data = 1
      Repeat 32
        KwikOut
      End Repeat
    
      Let D_Data = 0
    Else
      'Send Start Frame
      Let D_Data = 0
      Repeat 32
        ClockOut
      End Repeat
    
      Repeat Elements
        Let D_Data = 1
        Repeat 3
          ClockOut
        End Repeat
    
        Let D_Data = 0
        'five brightness bits
        Repeat 5
          ClockOut
        End Repeat
        '24 RGB bits (8 x Red, 8 x Green, 8 x Blue)
        Repeat 24
          ClockOut
        End Repeat
    
      End Repeat
    
      'Send End Frame
      Let D_Data = 1
      Repeat 32
        ClockOut
      End Repeat
    
      Let D_Data = 0
    End If
    End Sub
    
    Sub RedDisplay(SlowD As Byte)
    If SlowD = 0 Then
      'Send Start Frame
      Let D_Data = 0
      Repeat 32
        KwikOut
      End Repeat
    
      Repeat Elements
        Let D_Data = 1
        Repeat 3
          KwikOut
        End Repeat
    
        Let D_Data = 0
        'Four brightness bits of zero
        Repeat 4
          KwikOut
        End Repeat
        'One brightness bit of one
        Let D_Data = 1
        KwikOut
    
        '24 RGB bits (8 x Blue, 8 x Green, 8 x Red
        Let D_Data = 0
        Repeat 8
          KwikOut
        End Repeat
    
        Let D_Data = 0
        Repeat 8
          KwikOut
        End Repeat
    
        Let D_Data = 1
        Repeat 8
          KwikOut
        End Repeat
    
      End Repeat
    
      'Send End Frame
      Let D_Data = 1
      Repeat 32
        KwikOut
      End Repeat
    
      Let D_Data = 0
    Else
      'Send Start Frame
      Let D_Data = 0
      Repeat 32
        ClockOut
      End Repeat
    
      Repeat Elements
        Let D_Data = 1
        Repeat 3
          ClockOut
        End Repeat
    
        Let D_Data = 0
        'Four brightness bits of zero
        Repeat 4
          ClockOut
        End Repeat
        'One brightness bit of one
        Let D_Data = 1
        ClockOut
    
        '24 RGB bits (8 x Blue, 8 x Green, 8 x Red
        Let D_Data = 0
        Repeat 8
          ClockOut
        End Repeat
    
        Let D_Data = 0
        Repeat 8
          ClockOut
        End Repeat
    
        Let D_Data = 1
        Repeat 8
          ClockOut
        End Repeat
    
      End Repeat
    
      'Send End Frame
      Let D_Data = 1
      Repeat 32
        ClockOut
      End Repeat
    
      Let D_Data = 0
    End If
    End Sub
    
    Sub GreendDisplay(In SlowD As Byte)
    
    If SlowD = 0 Then
      'Send Start Frame
      Let D_Data = 0
      Repeat 32
        KwikOut
      End Repeat
    
      Repeat Elements
        Let D_Data = 1
        Repeat 3
          KwikOut
        End Repeat
    
        Let D_Data = 0
        'Four brightness bits of zero
        Repeat 4
          KwikOut
        End Repeat
        'One brightness bit of one
        Let D_Data = 1
        KwikOut
    
        '24 RGB bits (8 x Blue, 8 x Green, 8 x Red
        Let D_Data = 0
        Repeat 8
          KwikOut
        End Repeat
    
        Let D_Data = 1
        Repeat 8
          KwikOut
        End Repeat
    
        Let D_Data = 0
        Repeat 8
          KwikOut
        End Repeat
    
      End Repeat
    
      'Send End Frame
      Let D_Data = 1
      Repeat 32
        KwikOut
      End Repeat
    
      Let D_Data = 0
    Else
      'Send Start Frame
      Let D_Data = 0
      Repeat 32
        ClockOut
      End Repeat
    
      Repeat Elements
        Let D_Data = 1
        Repeat 3
          ClockOut
        End Repeat
    
        Let D_Data = 0
        'Four brightness bits of zero
        Repeat 4
          ClockOut
        End Repeat
        'One brightness bit of one
        Let D_Data = 1
        ClockOut
    
        '24 RGB bits (8 x Blue, 8 x Green, 8 x Red
        Let D_Data = 0
        Repeat 8
          ClockOut
        End Repeat
    
        Let D_Data = 1
        Repeat 8
          ClockOut
        End Repeat
    
        Let D_Data = 0
        Repeat 8
          ClockOut
        End Repeat
    
      End Repeat
    
      'Send End Frame
      Let D_Data = 1
      Repeat 32
        ClockOut
      End Repeat
    
      Let D_Data = 0
    End If
    End Sub
    
    Sub BlueDisplay(SlowD As Byte)
    
    If SlowD = 0 Then
    
      'Send Start Frame
      Let D_Data = 0
      Repeat 32
        KwikOut
      End Repeat
    
      Repeat Elements
        Let D_Data = 1
        Repeat 3
          KwikOut
        End Repeat
    
        Let D_Data = 0
        'Four brightness bits of zero
        Repeat 4
          KwikOut
        End Repeat
        'One brightness bit of one
        Let D_Data = 1
        KwikOut
    
        '24 RGB bits (8 x Blue, 8 x Green, 8 x Red
        Let D_Data = 1
        Repeat 8
          KwikOut
        End Repeat
    
        Let D_Data = 0
        Repeat 8
          KwikOut
        End Repeat
    
        Let D_Data = 0
        Repeat 8
          KwikOut
        End Repeat
    
      End Repeat
      'Send End Frame
    
      Let D_Data = 1
      Repeat 32
        KwikOut
      End Repeat
    
      Let D_Data = 0
    Else
      'Send Start Frame
      Let D_Data = 0
      Repeat 32
        ClockOut
      End Repeat
    
      Repeat Elements
        Let D_Data = 1
        Repeat 3
          ClockOut
        End Repeat
    
        Let D_Data = 0
        'Four brightness bits of zero
        Repeat 4
          ClockOut
        End Repeat
        'One brightness bit of one
        Let D_Data = 1
        ClockOut
    
        '24 RGB bits (8 x Blue, 8 x Green, 8 x Red
        Let D_Data = 1
        Repeat 8
          ClockOut
        End Repeat
    
        Let D_Data = 0
        Repeat 8
          ClockOut
        End Repeat
    
        Let D_Data = 0
        Repeat 8
          ClockOut
        End Repeat
    
      End Repeat
      'Send End Frame
    
      Let D_Data = 1
      Repeat 32
        ClockOut
      End Repeat
    End If
    
    End Sub
    
     
  • Anobium

    Anobium - 2022-07-04

    Cool.

    Which part/display was this?

     
  • mkstevo

    mkstevo - 2022-07-06

    It is a string of LEDs arranged by the Chinese manufacturer of some of the machines we import into a 4 x seven segment LED display. The manufacturer also uses the same devices in "strings" for lighting the cabinets.

    The part number is APA102, though the ones installed in the displays I have could be "clones".

    I should add that in the program above the global brightness is set to 1 by setting the first four bits of the brightness frame to 0, then the final bit of the brightness frame set to 1.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.