Go Down

Topic: dmx simple speed issues (Read 432 times) previous topic - next topic


Jun 08, 2012, 08:50 pm Last Edit: Jun 08, 2012, 09:08 pm by mrboni Reason: 1
Hi, I'm having trouble sending any large number ( 60+) of dmx channels simultaneously at a decent framerate (30+fps).  The library can supposedly handle the full 512 channel count of the dmx512 standard. 

I'm using a mega2560 and a dmx shield.

With this I'm using dmxsimple.cpp. The function to update a channel - DmxSimple.write(channel, brightness); only allows you to update one channel at a time, then another function in the lib updates the 'frame' with this new value. Is it possible to modify the library to allow the whole buffer array to be updated first, then the update function be called?

(I may have misunderstood how this is working btw)

Here is the code for dmxsimple.cpp. Hope you can help!...

Code: [Select]
* DmxSimple - A simple interface to DMX.
* Copyright (c) 2008-2009 Peter Knight, Tinker.it! All rights reserved.
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "pins_arduino.h"

#include "Arduino.h"
#include "DmxSimple.h"

/** dmxBuffer contains a software copy of all the DMX channels.
volatile uint8_t dmxBuffer[DMX_SIZE];
static uint16_t dmxMax = 16; /* Default to sending the first 16 channels */
static uint8_t dmxStarted = 0;
static uint16_t dmxState = 0;

static volatile uint8_t *dmxPort;
static uint8_t dmxBit = 0;
static uint8_t dmxPin = 3; // Defaults to output on pin 3 to support Tinker.it! DMX shield

void dmxBegin();
void dmxEnd();
void dmxSendByte(volatile uint8_t);
void dmxWrite(int,uint8_t);
void dmxMaxChannel(int);

/* TIMER2 has a different register mapping on the ATmega8.
* The modern chips (168, 328P, 1280) use identical mappings.
#if defined(__AVR_ATmega2560__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega1280__)
#elif defined(__AVR_ATmega8__)
/* Produce an appropriate message to aid error reporting on nonstandard
* platforms such as Teensy.
#warning "DmxSimple does not support this CPU"

/** Initialise the DMX engine
void dmxBegin()
  dmxStarted = 1;

  // Set up port pointers for interrupt routine
  dmxPort = portOutputRegister(digitalPinToPort(dmxPin));
  dmxBit = digitalPinToBitMask(dmxPin);

  // Set DMX pin to output

  // Initialise DMX frame interrupt
  // Presume Arduino has already set Timer2 to 64 prescaler,
  // Phase correct PWM mode
  // So the overflow triggers every 64*510 clock cycles
  // Which is 510 DMX bit periods at 16MHz,
  //          255 DMX bit periods at 8MHz,
  //          637 DMX bit periods at 20MHz

/** Stop the DMX engine
* Turns off the DMX interrupt routine
void dmxEnd()
  dmxStarted = 0;
  dmxMax = 0;

/** Transmit a complete DMX byte
* We have no serial port for DMX, so everything is timed using an exact
* number of instruction cycles.
* Really suggest you don't touch this function.
void dmxSendByte(volatile uint8_t value)
  uint8_t bitCount, delCount;
  __asm__ volatile (
    "ld __tmp_reg__,%a[dmxPort]\n"
    "and __tmp_reg__,%[outMask]\n"
    "st %a[dmxPort],__tmp_reg__\n"
    "ldi %[bitCount],11\n" // 11 bit intervals per transmitted byte
    "rjmp bitLoop%=\n"     // Delay 2 clock cycles.
    "ldi %[delCount],%[delCountVal]\n"
    "dec %[delCount]\n"
    "brne delLoop%=\n"
    "ld __tmp_reg__,%a[dmxPort]\n"
    "and __tmp_reg__,%[outMask]\n"
    "ror %[value]\n"
    "brcc sendzero%=\n"
    "or __tmp_reg__,%[outBit]\n"
    "st %a[dmxPort],__tmp_reg__\n"
    "dec %[bitCount]\n"
    "brne bitLoop%=\n"
      [bitCount] "=&d" (bitCount),
      [delCount] "=&d" (delCount)
      [dmxPort] "e" (dmxPort),
      [outMask] "r" (~dmxBit),
      [outBit] "r" (dmxBit),
      [delCountVal] "M" (F_CPU/1000000-3),
      [value] "r" (value)

/** DmxSimple interrupt routine
* Transmit a chunk of DMX signal every timer overflow event.
* The full DMX transmission takes too long, but some aspects of DMX timing
* are flexible. This routine chunks the DMX signal, only sending as much as
* it's time budget will allow.
* This interrupt routine runs with interrupts enabled most of the time.
* With extremely heavy interrupt loads, it could conceivably interrupt its
* own routine, so the TIMER2 interrupt is disabled for the duration of
* the service routine.

  // Prevent this interrupt running recursively

  uint16_t bitsLeft = F_CPU / 31372; // DMX Bit periods per timer tick
  bitsLeft >>=2; // 25% CPU usage
  while (1) {
    if (dmxState == 0) {
      // Next thing to send is reset pulse and start code
      // which takes 35 bit periods
      uint8_t i;
      if (bitsLeft < 35) break;
      *dmxPort &= ~dmxBit;
      for (i=0; i<11; i++) _delay_us(8);
      *dmxPort |= dmxBit;
    } else {
      // Now send a channel which takes 11 bit periods
      if (bitsLeft < 11) break;
    // Successfully completed that stage - move state machine forward
    if (dmxState > dmxMax) {
      dmxState = 0; // Send next frame
  // Enable interrupts for the next transmission chunk

void dmxWrite(int channel, uint8_t value) {
  if (!dmxStarted) dmxBegin();
  if ((channel > 0) && (channel <= DMX_SIZE)) {
    if (value<0) value=0;
    if (value>255) value=255;
    dmxMax = max((unsigned)channel, dmxMax);
    dmxBuffer[channel-1] = value;

void dmxMaxChannel(int channel) {
  if (channel <=0) {
    // End DMX transmission
    dmxMax = 0;
  } else {
    dmxMax = min(channel, DMX_SIZE);
    if (!dmxStarted) dmxBegin();

/* C++ wrapper */

/** Set output pin
* @param pin Output digital pin to use
void DmxSimpleClass::usePin(uint8_t pin) {
  dmxPin = pin;
  if (dmxStarted && (pin != dmxPin)) {

/** Set DMX maximum channel
* @param channel The highest DMX channel to use
void DmxSimpleClass::maxChannel(int channel) {

/** Write to a DMX channel
* @param address DMX address in the range 1 - 512
void DmxSimpleClass::write(int address, uint8_t value)
dmxWrite(address, value);
DmxSimpleClass DmxSimple;

edit - I think actually the update function runs constantly, and only channels that have been changed between updates will be updated.  It seems awfully slow.  Might it be possible to update the dmxBuffer array (where channel info is stored) using something like memcopy?  This is a massively uneducated guess by the way :)

edit2 - or maybe access dmxSendByte directly?


mrboni ...

The flat out fastest rate possible for your parameters of 60 channels at 30 frames per second is around 85 milliseconds, but that is only if the DMX hardware double buffers the Channel Data transfers so that there is no latency in the hardware waiting for the next data byte, and it also depends on the addressing of the DMX channels that you are using.  If you are not using addresses 1 through 60 for your 60 channels, there will be at least one byte of overhead for every channel skipped from 1 through the maximum channel number you are using.  Also, you'll need to know what "Mark Time Between Frames" and "Mark Time Between Packets" your hardware is using ... these times can be substantial, and will slow down your transfers.

I hope this might be helpful ...
Blessings in abundance, all the best, & ENJOY!
Art in Carlisle, PA USA
Blessings in abundance, all the best, & ENJOY!
Art in Carlisle, PA USA

Go Up