Rev 1068 | Rev 1075 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1068 | Rev 1069 | ||
|---|---|---|---|
| Line 53... | Line 53... | ||
| 53 | state = ACT_DIMMMER230_STATE_IDLE; |
53 | state = ACT_DIMMMER230_STATE_IDLE; |
| 54 | } |
54 | } |
| 55 | 55 | ||
| 56 | ISR (act_dimmer230_ZC_PCINT_vect) |
56 | ISR (act_dimmer230_ZC_PCINT_vect) |
| 57 | { |
57 | { |
| 58 | //only execute if zerocross pin is |
58 | //only execute if zerocross pin is high |
| 59 | if ( |
59 | if ((act_dimmer230_ZC_PIN&(1<<act_dimmer230_ZC_BIT))) |
| 60 | { |
60 | { |
| - | 61 | PORTD ^= (1<<PD2); |
|
| 61 | uint16_t newTimerVal = xcTimeDiff; |
62 | uint16_t newTimerVal = xcTimeDiff; |
| 62 | 63 | ||
| 63 | //here new timer value is calculated |
64 | //here new timer value is calculated |
| 64 | 65 | ||
| 65 | if (dimmerValueCh1 > 0 && dimmerValueCh1 <= 127) |
66 | if (dimmerValueCh1 > 0 && dimmerValueCh1 <= 127) |
| Line 160... | Line 161... | ||
| 160 | } |
161 | } |
| 161 | } |
162 | } |
| 162 | 163 | ||
| 163 | void act_dimmer230_Init(void) |
164 | void act_dimmer230_Init(void) |
| 164 | { |
165 | { |
| - | 166 | DDRD |= (1<<PD2); |
|
| - | 167 | PORTD &= ~(1<<PD2); |
|
| - | 168 | ||
| 165 | // set dimmer channel1 port to output 0 |
169 | // set dimmer channel1 port to output 0 |
| 166 | act_dimmer230_CHAN1_PORT &= ~_BV(act_dimmer230_CHAN1_BIT); |
170 | act_dimmer230_CHAN1_PORT &= ~_BV(act_dimmer230_CHAN1_BIT); |
| 167 | act_dimmer230_CHAN1_DDR |= _BV(act_dimmer230_CHAN1_BIT); |
171 | act_dimmer230_CHAN1_DDR |= _BV(act_dimmer230_CHAN1_BIT); |
| 168 | // set zero cross detection port to input, no pullup |
172 | // set zero cross detection port to input, no pullup |
| 169 | act_dimmer230_ZC_PORT &= ~_BV(act_dimmer230_ZC_BIT); |
173 | act_dimmer230_ZC_PORT &= ~_BV(act_dimmer230_ZC_BIT); |
| 170 | act_dimmer230_ZC_DDR &= ~_BV(act_dimmer230_ZC_BIT); |
174 | act_dimmer230_ZC_DDR &= ~_BV(act_dimmer230_ZC_BIT); |
| 171 | 175 | ||
| 172 | TIMSK1=0; //disable timer interrupt |
176 | TIMSK1=0; //disable timer interrupt |
| 173 | TCCR1A=0; |
177 | TCCR1A=0; |
| 174 | 178 | ||
| 175 | //wait for |
179 | //wait for low (ac leaving zero cross) |
| 176 | while ( |
180 | while (act_dimmer230_ZC_PIN&(1<<act_dimmer230_ZC_BIT)) { } |
| 177 | TCCR1B=(1<<CS11); //enable timer, set to prescaler 8, must be changed if cpu freq is changed |
181 | TCCR1B=(1<<CS11); //enable timer, set to prescaler 8, must be changed if cpu freq is changed |
| 178 | //wait for |
182 | //wait for high (ac approaching zero cross) |
| 179 | while (act_dimmer230_ZC_PIN&(1<<act_dimmer230_ZC_BIT)) { } |
- | |
| 180 | uint16_t timeAtFall = TCNT1; //Timer_GetTicks(); |
- | |
| 181 | //wait for high (ac leaving zero cross) |
- | |
| 182 | while (!(act_dimmer230_ZC_PIN&(1<<act_dimmer230_ZC_BIT))) { } |
183 | while (!(act_dimmer230_ZC_PIN&(1<<act_dimmer230_ZC_BIT))) { } |
| - | 184 | uint16_t timeAtFall = TCNT1; //Timer_GetTicks(); |
|
| - | 185 | //wait for low (ac leaving zero cross) |
|
| - | 186 | while (act_dimmer230_ZC_PIN&(1<<act_dimmer230_ZC_BIT)) { } |
|
| 183 | uint16_t timeAtRise = TCNT1; //Timer_GetTicks(); |
187 | uint16_t timeAtRise = TCNT1; //Timer_GetTicks(); |
| 184 | 188 | ||
| 185 | #ifdef MEASURE_PERIOD |
189 | #ifdef MEASURE_PERIOD |
| 186 | //wait for |
190 | //wait for high (ac approaching zero cross) |
| 187 | while (act_dimmer230_ZC_PIN&(1<<act_dimmer230_ZC_BIT)) { } |
- | |
| 188 | //wait for high (ac leaving zero cross) |
- | |
| 189 | while (!(act_dimmer230_ZC_PIN&(1<<act_dimmer230_ZC_BIT))) { } |
191 | while (!(act_dimmer230_ZC_PIN&(1<<act_dimmer230_ZC_BIT))) { } |
| - | 192 | //wait for low (ac leaving zero cross) |
|
| - | 193 | while (act_dimmer230_ZC_PIN&(1<<act_dimmer230_ZC_BIT)) { } |
|
| 190 | uint16_t timeAtRise2 = TCNT1; //Timer_GetTicks(); |
194 | uint16_t timeAtRise2 = TCNT1; //Timer_GetTicks(); |
| 191 | if (timeAtRise < timeAtRise2) { |
195 | if (timeAtRise < timeAtRise2) { |
| 192 | periodTime = timeAtRise2 - timeAtRise; |
196 | periodTime = timeAtRise2 - timeAtRise; |
| 193 | } |
197 | } |
| 194 | #endif |
198 | #endif |
| Line 196... | Line 200... | ||
| 196 | // warning, watch out for wrap around on timestamps (something is broken if wrapped) |
200 | // warning, watch out for wrap around on timestamps (something is broken if wrapped) |
| 197 | if (timeAtFall < timeAtRise) { |
201 | if (timeAtFall < timeAtRise) { |
| 198 | //calculate time from falling edge to ac zero cross value |
202 | //calculate time from falling edge to ac zero cross value |
| 199 | xcTimeDiff = timeAtRise-timeAtFall; |
203 | xcTimeDiff = timeAtRise-timeAtFall; |
| 200 | xcTimeDiff = xcTimeDiff >> 1; //divide by two |
204 | xcTimeDiff = xcTimeDiff >> 1; //divide by two |
| 201 | } |
205 | } |
| 202 | 206 | ||
| 203 | //setup interrupt on zerocross, pcint |
207 | //setup interrupt on zerocross, pcint |
| 204 |
|
208 | act_dimmer230_ZC_PCMSK=(1<<act_dimmer230_ZC_PCINT_BIT); |
| 205 | PCIFR=(1<<act_dimmer230_ZC_PCIF); //clear any pending interrupt before enabling interrupts |
209 | PCIFR=(1<<act_dimmer230_ZC_PCIF); //clear any pending interrupt before enabling interrupts |
| 206 | PCICR=(1<<act_dimmer230_ZC_PCIE); //enable interrupt for PCINT |
210 | PCICR=(1<<act_dimmer230_ZC_PCIE); //enable interrupt for PCINT |
| - | 211 | ||
| - | 212 | //PORTD ^= (1<<PD2); |
|
| 207 | 213 | ||
| 208 | 214 | ||
| 209 | //printf("Hello world!\n"); |
215 | //printf("Hello world!\n"); |
| 210 | } |
216 | } |
| 211 | 217 | ||
| 212 | void act_dimmer230_Process(void) |
218 | void act_dimmer230_Process(void) |
| 213 | { |
219 | { |
| 214 | ///TODO: Stuff that needs doing is done here |
220 | ///TODO: Stuff that needs doing is done here |
| - | 221 | /*if (PIND & (1<<PD7)) { |
|
| - | 222 | PORTD |= (1<<PD2); |
|
| - | 223 | } else { |
|
| - | 224 | PORTD &= ~(1<<PD2); |
|
| - | 225 | }*/ |
|
| - | 226 | ||
| 215 | } |
227 | } |
| 216 | 228 | ||
| 217 | void act_dimmer230_HandleMessage(StdCan_Msg_t *rxMsg) |
229 | void act_dimmer230_HandleMessage(StdCan_Msg_t *rxMsg) |
| 218 | { |
230 | { |
| 219 | if ( StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_ACT && |
231 | if ( StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_ACT && |