When I searched the forum I could not find any posts about driving APA102 RGB LED strings on M2 chips using the hardware SPI module but I did discover this post M2 series hardware SPI by GrahamGo in 2012 using PEEKSFR and POKESFR to write a byte to, and read a byte from, an SPI chip using the hardware SPI module.
The purpose of this post is to provide some example programs using PEEKSFR and POKESFR to drive APA102 & SK9822 RGB LED strings using the hardware SPI module on all the M2 chip's.
These programs define the macro m2spi_init() to initialized the hardware SPI module and the macros m2spi_head(), m2spi_send() and m2spi_tail() to send sets of RGB colours to the LED string as well as the symbol constant definitions that are used by these macros.
My example programs:
- Copy GrahamGo's code to initialize the SPI hardware module, the only differences being that I've hardcoded the spi mode to use to drive APA102 LEDs in the m2spi_init() macro and I've formatted the code in my preferred way.
- Do not include GrahamGo's code to read the SDI register or to use the /CS [Chip Select] function as the APA102 & SK9822 RGB LED strings do not use these.
- Only require you to uncomment the correct dirsX commands for the chip you want to use. You can optionally update the value of the BRIGHTNESS constant or the PAUSE in the main loop to alter the speed of the display.
- Are tested on the 08M2, 14M2 & 18M2 chips. I have also provided the dirsX commands for the 20M2 chip but this is not tested as I do not own a 20M2 chip.
These example program can be used on any M2 chip as the Special Function Registers values and variable usage are the same:
M2SPI_Rainbow_fade.bas: Fade the whole string through the rainbow one colour at a time.
This example will work with strings of any length by setting the symbol HOW_MANY_LEDS.
M2SPI_Rainbow_Scroll.bas: Displays the rainbow along the length of the string, scrolling the colours to the left.
The storage array that these examples all use has a rainbow of 40 colours and that means this rainbow scrolling example will only work with strings up to 40 LEDs long. It works fine with shorter strings.
M2SPI_Breathing: Makes all the LEDs in the string do a "breathing" effect.
This example will work with strings of any length by setting the symbol HOW_MANY_LEDS.
To write your own programs all you have to do is:
1) Include these macros and the symbol constants that they use.
2) Execute the correct dirsX commands for the chip you want to use.
3) Update the symbol HOW_MANY_LEDS value to match how many LEDs your string has.
4) Update the symbol BRIGHTNESS that you want to use
5) Call the m2spi_init() macro to initialize the SPI module. Using m2spi_init(SpiSlow) is recommended before the main loop.
6) Make calls to the macros head, send and tail in the main loop to send sets of BGR values to the LED string.
NOTES:
Spimode01 is the correct mode to use to with the APA102 & SK9822 RGB LEDs so I have hardcoded in in the m2spi_init() macro.
Using spislow the SPI module transmits each byte in a fraction of the time that the PICAXE POKESFR command takes to complete. There is nothing to be gained by using a faster speed so I recommend that spislow is always used.
These LEDs are commonly referred to as RGB serial LEDs but with the APA102 & SK9822 RGB LEDs the bytes need to be sent in BGR order.
The Microchip documentation states "The pins must have their data direction bits appropriately programmed" so the dirsX commands in the example programs set the SDO & SCK pins as outputs on all M2 chips so:
The PIC microcontrollers also have a feature that allows the SDO function to be assigned to to a different pin from the default.
If you would like assistance to use the second SPI hardware module on the 18M2 & 20M2 chips simply post a question in the forum and I, or one of the other forum members, will be able to help you.
This link Arduino – LEDStrip effects for NeoPixel and FastLED has gifs showing different LED string displays. The code is Arduino but the post provides some inspiration for implementing similar displays using PICAXE chips.
The purpose of this post is to provide some example programs using PEEKSFR and POKESFR to drive APA102 & SK9822 RGB LED strings using the hardware SPI module on all the M2 chip's.
These programs define the macro m2spi_init() to initialized the hardware SPI module and the macros m2spi_head(), m2spi_send() and m2spi_tail() to send sets of RGB colours to the LED string as well as the symbol constant definitions that are used by these macros.
My example programs:
- Copy GrahamGo's code to initialize the SPI hardware module, the only differences being that I've hardcoded the spi mode to use to drive APA102 LEDs in the m2spi_init() macro and I've formatted the code in my preferred way.
- Do not include GrahamGo's code to read the SDI register or to use the /CS [Chip Select] function as the APA102 & SK9822 RGB LED strings do not use these.
- Only require you to uncomment the correct dirsX commands for the chip you want to use. You can optionally update the value of the BRIGHTNESS constant or the PAUSE in the main loop to alter the speed of the display.
- Are tested on the 08M2, 14M2 & 18M2 chips. I have also provided the dirsX commands for the 20M2 chip but this is not tested as I do not own a 20M2 chip.
These example program can be used on any M2 chip as the Special Function Registers values and variable usage are the same:
M2SPI_Rainbow_fade.bas: Fade the whole string through the rainbow one colour at a time.
This example will work with strings of any length by setting the symbol HOW_MANY_LEDS.
M2SPI_Rainbow_Scroll.bas: Displays the rainbow along the length of the string, scrolling the colours to the left.
The storage array that these examples all use has a rainbow of 40 colours and that means this rainbow scrolling example will only work with strings up to 40 LEDs long. It works fine with shorter strings.
M2SPI_Breathing: Makes all the LEDs in the string do a "breathing" effect.
This example will work with strings of any length by setting the symbol HOW_MANY_LEDS.
To write your own programs all you have to do is:
1) Include these macros and the symbol constants that they use.
2) Execute the correct dirsX commands for the chip you want to use.
3) Update the symbol HOW_MANY_LEDS value to match how many LEDs your string has.
4) Update the symbol BRIGHTNESS that you want to use
5) Call the m2spi_init() macro to initialize the SPI module. Using m2spi_init(SpiSlow) is recommended before the main loop.
6) Make calls to the macros head, send and tail in the main loop to send sets of BGR values to the LED string.
NOTES:
Spimode01 is the correct mode to use to with the APA102 & SK9822 RGB LEDs so I have hardcoded in in the m2spi_init() macro.
Using spislow the SPI module transmits each byte in a fraction of the time that the PICAXE POKESFR command takes to complete. There is nothing to be gained by using a faster speed so I recommend that spislow is always used.
These LEDs are commonly referred to as RGB serial LEDs but with the APA102 & SK9822 RGB LEDs the bytes need to be sent in BGR order.
The Microchip documentation states "The pins must have their data direction bits appropriately programmed" so the dirsX commands in the example programs set the SDO & SCK pins as outputs on all M2 chips so:
- The dirsX commands in the example programs set the SDI pin as an output on all M2 chips. I have tested that I can set the overridden SDI pin HIGH and LOW with no impact on the sending of RGB values to the LED string.
- The dirsX commands in the example programs set the SS pin as an input on the 18M2 & 20M2 chips. I did not test reading from overridden the SS pin.
The PIC microcontrollers also have a feature that allows the SDO function to be assigned to to a different pin from the default.
- I have attached the program M2SPI_Rainbow_Fade_APF.bas to post #2 of this thread showing how this is done.
- M2SPI_Rainbow_Fade_APF.bas is the example M2SPI_Rainbow_Fade.bas from this post modified to use the alternative pin for the SDO function for each chip where this is supported.
If you would like assistance to use the second SPI hardware module on the 18M2 & 20M2 chips simply post a question in the forum and I, or one of the other forum members, will be able to help you.
This link Arduino – LEDStrip effects for NeoPixel and FastLED has gifs showing different LED string displays. The code is Arduino but the post provides some inspiration for implementing similar displays using PICAXE chips.
Attachments
Last edited: