Page 2 of 2 FirstFirst 12
Results 11 to 19 of 19

Thread: DMX Interface - Anyone build one?

  1. #11
    Join Date
    Jul 2007
    Location
    Knoxville, TN, USA
    Posts
    3,154

    Default

    Hey MAx -

    Just an FYI in case you want to think about combining lasers & DMX lighting at some point in the future:

    http://www.photonlexicon.com/forums/...296#post129296


    RR

    Metrologic HeNe 3.3mw Modulated laser, 2 Radio Shack motors, and a broken mirror.
    1979.
    Sweet.....

  2. #12
    Join Date
    May 2007
    Location
    Romania
    Posts
    1,041

    Default

    I beleieve its better to use a 3 channel DMX to PWM controller with interrupt driven DMX receiver code.

    DMX is quite easy on most Micro controllers that have a built in UART with frame error detection.

    basically, in some kind of nut shell.

    As long as a framing error exists
    (we dont have a valid data streem on the UART that is configured for 250Kbaud & 8bits,N,1 input or we are between the IDLE, BREAK or MAB part of the signal) If we have a framing error no action is taken.

    As soon as the framing error clears, start reading bytes from the uart at 250,000 baud this way you can easily read all 512 values of dmx.

    The framing error clear is tied to generate an interrupt that starts the reading of values.

    if you just need three thats very easy and you can update the PWM values directly from the read DMX without much code overhead.

  3. #13
    Join Date
    Nov 2005
    Location
    Melbourne, Australia
    Posts
    3,702

    Default

    There s a catch to using the AVR's onboard PWM, in that in some situations the PWM frequency will cause interference with audio gear.

    LOL, mega328 to driver a few LEDS - that's like using TNT to kill a fly...
    KVANT Australian projector sales
    https://www.facebook.com/kvantaus/

    Lasershowparts- Laser Parts at great prices
    https://www.facebook.com/lasershowparts/

  4. #14
    Join Date
    May 2007
    Location
    Romania
    Posts
    1,041

    Default

    Quote Originally Posted by dave View Post
    There s a catch to using the AVR's onboard PWM, in that in some situations the PWM frequency will cause interference with audio gear.

    LOL, mega328 to driver a few LEDS - that's like using TNT to kill a fly...
    As far as I am aware using PWMs with AC to control 120/220 lighting or other equipment indeed can do that even with zero crossovers but in a DC drive with the MCU correctly opto isolated I doubt in my experience there will be any problems, anyway it's usual in lighting controlls to isolated from the audio system using a DI box. If you run the whole show from one laptop then of course troubles will exist.

    PWM is the right way to do it for eventual perfection but not the only one. Direct current control will cause many issues, PWM will remove them.

    mega328, maybe is overkill but who knows how much he wants to do later.

    Max, if you want some serious sollutions PM me and we'll descuss it.

    Andy

  5. #15
    Join Date
    Nov 2007
    Location
    Cairns, Australia
    Posts
    1,896

    Default

    If anyone just happens to have an ATMega168 or 328 with the Arduino bootloader, I have been working on this code and have got it to a working stage. It will however ONLY work on the ATMega168 or 328's, any others will need modifications in the code. I decided to use an ATMega as it is extremely easy to program them without and AVR programmer (With the Arduino bootloader). Yes, it may be overkill, but there is room to expand if you want a strobing feature, colour fade or whatever else you want!

    Code:
    /******************************* Addressing variable declarations *****************************/
    
    #include <EEPROM.h>
    #define NUMBER_OF_CHANNELS 8
    //the number of channels we want to receive (8 by default).
    
    #define SWITCH_PIN_0 11 //the pin number of our "0" switch
    #define SWITCH_PIN_1 12 //the pin number of our "1" switch
    int dmxaddress = 1;
    /* The dmx address we will be listening to.  The value of this will be set in the Addressing()
    *  function, if triggered, and read from EEPROM addresses 510 and 511.
    
    /******************************* MAX485 variable declarations *****************************/
    
    #define RECEIVER_OUTPUT_ENABLE 2
    /* receiver output enable (pin2) on the max485.  
    *  will be left low to set the max485 to receive data. */
    
    #define DRIVER_OUTPUT_ENABLE 3
    /* driver output enable (pin3) on the max485.  
    *  will left low to disable driver output. */
    
    #define RX_PIN 0   // serial receive pin, which takes the incoming data from the MAX485.
    #define TX_PIN 1   // serial transmission pin
    
    /******************************* DMX variable declarations ********************************/
    
    volatile byte i = 0;              //dummy variable for dmxvalue[]
    volatile byte dmxreceived = 0;    //the latest received value
    volatile unsigned int dmxcurrent = 0;     //counter variable that is incremented every time we receive a value.
    volatile byte dmxvalue[NUMBER_OF_CHANNELS];     //stores the DMX values we're interested in using.
    volatile boolean dmxnewvalue = 0; //set to 1 when new dmx values are received.
    
    /******************************* Timer2 variable declarations *****************************/
    
    volatile byte zerocounter = 0;          
    /* a counter to hold the number of zeros received in sequence on the serial receive pin.  
    *  When we've received a minimum of 11 zeros in a row, we must be in a break.  As written,
    *  the timer2 ISR actually checks for 22 zeros in a row, for the full 88uS break.       */
    
    
    
    
    void setup() {
      
      /******************************* Max485 configuration ***********************************/
      
      pinMode(RECEIVER_OUTPUT_ENABLE, OUTPUT);
      pinMode(DRIVER_OUTPUT_ENABLE, OUTPUT);
      digitalWrite(RECEIVER_OUTPUT_ENABLE, LOW);
      digitalWrite(DRIVER_OUTPUT_ENABLE, LOW);    //sets pins 3 and 4 to low to enable reciever mode on the MAX485.
    
      pinMode(RX_PIN, INPUT);  //sets serial pin to receive data
    
      /******************************* Addressing subroutine (Not used for testing) *********************************/
    
     /*  pinMode(SWITCH_PIN_0, INPUT);           //sets pin for '0' switch to input
      digitalWrite(SWITCH_PIN_0, HIGH);       //turns on the internal pull-up resistor for '0' switch pin
      pinMode(SWITCH_PIN_1, INPUT);           //sets pin for '1' switch to input  
      digitalWrite(SWITCH_PIN_1, HIGH);       //turns on the internal pull-up resistor for '1' switch pin
       
      byte switch1 = 0;
      byte switch0 = 0;  //the switch states for the 1 and 0 pin 
      switch0 = digitalRead(SWITCH_PIN_0);
      switch1 = digitalRead(SWITCH_PIN_1);
      
      if (switch0 == 0 || switch1 == 0)
      {
      Addressing(switch0, switch1);
      //call the addressing subroutine if either switch is pressed.
      }*/
    
      //read the previously stored value from EEPROM addresses 510 and 511.
      dmxaddress = EEPROM.read(511);   //read the high byte into dmxaddress
      dmxaddress = dmxaddress << 8;  
      //bitshift the high byte left 8 bits to make room for the low byte
      dmxaddress =  dmxaddress | EEPROM.read(510);   //read the low byte into dmxaddress
      dmxaddress = dmxaddress + 3;
      /*  this will allow the USART receive interrupt to fire an additional 3 times for every dmx frame.  
      *   Here's why:
      *   Once to account for the fact that DMX addresses run from 0-511, whereas channel numbers
      *        start numbering at 1.
      *   Once for the Mark After Break (MAB), which will be detected by the USART as a valid character 
      *        (a zero, eight more zeros, followed by a one)
      *   Once for the START code that precedes the 512 DMX values (used for RDM).  */
    
    
      /******************************* USART configuration ************************************/
      
      Serial.begin(250000);
      /* Each bit is 4uS long, hence 250Kbps baud rate */
      
      cli(); //disable interrupts while we're setting bits in registers
      
      bitClear(UCSR0B, RXCIE0);  //disable USART reception interrupt
      
      /******************************* Timer2 configuration ***********************************/
      
      //NOTE:  this will disable PWM on pins 3 and 11.
      bitClear(TCCR2A, COM2A1);
      bitClear(TCCR2A, COM2A0); //disable compare match output A mode
      bitClear(TCCR2A, COM2B1);
      bitClear(TCCR2A, COM2B0); //disable compare match output B mode
      bitSet(TCCR2A, WGM21);
      bitClear(TCCR2A, WGM20);  //set mode 2, CTC.  TOP will be set by OCRA.
      
      bitClear(TCCR2B, FOC2A);
      bitClear(TCCR2B, FOC2B);  //disable Force Output Compare A and B.
      bitClear(TCCR2B, WGM22);  //set mode 2, CTC.  TOP will be set by OCRA.
      bitClear(TCCR2B, CS22);
      bitClear(TCCR2B, CS21);
      bitSet(TCCR2B, CS20);   // no prescaler means the clock will increment every 62.5ns (assuming 16Mhz clock speed).
      
      OCR2A = 64;                
      /* Set output compare register to 64, so that the Output Compare Interrupt will fire
      *  every 4uS.  */
      
      bitClear(TIMSK2, OCIE2B);  //Disable Timer/Counter2 Output Compare Match B Interrupt
      bitSet(TIMSK2, OCIE2A);    //Enable Timer/Counter2 Output Compare Match A Interrupt
      bitClear(TIMSK2, TOIE2);   //Disable Timer/Counter2 Overflow Interrupt Enable          
      
      sei();                     //reenable interrupts now that timer2 has been configured. 
      
    }  //end setup()
    
    
    void loop()  {
      // the processor gets parked here while the ISRs are doing their thing. 
      
      if (dmxnewvalue == 1) {    //when a new set of values are received, jump to action loop...
      digitalWrite(13, HIGH);
      byte j;
      for(j = 0; j < NUMBER_OF_CHANNELS; j++) 
      {
      Serial.print(dmxvalue[j]);
      }
      analogWrite(5, dmxvalue[2]);  // FOR MY DMX CONTROLLER, ARRAY VALUES 0 AND 1 ARE USELESS. THIS MAY NOT BE THR CASE FOR YOU, SO MAKE SURE TO CHECK!
      analogWrite(6, dmxvalue[3]);
      analogWrite(6, dmxvalue[4]);
        dmxnewvalue = 0;
        dmxcurrent = 0;
        zerocounter = 0;      //and then when finished reset variables and enable timer2 interrupt
        i = 0;
        bitSet(TIMSK2, OCIE2A);    //Enable Timer/Counter2 Output Compare Match A Interrupt
      }
      digitalWrite(13, LOW);
    } //end loop()
    
    
    //Timer2 compare match interrupt vector handler
    ISR(TIMER2_COMPA_vect) {
      if (bitRead(PIND, PIND0)) {  // if a one is detected, we're not in a break, reset zerocounter.
        zerocounter = 0;
        }
      else {
        zerocounter++;             // increment zerocounter if a zero is received.
        if (zerocounter == 22)     // if 22 0's are received in a row (88uS break)
          {   
          bitClear(TIMSK2, OCIE2A);    //disable this interrupt and enable reception interrupt now that we're in a break.
          bitSet(UCSR0B, RXCIE0);
          }
      }
    } //end Timer2 ISR
    
    
    
    ISR(USART_RX_vect){
      dmxreceived = UDR0;
      /* The receive buffer (UDR0) must be read during the reception ISR, or the ISR will just 
      *  execute again immediately upon exiting. */
     
      dmxcurrent++;                        //increment address counter
     
      if(dmxcurrent > dmxaddress) {         //check if the current address is the one we want.
        dmxvalue[i] = dmxreceived;
        i++;
        if(i == NUMBER_OF_CHANNELS) {
          bitClear(UCSR0B, RXCIE0); 
          dmxnewvalue = 1;                        //set newvalue, so that the main code can be executed.
        } 
      }
    } // end ISR
    Wire it like this:

    And you will need to replace your HardwareSerial.cpp file in /hardware/core/arduino with this for it to compile (MAKE SURE TO BACKUP YOUR ORIGINAL AS YOU WILL HAVE TO REPLACE IT TO RETURN TO NORMAL PROGRAMMING!!):
    Code:
    /*
      HardwareSerial.cpp - Hardware serial library for Wiring
      Copyright (c) 2006 Nicholas Zambetti.  All right reserved.
    
      This library is free software; you can redistribute it and/or
      modify it under the terms of the GNU Lesser General Public
      License as published by the Free Software Foundation; either
      version 2.1 of the License, or (at your option) any later version.
    
      This library is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      Lesser General Public License for more details.
    
      You should have received a copy of the GNU Lesser General Public
      License along with this library; if not, write to the Free Software
      Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
      
      Modified 23 November 2006 by David A. Mellis
    */
    
    #include <stdio.h>
    #include <string.h>
    #include <inttypes.h>
    #include "wiring.h"
    #include "wiring_private.h"
    
    #include "HardwareSerial.h"
    
    // Define constants and variables for buffering incoming serial data.  We're
    // using a ring buffer (I think), in which rx_buffer_head is the index of the
    // location to which to write the next incoming character and rx_buffer_tail
    // is the index of the location from which to read.
    #define RX_BUFFER_SIZE 128
    
    struct ring_buffer {
      unsigned char buffer[RX_BUFFER_SIZE];
      int head;
      int tail;
    };
    
    ring_buffer rx_buffer = { { 0 }, 0, 0 };
    
    #if defined(__AVR_ATmega1280__)
    ring_buffer rx_buffer1 = { { 0 }, 0, 0 };
    ring_buffer rx_buffer2 = { { 0 }, 0, 0 };
    ring_buffer rx_buffer3 = { { 0 }, 0, 0 };
    #endif
    
    inline void store_char(unsigned char c, ring_buffer *rx_buffer)
    {
      int i = (rx_buffer->head + 1) % RX_BUFFER_SIZE;
    
      // if we should be storing the received character into the location
      // just before the tail (meaning that the head would advance to the
      // current location of the tail), we're about to overflow the buffer
      // and so we don't write the character or advance the head.
      if (i != rx_buffer->tail) {
        rx_buffer->buffer[rx_buffer->head] = c;
        rx_buffer->head = i;
      }
    }
    
    #if defined(__AVR_ATmega1280__)
    
    SIGNAL(SIG_USART0_RECV)
    {
      unsigned char c = UDR0;
      store_char(c, &rx_buffer);
    }
    
    SIGNAL(SIG_USART1_RECV)
    {
      unsigned char c = UDR1;
      store_char(c, &rx_buffer1);
    }
    
    SIGNAL(SIG_USART2_RECV)
    {
      unsigned char c = UDR2;
      store_char(c, &rx_buffer2);
    }
    
    SIGNAL(SIG_USART3_RECV)
    {
      unsigned char c = UDR3;
      store_char(c, &rx_buffer3);
    }
    /*
    #else
    
    #if defined(__AVR_ATmega8__)
    SIGNAL(SIG_UART_RECV)
    #else
    SIGNAL(USART_RX_vect)
    #endif
    */
    {
    #if defined(__AVR_ATmega8__)
      unsigned char c = UDR;
    #else
      unsigned char c = UDR0;
    #endif
      store_char(c, &rx_buffer);
    }
    
    #endif
    
    // Constructors ////////////////////////////////////////////////////////////////
    
    HardwareSerial::HardwareSerial(ring_buffer *rx_buffer,
      volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
      volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
      volatile uint8_t *udr,
      uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udre, uint8_t u2x)
    {
      _rx_buffer = rx_buffer;
      _ubrrh = ubrrh;
      _ubrrl = ubrrl;
      _ucsra = ucsra;
      _ucsrb = ucsrb;
      _udr = udr;
      _rxen = rxen;
      _txen = txen;
      _rxcie = rxcie;
      _udre = udre;
      _u2x = u2x;
    }
    
    // Public Methods //////////////////////////////////////////////////////////////
    
    void HardwareSerial::begin(long baud)
    {
      uint16_t baud_setting;
      bool use_u2x;
    
      // U2X mode is needed for baud rates higher than (CPU Hz / 16)
      if (baud > F_CPU / 16) {
        use_u2x = true;
      } else {
        // figure out if U2X mode would allow for a better connection
        
        // calculate the percent difference between the baud-rate specified and
        // the real baud rate for both U2X and non-U2X mode (0-255 error percent)
        uint8_t nonu2x_baud_error = abs((int)(255-((F_CPU/(16*(((F_CPU/8/baud-1)/2)+1))*255)/baud)));
        uint8_t u2x_baud_error = abs((int)(255-((F_CPU/(8*(((F_CPU/4/baud-1)/2)+1))*255)/baud)));
        
        // prefer non-U2X mode because it handles clock skew better
        use_u2x = (nonu2x_baud_error > u2x_baud_error);
      }
      
      if (use_u2x) {
        *_ucsra = 1 << _u2x;
        baud_setting = (F_CPU / 4 / baud - 1) / 2;
      } else {
        *_ucsra = 0;
        baud_setting = (F_CPU / 8 / baud - 1) / 2;
      }
    
      // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
      *_ubrrh = baud_setting >> 8;
      *_ubrrl = baud_setting;
    
      sbi(*_ucsrb, _rxen);
      sbi(*_ucsrb, _txen);
      sbi(*_ucsrb, _rxcie);
    }
    
    uint8_t HardwareSerial::available(void)
    {
      return (RX_BUFFER_SIZE + _rx_buffer->head - _rx_buffer->tail) % RX_BUFFER_SIZE;
    }
    
    int HardwareSerial::read(void)
    {
      // if the head isn't ahead of the tail, we don't have any characters
      if (_rx_buffer->head == _rx_buffer->tail) {
        return -1;
      } else {
        unsigned char c = _rx_buffer->buffer[_rx_buffer->tail];
        _rx_buffer->tail = (_rx_buffer->tail + 1) % RX_BUFFER_SIZE;
        return c;
      }
    }
    
    void HardwareSerial::flush()
    {
      // don't reverse this or there may be problems if the RX interrupt
      // occurs after reading the value of rx_buffer_head but before writing
      // the value to rx_buffer_tail; the previous value of rx_buffer_head
      // may be written to rx_buffer_tail, making it appear as if the buffer
      // don't reverse this or there may be problems if the RX interrupt
      // occurs after reading the value of rx_buffer_head but before writing
      // the value to rx_buffer_tail; the previous value of rx_buffer_head
      // may be written to rx_buffer_tail, making it appear as if the buffer
      // were full, not empty.
      _rx_buffer->head = _rx_buffer->tail;
    }
    
    void HardwareSerial::write(uint8_t c)
    {
      while (!((*_ucsra) & (1 << _udre)))
        ;
    
      *_udr = c;
    }
    
    // Preinstantiate Objects //////////////////////////////////////////////////////
    
    #if defined(__AVR_ATmega8__)
    HardwareSerial Serial(&rx_buffer, &UBRRH, &UBRRL, &UCSRA, &UCSRB, &UDR, RXEN, TXEN, RXCIE, UDRE, U2X);
    #else
    HardwareSerial Serial(&rx_buffer, &UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UDR0, RXEN0, TXEN0, RXCIE0, UDRE0, U2X0);
    #endif
    
    #if defined(__AVR_ATmega1280__)
    HardwareSerial Serial1(&rx_buffer1, &UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UDR1, RXEN1, TXEN1, RXCIE1, UDRE1, U2X1);
    HardwareSerial Serial2(&rx_buffer2, &UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UDR2, RXEN2, TXEN2, RXCIE2, UDRE2, U2X2);
    HardwareSerial Serial3(&rx_buffer3, &UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UDR3, RXEN3, TXEN3, RXCIE3, UDRE3, U2X3);
    #endif

  6. #16
    Join Date
    Nov 2005
    Location
    Melbourne, Australia
    Posts
    3,702

    Default

    Not so sure about that code.

    Whilst I'm no C programmer by any means, it looks like the code doesn't use the UART hardware to detect a break, but instead assumes a break after 22 bytes are received that are 0.

    Could be wrong tho, as I prefer AVR assembler and so i have never bothered to delve too deeply into C
    KVANT Australian projector sales
    https://www.facebook.com/kvantaus/

    Lasershowparts- Laser Parts at great prices
    https://www.facebook.com/lasershowparts/

  7. #17
    Join Date
    Nov 2007
    Location
    Cairns, Australia
    Posts
    1,896

    Default

    It should be fine with most DMX controllers. If not, it's not hard to change. You can use a program to read the data from the Arduino and display it on your computer, if it supports the 250K baud rate.

  8. #18
    Join Date
    Jul 2012
    Posts
    1

    Default

    hi max...i am new
    can you send schematic and pcb lay out for me? i will to make rgb par using 5 mm led but i amnt have dmx receivr
    thx

  9. #19
    Join Date
    Apr 2006
    Location
    Miami, FL
    Posts
    3,590

    Default

    there are a lot of DMX libraries for the arduino, should be pretty easy to do just about anything with one of those

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •