您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 能源与动力工程 > 在AVR单片机上实现DMX512协议dmx512-avr
/*=====================================================*//*1.ThisProgramUseICCAVR6.31ACompileSystem*//*2.MakeUseOfAttiny2313SinglechipFosc=16MHz*//*3.LastModified:2006-09-1615:54*//*4VersionNumber:V1.0*//*5.Editor:LONGCHUANLAO.*//*=====================================================*/#includeiot2313v.h#includemacros.h#defineUintunsignedint#defineULintunsignedlongint#defineUcharunsignedchar//1ms---1333//#defineMax_Level255//前导码//#defineImage0xAA#defineWrite_Code0xC3#defineSwitch_Bus0xD4#defineDmx0x00#definePixel0x10#defineSeven_Color_Change_Time50000UcharconstVerify[8]={0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF,};UintSect_Channel_Count;UintSect1_Start_Channel[4];UintSeven_Color_Change_Count;//---------------------------//UcharNodes;UcharRow_Number;UcharCol_Number;UintAddress_Number;//---------------------------//UcharMode;UcharStep;UcharChoose;UcharPass_Fg;UcharByte_Count;UcharGray_Level_Count;//---------------------------//charCycle;UcharArray[3];UcharRead_Eeprom(UcharAddress){while(EECR&0x02);EEAR=Address;EECR|=0x01;returnEEDR;}voidWrite_Eeprom(UcharAddress,UcharDatum){while(EECR&0x02);EEAR=Address;EEDR=Datum;EECR|=0x04;EECR|=0x02;}voidport_init(void){PORTA=0xFF;DDRA=0xFF;PORTB=0xFF;DDRB=0xFF;PORTD=0xFF;DDRD=0xFF;}//UART0initialize//desiredbaudrate:250000//actual:baudrate:250000(0.0%)//charsize:9bit//parity:Disablevoiduart0_init(void){UCSRB=0x00;//disablewhilesettingbaudrateUBRRH=0x00;//setbaudrateupperUBRRL=0x03;//setbaudratelowerUCSRA=0x00;UCSRC=0x06;UCSRB=0x9C;//enable}voidInit_Devices(void){//stoperrantinterruptsuntilsetupCLI();//disableallinterruptsport_init();uart0_init();WDR();//thispreventsatimoutonenablingWDTCR=0x18;//WATCHDOGENABLED-dontforgetWDRWDTCR=0x0A;//64msMCUCR=0x00;GIMSK=0x00;TIMSK=0x00;SEI();//re-enableinterrupts//allperipheralsarenowinitialized}voidGray_Scan_Disp(void){if(--Gray_Level_Count==0x00)Gray_Level_Count=Max_Level;//------------------------------------------////PB2-----Red0x040xFB//PB3-----Green0x080xF7//PB4-----Blue0x100xEF/*if(Gray_Level_CountArray[0])PORTB&=0xFB;elsePORTB|=0x04;if(Gray_Level_CountArray[1])PORTB&=0xF7;elsePORTB|=0x08;if(Gray_Level_CountArray[2])PORTB&=0xEF;elsePORTB|=0x10;*///------------------------------------------////PB5-----Red0x200xDF//PB6-----Green0x400xBF//PB7-----Blue0x800x7Fif(Gray_Level_CountArray[0])PORTB&=0xDF;elsePORTB|=0x20;if(Gray_Level_CountArray[1])PORTB&=0xBF;elsePORTB|=0x40;if(Gray_Level_CountArray[2])PORTB&=0x7F;elsePORTB|=0x80;//------------------------------------------//}#pragmainterrupt_handlerSerial:8voidSerial(void){UcharTemp;Temp=UDR;if((UCSRB|0xFD)==0xFD){Step=0;switch(UDR){caseImage:Step=1;break;caseDmx:Step=7;break;}}else{switch(Step){//----------------------------------------------------------//case1:Step=0;Temp=UDR;switch(Temp){caseSwitch_Bus:Step=2;Byte_Count=0;break;caseWrite_Code:Step=3;break;}break;//----------------------------------------------------------//case2:if(Byte_Count==UDR){if(++Byte_Count=201&&(Pass_Fg&0x01)==0x00){Pass_Fg++;}}elseStep=0;break;//----------------------------------------------------------//case3:Step=4;Row_Number=UDR;break;case4:Step=5;Col_Number=UDR;break;case5:Step=6;Nodes=UDR;Byte_Count=0;break;case6:if(UDR==Verify[Byte_Count]){if(++Byte_Count=8&&(Pass_Fg&0x01)==0x01){Step=0;Pass_Fg--;Pass_Fg|=0x80;}}else{//RESET//Step=0;Pass_Fg=0;}break;//----------------------------------------------------------//case7:if(UDR==0x00)Step=8;elseStep=0;break;case8:Temp=UDR&0xF0;Choose=UDR&0x03;if(Temp==Pixel){Step=9;Byte_Count=0;Sect_Channel_Count=3;}else{Step=0;}break;case9:if(Sect_Channel_Count=Sect1_Start_Channel[Choose]){Array[Byte_Count]=UDR;if(++Byte_Count=3){Step=0;Pass_Fg=0;}}else{Sect_Channel_Count++;}break;}}}voidCalculator(void){Address_Number=Row_Number-1;Address_Number*=Nodes;Address_Number+=Col_Number;Sect1_Start_Channel[0]=Row_Number1;Sect1_Start_Channel[0]+=Row_Number;//x3Sect1_Start_Channel[1]=Col_Number1;Sect1_Start_Channel[1]+=Col_Number;//x3Sect1_Start_Channel[2]=Address_Number1;Sect1_Start_Channel[2]+=Address_Number;//x3Sect1_Start_Channel[3]=0x00;}main(void){WDR();Init_Devices();Gray_Level_Count=Max_Level;Pass_Fg=0x10;Nodes=Read_Eeprom(0x06);Row_Number=Read_Eeprom(0x07);Col_Number=Read_Eeprom(0x08);Calculator();do{WDR();Gray_Scan_Disp();switch(Pass_Fg){case0x80:Pass_Fg=0;Calculator();Array[0]=0;Array[1]=0;Array[2]=255;Write_Eeprom(0x06,Nodes);Write_Eeprom(0x07,Row_Number);Write_Eeprom(0x08,Col_Number);break;case0x10:if(Seven_Color_Change_Count==0){Seven_Color_Change_Count=Seven_Color_Change_Time;if(++Mode7)Mode=1;if(Mode&0x01)Array[0]=0xFF;elseArray[0]=0x00;if(Mode&0x02)Array[1]=0xFF;elseArray[1]=0x00;if(Mode&0x04)Array[2]=0xFF;elseArray[2]=0x00;}else{Seven_Color_Change_Count--;}break;}//--------------------------------------------------//}while(1);///while(1)end}///mainend
本文标题:在AVR单片机上实现DMX512协议dmx512-avr
链接地址:https://www.777doc.com/doc-2600805 .html