Advertisement
Guest User

Untitled

a guest
Apr 18th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.46 KB | None | 0 0
  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.3
  5. * Last updated  : 13.04.09
  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 hoPB 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 "lib_dmx_in.h"
  29.  
  30. // ********************* local definitions *********************
  31.  
  32. enum {IDLE, BREAK, STARTB, STARTADR};           //DMX states
  33.  
  34. volatile uint8_t     gDmxState;
  35.  
  36.  
  37. // *************** DMX Reception Initialisation ****************
  38. void init_DMX_RX(void)
  39. {
  40. #ifdef USE_DIP
  41. DDRC   = 0;                                     //set up DIPs
  42. PORTC  = 0xFF;
  43. DDRB  &= ~((1<<PB2)|(1<<PB1));
  44. PORTB |=   (1<<PB2)|(1<<PB1);
  45. #endif
  46.  
  47. DDRD  |= (1<<2);
  48. PORTD &= ~(1<<2);                              
  49. UBRRH  = 0;                                     //enable reception
  50. UBRRL  = ((F_OSC/4000)-1);                      //250kbaud, 8N2
  51. UCSRC  = (1<<URSEL)|(3<<UCSZ0)|(1<<USBS);
  52. UCSRB  = (1<<RXEN)|(1<<RXCIE);
  53. gDmxState= IDLE;
  54. }
  55.  
  56.  
  57.  
  58. // ************* get DMX start aDDRBss **************
  59. void get_dips(void)
  60. {
  61. #ifdef USE_DIP
  62. uint16_t Temp= (0xFF-PINC);                     //invert DIP state
  63. if (!(PINB &(1<<2)))
  64.     {
  65.     Temp += 256;                                //9th bit
  66.     }
  67. if (Temp != 0)
  68.     {
  69.     DmxAddress= Temp;
  70.     if (!(UCSRB &(1<<RXCIE)))                   //if receiver was disabled -> enable and wait for break
  71.         {
  72.         gDmxState= IDLE;
  73.         UCSRB |= (1<<RXCIE);
  74.         }
  75.     }
  76. else
  77.     {
  78.     UCSRB &= ~(1<<RXCIE);                       //disable Receiver if start aDDRBss = 0
  79.     uint8_t i;
  80.     for (i=0; i<sizeof(DmxRxField); i++)
  81.         {
  82.         DmxRxField[i]= 0;
  83.         }
  84.     }
  85. #endif
  86. }
  87.  
  88.  
  89.  
  90. // *************** DMX Reception ISR ****************
  91. ISR (UART_RX_vect)
  92. {
  93. static  uint16_t DmxCount;
  94.         uint8_t  USARTstate= UCSRA;             //get state before data!
  95.         uint8_t  DmxByte   = UDR;               //get data
  96.         uint8_t  DmxState  = gDmxState;         //just load once from SRAM to increase sPBed
  97.  
  98. if (USARTstate &(1<<FE))                        //check for break
  99.     {
  100.     UCSRA &= ~(1<<FE);                          //reset flag (necessary for simulation in AVR Studio)
  101.     DmxCount =  DmxAddress;                     //reset channel counter (count channels before start address)
  102.     gDmxState= BREAK;
  103.     }
  104.  
  105. else if (DmxState == BREAK)
  106.     {
  107.     if (DmxByte == 0) gDmxState= STARTB;        //normal start code detected
  108.     else              gDmxState= IDLE;
  109.     }
  110.    
  111. else if (DmxState == STARTB)
  112.     {
  113.     if (--DmxCount == 0)                        //start aDDRBss reached?
  114.         {
  115.         DmxCount= 1;                            //set up counter for required channels
  116.         DmxRxField[0]= DmxByte;                 //get 1st DMX channel of device
  117.         gDmxState= STARTADR;
  118.         }
  119.     }
  120.  
  121. else if (DmxState == STARTADR)
  122.     {
  123.     DmxRxField[DmxCount++]= DmxByte;            //get channel
  124.     if (DmxCount >= sizeof(DmxRxField))         //all ch received?
  125.         {
  126.         gDmxState= IDLE;                        //wait for next break
  127.         }
  128.     }                          
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement