1 | /**** A P P L I C A T I O N N O T E ************************************
|
2 | *
|
3 | * Title : DMX512 reception library
|
4 | * Version : v1.2
|
5 | * Last updated : 21.04.07
|
6 | * Target : Transceiver Rev.3.01 [ATmega8515]
|
7 | * Clock : 8MHz, 16MHz
|
8 | *
|
9 | * written by hendrik hoelscher, www.hoelscher-hi.de
|
10 | ***************************************************************************
|
11 | This program is free software; you can redistribute it and/or
|
12 | modify it under the terms of the GNU General Public License
|
13 | as published by the Free Software Foundation; either version2 of
|
14 | the License, or (at your option) any later version.
|
15 |
|
16 | This program is distributed in the hope that it will be useful,
|
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
19 | General Public License for more details.
|
20 |
|
21 | If you have no copy of the GNU General Public License, write to the
|
22 | Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
23 |
|
24 | For other license models, please contact the author.
|
25 |
|
26 | ;***************************************************************************/
|
27 |
|
28 | #include <avr/io.h>
|
29 | #include "lib_dmx_in.h"
|
30 |
|
31 | // ********************* local definitions *********************
|
32 |
|
33 | enum {IDLE, BREAK, STARTB, STARTADR}; //DMX states
|
34 |
|
35 | uint8_t gDmxState;
|
36 | uint8_t *gDmxPnt;
|
37 | uint16_t DmxCount;
|
38 |
|
39 |
|
40 | // *************** DMX Reception Initialisation ****************
|
41 | void init_DMX(void)
|
42 | {
|
43 | #ifdef USE_DIP
|
44 | DDRC= 0; //set up DIPs
|
45 | PORTC= 0xFF;
|
46 | DDRE &= ~((1<<2)|(1<<1));
|
47 | PORTE |= (1<<2)|(1<<1);
|
48 | #endif
|
49 |
|
50 | DDRD |= (1<<2);
|
51 | PORTD &= ~(1<<2); //enable reception
|
52 | //UBRRH = 0;
|
53 | UBRR0H = 0; // NEW
|
54 | //UBRRL = ((F_OSC/4000)-1); //250kbaud, 8N2
|
55 | UBRR0L = ((F_OSC/4000)-1); // NEW //250kbaud, 8N2
|
56 | //UCSRC |= (1<<URSEL)|(3<<UCSZ0)|(1<<USBS);
|
57 | UCSR0C |= (3<<UCSZ01)|(1<<USBS0); // NEW
|
58 | //UCSRB |= (1<<RXEN)|(1<<RXCIE);
|
59 | UCSR0B |= (1<<RXEN0)|(1<<RXCIE0); // NEW
|
60 | gDmxState= IDLE;
|
61 |
|
62 | uint8_t i;
|
63 | for (i=0; i<DMX_CHANNELS; i++)
|
64 | {
|
65 | DmxField[i]= 0;
|
66 | }
|
67 |
|
68 | }
|
69 |
|
70 |
|
71 | // ************* get DMX start address **************
|
72 | void get_dips(void)
|
73 | {
|
74 | #ifdef USE_DIP
|
75 | uint16_t Temp= (255-PINC); //invert DIP state
|
76 | if (!(PINE &(1<<2)))
|
77 | {
|
78 | Temp= Temp +256; //9th bit
|
79 | }
|
80 | if (Temp != 0)
|
81 | {
|
82 | DmxAddress= Temp;
|
83 | if (!(UCSRB &(1<<RXCIE))) //if receiver was disabled -> enable and wait for break
|
84 | {
|
85 | gDmxState= IDLE;
|
86 | UCSRB |= (1<<RXCIE);
|
87 | }
|
88 | }
|
89 | else
|
90 | {
|
91 | UCSRB &= ~(1<<RXCIE); //disable Receiver if start address = 0
|
92 | for (Temp=0; Temp<DMX_CHANNELS; Temp++)
|
93 | {
|
94 | DmxField[Temp]= 0;
|
95 | }
|
96 | }
|
97 | #endif
|
98 | DmxAddress = 1;
|
99 | }
|
100 |
|
101 |
|
102 | // *************** DMX Reception ISR ****************
|
103 | ISR (USART0_RX_vect)
|
104 | {
|
105 | //uint8_t USARTstate= UCSRA; //get state
|
106 | uint8_t USARTstate= UCSR0A; //NEW //get state
|
107 | //uint8_t DmxByte= UDR; //get data
|
108 | uint8_t DmxByte= UDR0; // NEW //get data
|
109 | uint8_t DmxState= gDmxState; //just get once from SRAM!!!
|
110 |
|
111 | //if (USARTstate &(1<<FE)) //check for break
|
112 | if (USARTstate &(1<<FE0)) // NEW //check for break
|
113 | {
|
114 | //UCSRA &= ~(1<<FE); //reset flag
|
115 | UCSR0A &= ~(1<<FE0); //NEW //reset flag
|
116 | DmxCount= DmxAddress; //reset frame counter
|
117 | gDmxState= BREAK;
|
118 | }
|
119 |
|
120 | else if (DmxState == BREAK)
|
121 | {
|
122 | if (DmxByte == 0)
|
123 | {
|
124 | gDmxState= STARTB; //normal start code detected
|
125 | gDmxPnt= ((uint8_t*)DmxField +1);
|
126 | }
|
127 | else gDmxState= IDLE;
|
128 | }
|
129 |
|
130 | else if (DmxState == STARTB)
|
131 | {
|
132 | if (--DmxCount == 0) //start address reached?
|
133 | {
|
134 | gDmxState= STARTADR;
|
135 | DmxField[0]= DmxByte;
|
136 | }
|
137 | }
|
138 |
|
139 | else if (DmxState == STARTADR)
|
140 | {
|
141 | uint8_t *DmxPnt;
|
142 | DmxPnt= gDmxPnt;
|
143 | *DmxPnt= DmxByte;
|
144 | if (++DmxPnt >= (DmxField +DMX_CHANNELS)) //all ch received?
|
145 | {
|
146 | gDmxState= IDLE;
|
147 | }
|
148 | else gDmxPnt= DmxPnt;
|
149 | }
|
150 | }
|