dmx.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*********************************************************************
  2. *
  3. * DMX library for Fraise pic18f device
  4. *
  5. *********************************************************************
  6. * Author Date Comment
  7. *********************************************************************
  8. * Antoine Rousseau may 2012 Original.
  9. ********************************************************************/
  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 version 2
  14. # of 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
  19. # GNU General Public License for more details.
  20. # You should have received a copy of the GNU General Public License
  21. # along with this program; if not, write to the Free Software
  22. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  23. # MA 02110-1301, USA.
  24. */
  25. #include <dmx.h>
  26. //serial port:
  27. #if DMX_UART_NUM==1
  28. #define SPBRGx SPBRG1
  29. #define SPBRGHx SPBRGH1
  30. #define BAUDCONx BAUDCON1
  31. #define BAUDCONxbits BAUDCON1bits
  32. #define RCREGx RCREG1
  33. #define RCSTAx RCSTA1
  34. #define RCSTAxbits RCSTA1bits
  35. #define TXREGx TXREG1
  36. #define TXSTAx TXSTA1
  37. #define TXSTAxbits TXSTA1bits
  38. #define RCxIF PIR1bits.RC1IF
  39. #define TXxIF PIR1bits.TX1IF
  40. #define RCxIE PIE1bits.RC1IE
  41. #define TXxIE PIE1bits.TX1IE
  42. #define RCxIP IPR1bits.RC1IP
  43. #define TXxIP IPR1bits.TX1IP
  44. #else
  45. #define SPBRGx SPBRG2
  46. #define SPBRGHx SPBRGH2
  47. #define BAUDCONx BAUDCON2
  48. #define BAUDCONxbits BAUDCON2bits
  49. #define RCREGx RCREG2
  50. #define RCSTAx RCSTA2
  51. #define RCSTAxbits RCSTA2bits
  52. #define TXREGx TXREG2
  53. #define TXSTAx TXSTA2
  54. #define TXSTAxbits TXSTA2bits
  55. #define RCxIF PIR3bits.RC2IF
  56. #define TXxIF PIR3bits.TX2IF
  57. #define RCxIE PIE3bits.RC2IE
  58. #define TXxIE PIE3bits.TX2IE
  59. #define RCxIP IPR3bits.RC2IP
  60. #define TXxIP IPR3bits.TX2IP
  61. #endif
  62. //(1 port equ)
  63. #ifndef BAUDCON1
  64. #define SPBRG1 SPBRG
  65. #define SPBRGH1 SPBRGH
  66. #define BAUDCON1 BAUDCON
  67. #define BAUDCON1bits BAUDCONbits
  68. #define RCREG1 RCREG
  69. #define RCSTA1 RCSTA
  70. #define RCSTA1bits RCSTAbits
  71. #define TXREG1 TXREG
  72. #define TXSTA1 TXSTA
  73. #define TXSTA1bits TXSTAbits
  74. #define RC1IF RCIF
  75. #define TX1IF TXIF
  76. #define RC1IE RCIE
  77. #define TX1IE TXIE
  78. #define RC1IP RCIP
  79. #define TX1IP TXIP
  80. #endif
  81. unsigned char DMXRegisters[DMX_NBCHAN];
  82. void Set250kB(void)
  83. {
  84. //baud rate : br=FOSC/[4 (n+1)] : n=FOSC/(4*br)-1 : br=250kHz, n=FOSC/1000000 - 1
  85. #define BRGHL (FOSC/1000000 - 1)
  86. SPBRGHx=BRGHL/256;
  87. SPBRGx=BRGHL%256;
  88. }
  89. void Set96kB(void)
  90. {
  91. //baud rate : br=FOSC/[4 (n+1)] : n=FOSC/(4*br)-1 : br=96kHz, n=FOSC/384000 - 1
  92. #define BRGHL96 (FOSC/384000 - 1)
  93. SPBRGHx=BRGHL96/256;
  94. SPBRGx=BRGHL96%256;
  95. }
  96. void DMXInit(void)
  97. {
  98. int i;
  99. for(i=0;i<DMX_NBCHAN;i++) DMXRegisters[i]=0; //clear all channels
  100. /*DMX_UART_PIN=1;
  101. DMX_UART_TRIS=0;*/
  102. digitalSet(DMX_UART_PIN);
  103. pinModeDigitalOut(DMX_UART_PIN);
  104. BAUDCONxbits.BRG16=1;
  105. TXSTAxbits.TXEN=1;
  106. TXSTAxbits.BRGH=1;
  107. TXSTAxbits.TX9=1;
  108. TXSTAxbits.TX9D=1;
  109. RCSTAxbits.SPEN=1;
  110. }
  111. void DMXSet(unsigned int channel, unsigned char value)
  112. {
  113. if(channel==0) return;
  114. DMXRegisters[channel]=value;
  115. }
  116. void DMXService()
  117. {
  118. static int channel=-1;
  119. if(!TXSTAxbits.TRMT) return;
  120. if(!TXxIF) return;
  121. if(channel==-1) {
  122. Set96kB();
  123. TXREGx=0;
  124. channel=0;
  125. } else if(channel==0) {
  126. Set250kB();
  127. TXREGx=0;
  128. channel=1;
  129. } else {
  130. TXREGx=DMXRegisters[channel++];
  131. if(channel==DMX_NBCHAN) channel=-1;
  132. }
  133. }