《LED调光-DMX512灯光协义接收控制》转

时间:2023-03-09 06:58:52
《LED调光-DMX512灯光协义接收控制》转

源:http://blog.163.com/zhaojun_xf/blog/static/30050580200951023046891

来自:

作者:龙图开发网

文章来源:龙图开发网

时间:2009-5-18 10:15:01

//LED调光-DMX512灯光协义接收控制
//龙图开发网 http://www.longtoo.com 原创,转载请注明出处
//ICC-AVR application builder : 2007/12/14
// 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=,JS_002=,JS_003=;
int CUR_BYTE_A=,IADDRESS_A=;
char JS_1=,JS_2=,JS_3=,JS_4=,JS_5=,JS_6=;
// RED GREEN BLUE RGB
char DMX_DATA_1=,DMX_DATA_2=,DMX_DATA_3=,DMX_DATA_4=; char LED_TRANSFER_DATA[]=
{
,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,
}; //------------------------------------------------
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 LED_FLASH()
{
++JS_01;
if (JS_01==)
{
//FLASH_LED_PORTC^=FLASH_LED_R_OFF;
//FLASH_LED_PORTC^=FLASH_LED_G_OFF;
//FLASH_LED_PORTC^=FLASH_LED_B_OFF;
}
if (JS_01==)
{
JS_01&=;
}
} 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==)
{JS_1=;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 & (<<RXC)) ); // NO USE
// from buffer
status = UCSRA;
resh = UCSRB;
resl = UDR;
resh = (resh >> ) & 0x01; // TAKE RXB8
if (resh==) //(1<<RXB8))
{ //LED_DISPLAY_V4=15; //test ok
DMX_DATA_PAN=;
if (CUR_BYTE_A==IADDRESS_A+) DMX_DATA_1=resl;
if (CUR_BYTE_A==IADDRESS_A+) DMX_DATA_2=resl;
if (CUR_BYTE_A==IADDRESS_A+) DMX_DATA_3=resl;
if (CUR_BYTE_A==IADDRESS_A+) DMX_DATA_4=resl;
if (DMX_ADDRESS_PAN==)
++CUR_BYTE_A; //DMX address count register
if (CUR_BYTE_A==) CUR_BYTE_A&=;
}
else //(0<<RXB8)
{
CODE_Data=resl;
if (CODE_Data==)
{ //test ok
CUR_BYTE_A = ;
CODE_Data = 0xff;
DMX_ADDRESS_PAN=;
}
else
DMX_ADDRESS_PAN=;
}
// DMX_PORTD|=DMX_control_send; //pd6=1
} void main(void)
{
init_devices();
FLASH_LED_PORTD=DMX_control_receive; //DMX_control_receive; //pd2=0 PD2
//insert your functional code here...
}