UPDATED!!! Wrote my own DMX Receiver

General discussion on mikroC for dsPIC30/33 and PIC24.
Post Reply
Author
Message
GregoryArndt
Posts: 6
Joined: 31 Jul 2008 03:17
Location: California, USA
Contact:

UPDATED!!! Wrote my own DMX Receiver

#1 Post by GregoryArndt » 07 Aug 2008 10:16

I've searched through the forums and can't seem to find some good DMX code for mikroC and PIC24. I have found good asm code, but owuld like to have it in C. Maybe I'm not looking hard enough.

Does anyone have a good example of both DMX in and out routines for the dsPIC or PIC24? Or maybe point me in the right direction.

Thank you ,
Gregory
Last edited by GregoryArndt on 11 Aug 2008 14:17, edited 1 time in total.

GregoryArndt
Posts: 6
Joined: 31 Jul 2008 03:17
Location: California, USA
Contact:

No Replies, So I wrote my own DMX receiver

#2 Post by GregoryArndt » 11 Aug 2008 09:07

Since I got no replies I decided to write my own. This is my first MikroC project and the following code is based on Kathy Quinlin's 9-bit DMX theory (http://www.8052.com/users/dmx512/recieve.phtml). This reads and stores all 512 channels and outputs 4 channels to LCD. DMX Address is selected by DMX Channel 99 (I'm working on the switch inputs now). This is the beginning of larger firmware and is organized as such.

I'm using the LV-24-33 with PIC24FJ96GA008, but have changed to 16MHz crystal. LCD is in 4-bit mode. DMX Input (75176 RS-485 transceiver through 6N137 optocoupler) on UART2. Kathy's schematic does not use the optocoupler, so make sure to set U2MODEbits.RXINV to invert the input.

Any comments and questions welcome.

Gregory

Code: Select all

// SYSTEM DEFINITIONS
void Setup_System ();
//LCD DEFINITIONS
void Lcd_Screen(unsigned int ScreenNum);
// RS232 DEFINITIONS
void RS232_Write_String (char TX1_Buffer[30]);
void Process232Int ();
void Uart1_Receive_Int ();
// DMX DEFINITIONS
void Setup_DMX ();
void ProcessDMXRcvInt ();
void ProcessDMXErrInt ();
void Uart2_Receive_Int ();
void Uart2_Error_Int ();

// SYSTEM VARIABLES
unsigned short x;

// LCD VARIABLES
char LCD_String[16];
char AE_Name1[13] = "ArtistryWare";
char AE_Name2[14] = "P24MCU rev1.0";

// RS232 VARIABLES
char rx1;

// DMX VARIABLES
char rx2;
register volatile char U2RX9 absolute 0x237;  //For 9th bit Check
unsigned short DMX_Counter;
unsigned short DMX_Data[512];
unsigned int DMX_Address;

// SYSTEM FUNCTIONS
void Setup_System ()
  {
    ADPCFG = 0xFFFF;
//    TRISA = 0;
//    PORTA = 0;
    TRISA = 0xFFFF;
    TRISB = 0;
    PORTB = 0;
    TRISE = 0;
    PORTE = 0;
  }
void Initialize_Variables ()
  {
    DMX_Address = 1;
    DMX_Counter = 0;
    rx1 = 0;
    rx2 = 0;
  }
  
// LCD FUNCTIONS
void Setup_LCD ()
  {
    Lcd_Custom_Init_LV_24_33();
    Lcd_Custom_Cmd(LCD_Cursor_Off);
  }
void Lcd_Screen(int ScreenNum)
  { switch (ScreenNum)
     { case 1:
        { Lcd_Custom_Cmd(LCD_Clear);
          Lcd_Custom_Out(1,3, AE_Name1);
          Lcd_Custom_Out(2,2, AE_Name2);
          break;
        }
       case 2:
        { Lcd_Custom_Cmd(LCD_Clear);
          sprinti(LCD_String,"%3u %3u %3u %3u",
              DMX_Address,DMX_Address+1,DMX_Address+2,DMX_Address+3);
          Lcd_Custom_Out(1,1,LCD_String);
          break;
        }
     }
  }
void Show_Startup ()
  {
    Lcd_Screen(1);
    RS232_Write_String(AE_Name1);
    RS232_Write_String(AE_Name2);
    Delay_ms(2000);
    RS232_Write_String("Start DMX Monitoring");
    Lcd_Screen(2);
  }

//  RS232 FUNCTIONS
void Setup_RS232 ()
  {
    Uart1_Init(9600);
    IEC0bits.U1RXIE = 1;  //Enable U1 Receive Interrupt
    IEC0bits.U1TXIE = 0;  // Disable U1 Transmit Interrupt
  }
void Process232Int ()
  {
  }
void RS232_Write_String (char TX1_Buffer[30])
  { static int x;

    x = 0;
    do
      { Uart1_Write_Char(TX1_Buffer[x]);
        x++;
      } while ( TX1_Buffer[x] );
    Uart1_Write_char(0x0D);
    Uart1_Write_char(0x0A);
  }
  
// DMX FUNCTIONS
void Setup_DMX ()
  {
    Uart2_Init(250000);
    U2MODEbits.PDSEL0 = 1;  // 9 bit Mode low bit
    U2MODEbits.PDSEL1 = 1;  // 9 bit Mode high bit
    U2MODEbits.RXINV = 1;  // RX Invert (Incoming DMX is Opto-Coupled)
    Delay_ms(1);
    IEC4bits.U2ERIE = 1;  // Enable U2 Error Interrupt
    IEC1bits.U2RXIE = 1;  // Enable U2 Receive Interrupt
    IEC1bits.U2TXIE = 0;  // Disable U2 Transmit Interrupt
  }
void ProcessDMXRcvInt ()
  {
  }
void ProcessDMXErrInt ()
  {
  }

// INTERRUPT ROUTINES
void Uart1_Receive_Int () org 0x002A
  { IFS0bits.U1RXIF = 0;
    rx1 = Uart1_Read_Char ();
    Uart1_Write_Char(rx1);
    //Process232Int ();
    return;
  }
void Uart2_Receive_Int () org 0x50
  { IFS1bits.U2RXIF = 0;
    if ( U2RX9 )
      { DMX_Data[DMX_Counter] = Uart2_Read_Char();
        DMX_Counter++;
      }
      else DMX_Counter = 0;
    //ProcessDMXRcvInt ();
    return;
  }
void Uart2_Error_Int () org 0x98
  { IFS4bits.U2ERIF = 0;
    if ( U2STAbits.FERR == 1 )
      { rx2 = Uart2_Read_Char();
        DMX_Counter = 0; }
    if  ( U2STAbits.OERR == 1 ) U2STAbits.OERR = 0;
    //ProcessDMXErrInt ();
    return;
  }

// MAIN PROGRAM
void main ()
  {
    Setup_System ();
    Initialize_Variables ();
    Setup_LCD ();
    Setup_RS232 ();
    Show_Startup ();
    Setup_DMX ();
    
    // MAIN LOOP
    while (1)
      {
        sprinti(LCD_String,"%3u %3u %3u %3u",
            abs(DMX_Data[DMX_Address]),abs(DMX_Data[DMX_Address+1]),
            abs(DMX_Data[DMX_Address+2]),abs(DMX_Data[DMX_Address+3]));
        Lcd_Custom_Out(2,1,LCD_String);
        if (DMX_Address != DMX_Data[99])
          { DMX_Address = DMX_Data[99];
            LCD_Screen (2);
          }
      }

  }
8) 8)

dragonsoldier
Posts: 1
Joined: 16 Nov 2012 09:07

Re: UPDATED!!! Wrote my own DMX Receiver

#3 Post by dragonsoldier » 16 Nov 2012 09:23

Dear Gregory,

I was looking for a dmx code in c, and that's when I found yours. Unfortunately the web page on 8052.com is off-line. Do you have any sample schematic?

I would love to hear from you!
Greetings,
Denny Beulen

Post Reply

Return to “mikroC for dsPIC30/33 and PIC24 General”