⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dmx512led.txt

📁 DMX512协议的应用
💻 TXT
字号:
LED调光-DMX512灯光协义接收控制
作者:net  文章来源:net   时间:2007-10-06 14:37:09   阅读次数:450
--------------------------------------------------------------------------------
 

 
//LED调光-DMX512灯光协义接收控制   

//ICC-AVR application builder : 2007/8/21 と 05:29:45
// Target : M8
// Crystal: 8.000Mhz

#include <iom8v.h>
#include <macros.h>
#define uint unsigned int               //16 bit
#define uchar unsigned char             //8 bit
#define bit char                        // 1     bit
#define ulong unsigned long             //32     bit
#define     FLASH_LED_PORTC          PORTC
#define     FLASH_LED_PORTD          PORTD
#define     FLASH_LED_R_ON          (1<<PC1) 
#define     FLASH_LED_R_OFF         (0<<PC1)
#define     FLASH_LED_G_ON          (1<<PC2) 
#define     FLASH_LED_G_OFF         (0<<PC2)
#define     FLASH_LED_B_ON          (1<<PC0) 
#define     FLASH_LED_B_OFF         (0<<PC0)
#define     DMX_control_receive     (0<<PD2)
#define     DMX_control_send        (1<<PD2)
int JS_01;  
int JS_001=0,JS_002=0,JS_003=0;
int CUR_BYTE_A=0,IADDRESS_A=1;
char JS_1=0,JS_2=0,JS_3=0,JS_4=0,JS_5=0,JS_6=0;
//              RED          GREEN           BLUE           RGB
char DMX_DATA_1=0,DMX_DATA_2=0,DMX_DATA_3=0,DMX_DATA_4=0;

char LED_TRANSFER_DATA[256]=
{
50,50,50,50,50,50,
49,49,49,49,49,48,48,48,48,48,
47,47,47,47,47,46,46,46,46,46,
45,45,45,45,45,44,44,44,44,44,
43,43,43,43,43,42,42,42,42,42,
41,41,41,41,41,40,40,40,40,40,
39,39,39,39,39,38,38,38,38,38,
37,37,37,37,37,36,36,36,36,36,
35,35,35,35,35,34,34,34,34,34,
33,33,33,33,33,32,32,32,32,32,
31,31,31,31,31,30,30,30,30,30,
29,29,29,29,29,28,28,28,28,28,
27,27,27,27,27,26,26,26,26,26,
25,25,25,25,25,24,24,24,24,24,
23,23,23,23,23,22,22,22,22,22,
21,21,21,21,21,20,20,20,20,20,
19,19,19,19,19,18,18,18,18,18,
17,17,17,17,17,16,16,16,16,16,
15,15,15,15,15,14,14,14,14,14,
13,13,13,13,13,12,12,12,12,12,
11,11,11,11,11,10,10,10,10,10,
9,9,9,9,9,8,8,8,8,8,
7,7,7,7,7,6,6,6,6,6,
5,5,5,5,5,4,4,4,4,4,
3,3,3,3,3,2,2,2,2,2,
1,1,1,1,1,0,0,0,0,0
};
//------------------------------------------------
void port_init(void)
{
PORTB = 0xFF;
DDRB     = 0x00;
PORTC = 0xFF; //m103 output only
DDRC     = 0xFF;
PORTD = 0xFF;
DDRD     = 0xFF;
}

//TIMER0 initialisation - prescale:64
// WGM: Normal
// desired value: 1mSec
// actual value:     1.000mSec (0.0%)
void timer0_init(void)
{
TCCR0 = 0x00; //stop
TCNT0 = 0x83; //set count
TCCR0 = 0x03; //start timer
}

#pragma interrupt_handler timer0_ovf_isr:10
void timer0_ovf_isr(void)
{
TCNT0 = 0x83; //reload counter value
}
//TIMER1 initialisation - prescale:8
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 100uSec
// actual value: 100.000uSec (0.0%)
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0xFF; //setup
TCNT1L = 0x9C;
OCR1AH = 0x00;
OCR1AL = 0x64;
OCR1BH = 0x00;
OCR1BL = 0x64;
ICR1H     = 0x00;
ICR1L     = 0x64;
TCCR1A = 0x00;
TCCR1B = 0x02; //start Timer
}

#pragma interrupt_handler timer1_ovf_isr:9
void timer1_ovf_isr(void)
{
//TIMER1 has overflowed
TCNT1H = 0xFF; //reload counter high value
TCNT1L = 0x9C; //reload counter low value
LED_RGB();

}
//UART0 initialisation
// desired baud rate: 250000
// actual: baud rate:250000 (0.0%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
UCSRB = 0x00; // RXEN //disable while setting baud rate
UCSRA = 0x02; // RXC=0--     RXC      TXC      UDRE     FE      DOR     PE       U2X      MPCM
UCSRB = 0xFC; // 0XDF --     RXCIE TXCIE UDRIE RXEN TXEN UCSZ2 RXB8     TXB8
UCSRC = 0x06; // 9 bit--     URSEL UMSEL UPM1 UPM0     USBS UCSZ1 UCSZ0 UCPOL 
UBRRH = 0x00; // set baud rate hi
UCSRC = 0x8E; // 9 bit     86 
UBRRL = 0x03; //set baud rate lo       ( 0x01 u2x=0)
}

#pragma interrupt_handler uart0_rx_isr:12
void uart0_rx_isr(void)
{    
DMX_RECEIVE();     //uart has received a character in UDR
}
#pragma interrupt_handler uart0_udre_isr:13
void uart0_udre_isr(void)
{
//character transferred to shift register so UDR is now empty
//FLASH_LED_PORTD=DMX_control_receive;         //DMX_control_receive; //pd2=0     PD2 
}

#pragma interrupt_handler uart0_tx_isr:14
void uart0_tx_isr(void)
{
//character has been transmitted
}
//call this routine to initialise all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer0_init();
timer1_init();
uart0_init();
MCUCR = 0x00;
GICR     = 0x00;
TIMSK = 0x05; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialised
}

//
void main(void)
{
init_devices();
FLASH_LED_PORTD=DMX_control_receive;         //DMX_control_receive; //pd2=0     PD2
//insert your functional code here...
}

void LED_FLASH()
{
           ++JS_01;
      if (JS_01==500)
         {
      //FLASH_LED_PORTC^=FLASH_LED_R_OFF;
         //FLASH_LED_PORTC^=FLASH_LED_G_OFF;
      //FLASH_LED_PORTC^=FLASH_LED_B_OFF;
             }
      if (JS_01==1000)
      {
          JS_01&=0;
      }
}
void LED_RGB()     //100us
{

//FLASH_LED_PORTC=FLASH_LED_R_ON|FLASH_LED_R_ON|FLASH_LED_R_ON;
++JS_1;++JS_2;++JS_3;
if (JS_1==50) 
{JS_1=0;FLASH_LED_PORTC=FLASH_LED_R_OFF|FLASH_LED_G_OFF|FLASH_LED_B_OFF;}      //ALL==0
if (JS_1==LED_TRANSFER_DATA[DMX_DATA_1]) {FLASH_LED_PORTC|=0x02;}//FLASH_LED_R_ON;}     // on==1
if (JS_1==LED_TRANSFER_DATA[DMX_DATA_2]) {FLASH_LED_PORTC|=0x04;}//FLASH_LED_G_ON;}
if (JS_1==LED_TRANSFER_DATA[DMX_DATA_3]) {FLASH_LED_PORTC|=0x01;}//FLASH_LED_B_ON;}
if (JS_1==LED_TRANSFER_DATA[DMX_DATA_4]) 
{FLASH_LED_PORTC|=0x02;FLASH_LED_PORTC|=0x04;FLASH_LED_PORTC|=0x01; }//RGB
}

//*****************************************
void     DMX_RECEIVE()
{        unsigned char status, resh, resl;
          char DMX_ADDRESS_PAN,CODE_Data,DMX_DATA_PAN;
while ( !(UCSRA & (1<<RXC)) );      // NO USE 
// from buffer  
status = UCSRA;  
resh      = UCSRB;                                          
resl      = UDR;
resh = (resh >> 1) & 0x01;     // TAKE RXB8                               
           if (resh==1)           //(1<<RXB8))
            {                     //LED_DISPLAY_V4=15;         //test ok  
                 DMX_DATA_PAN=1;                                             
          if     (CUR_BYTE_A==IADDRESS_A+0)     DMX_DATA_1=resl; 
          if     (CUR_BYTE_A==IADDRESS_A+1)     DMX_DATA_2=resl;  
       if     (CUR_BYTE_A==IADDRESS_A+2)     DMX_DATA_3=resl;
       if     (CUR_BYTE_A==IADDRESS_A+3)     DMX_DATA_4=resl;
       if     (DMX_ADDRESS_PAN==1)      
       ++CUR_BYTE_A;     //DMX address count register
        if (CUR_BYTE_A==513)           CUR_BYTE_A&=0;        
            }   
          else                     //(0<<RXB8)
              {                
            CODE_Data=resl; 
      if (CODE_Data==0) 
      {        //test ok  
               CUR_BYTE_A = 0; 
            CODE_Data     = 0xff;
      DMX_ADDRESS_PAN=1;
      }
      else
      DMX_ADDRESS_PAN=0;
           }   
// DMX_PORTD|=DMX_control_send;     //pd6=1             
}
//****************************************

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -