Subversion Repositories HomeAutomation

Rev

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

Rev 895 Rev 896
Line 2... Line 2...
2
#include "act_dimmer230.h"
2
#include "act_dimmer230.h"
3
 
3
 
-
 
4
uint8_t demo = 0;
-
 
5
//uint8_t demo = 2; //disable demo
-
 
6
 
-
 
7
//uint16_t periodTime = 0;
-
 
8
            //measured during init (by taking the time difference between two zero cross)
-
 
9
            //not used, bad idea, the powerStepTable below asumes 10000
-
 
10
uint16_t xcTimeDiff=0;
-
 
11
            //zero cross sync (difference between pos and neg flank of zero cross, divided by 2)
-
 
12
uint8_t dimmerValueCh1 = 0;
4
uint8_t dimmerValue = 0;    //store in eeprom later
13
            //store in eeprom later
5
                            //value of 0 is zero output, value of 255 is max output
14
            //value of 0 is zero output, value of 255 is max output
6
uint8_t state = ACT_DIMMMER230_STATE_IDLE;
15
uint8_t state = ACT_DIMMMER230_STATE_IDLE;
7
uint16_t timerStore = 0;    //used if triac should be trigged after zerocross detected but before real zerocross event
-
 
8
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};
16
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};
9
 
17
 
10
#if act_dimmer230_SYNC>0
18
#if act_dimmer230_SYNC>0
11
uint8_t zeroCrossCnt = 0;
19
uint8_t zeroCrossCnt = 0;
12
#endif
20
#endif
13
 
21
 
14
ISR (TIMER1_COMPA_vect)
22
ISR (TIMER1_COMPA_vect)
15
{
23
{
16
    //TODO: testa hur lång denna loop behöver vara
24
    //testa hur lång denna loop behöver vara
-
 
25
    //verkar fungera upp till 249-250 sen blir det för korta pulser
17
    PORTC |= _BV(PC4);
26
    PORTC |= _BV(PC4);
18
    uint8_t dummycnt = 1;
27
    uint8_t dummycnt = 240;
19
    while (dummycnt > 0)
28
    while (dummycnt > 0)
20
    {
29
    {
21
        dummycnt++;
30
        dummycnt++;
22
        asm("nop");
31
        asm("nop");
23
        asm("nop");
-
 
24
    }
32
    }
25
    PORTC &= ~_BV(PC4);        
33
    PORTC &= ~_BV(PC4);        
26
   
34
   
27
    if (timerStore == 0)
-
 
28
    {
-
 
29
        TIMSK1=0;                           //disable timer interrupt
35
    TIMSK1=0;                           //disable timer interrupt
30
        state = ACT_DIMMMER230_STATE_IDLE;
36
    state = ACT_DIMMMER230_STATE_IDLE;
31
    }
37
}
32
    else
-
 
33
    {
-
 
34
        TCNT1 = 0;                          //reset timer1 count register
-
 
35
        OCR1A = timerStore;
-
 
36
        timerStore = 0;
-
 
37
    }
-
 
38
}
-
 
39
 
38
 
40
ISR (act_dimmer230_ZC_PCINT_vect)
39
ISR (act_dimmer230_ZC_PCINT_vect)
41
{
40
{
-
 
41
    //only execute if zerocross pin is low
42
    if (act_dimmer230_ZC_PIN&(1<<act_dimmer230_ZC_BIT))
42
    if (!(act_dimmer230_ZC_PIN&(1<<act_dimmer230_ZC_BIT)))
43
    {
43
    {
44
        //OBS glöm inte timeDiff
-
 
45
        uint16_t newTimerVal = 0; //timeDiff
44
        uint16_t newTimerVal = xcTimeDiff;
46
 
45
 
47
        //here new timer value is calculated 
46
        //here new timer value is calculated 
48
 
47
 
49
        if (dimmerValue > 0 && dimmerValue < 127)
48
        if (dimmerValueCh1 > 0 && dimmerValueCh1 <= 127)
50
        {
49
        {
51
            newTimerVal += ACT_DIMMMER230_PERIOD_TIME - pgm_read_word(&powerStepTable[dimmerValue-1]);
50
            newTimerVal += ACT_DIMMMER230_PERIOD_TIME - pgm_read_word(&powerStepTable[dimmerValueCh1-1]);
52
        }
51
        }
53
        else if (dimmerValue < 255)
52
        else if (dimmerValueCh1 < 255)
54
        {
53
        {
55
            newTimerVal += pgm_read_word(&powerStepTable[254-dimmerValue]);
54
            newTimerVal += pgm_read_word(&powerStepTable[254-dimmerValueCh1]);
56
        }
55
        }
57
        else if (dimmerValue == 255)
56
        else if (dimmerValueCh1 == 255)
58
        {
57
        {
59
            newTimerVal += 100; //almost max, to make sure we don't fire before zerocross
58
            newTimerVal += 100; //almost max, to make sure we don't fire before zerocross
60
        }
59
        }
61
       
60
       
62
        if (newTimerVal > ACT_DIMMMER230_PERIOD_TIME)
61
        if (newTimerVal > ACT_DIMMMER230_PERIOD_TIME)
63
        {
62
        {
64
            newTimerVal -= ACT_DIMMMER230_PERIOD_TIME;
63
            newTimerVal -= ACT_DIMMMER230_PERIOD_TIME;
65
        }
64
        }
66
 
65
 
67
        if (dimmerValue > 0)
66
        if (dimmerValueCh1 > 0)
68
        {
67
        {
69
            TCNT1 = 0;              //reset timer1 count register
68
            TCNT1 = 0;              //reset timer1 count register
70
            OCR1A = newTimerVal;
69
            OCR1A = newTimerVal;
71
 
70
 
72
            //enable timer and timer interrupt
71
            //enable timer and timer interrupt
73
            TIFR1=(1<<OCF1A);       //clear interrupt flag before enabling interrupt
72
            TIFR1=(1<<OCF1A);       //clear interrupt flag before enabling interrupt
74
            TIMSK1|=(1<<OCIE1A);
73
            TIMSK1|=(1<<OCIE1A);
75
 
74
 
76
            state = ACT_DIMMMER230_STATE_TIMER_ON;
75
            state = ACT_DIMMMER230_STATE_TIMER_ON;
77
        }
76
        }
78
 
77
 
79
#if act_dimmer230_SYNC>0
78
#if act_dimmer230_SYNC>0
80
        zeroCrossCnt++;
79
        zeroCrossCnt++;
81
        if (zeroCrossCnt >= act_dimmer230_SYNC)
80
        if (zeroCrossCnt >= act_dimmer230_SYNC)
82
        {
81
        {
83
            zeroCrossCnt = 0;
82
            zeroCrossCnt = 0;
84
            //TODO: send on can
83
            //TODO: send on can
-
 
84
 
-
 
85
            StdCan_Msg_t txMsg;
-
 
86
            txMsg.Length = 6;
-
 
87
            txMsg.Data[0] = (newTimerVal>>8)&0xff;
-
 
88
            txMsg.Data[1] = newTimerVal&0xff;
-
 
89
            //txMsg.Data[2] = (periodTime>>8)&0xff;
-
 
90
            //txMsg.Data[3] = periodTime&0xff;
-
 
91
            txMsg.Data[4] = (xcTimeDiff>>8)&0xff;
-
 
92
            txMsg.Data[5] = xcTimeDiff&0xff;
-
 
93
            //StdCan_Put(&txMsg);
-
 
94
 
-
 
95
            if (demo == 0) {
-
 
96
                dimmerValueCh1++;
-
 
97
                if (dimmerValueCh1 == 255) {
-
 
98
                    demo = 1;
-
 
99
                }
-
 
100
            } else if (demo == 1) {
-
 
101
                dimmerValueCh1--;
-
 
102
                if (dimmerValueCh1 == 0) {
-
 
103
                    demo = 0;
-
 
104
                }
-
 
105
            }
85
        }
106
        }
86
#endif      
107
#endif      
87
    }
108
    }
88
}
109
}
89
 
110
 
90
void act_dimmer230_Init(void)
111
void act_dimmer230_Init(void)
91
{
112
{
92
    // set dimmer channel1 port to output 0
113
    // set dimmer channel1 port to output 0
93
    act_dimmer230_CHAN1_PORT &= ~_BV(act_dimmer230_CHAN1_BIT);
114
    act_dimmer230_CHAN1_PORT &= ~_BV(act_dimmer230_CHAN1_BIT);
Line 107... Line 128...
107
    uint16_t timeAtFall = TCNT1;    //Timer_GetTicks();
128
    uint16_t timeAtFall = TCNT1;    //Timer_GetTicks();
108
    //wait for high (ac leaving zero cross)
129
    //wait for high (ac leaving zero cross)
109
    while (!(act_dimmer230_ZC_PIN&(1<<act_dimmer230_ZC_BIT))) { }
130
    while (!(act_dimmer230_ZC_PIN&(1<<act_dimmer230_ZC_BIT))) { }
110
    uint16_t timeAtRise = TCNT1;    //Timer_GetTicks();
131
    uint16_t timeAtRise = TCNT1;    //Timer_GetTicks();
111
   
132
 
-
 
133
    //wait for low (ac approaching zero cross)
-
 
134
    //while (act_dimmer230_ZC_PIN&(1<<act_dimmer230_ZC_BIT)) { }
-
 
135
    //wait for high (ac leaving zero cross)
-
 
136
    //while (!(act_dimmer230_ZC_PIN&(1<<act_dimmer230_ZC_BIT))) { }
-
 
137
    //uint16_t timeAtRise2 = TCNT1; //Timer_GetTicks();
112
    uint16_t timeDiff=0;
138
    //if (timeAtRise < timeAtRise2) {
-
 
139
    //  periodTime = timeAtRise2 - timeAtRise;
-
 
140
    //}
-
 
141
 
113
    // warning, watch out for wrap around on timestamps (something is broken if wrapped)
142
    // warning, watch out for wrap around on timestamps (something is broken if wrapped)
114
    if (timeAtFall < timeAtRise) {
143
    if (timeAtFall < timeAtRise) {
115
        //calculate time from falling edge to ac zero cross value
144
        //calculate time from falling edge to ac zero cross value
116
        timeDiff = timeAtRise-timeAtFall;
145
        xcTimeDiff = timeAtRise-timeAtFall;
117
        timeDiff = timeDiff >> 1; //divide by two
146
        xcTimeDiff = xcTimeDiff >> 1; //divide by two
118
    }
147
    }
119
 
148
 
120
    //setup interrupt on zerocross, pcint
149
    //setup interrupt on zerocross, pcint
121
    PCMSK0=(1<<act_dimmer230_ZC_PCINT_BIT);
150
    PCMSK0=(1<<act_dimmer230_ZC_PCINT_BIT);
122
    PCIFR=(1<<act_dimmer230_ZC_PCIF);   //clear any pending interrupt before enabling interrupts
151
    PCIFR=(1<<act_dimmer230_ZC_PCIF);   //clear any pending interrupt before enabling interrupts