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

📄 zsdmx512in.c

📁 dmx512通讯程序,可以接收dmx512格式的数据
💻 C
字号:
//ICC-AVR application builder : 2006-2-23
// Target : ATmega8515
// Crystal: 16.000Mhz

#include <io8515v.h>
#include <eeprom.h>
#include <macros.h>

//#define EMCUCR	(*(volatile unsigned char *)0x56)

//declare memory mapped variables

#define COM_RHR (*(volatile unsigned char *)0x8000)
#define COM_THR (*(volatile unsigned char *)0x8000)
#define COM_IER (*(volatile unsigned char *)0x8001)
#define COM_DLL (*(volatile unsigned char *)0x8000)
#define COM_DLM (*(volatile unsigned char *)0x8001)
#define COM_ISR (*(volatile unsigned char *)0x8002)
#define COM_FCR (*(volatile unsigned char *)0x8002)
#define COM_LCR (*(volatile unsigned char *)0x8003)
#define COM_MCR (*(volatile unsigned char *)0x8004)
#define COM_LSR (*(volatile unsigned char *)0x8005)
#define COM_MSR (*(volatile unsigned char *)0x8006)
#define COM_SPR (*(volatile unsigned char *)0x8007)

///EEPROM address//////
#define local_set0 100
#define local_set1 101


unsigned char msg_byte0,msg_byte1,msg_byte2,msg_byte3,msg_byte4;
unsigned char set_byte0,set_byte1;

unsigned int local_adr,target_adr;
unsigned int red,green,blue;

unsigned char frame_end;
unsigned int byte_count,byte24;

unsigned char switch_data;

unsigned char clrdata[150];
 
void port_init(void)
{
 PORTA = 0xFF;
 DDRA  = 0x00;
 
 PORTB = 0x0;
 DDRB  = 0xFF;//设置PB7~PB0为输出.
 
 PORTC = 0xFF;
 DDRC  = 0xFF;
 
 PORTD = 0xFF;
 DDRD  = 0xE0; //PD7~PD5为输出,PD5~PD0为输入(包括int0,int1),输入均有弱上拉。 
}

void uart0_init(void)
{
 UCR  = 0x00; //disable while setting baud rate

 UBRR = 8;//set baud rate=115.2K.
 UCR  = 0x98; //enable
}

void sendchar(unsigned char);
void sendchar_com(unsigned char);
void init_COM(void);

void delay(unsigned long j)
{
  for(;j>0;j--) WDR();
}
 

/*#pragma interrupt_handler uart0_rx_isr:10
void uart0_rx_isr(void)
{
 //uart has received a character in UDR
 unsigned char rxd;
 
 rxd=UDR;
 //switch_data=10;
 //sendchar(rxd);
 if((rxd&0b11111000)==0xc8)
 {
   set_byte1=rxd&0b00000111;
   
   local_adr=set_byte1*64+set_byte0;
   
   EEPROMwrite(local_set0,set_byte0);
   EEPROMwrite(local_set1,set_byte1);
      
   sendchar(set_byte0);
   sendchar(0xc8+set_byte1);
 }
 else
   set_byte0=rxd;
   
 if (set_byte0==0xd9)
 {
   set_byte0=0;
   rxd=0;
   
   sendchar(EEPROMread(local_set0));
   sendchar(0xc8+EEPROMread(local_set1));
 }
}*/

#pragma interrupt_handler int0_isr:2
void int0_isr(void)//dmx512信号格式:
{
 unsigned char rxd,lsr,lsr_rxd,lsr_break,isr;
 int i;

 while((COM_ISR&0b00000001)==0)
{

 lsr=COM_LSR;
 lsr_break=lsr&0b00010000;//判断是否是BREAK信号
 lsr_rxd=lsr&0b00000001;//判断是否是数据信号
 
 if(lsr_break!=0)
 {
  frame_end=10;
  byte_count=0;
  byte24=0;
 
  rxd=COM_RHR;//!!!!!!!!!!!//
   
 }

 if(lsr_rxd!=0)
 {
   rxd=COM_RHR;
   
   if(frame_end==30)//接收本地址的数据
   {
   byte_count++;
   if(byte_count>local_adr && byte_count<(local_adr+25))//只接收n个数据
   {
   if(byte24>25)byte24=0;//一桢完发生一2006.5.10
   
  	clrdata[byte24]=rxd;//(rxd<<2);	
	//sendchar(clrdata[byte24]);	//2006.2.282
	byte24++;
   }   
   }
   else if(frame_end==10) frame_end=20;
   else if(frame_end==20 && rxd==0)//准备收数据
    {      switch_data=10;
	       frame_end=30;
		   byte_count=0;
		   byte24=0;
		   }
	
	
   else if(frame_end==20 && (rxd ==0xaa) )//准备设地址
   {  
     frame_end=40;
    }
   
   else if(frame_end==40 && (rxd ==0x55))//准备设地址
   {  
     frame_end=50;
    }
	
//-------------------2006.5.10-------------------//	
	else if(frame_end==50 && (rxd ==0xa5) )//准备设地址
   {  
     frame_end=60;
    }
   
   else if(frame_end==60 && (rxd ==0x5a))//准备设地址
   {  
     frame_end=70;
    }
	
	else if(frame_end==70 && (rxd ==0x77) )//准备设地址
   {  
     frame_end=80;
    }
   
   else if(frame_end==80 && (rxd ==0x99))//准备设地址
   {  
     frame_end=90;
    }
	
   else if(frame_end==90 && (rxd ==0x58))//准备设地址
   {  
     frame_end=100;
    }
	
	
	
	
//-------------------------------------------------//	
  
	
	
   
		   
   else if(frame_end==100 && (rxd & 0xf0)==0x50)//准备设地址
   {  
     set_byte1=rxd & 0x0f;
     frame_end=110;
    }
   
   else if(frame_end==110)//自动设地址
   {
   
   set_byte0=rxd;
   local_adr=set_byte1*64+set_byte0;
   EEPROMwrite(local_set0,set_byte0);//sendchar(set_byte0);
   EEPROMwrite(local_set1,set_byte1);//sendchar(set_byte1); 
   
   frame_end=0; 
   for(i=0;i<24;i++)
  {
  clrdata[3*i]=0x24;  
  clrdata[3*i+1]=0x49;
  clrdata[3*i+2]=0x92;  
  }
   switch_data=10;
     
   }
 }
}

}

#pragma interrupt_handler int1_isr:3
void int1_isr(void)
{
   switch_data=10;//
}

void setlum(void)
{
  int i,j;
  
  byte24=0;
  
  for(i=0;i<24;i++) sendchar(clrdata[i]);//for temp test.//
  
  return;

/* 
//////////在此作伽玛矫正////////  
  for(i=0;i<24;i++)
  { 
  if(clrdata[i]>200) clrdata[i]=clrdata[i]*11-1800;
  else if(clrdata[i]>100) clrdata[i]=clrdata[i]*3-200;
  WDR();
  }
*/  
/////////在此将数据交换到"输出单片机"////////  

}

//call this routine to initialise all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
// mapping_init();
 port_init();
 uart0_init();
 
 MCUCR = 0x8F;//SRE=1:使能外部SRAM。ISC11=ISC10=ISC01=ISC00=1:上升沿触发中断。
 MCUCR=0x8f;
 
//SFIOR = 0x40;
 GIMSK = 0xC0;//int0,int1中断使能。
 TIMSK = 0x00;
 
 rst_COM();
 init_COM();
 
 SEI(); //re-enable interrupts
 //all peripherals are now initialised
}

void sendchar(unsigned char c)
{
 	 UDR=c;//0x55;
	 while(!(USR&0x40));
	 USR|=0x40;//the TXC bit is cleared(zero) by writing a 
	           //logica "1" to the bit.
 }

void sendchar_com(unsigned char byte_data)
{
   COM_THR=byte_data;
   while(!(COM_LSR&0x20));
}
   
void init_COM(void)
{
 
     COM_LCR=0x80;//LCR-bit7=1,配置波特率。
 
  	 COM_DLL=0x01;//0x01:baud=115.2K.
	 
	 COM_DLM=0x00;
	 
	 COM_LCR=0x07;//8 bit数据,无校验,2bit停止位。
	 
//	 COM_FCR=0x0f;//使用FIFO,RX FIFO trigger level=1.(=14时通信出错)
//	 COM_FCR=0x4f;
//	 COM_MCR=0x02;

	 COM_FCR=0;
	 COM_MCR=0;
	 
	 COM_IER=0b00000101;//modify by weizl at 2005-03-17.    
}

void rst_COM(void){

  PORTD=0b11000000;
  delay(100);
  PORTD=0b11100000;//rst=1
  delay(100);
//PORTD&=0b11011111;//RST=0
  PORTD=0b11000000;//rst=0;
  delay(100);
  	 
}


void main(void)
{

 unsigned int i,j,k,m,n;
 
// unsigned int yes;

 init_devices();
 
 SPH=0x02;
 SPL=0x5f;
 
// rst_COM();
// init_COM();
 
 WDR();
 WDTCR=0x1f;//设置看门狗.
 WDTCR=0x1f;
 WDTCR=0x1f;

// sendchar(97);//!!!!!!!!!!!!!!!!!!//
 
 target_adr=0;
 
 set_byte0=EEPROMread(local_set0);
 set_byte1=EEPROMread(local_set1);
 
 if(set_byte0==0xff && set_byte1==0xff)
   {
      EEPROMwrite(local_set0,0);
	  EEPROMwrite(local_set1,0);
	  local_adr=0;
   }
 else 
 	  local_adr=set_byte1*64+set_byte0; 
 
 //sendchar(97);
 
 
 for(i=0;i<24;i++)
  {
  clrdata[3*i]=0x49;
  
  clrdata[3*i+1]=0x92;
  
  clrdata[3*i+2]=0x24;
  
  }//初始化为红光
  

 switch_data=0;
 
 while(1){
 WDR(); //喂狗.

/* 
  if(byte24==24)
   {
     CLI();//关闭全局中断. 
     setlum();
     SEI();//打开全局中断.
//	 PORTD=~PIND;//驱动LED指示一帧数据到达.
   }
*/
  if(switch_data==10)
  {
     int k;
     switch_data=0;
	 sendchar(0x51);
	 //sendchar(clrdata[k]);
	 sendchar(0x15);
	 //sendchar(clrdata[k]);
	 for(k=0;k<24;k++){
	   WDR();
	   sendchar(clrdata[k]);
	 }
   }
 }
}

⌨️ 快捷键说明

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