Subversion Repositories HomeAutomation

Rev

Rev 1257 | Rev 1611 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1257 Rev 1265
Line 4... Line 4...
4
 
4
 
-
 
5
#ifdef CHN_LOADED
-
 
6
#include "../chn_ChnMaster/chn_ChnMaster.h"
-
 
7
#endif
-
 
8
 
-
 
9
#ifdef CHN_LOADED
-
 
10
void act_dimmer230_chn_update( uint16_t channel_id, uint16_t value );
-
 
11
#endif
5
 
12
 
6
int8_t fadeSpeed = 0;
13
int8_t fadeSpeed = 0;
7
uint8_t fadeSpeedFrac = 0;
14
uint8_t fadeSpeedFrac = 0;
8
uint8_t fadeTarget = 0;
15
uint8_t fadeTarget = 0;
9
uint8_t fadeSpeedCnt = 0;
16
uint8_t fadeSpeedCnt = 0;
Line 15... Line 22...
15
/*  This bool is set to make process routine send the NetInfo packet */
22
/*  This bool is set to make process routine send the NetInfo packet */
16
uint8_t sendNetInfo = 0;
23
uint8_t sendNetInfo = 0;
17
 
24
 
18
/*  netConnected stores the connection status, if connected to the net or not */
25
/*  netConnected stores the connection status, if connected to the net or not */
19
uint8_t netConnected = CAN_MODULE_ENUM_DIMMER230_NETINFO_CONNECTION_DISCONNECTED;
26
uint8_t netConnected = CAN_MODULE_ENUM_DIMMER230_NETINFO_CONNECTION_DISCONNECTED;
20
 
27
 
21
/*  frequency stores the evaluated frequency, 50 or 60 Hz
28
/*  frequency stores the evaluated frequency, 50 or 60 Hz
22
    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
23
    should be used for switching powerStepTable depending on frequency */
30
    should be used for switching powerStepTable depending on frequency */
24
uint8_t frequency = CAN_MODULE_ENUM_DIMMER230_NETINFO_FREQUENCY_ERROR_UNKNOWN; 
31
uint8_t frequency = CAN_MODULE_ENUM_DIMMER230_NETINFO_FREQUENCY_ERROR_UNKNOWN; 
25
           
32
           
26
/*  zero cross sync (time between pos flank of xc-detection and real zero cross) */
33
/*  zero cross sync (time between pos flank of xc-detection and real zero cross) */
27
uint16_t xcTimeDiff=0;
34
uint16_t xcTimeDiff=0;
28
 
35
 
29
/*  dimmerValue is the current output
36
/*  dimmerValue is the current output
30
    value of 0 is zero output, value of 255 is max output (ACT_DIMMMER230_MIN_DIM, ACT_DIMMMER230_MAX_DIM)
37
    value of 0 is zero output, value of 255 is max output (ACT_DIMMMER230_MIN_DIM, ACT_DIMMMER230_MAX_DIM)
31
    stored in eeprom, and restored at startup */
38
    stored in eeprom, and restored at startup */
32
uint8_t dimmerValue = ACT_DIMMMER230_MIN_DIM;
39
uint8_t dimmerValue = ACT_DIMMMER230_MIN_DIM;
33
           
40
           
34
uint8_t state = ACT_DIMMMER230_STATE_IDLE;
41
uint8_t state = ACT_DIMMMER230_STATE_IDLE;
35
 
42
 
36
/*  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) */
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) */
37
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};
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};
38
 
45
 
39
#if act_dimmer230_SYNC>0
46
#if act_dimmer230_SYNC>0
40
uint8_t zeroCrossCnt = 0;
47
uint8_t zeroCrossCnt = 0;
41
#endif
48
#endif
42
 
49
 
43
 
50
 
44
/*  Set the variable to "disconnected" if no zerocross have arrived for a while */
51
/*  Set the variable to "disconnected" if no zerocross have arrived for a while */
45
void Net_Connection_callback(uint8_t timer)
52
void Net_Connection_callback(uint8_t timer)
46
{
53
{
47
    netConnected = CAN_MODULE_ENUM_DIMMER230_NETINFO_CONNECTION_DISCONNECTED;
54
    netConnected = CAN_MODULE_ENUM_DIMMER230_NETINFO_CONNECTION_DISCONNECTED;
48
}
55
}
Line 60... Line 67...
60
    uint8_t dummycnt = 40;
67
    uint8_t dummycnt = 40;
61
    while (dummycnt > 0)
68
    while (dummycnt > 0)
62
    {
69
    {
63
        dummycnt++;
70
        dummycnt++;
64
        asm("nop");
71
        asm("nop");
65
        asm("nop");
72
        asm("nop");
66
    }
73
    }
67
    gpio_clr_pin(act_dimmer230_CHAN1_IO);
74
    gpio_clr_pin(act_dimmer230_CHAN1_IO);
68
   
75
   
69
    TIMSK1=0;                           //disable timer interrupt
76
    TIMSK1=0;                           //disable timer interrupt
70
    state = ACT_DIMMMER230_STATE_IDLE;
77
    state = ACT_DIMMMER230_STATE_IDLE;
71
}
78
}
-
 
79
 
-
 
80
#ifdef CHN_LOADED
-
 
81
void act_dimmer230_chn_update( uint16_t channel_id, uint16_t value ) {
-
 
82
    fadeTarget = dimmerValue = value>>8;
-
 
83
}
-
 
84
#endif
72
 
85
 
73
/*  Interrupted at a pin change on zero-cross pin
86
/*  Interrupted at a pin change on zero-cross pin
74
    The zero-cross pin will go high just before the real zero-cross and
87
    The zero-cross pin will go high just before the real zero-cross and
75
    low just after the real zero-cross. The time difference is measured
88
    low just after the real zero-cross. The time difference is measured
76
    and adjusted for. */
89
    and adjusted for. */
Line 83... Line 96...
83
    {
96
    {
84
        xcTimeDiff = TCNT1;
97
        xcTimeDiff = TCNT1;
85
        xcTimeDiff = xcTimeDiff >> 1; //divide by two
98
        xcTimeDiff = xcTimeDiff >> 1; //divide by two
86
        if (xcTimeDiff > 600) {
99
        if (xcTimeDiff > 600) {
87
            xcTimeDiff = 600;
100
            xcTimeDiff = 600;
88
        }
101
        }
89
    }  
102
    }  
90
    /* only execute if zerocross pin is high (before real zerocross) */
103
    /* only execute if zerocross pin is high (before real zerocross) */
91
    else
104
    else
92
    {
105
    {
93
        /* Reset the timer for checking net connection. If no zero-cross has come in 500ms then consider us disconnected */
106
        /* Reset the timer for checking net connection. If no zero-cross has come in 500ms then consider us disconnected */
94
        Timer_SetTimeout(act_dimmer230_NET_CONNECT_TIMEOUT, 500, TimerTypeOneShot, &Net_Connection_callback);
107
        Timer_SetTimeout(act_dimmer230_NET_CONNECT_TIMEOUT, 500, TimerTypeOneShot, &Net_Connection_callback);
95
        /* Currently we are connected */
108
        /* Currently we are connected */
96
        netConnected = CAN_MODULE_ENUM_DIMMER230_NETINFO_CONNECTION_CONNECTED;
109
        netConnected = CAN_MODULE_ENUM_DIMMER230_NETINFO_CONNECTION_CONNECTED;
97
       
110
       
98
        /* Check frequency by measure the time since last zero-cross */
111
        /* Check frequency by measure the time since last zero-cross */
99
        uint16_t periodTime = TCNT1;
112
        uint16_t periodTime = TCNT1;
100
        if (periodTime > ACT_DIMMMER230_50HZ_PERIOD_TIME-ACT_DIMMMER230_50HZ_PERIOD_TIME/10 && periodTime < ACT_DIMMMER230_50HZ_PERIOD_TIME+ACT_DIMMMER230_50HZ_PERIOD_TIME/10)
113
        if (periodTime > ACT_DIMMMER230_50HZ_PERIOD_TIME-ACT_DIMMMER230_50HZ_PERIOD_TIME/10 && periodTime < ACT_DIMMMER230_50HZ_PERIOD_TIME+ACT_DIMMMER230_50HZ_PERIOD_TIME/10)
101
        {
114
        {
102
            frequency = CAN_MODULE_ENUM_DIMMER230_NETINFO_FREQUENCY_50HZ;
115
            frequency = CAN_MODULE_ENUM_DIMMER230_NETINFO_FREQUENCY_50HZ;
103
        }
116
        }
104
        else if (periodTime > ACT_DIMMMER230_60HZ_PERIOD_TIME-ACT_DIMMMER230_60HZ_PERIOD_TIME/10 && periodTime < ACT_DIMMMER230_60HZ_PERIOD_TIME+ACT_DIMMMER230_60HZ_PERIOD_TIME/10)
117
        else if (periodTime > ACT_DIMMMER230_60HZ_PERIOD_TIME-ACT_DIMMMER230_60HZ_PERIOD_TIME/10 && periodTime < ACT_DIMMMER230_60HZ_PERIOD_TIME+ACT_DIMMMER230_60HZ_PERIOD_TIME/10)
105
        {
118
        {
106
            frequency = CAN_MODULE_ENUM_DIMMER230_NETINFO_FREQUENCY_60HZ;
119
            frequency = CAN_MODULE_ENUM_DIMMER230_NETINFO_FREQUENCY_60HZ;
107
        }
120
        }
108
        else
121
        else
109
        {
122
        {
110
            frequency = CAN_MODULE_ENUM_DIMMER230_NETINFO_FREQUENCY_ERROR_UNKNOWN;
123
            frequency = CAN_MODULE_ENUM_DIMMER230_NETINFO_FREQUENCY_ERROR_UNKNOWN;
Line 122... Line 135...
122
                fadeSpeed = -fadeSpeed;
135
                fadeSpeed = -fadeSpeed;
123
            break;
136
            break;
124
            case ACT_DIMMMER230_DEMO_STATE_INCREASE:
137
            case ACT_DIMMMER230_DEMO_STATE_INCREASE:
125
                demoState = ACT_DIMMMER230_DEMO_STATE_GOBACK;
138
                demoState = ACT_DIMMMER230_DEMO_STATE_GOBACK;
126
                fadeTarget = demoEndValue;
139
                fadeTarget = demoEndValue;
127
                fadeSpeed = -fadeSpeed;
140
                fadeSpeed = -fadeSpeed;
128
            break;
141
            break;
129
            case ACT_DIMMMER230_DEMO_STATE_GOBACK:
142
            case ACT_DIMMMER230_DEMO_STATE_GOBACK:
130
                demoState = ACT_DIMMMER230_DEMO_STATE_NOT_RUNNING;
143
                demoState = ACT_DIMMMER230_DEMO_STATE_NOT_RUNNING;
131
            break;
144
            break;
132
            }
145
            }
133
        }
146
        }
134
       
147
       
135
        /* Calculate new time to triac triggering */
148
        /* Calculate new time to triac triggering */
136
        uint16_t newTimerVal = xcTimeDiff;      /* add the zerocross timediff */
149
        uint16_t newTimerVal = xcTimeDiff;      /* add the zerocross timediff */
137
 
150
 
138
        if (dimmerValue > 0 && dimmerValue <= 127)
151
        if (dimmerValue > 0 && dimmerValue <= 127)
139
        {
152
        {
Line 144... Line 157...
144
            newTimerVal += pgm_read_word(&powerStepTable[254-dimmerValue]);
157
            newTimerVal += pgm_read_word(&powerStepTable[254-dimmerValue]);
145
        }
158
        }
146
        else if (dimmerValue == 255)
159
        else if (dimmerValue == 255)
147
        {
160
        {
148
            newTimerVal += 100; /* almost max, to make sure we don't fire before zerocross */
161
            newTimerVal += 100; /* almost max, to make sure we don't fire before zerocross */
149
        }
162
        }
150
       
163
       
151
        if (newTimerVal > ACT_DIMMMER230_50HZ_PERIOD_TIME)
164
        if (newTimerVal > ACT_DIMMMER230_50HZ_PERIOD_TIME)
152
        {
165
        {
153
            newTimerVal -= ACT_DIMMMER230_50HZ_PERIOD_TIME;
166
            newTimerVal -= ACT_DIMMMER230_50HZ_PERIOD_TIME;
154
        }
167
        }
Line 174... Line 187...
174
            dimmerValue += fadeSpeed;
187
            dimmerValue += fadeSpeed;
175
            if ((fadeSpeed > 0 && (dimmerValue < tempDimVal || dimmerValue >= fadeTarget)) ||
188
            if ((fadeSpeed > 0 && (dimmerValue < tempDimVal || dimmerValue >= fadeTarget)) ||
176
                (fadeSpeed < 0 && (dimmerValue > tempDimVal || dimmerValue <= fadeTarget)))
189
                (fadeSpeed < 0 && (dimmerValue > tempDimVal || dimmerValue <= fadeTarget)))
177
            {
190
            {
178
                dimmerValue = fadeTarget;
191
                dimmerValue = fadeTarget;
179
            }
192
            }
180
           
193
           
181
            /* if targetvalue was reached then send the netinfo packet */
194
            /* if targetvalue was reached then send the netinfo packet */
182
            if (dimmerValue == fadeTarget)
195
            if (dimmerValue == fadeTarget)
183
            {
196
            {
184
                sendNetInfo = 1;
197
                sendNetInfo = 1;
185
            }
198
            }
186
            /* 5 seconds after we have stopped fading we should store the current dimmerValue to eeprom */
199
            /* 5 seconds after we have stopped fading we should store the current dimmerValue to eeprom */
187
            Timer_SetTimeout(act_dimmer230_STORE_VALUE_TIMEOUT, 5000, TimerTypeOneShot, 0);
200
            Timer_SetTimeout(act_dimmer230_STORE_VALUE_TIMEOUT, 5000, TimerTypeOneShot, 0);
188
        }
201
        }
189
       
202
       
190
#if act_dimmer230_SYNC>0
203
#if act_dimmer230_SYNC>0
191
        /* Currently disabled, must add this packet to data.xml */
204
        /* Currently disabled, must add this packet to data.xml */
192
        zeroCrossCnt++;
205
        zeroCrossCnt++;
193
        if (zeroCrossCnt >= act_dimmer230_SYNC)
206
        if (zeroCrossCnt >= act_dimmer230_SYNC)
194
        {
207
        {
Line 249... Line 262...
249
   
262
   
250
    TIMSK1=0;                           /* disable timer interrupt */
263
    TIMSK1=0;                           /* disable timer interrupt */
251
    TCCR1A=0;
264
    TCCR1A=0;
252
   
265
   
253
    TCCR1B=(1<<CS11);                   /* enable timer, set to prescaler 8, must be changed if cpu freq is changed */
266
    TCCR1B=(1<<CS11);                   /* enable timer, set to prescaler 8, must be changed if cpu freq is changed */
-
 
267
 
-
 
268
#ifdef CHN_LOADED
-
 
269
    chn_ChnMaster_RegisterListener( act_dimmer230_CHN_CHANNEL, act_dimmer230_chn_update );
-
 
270
#endif
254
 
271
 
255
    /* setup interrupt on zerocross, pcint */
272
    /* setup interrupt on zerocross, pcint */
256
    //act_dimmer230_ZC_PCMSK=(1<<(act_dimmer230_ZC_PCINT_BIT));
273
    //act_dimmer230_ZC_PCMSK=(1<<(act_dimmer230_ZC_PCINT_BIT));
257
    //PCIFR=(1<<act_dimmer230_ZC_PCIF); /* clear any pending interrupt before enabling interrupts */
274
    //PCIFR=(1<<act_dimmer230_ZC_PCIF); /* clear any pending interrupt before enabling interrupts */
258
    //PCICR=(1<<act_dimmer230_ZC_PCIE); /* enable interrupt for PCINT */
275
    //PCICR=(1<<act_dimmer230_ZC_PCIE); /* enable interrupt for PCINT */
Line 360... Line 377...
360
                fadeSpeed = 0;
377
                fadeSpeed = 0;
361
                uint8_t endValue = 0;
378
                uint8_t endValue = 0;
362
                if (direction == CAN_MODULE_ENUM_DIMMER230_START_FADE_DIRECTION_INCREASE) {
379
                if (direction == CAN_MODULE_ENUM_DIMMER230_START_FADE_DIRECTION_INCREASE) {
363
                    endValue = ACT_DIMMMER230_MAX_DIM;
380
                    endValue = ACT_DIMMMER230_MAX_DIM;
364
                } else if (direction == CAN_MODULE_ENUM_DIMMER230_START_FADE_DIRECTION_DECREASE) {
381
                } else if (direction == CAN_MODULE_ENUM_DIMMER230_START_FADE_DIRECTION_DECREASE) {
365
                    endValue = ACT_DIMMMER230_MIN_DIM;
382
                    endValue = ACT_DIMMMER230_MIN_DIM;
366
                }
383
                }
367
                   
384
                   
368
                if (speed == 0) {
385
                if (speed == 0) {
369
                    dimmerValue = endValue; /* set dimmer value immediately */
386
                    dimmerValue = endValue; /* set dimmer value immediately */
370
                    sendNetInfo = 1;        /* send netinfo with the current dimmervalue*/
387
                    sendNetInfo = 1;        /* send netinfo with the current dimmervalue*/
371
                } else {
388
                } else {
372
                    fadeTarget = endValue;
389
                    fadeTarget = endValue;
373
                    if (fadeTarget != dimmerValue) {
390
                    if (fadeTarget != dimmerValue) {
374
                        if ((speed&0x80) == 0x80) {
391
                        if ((speed&0x80) == 0x80) {
375
                            fadeSpeed = (speed&0x7f)+1;
392
                            fadeSpeed = (speed&0x7f)+1;
376
                            fadeSpeedFrac = 1;
393
                            fadeSpeedFrac = 1;
377
                        } else {
394
                        } else {
378
                            fadeSpeed = 1;
395
                            fadeSpeed = 1;
Line 492... Line 509...
492
    txMsg.Data[4] = NUMBER_OF_MODULES;
509
    txMsg.Data[4] = NUMBER_OF_MODULES;
493
    txMsg.Data[5] = ModuleSequenceNumber;
510
    txMsg.Data[5] = ModuleSequenceNumber;
494
   
511
   
495
    while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
512
    while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
496
}
513
}
-
 
514