• Full Color Dimmable RGB LED with only two PWM outputs

    greg12/16/2021 at 04:55 0 comments

    Every design requires some compromises.  This is especially true when aiming for backward compatibility, and was certainly a factor when designing KUIIC to be compatible with XIAO boards. 

    After assigning SPI, I2C, UART and Analog capable pins to match the pinout, I was able to put get all 6 timer 0 pins and both timer 1 pins on the expansion header for user access.  This left only the 2 timer 2 PWM outputs available to drive the on board RGB LED.  PWM outputs are nice for LEDs because they make dimming easier.  Some may say that 2 PWM outputs are not enough to drive an RGB LED, but I beg to differ.  It just takes a little inspiration from Charlie Allen.

    Consider the circuit below...

    Let's look at the truth table for the logical combinations of B/R# and G/B#:

    B/R#G/B#RedGreenBlue
    ZZoffoffoff
    00ONoffoff
    01ONONoff
    10offoffON
    11offON
    off

    You can see that if we include tri-state, there are 5 combinations including red, blue, green and we get yellow as a bonus.  These 5 combinations are easily created with simple GPIO controls (we'll leave that as an exercise for the reader), but maybe we can do better. 

    Follow the process in the timing diagram below.  If we start pulses simultaneously on B/R# and G/B# the green LED will light.  If we hold B/R# and drop G/B# it will switch to blue.  If we then drop B/R# it will transition to red.  Tri-stating both pins turns all the LEDs off. This can all be done with a single dual channel timer.  The G/B# channel counts the green period.  B/R# counts both green and blue periods.  After the terminal count of green + blue + red, timer gets disabled.  The timer in K32L2B is not able to disable the outputs automatically, but it can trigger a DMA transfer that is able to disable the timer and its outputs.  This means the timer needs to be restarted regularly, but that is easily accomplished with the SysTick timer.

    A functional example of this can be found in the KUIIC port of TinyUF2.