Rev 1265 | Rev 1613 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1265 | Rev 1611 | ||
|---|---|---|---|
| Line 27... | Line 27... | ||
| 27 | 27 | ||
| 28 | /* frequency stores the evaluated frequency, 50 or 60 Hz |
28 | /* frequency stores the evaluated frequency, 50 or 60 Hz |
| 29 | don't calculate net frequency since the internal oscillator is not to be trusted, instead check if 50Hz or 60Hz system |
29 | don't calculate net frequency since the internal oscillator is not to be trusted, instead check if 50Hz or 60Hz system |
| 30 | should be used for switching powerStepTable depending on frequency */ |
30 | should be used for switching powerStepTable depending on frequency */ |
| 31 | uint8_t frequency = CAN_MODULE_ENUM_DIMMER230_NETINFO_FREQUENCY_ERROR_UNKNOWN; |
31 | uint8_t frequency = CAN_MODULE_ENUM_DIMMER230_NETINFO_FREQUENCY_ERROR_UNKNOWN; |
| 32 | - | ||
| 33 | /* zero cross sync (time between pos flank of xc-detection and real zero cross) */ |
32 | /* zero cross sync (time between pos flank of xc-detection and real zero cross) */ |
| 34 | uint16_t xcTimeDiff=0; |
33 | uint16_t xcTimeDiff=0; |
| 35 | 34 | ||
| 36 | /* dimmerValue is the current output |
35 | /* dimmerValue is the current output |
| 37 | value of 0 is zero output, value of 255 is max output (ACT_DIMMMER230_MIN_DIM, ACT_DIMMMER230_MAX_DIM) |
36 | value of 0 is zero output, value of 255 is max output (ACT_DIMMMER230_MIN_DIM, ACT_DIMMMER230_MAX_DIM) |
| 38 | stored in eeprom, and restored at startup */ |
37 | stored in eeprom, and restored at startup */ |
| 39 | uint8_t dimmerValue = ACT_DIMMMER230_MIN_DIM; |
38 | uint8_t dimmerValue = ACT_DIMMMER230_MIN_DIM; |
| 40 | 39 | ||
| 41 | uint8_t state = ACT_DIMMMER230_STATE_IDLE; |
40 | uint8_t state = ACT_DIMMMER230_STATE_IDLE; |
| 42 | 41 | ||
| 43 | /* powerStepTable is used for stepping the output for constant increase/decrease in power, this should be a quater of a sin^2-wave (since the second quater can be calculated) */ |
42 | /* powerStepTable is used for stepping the output for constant increase/decrease in power, this should be a quater of a sin^2-wave (since the second quater can be calculated) */ |
| 44 | const uint16_t powerStepTable[] PROGMEM = {845, 1068, 1225, 1352, 1459, 1553, 1638, 1716, 1788, 1855, 1917, 1976, 2032, 2086, 2138, 2188, 2236, 2282, 2327, 2370, 2412, 2453, 2493, 2532, 2570, 2607, 2643, 2679, 2714, 2748, 2782, 2815, 2848, 2880, 2911, 2942, 2972, 3002, 3032, 3061, 3090, 3119, 3147, 3175, 3203, 3230, 3257, 3284, 3311, 3337, 3363, 3389, 3415, 3440, 3465, 3490, 3515, 3539, 3563, 3587, 3611, 3635, 3659, 3682, 3705, 3728, 3751, 3774, 3797, 3820, 3843, 3865, 3887, 3909, 3931, 3953, 3975, 3997, 4019, 4041, 4062, 4083, 4104, 4125, 4146, 4167, 4188, 4209, 4230, 4251, 4272, 4292, 4313, 4333, 4354, 4374, 4395, 4415, 4435, 4455, 4476, 4496, 4516, 4536, 4556, 4576, 4596, 4615, 4635, 4655, 4675, 4694, 4714, 4734, 4754, 4773, 4793, 4813, 4832, 4852, 4872, 4891, 4911, 4931, 4951, 4970, 4990}; |
43 | const uint16_t powerStepTable[] PROGMEM = {845, 1068, 1225, 1352, 1459, 1553, 1638, 1716, 1788, 1855, 1917, 1976, 2032, 2086, 2138, 2188, 2236, 2282, 2327, 2370, 2412, 2453, 2493, 2532, 2570, 2607, 2643, 2679, 2714, 2748, 2782, 2815, 2848, 2880, 2911, 2942, 2972, 3002, 3032, 3061, 3090, 3119, 3147, 3175, 3203, 3230, 3257, 3284, 3311, 3337, 3363, 3389, 3415, 3440, 3465, 3490, 3515, 3539, 3563, 3587, 3611, 3635, 3659, 3682, 3705, 3728, 3751, 3774, 3797, 3820, 3843, 3865, 3887, 3909, 3931, 3953, 3975, 3997, 4019, 4041, 4062, 4083, 4104, 4125, 4146, 4167, 4188, 4209, 4230, 4251, 4272, 4292, 4313, 4333, 4354, 4374, 4395, 4415, 4435, 4455, 4476, 4496, 4516, 4536, 4556, 4576, 4596, 4615, 4635, 4655, 4675, 4694, 4714, 4734, 4754, 4773, 4793, 4813, 4832, 4852, 4872, 4891, 4911, 4931, 4951, 4970, 4990}; |
| 45 | 44 | ||
| 46 | #if act_dimmer230_SYNC>0 |
45 | #if act_dimmer230_SYNC>0 |
| 47 | uint8_t zeroCrossCnt = 0; |
46 | uint8_t zeroCrossCnt = 0; |
| 48 | #endif |
47 | #endif |
| 49 | 48 | ||
| 50 | 49 | ||
| 51 | /* Set the variable to "disconnected" if no zerocross have arrived for a while */ |
50 | /* Set the variable to "disconnected" if no zerocross have arrived for a while */ |
| 52 | void Net_Connection_callback(uint8_t timer) |
51 | void Net_Connection_callback(uint8_t timer) |
| 53 | { |
52 | { |
| Line 91... | Line 90... | ||
| 91 | { |
90 | { |
| 92 | /* only execute if zerocross pin is low (after real zerocross). |
91 | /* only execute if zerocross pin is low (after real zerocross). |
| 93 | Calculate the diff from real zero-cross. |
92 | Calculate the diff from real zero-cross. |
| 94 | */ |
93 | */ |
| 95 | if (!status) |
94 | if (!status) |
| 96 | { |
95 | { |
| 97 | xcTimeDiff = TCNT1; |
96 | xcTimeDiff = TCNT1; |
| 98 | xcTimeDiff = xcTimeDiff >> 1; //divide by two |
97 | xcTimeDiff = xcTimeDiff >> 1; //divide by two |
| 99 | if (xcTimeDiff > 600) { |
98 | if (xcTimeDiff > 600) { |
| 100 | xcTimeDiff = 600; |
99 | xcTimeDiff = 600; |
| 101 | } |
100 | } |
| Line 124... | Line 123... | ||
| 124 | } |
123 | } |
| 125 | 124 | ||
| 126 | /* Check demo states */ |
125 | /* Check demo states */ |
| 127 | if (dimmerValue == fadeTarget) { |
126 | if (dimmerValue == fadeTarget) { |
| 128 | switch (demoState) |
127 | switch (demoState) |
| 129 | { |
128 | { |
| 130 | case ACT_DIMMMER230_DEMO_STATE_NOT_RUNNING: |
129 | case ACT_DIMMMER230_DEMO_STATE_NOT_RUNNING: |
| 131 | break; |
130 | break; |
| 132 | case ACT_DIMMMER230_DEMO_STATE_DECREASE: |
131 | case ACT_DIMMMER230_DEMO_STATE_DECREASE: |
| 133 | demoState = ACT_DIMMMER230_DEMO_STATE_INCREASE; |
132 | demoState = ACT_DIMMMER230_DEMO_STATE_INCREASE; |
| 134 | fadeTarget = demoHighValue; |
133 | fadeTarget = demoHighValue; |
| 135 | fadeSpeed = -fadeSpeed; |
134 | fadeSpeed = -fadeSpeed; |
| 136 | break; |
135 | break; |
| 137 | case ACT_DIMMMER230_DEMO_STATE_INCREASE: |
136 | case ACT_DIMMMER230_DEMO_STATE_INCREASE: |
| 138 | demoState = ACT_DIMMMER230_DEMO_STATE_GOBACK; |
137 | demoState = ACT_DIMMMER230_DEMO_STATE_GOBACK; |
| 139 | fadeTarget = demoEndValue; |
138 | fadeTarget = demoEndValue; |
| 140 | fadeSpeed = -fadeSpeed; |
139 | fadeSpeed = -fadeSpeed; |
| 141 | break; |
140 | break; |
| 142 | case ACT_DIMMMER230_DEMO_STATE_GOBACK: |
141 | case ACT_DIMMMER230_DEMO_STATE_GOBACK: |
| 143 | demoState = ACT_DIMMMER230_DEMO_STATE_NOT_RUNNING; |
142 | demoState = ACT_DIMMMER230_DEMO_STATE_NOT_RUNNING; |
| 144 | break; |
143 | break; |
| 145 | } |
144 | } |
| 146 | } |
145 | } |
| 147 | 146 | ||
| 148 | /* Calculate new time to triac triggering */ |
147 | /* Calculate new time to triac triggering */ |
| 149 | uint16_t newTimerVal = xcTimeDiff; /* add the zerocross timediff */ |
148 | uint16_t newTimerVal = xcTimeDiff; /* add the zerocross timediff */ |
| 150 | 149 | ||
| 151 | if (dimmerValue > 0 && dimmerValue <= 127) |
150 | if (dimmerValue > 0 && dimmerValue <= 127) |
| 152 | { |
151 | { |
| 153 | newTimerVal += ACT_DIMMMER230_50HZ_PERIOD_TIME - pgm_read_word(&powerStepTable[dimmerValue-1]); |
152 | newTimerVal += ACT_DIMMMER230_50HZ_PERIOD_TIME - pgm_read_word(&powerStepTable[dimmerValue-1]); |
| 154 | } |
153 | } |
| 155 | else if (dimmerValue < 255) |
154 | else if (dimmerValue < 255) |
| 156 | { |
155 | { |
| 157 | newTimerVal += pgm_read_word(&powerStepTable[254-dimmerValue]); |
156 | newTimerVal += pgm_read_word(&powerStepTable[254-dimmerValue]); |
| 158 | } |
157 | } |
| 159 | else if (dimmerValue == 255) |
158 | else if (dimmerValue == 255) |
| 160 | { |
159 | { |
| 161 | newTimerVal += 100; /* almost max, to make sure we don't fire before zerocross */ |
160 | newTimerVal += 100; /* almost max, to make sure we don't fire before zerocross */ |
| - | 161 | gpio_set_pin(act_dimmer230_CHAN1_IO); |
|
| 162 | } |
162 | } |
| 163 | 163 | ||
| 164 | if (newTimerVal > ACT_DIMMMER230_50HZ_PERIOD_TIME) |
164 | if (newTimerVal > ACT_DIMMMER230_50HZ_PERIOD_TIME) |
| 165 | { |
165 | { |
| 166 | newTimerVal -= ACT_DIMMMER230_50HZ_PERIOD_TIME; |
166 | newTimerVal -= ACT_DIMMMER230_50HZ_PERIOD_TIME; |
| 167 | } |
167 | } |
| 168 | 168 | ||
| 169 | TCNT1 = 0; /* reset timer1 count register */ |
169 | TCNT1 = 0; /* reset timer1 count register */ |
| 170 | if (dimmerValue > 0) |
170 | if (dimmerValue > 0 && dimmerValue != 255 ) /* dont trigger the triac if dimmerValue is 0 */ |
| 171 | { |
171 | { |
| 172 | OCR1A = newTimerVal; |
172 | OCR1A = newTimerVal; |
| 173 | 173 | ||
| 174 | /* enable timer and timer interrupt */ |
174 | /* enable timer and timer interrupt */ |
| 175 | TIFR1=(1<<OCF1A); /* clear interrupt flag before enabling interrupt */ |
175 | TIFR1=(1<<OCF1A); /* clear interrupt flag before enabling interrupt */ |
| Line 294... | Line 294... | ||
| 294 | txMsg.Header.ModuleId = act_dimmer230_ID; |
294 | txMsg.Header.ModuleId = act_dimmer230_ID; |
| 295 | txMsg.Header.Command = CAN_MODULE_CMD_DIMMER230_NETINFO; |
295 | txMsg.Header.Command = CAN_MODULE_CMD_DIMMER230_NETINFO; |
| 296 | txMsg.Length = 2; |
296 | txMsg.Length = 2; |
| 297 | txMsg.Data[0] = ((netConnected&0x1)<<7 | (frequency&0x3)<<5); |
297 | txMsg.Data[0] = ((netConnected&0x1)<<7 | (frequency&0x3)<<5); |
| 298 | txMsg.Data[1] = dimmerValue; |
298 | txMsg.Data[1] = dimmerValue; |
| - | 299 | //txMsg.Data[2] = 0; //Channel |
|
| 299 | StdCan_Put(&txMsg); |
300 | StdCan_Put(&txMsg); |
| 300 | } |
301 | } |
| 301 | 302 | ||
| 302 | /* If dimmervalue was changed over 5 seconds ago then store the new dimmervalue */ |
303 | /* If dimmervalue was changed over 5 seconds ago then store the new dimmervalue */ |
| 303 | if (Timer_Expired(act_dimmer230_STORE_VALUE_TIMEOUT)) |
304 | if (Timer_Expired(act_dimmer230_STORE_VALUE_TIMEOUT)) |