dimmer.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*********************************************************************
  2. *
  3. * 8 channels AC dimmer library for Fraise pic18f device
  4. *
  5. *********************************************************************
  6. * Author Date Comment
  7. *********************************************************************
  8. * Antoine Rousseau apr 2016 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 <dimmer.h>
  26. static unsigned int Val[8]; // channels values
  27. // following values are duplicated for page swaping
  28. static unsigned char first[2]; // first channel to go (128 if none)
  29. static unsigned int firstTime[2]; // time of the first channel
  30. static unsigned char follower[16]; // channel following each channel (8 when last)
  31. static unsigned int followerTime[16]; // values of the sorted ports
  32. static unsigned char sorted[8];
  33. union{
  34. unsigned char flags;
  35. struct {
  36. unsigned page:1; // page number to be used by interrupt
  37. unsigned pageInt:1; // page currently used by interrupt
  38. unsigned changed:1; // one (or more) channel value changed
  39. unsigned is60Hz; // AC frequency is 60Hz (if false freq is 50Hz) ; measured by interrupt routine
  40. };
  41. } status;
  42. // Timer macros
  43. #define TIMER DIMMER_TIMER //5
  44. #include <timer.h>
  45. #define TIMER_INIT() do{\
  46. TIMER_CON = 0; \
  47. TIMER_PS1 = 1; /* prescaler 4 (->4MHz at 64MHz) */\
  48. TIMER_16BIT = 1;/* 16bits */\
  49. TIMER_IP = DIMMER_INTPRI; /* high/low priority interrupt */\
  50. TIMER_ON = 0; /* stop timer */\
  51. TIMER_IF = 0; /* clear flag */\
  52. TIMER_IE = 1; /* enable timer interrupt */\
  53. } while(0)
  54. // Timer macros
  55. #define INTPIN KINT(DIMMER_INTPIN)
  56. //#define INTPIN 2
  57. #include <intpin.h>
  58. void dimmerInit()
  59. {
  60. unsigned char i;
  61. status.flags = 0;
  62. TIMER_INIT();
  63. for(i = 0; i < 8; i++) {
  64. Val[i] = 65535;
  65. sorted[i] = i;
  66. }
  67. pinModeDigitalIn(DIMMER_INTPIN);
  68. INTPIN_EDGE = DIMMER_INTEDGE;
  69. INTPIN_IP = DIMMER_INTPRI; // high priority
  70. INTPIN_IF = 0; // clear flag
  71. INTPIN_IE = 1; // enable interrupt
  72. digitalClear(DIMMER_K0);
  73. digitalClear(DIMMER_K1);
  74. digitalClear(DIMMER_K2);
  75. digitalClear(DIMMER_K3);
  76. digitalClear(DIMMER_K4);
  77. digitalClear(DIMMER_K5);
  78. digitalClear(DIMMER_K6);
  79. digitalClear(DIMMER_K7);
  80. pinModeDigitalOut(DIMMER_K0);
  81. pinModeDigitalOut(DIMMER_K1);
  82. pinModeDigitalOut(DIMMER_K2);
  83. pinModeDigitalOut(DIMMER_K3);
  84. pinModeDigitalOut(DIMMER_K4);
  85. pinModeDigitalOut(DIMMER_K5);
  86. pinModeDigitalOut(DIMMER_K6);
  87. pinModeDigitalOut(DIMMER_K7);
  88. }
  89. void dimmerSet(unsigned char num,unsigned int val)
  90. {
  91. if(status.is60Hz) Val[num] = DIMMER_TMIN + (((unsigned long)(0xFFFF - val) * (33000UL- DIMMER_TMIN)) / 0xFFFF);
  92. else Val[num] = 8000UL + (((unsigned long)(0xFFFF - val) * (40000UL - DIMMER_TMIN)) / 0xFFFF);
  93. status.changed = 1;
  94. }
  95. void dimmerService(void)
  96. {
  97. unsigned char i, j, tmp;
  98. if( (!status.changed) || (status.page != status.pageInt)) return;
  99. status.changed = 0;
  100. // sort channels ascendingly, by insertion algorithm (https://en.wikipedia.org/wiki/Insertion_sort)
  101. for(i = 1 ; i < 8 ; i++) {
  102. j = i;
  103. while((j > 0) && (Val[sorted[j-1]] > Val[sorted[j]])) {
  104. tmp = sorted[j-1];
  105. sorted[j-1] = sorted[j];
  106. sorted[j] = tmp;
  107. j--;
  108. }
  109. }
  110. first[status.page==0] = sorted[0];
  111. firstTime[status.page==0] = 0xFFFF - Val[sorted[0]];
  112. for(i = 0 ; i < 7 ; i++) {
  113. tmp = sorted[i];
  114. if(status.page==0) tmp += 8;
  115. follower[tmp] = sorted[i+1];
  116. followerTime[tmp] = 0xFFFF - (Val[sorted[i+1]] - Val[sorted[i]]);
  117. }
  118. tmp = sorted[7];
  119. if(status.page==0) tmp += 8;
  120. follower[tmp] = 8;
  121. status.page = (status.page==0);
  122. }
  123. /*#define PROCESS_CHAN(chan, page) case chan : \
  124. digitalSet(DIMMER_K##chan); \
  125. next = follower[chan + (page?8:0)] ; \
  126. if(!(next != 8)) { \
  127. TIMER_H = (followerTime[chan + (page?8:0)]) >> 8; \
  128. TIMER_L = (followerTime[chan + (page?8:0)]) & 255; \
  129. TIMER_ON = 1; \
  130. } \
  131. break*/
  132. #define PROCESS_CHAN(chan) do { \
  133. digitalSet(DIMMER_K##chan); \
  134. if(status.pageInt) { \
  135. next = follower[chan + 8] ; \
  136. if(!(next & 8)) { \
  137. TIMER_H = (followerTime[chan + 8]) >> 8; \
  138. TIMER_L = (followerTime[chan + 8]) & 255; \
  139. TIMER_ON = 1; \
  140. } \
  141. } else { \
  142. next = follower[chan] ; \
  143. if(!(next & 8)) { \
  144. TIMER_H = (followerTime[chan]) >> 8; \
  145. TIMER_L = (followerTime[chan]) & 255; \
  146. TIMER_ON = 1; \
  147. } \
  148. } \
  149. } while(0)
  150. void dimmerInterrupt(void)
  151. {
  152. static unsigned char next;
  153. static unsigned int val;
  154. static t_time lastTime= 0;
  155. if(INTPIN_IF){
  156. //if(lastTime) status.is60Hz = elapsed(lastTime) < microToTime(9000);
  157. lastTime = timeISR();
  158. INTPIN_IF = 0;
  159. //status.pageInt = status.page;
  160. status.pageInt = 0;
  161. if(status.page) status.pageInt = 1;
  162. digitalClear(DIMMER_K0);
  163. digitalClear(DIMMER_K1);
  164. digitalClear(DIMMER_K2);
  165. digitalClear(DIMMER_K3);
  166. digitalClear(DIMMER_K4);
  167. digitalClear(DIMMER_K5);
  168. digitalClear(DIMMER_K6);
  169. digitalClear(DIMMER_K7);
  170. next = status.pageInt ? first[1] : first[0];
  171. val = status.pageInt ? firstTime[1] : firstTime[0];
  172. TIMER_ON = 0;
  173. TIMER_H = val>>8;
  174. TIMER_L = val&255;
  175. TIMER_IF = 0;
  176. TIMER_ON = 1;
  177. }
  178. if(TIMER_IF) {
  179. TIMER_ON = 0;
  180. TIMER_IF = 0;
  181. //if(!(next&8)) {
  182. if(!(next&4)) {
  183. if(!(next&2)) {
  184. if(!(next&1)) PROCESS_CHAN(0);
  185. else PROCESS_CHAN(1);
  186. } else {
  187. if(!(next&1)) PROCESS_CHAN(2);
  188. else PROCESS_CHAN(3);
  189. }
  190. } else {
  191. if(!(next&2)) {
  192. if(!(next&1)) PROCESS_CHAN(4);
  193. else PROCESS_CHAN(5);
  194. } else {
  195. if(!(next&1)) PROCESS_CHAN(6);
  196. else PROCESS_CHAN(7);
  197. }
  198. }
  199. //}
  200. /*if(status.pageInt) switch(next) {
  201. PROCESS_CHAN(0, 1);
  202. PROCESS_CHAN(1, 1);
  203. PROCESS_CHAN(2, 1);
  204. PROCESS_CHAN(3, 1);
  205. PROCESS_CHAN(4, 1);
  206. PROCESS_CHAN(5, 1);
  207. PROCESS_CHAN(6, 1);
  208. PROCESS_CHAN(7, 1);
  209. } else switch(next) {
  210. PROCESS_CHAN(0, 0);
  211. PROCESS_CHAN(1, 0);
  212. PROCESS_CHAN(2, 0);
  213. PROCESS_CHAN(3, 0);
  214. PROCESS_CHAN(4, 0);
  215. PROCESS_CHAN(5, 0);
  216. PROCESS_CHAN(6, 0);
  217. PROCESS_CHAN(7, 0);
  218. }*/
  219. //fraiseISR(); // accept a bit of jitter to better protect Fraise communication
  220. }
  221. }
  222. void dimmerHighInterrupt(void)
  223. {
  224. #if DIMMER_INTPRI == 1
  225. dimmerInterrupt();
  226. #endif
  227. }
  228. void dimmerLowInterrupt(void)
  229. {
  230. #if DIMMER_INTPRI != 1
  231. dimmerInterrupt();
  232. #endif
  233. }
  234. void dimmerReceive()
  235. {
  236. unsigned char c, c2;
  237. unsigned int i = 0;
  238. c=fraiseGetChar();
  239. if(c < 8) {
  240. dimmerSet(c, fraiseGetInt());
  241. } else if (c == 8) {
  242. status.is60Hz = (fraiseGetChar() != 0);
  243. }
  244. }
  245. void dimmerPrintDebug()
  246. {
  247. unsigned char i;
  248. putchar('B');
  249. for(i = 0 ; i < 8 ; i++) {
  250. putchar(sorted[i]);
  251. }
  252. for(i = 0 ; i < 8 ; i++) {
  253. putchar(follower[i]);
  254. }
  255. for(i = 0 ; i < 8 ; i++) {
  256. putchar(follower[i+8]);
  257. }
  258. //putchar(status.page*2 + status.pageInt);
  259. putchar(status.flags);
  260. putchar('\n');
  261. }