Actions

Difference between revisions of "Driving the WS2811 at 800 kHz with an 8 MHz AVR"

From Just in Time

(Attempt to correct syntax highlighting.)
 
(56 intermediate revisions by the same user not shown)
Line 1: Line 1:
'''Update''' (2013-5-23): Read about the 8-channel parallel version on [[Driving 8 WS2811 strips in parallel with an 8Mhz AVR|this page]]. More than 266kleds/second RGB from an 8 Mhz AVR!
+
This page describes how to drive a WS2811 from an 8Mhz or 9.6Mhz AVR like an Atmel ATmega88, ATtiny2313 or ATtiny13 '''without added components''' such as an external oscillator. With only 30 instructions for the driver loop (110 bytes for the complete function), the driver code presented here is likely to be the [https://github.com/DannyHavenith/ws2811/blob/master/ws2811/ws2811_8.h#L49 shortest code] that delivers the exact timing (i.e. exactly 10 clock cycles for 1 byte, exactly 60 clock cycles for 6 bytes, etc.). With even less code a "sparse driver" for 9.6Mhz MCUs can drive long led strings with only a few bytes of buffer in RAM.
 
 
WS2811 LED controllers are hot. Projects using WS2811 led strips have been featured on [http://hackaday.com/?s=ws2811 HackaDay] several times in the last few months. One feature showed how an AVR clocked at 16Mhz could send data at the required high rates. Inspired by this, I ordered an LED strip and 16Mhz oscillators from ebay. The LED strip arrived quickly, only the oscillators took weeks to arrive, which gave me plenty of time to think about the possibility of driving these led strips from an 8Mhz atmega88 without an external oscillator. With only 10 clock ticks per bit, this was going to be a challenge.
 
 
 
It turns out that this is doable, with the same output timing as the 16Mhz version...
 
 
 
On this page I'll describe how to drive a WS2811 from an 8Mhz AVR like an Atmel ATmega88, or ATtiny2313 '''without added components''' such as an external oscillator. I'll also describe the techniques I used to create the strictly timed assembly code that is necessary to do so. Most notably, I'll describe how:
 
* I used a spreadsheet (or in fact: some tabular layout) instead of an IDE so that I could keep an eye on the timing of each instruction I write;
 
* assembly code fragments were combined using a dedicated C++ program to minimize jump distances.
 
  
 +
Of course, if you're creating a hardware project that controls more than 1 LED, you're going to have to demonstrate it with a Knight Rider LED sequence (which, I just learned, is actually called a [https://en.wikipedia.org/wiki/Glen_A._Larson Larson] scanner)... The sources for all the demonstrations in these videos can be found on [https://github.com/DannyHavenith/ws2811 github].
 +
{|
 +
|- valign="top"
 +
|colspan="3"|{{#ev:youtube|mP7vBw4UeME|400|left|Knight Rider on Steroids ([https://github.com/DannyHavenith/ws2811/blob/master/effects/chasers.hpp])}}
 +
|- valign="top"
 +
|{{#ev:youtube|MdX45a3OFS0|300||the [https://hackaday.io/project/9393-wheres-my-christmas-light/log/50423-the-most-simplest-is-the-most-easiest where's my christmas light] project}}
 +
|{{#ev:youtube|_wkTtAk2V_k|300||"Water Torture" or "Lava Drops" demo ([https://github.com/DannyHavenith/ws2811/blob/master/effects/water_torture.hpp source code], [[WS2811 "Water torture"|details]])}}
 +
|{{#ev:youtube|jAm7nVRvY_I|300||[[Driving a large WS2811 LED string with an ATtiny13 and nothing else|Special sparse driver]] allows an attiny13 to drive arbitrarily large LED strings from 64 bytes of memory}}
 +
|- valign="top"
 +
|{{#ev:youtube|mItPxAtkuVI|300||Flares demo on an attiny13 ([https://github.com/DannyHavenith/ws2811/blob/master/effects/flares.hpp source code])}}
 +
|{{#ev:youtube|VM84Q1vWSZE|300||Fireflies!}}
 +
|{{#ev:youtube|otCWPGf6O6w|300||Fire without flies}}
 +
|}
 +
<div style="clear: both"></div>
 +
==Download source code==
 +
The library is header-only. Using it is a matter of downloading the source code and including it in your AVR code. Example source code can be found [https://github.com/DannyHavenith/ws2811 here]. The code comes as an avr-eclipse project consisting for a large part of C++ demonstration code and the main driver function in assembly, in files [https://github.com/DannyHavenith/ws2811/blob/master/ws2811/ws2811_8.h ws2811_8.h] and [https://github.com/DannyHavenith/ws2811/blob/master/ws2811/ws2811_96.h ws2811_96.h] (for the 9.6Mhz version).
  
==Download==
+
I don't recommend trying to understand the assembly code by reading these sources. How the code functions is described [[#Defining the puzzle|below]]. Usage information is in [[#Usage|the next section]]. The rest of this page describes the 8Mhz version. The 9.6Mhz code was added later, but is created in the same way.
For the impatient: example source code can be found [https://github.com/DannyHavenith/ws2811 here]. The code comes as an avr-eclipse project consisting for a large part of C++ demonstration code and the main driver function in assembly, in file [https://github.com/DannyHavenith/ws2811/blob/master/src/ws2811_8.h ws2811_8.h]. I don't recommend trying to understand the assembly code by reading these sources. How the code functions is described on [[Ws2811 driver code explained|another page]]. Usage information can be found [[#Usage|after the videos]].
 
  
==Demo==
+
'''New:''' If you'd like to see the assembly code in action, take a look at the [http://rurandom.org/avrgo_demo/ online AVR emulator]!
And of course, if you're creating a hardware project that controls more than 1 LED, you're going to have to demonstrate it with a Knight Rider display...
+
[[File:Avrgo demo screenshot.png|400px|link=https://rurandom.org/avrgo_demo/]]
 
 
{{#ev:youtube|mP7vBw4UeME|400|left|Knight Rider on Steroids}}{{#ev:youtube|_wkTtAk2V_k|400||"Water Torture" or "Lava Drops" demo}}
 
<div style="clear: both"></div>
 
  
 
==Usage==
 
==Usage==
You'll need the C++ compiler for this to work (turning ws2811.h into "pure C" is left as an exercise to the reader). A simple example of how this works is as follows:
+
You'll need the C++ compiler for this to work (turning ws2811.h into "pure C" is left as an exercise to the reader). I am told that this works just as good for an Arduino, but I haven't tested this myself. Remember that this code was written and optimized for 8Mhz and 9.6Mhz, it would run too fast on an 16Mhz Arduino. From the sources, you'll need files ''ws2811.h'', ''ws2811_8.h'', ''ws2811_96.h'' and ''rgb.h'', though you only include "ws2811.h". A simple example of how to use this code:
  
<source lang='cpp'>
+
<syntaxhighlight lang="c++">
 
#include <avr/io.h> // for _BV()
 
#include <avr/io.h> // for _BV()
  
 
#define WS2811_PORT PORTD// ** your port here **
 
#define WS2811_PORT PORTD// ** your port here **
#include "ws2811_8.h"
+
#include "ws2811.h" // this will auto-select the 8Mhz or 9.6Mhz version
  
 
using ws2811::rgb;
 
using ws2811::rgb;
Line 43: Line 47:
  
 
   // send the RGB-values in buffer via pin 3
 
   // send the RGB-values in buffer via pin 3
   // you can control up to 8 led strips from one AVR with this code, just
+
   // you can control up to 8 led strips from one AVR with this code, as long as they
 +
  // are connected to pins of the same port. Just
 
   // provide the pin number that you want to send the values to here.
 
   // provide the pin number that you want to send the values to here.
 
   send( buffer, output_pin);
 
   send( buffer, output_pin);
Line 52: Line 57:
 
   for(;;);
 
   for(;;);
 
}
 
}
 +
</syntaxhighlight>
  
</source>