Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**** A P P L I C A T I O N N O T E ************************************
- *
- * Title : DMX512 reception library
- * Version : v1.3
- * Last updated : 13.04.09
- * Target : Transceiver Rev.3.01 [ATmega8515]
- * Clock : 8MHz, 16MHz
- *
- * written by hendrik hoelscher, www.hoelscher-hi.de
- ***************************************************************************
- This program is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License
- as published by the Free Software Foundation; either version2 of
- the License, or (at your option) any later version.
- This program is distributed in the hoPB that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
- If you have no copy of the GNU General Public License, write to the
- Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- For other license models, please contact the author.
- ;***************************************************************************/
- #include "lib_dmx_in.h"
- // ********************* local definitions *********************
- enum {IDLE, BREAK, STARTB, STARTADR}; //DMX states
- volatile uint8_t gDmxState;
- // *************** DMX Reception Initialisation ****************
- void init_DMX_RX(void)
- {
- #ifdef USE_DIP
- DDRC = 0; //set up DIPs
- PORTC = 0xFF;
- DDRB &= ~((1<<PB2)|(1<<PB1));
- PORTB |= (1<<PB2)|(1<<PB1);
- #endif
- DDRD |= (1<<2);
- PORTD &= ~(1<<2);
- UBRRH = 0; //enable reception
- UBRRL = ((F_OSC/4000)-1); //250kbaud, 8N2
- UCSRC = (1<<URSEL)|(3<<UCSZ0)|(1<<USBS);
- UCSRB = (1<<RXEN)|(1<<RXCIE);
- gDmxState= IDLE;
- }
- // ************* get DMX start aDDRBss **************
- void get_dips(void)
- {
- #ifdef USE_DIP
- uint16_t Temp= (0xFF-PINC); //invert DIP state
- if (!(PINB &(1<<2)))
- {
- Temp += 256; //9th bit
- }
- if (Temp != 0)
- {
- DmxAddress= Temp;
- if (!(UCSRB &(1<<RXCIE))) //if receiver was disabled -> enable and wait for break
- {
- gDmxState= IDLE;
- UCSRB |= (1<<RXCIE);
- }
- }
- else
- {
- UCSRB &= ~(1<<RXCIE); //disable Receiver if start aDDRBss = 0
- uint8_t i;
- for (i=0; i<sizeof(DmxRxField); i++)
- {
- DmxRxField[i]= 0;
- }
- }
- #endif
- }
- // *************** DMX Reception ISR ****************
- ISR (UART_RX_vect)
- {
- static uint16_t DmxCount;
- uint8_t USARTstate= UCSRA; //get state before data!
- uint8_t DmxByte = UDR; //get data
- uint8_t DmxState = gDmxState; //just load once from SRAM to increase sPBed
- if (USARTstate &(1<<FE)) //check for break
- {
- UCSRA &= ~(1<<FE); //reset flag (necessary for simulation in AVR Studio)
- DmxCount = DmxAddress; //reset channel counter (count channels before start address)
- gDmxState= BREAK;
- }
- else if (DmxState == BREAK)
- {
- if (DmxByte == 0) gDmxState= STARTB; //normal start code detected
- else gDmxState= IDLE;
- }
- else if (DmxState == STARTB)
- {
- if (--DmxCount == 0) //start aDDRBss reached?
- {
- DmxCount= 1; //set up counter for required channels
- DmxRxField[0]= DmxByte; //get 1st DMX channel of device
- gDmxState= STARTADR;
- }
- }
- else if (DmxState == STARTADR)
- {
- DmxRxField[DmxCount++]= DmxByte; //get channel
- if (DmxCount >= sizeof(DmxRxField)) //all ch received?
- {
- gDmxState= IDLE; //wait for next break
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement