Rev 893 | Rev 895 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 893 | Rev 894 | ||
|---|---|---|---|
| Line 1... | Line 1... | ||
| 1 | 1 | ||
| 2 | #include "act_dimmer230.h" |
2 | #include "act_dimmer230.h" |
| - | 3 | ||
| - | 4 | uint8_t dimmerValue = 0; //store in eeprom later |
|
| - | 5 | //value of 0 is zero output, value of 255 is max output |
|
| 3 | uint8_t state = ACT_DIMMMER230_STATE_IDLE; |
6 | 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}; |
|
| - | 9 | ||
| - | 10 | #if act_dimmer230_SYNC>0 |
|
| - | 11 | uint8_t zeroCrossCnt = 0; |
|
| - | 12 | #endif |
|
| 4 | 13 | ||
| 5 | ISR (TIMER1_COMPA_vect) |
14 | ISR (TIMER1_COMPA_vect) |
| 6 | { |
15 | { |
| - | 16 | //TODO: testa hur lång denna loop behöver vara |
|
| - | 17 | PORTC |= _BV(PC4); |
|
| - | 18 | uint8_t dummycnt = 1; |
|
| - | 19 | while (dummycnt > 0) |
|
| - | 20 | { |
|
| - | 21 | dummycnt++; |
|
| - | 22 | asm("nop"); |
|
| - | 23 | asm("nop"); |
|
| - | 24 | } |
|
| - | 25 | PORTC &= ~_BV(PC4); |
|
| - | 26 | ||
| - | 27 | if (timerStore == 0) |
|
| - | 28 | { |
|
| - | 29 | TIMSK1=0; //disable timer interrupt |
|
| - | 30 | state = ACT_DIMMMER230_STATE_IDLE; |
|
| - | 31 | } |
|
| - | 32 | else |
|
| - | 33 | { |
|
| - | 34 | TCNT1 = 0; //reset timer1 count register |
|
| - | 35 | OCR1A = timerStore; |
|
| - | 36 | timerStore = 0; |
|
| - | 37 | } |
|
| - | 38 | } |
|
| 7 | 39 | ||
| 8 | } |
- | |
| 9 | ISR (act_dimmer230_ZC_PCINT_vect) |
40 | ISR (act_dimmer230_ZC_PCINT_vect) |
| 10 | { |
41 | { |
| 11 | if ( |
42 | if (act_dimmer230_ZC_PIN&(1<<act_dimmer230_ZC_BIT)) |
| 12 | { |
43 | { |
| - | 44 | //OBS glöm inte timeDiff |
|
| - | 45 | uint16_t newTimerVal = 0; //timeDiff |
|
| - | 46 | ||
| - | 47 | //here new timer value is calculated |
|
| - | 48 | ||
| - | 49 | if (dimmerValue > 0 && dimmerValue < 127) |
|
| 13 | 50 | { |
|
| - | 51 | newTimerVal += pgm_read_word(&powerStepTable[dimmerValue-1]); |
|
| - | 52 | } |
|
| 14 |
|
53 | else if (dimmerValue < 255) |
| - | 54 | { |
|
| - | 55 | newTimerVal += 10000 - pgm_read_word(&powerStepTable[254-dimmerValue]); |
|
| - | 56 | } |
|
| - | 57 | else if (dimmerValue == 255) |
|
| - | 58 | { |
|
| - | 59 | newTimerVal += 9900; //almost max, to make sure we don't fire after next zerocross |
|
| - | 60 | } |
|
| - | 61 | ||
| - | 62 | if (state==ACT_DIMMMER230_STATE_TIMER_ON) |
|
| - | 63 | { |
|
| - | 64 | //TODO: this will probably end up out of sync, not a good solution? |
|
| - | 65 | timerStore = newTimerVal + TCNT1 - OCR1A; |
|
| - | 66 | } |
|
| - | 67 | else |
|
| - | 68 | { |
|
| 15 | TCNT1 = 0; //reset timer1 count register |
69 | TCNT1 = 0; //reset timer1 count register |
| - | 70 | OCR1A = newTimerVal; |
|
| - | 71 | ||
| 16 | / |
72 | //enable timer and timer interrupt |
| 17 | TIFR1=(1<<OCF1A); //clear interrupt flag before enabling interrupt |
73 | TIFR1=(1<<OCF1A); //clear interrupt flag before enabling interrupt |
| 18 | TIMSK1|=(1<<OCIE1A); |
74 | TIMSK1|=(1<<OCIE1A); |
| 19 | 75 | ||
| 20 | state = ACT_DIMMMER230_STATE_TIMER_ON; |
76 | state = ACT_DIMMMER230_STATE_TIMER_ON; |
| - | 77 | } |
|
| - | 78 | ||
| - | 79 | #if act_dimmer230_SYNC>0 |
|
| - | 80 | zeroCrossCnt++; |
|
| - | 81 | if (zeroCrossCnt >= act_dimmer230_SYNC) |
|
| - | 82 | { |
|
| - | 83 | zeroCrossCnt = 0; |
|
| - | 84 | //TODO: send on can |
|
| - | 85 | } |
|
| - | 86 | #endif |
|
| 21 | } |
87 | } |
| 22 | } |
88 | } |
| 23 | 89 | ||
| 24 | void act_dimmer230_Init(void) |
90 | void act_dimmer230_Init(void) |
| 25 | { |
91 | { |
| 26 | ///TODO: Initialize hardware etc here |
- | |
| 27 | - | ||
| 28 | // set dimmer channel1 to output 0 |
92 | // set dimmer channel1 port to output 0 |
| 29 | act_dimmer230_CHAN1_PORT &= ~_BV(act_dimmer230_CHAN1_BIT); |
93 | act_dimmer230_CHAN1_PORT &= ~_BV(act_dimmer230_CHAN1_BIT); |
| 30 | act_dimmer230_CHAN1_DDR |= _BV(act_dimmer230_CHAN1_BIT); |
94 | act_dimmer230_CHAN1_DDR |= _BV(act_dimmer230_CHAN1_BIT); |
| 31 | // set zero cross detection to input, no pullup |
95 | // set zero cross detection port to input, no pullup |
| 32 | act_dimmer230_ZC_PORT &= ~_BV(act_dimmer230_ZC_BIT); |
96 | act_dimmer230_ZC_PORT &= ~_BV(act_dimmer230_ZC_BIT); |
| 33 | act_dimmer230_ZC_DDR &= ~_BV(act_dimmer230_ZC_BIT); |
97 | act_dimmer230_ZC_DDR &= ~_BV(act_dimmer230_ZC_BIT); |
| 34 | - | ||
| 35 | 98 | ||
| 36 | TIMSK1=0; //disable timer interrupt |
99 | TIMSK1=0; //disable timer interrupt |
| 37 | TCCR1A=0; |
100 | TCCR1A=0; |
| 38 | TCCR1B=(1<<CS11); //enable timer, set to prescaler 8, must be changed if clk is changed |
- | |
| 39 | 101 | ||
| 40 | //wait for high (ac leaving zero cross) |
102 | //wait for high (ac leaving zero cross) |
| 41 | while (!(act_dimmer230_ZC_PIN&(1<<act_dimmer230_ZC_BIT))) { } |
103 | while (!(act_dimmer230_ZC_PIN&(1<<act_dimmer230_ZC_BIT))) { } |
| - | 104 | TCCR1B=(1<<CS11); //enable timer, set to prescaler 8, must be changed if cpu freq is changed |
|
| 42 | //wait for low (ac approaching zero cross) |
105 | //wait for low (ac approaching zero cross) |
| 43 | while (act_dimmer230_ZC_PIN&(1<<act_dimmer230_ZC_BIT)) { } |
106 | while (act_dimmer230_ZC_PIN&(1<<act_dimmer230_ZC_BIT)) { } |
| 44 | uint16_t timeAtFall = TCNT1; //Timer_GetTicks(); |
107 | uint16_t timeAtFall = TCNT1; //Timer_GetTicks(); |
| 45 | //wait for high (ac leaving zero cross) |
108 | //wait for high (ac leaving zero cross) |
| 46 | while (!(act_dimmer230_ZC_PIN&(1<<act_dimmer230_ZC_BIT))) { } |
109 | while (!(act_dimmer230_ZC_PIN&(1<<act_dimmer230_ZC_BIT))) { } |
| 47 | uint16_t timeAtRise = TCNT1; //Timer_GetTicks(); |
110 | uint16_t timeAtRise = TCNT1; //Timer_GetTicks(); |
| - | 111 | ||
| - | 112 | uint16_t timeDiff=0; |
|
| - | 113 | // warning, watch out for wrap around on timestamps (something is broken if wrapped) |
|
| - | 114 | if (timeAtFall < timeAtRise) { |
|
| - | 115 | //calculate time from falling edge to ac zero cross value |
|
| - | 116 | timeDiff = timeAtRise-timeAtFall; |
|
| - | 117 | timeDiff = timeDiff >> 1; //divide by two |
|
| - | 118 | } |
|
| 48 | 119 | ||
| 49 | //setup interrupt on zerocross, pcint |
120 | //setup interrupt on zerocross, pcint |
| 50 | PCMSK0=(1<<act_dimmer230_ZC_PCINT_BIT); |
121 | PCMSK0=(1<<act_dimmer230_ZC_PCINT_BIT); |
| 51 | PCIFR=(1<<act_dimmer230_ZC_PCIF); //clear any pending interrupt before enabling interrupts |
122 | PCIFR=(1<<act_dimmer230_ZC_PCIF); //clear any pending interrupt before enabling interrupts |
| 52 | PCICR=(1<<act_dimmer230_ZC_PCIE); //enable interrupt for PCINT |
123 | PCICR=(1<<act_dimmer230_ZC_PCIE); //enable interrupt for PCINT |