Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <htc.h>
- #define _XTAL_FREQ 8e6 // 8MHz
- #define ZERO_CROSS_PIN GP0
- #define ZERO_CROSS_PIN_TRIS TRISIO0
- #define OUTPUT_PIN GP1
- #define OUTPUT_PIN_TRIS TRISIO1
- /*
- _delay( 1 ); // 1 instruction cycle delay
- __delay_us( 2 ); // 2 us delay
- __delay_ms( 3 ); // 3 ms delay
- */
- unsigned char timerArmed = 0; // set if timer is waiting to go off
- unsigned int delayTicks = 0; // number of ticks to set the timer to for the desired delay
- unsigned int delayFor = 0; // delay for this many uS
- int main (void)
- {
- // -- Oscillator setup
- OSCCONbits.IRCF = 7; // set internal osc to 8MHz
- while (!OSCCONbits.HTS); // wait for osc to be stable
- // -- IO setup
- ZERO_CROSS_PIN_TRIS = 1; // read from Z_C_PIN
- OUTPUT_PIN_TRIS = 0; // write to O_P
- OUTPUT_PIN = 0; // set it to a known state
- // -- Timer1 setup
- T1CONbits.T1CKPS = 0; // 1:1 prescaler
- T1CONbits.T1OSCEN = 0; // disable Timer1 oscillator
- T1CONbits.TMR1CS = 0; // internal clock (fosc/4)
- // -- Timer1 interrupt setup
- TMR1 = 0; // clear timer1 counter
- TMR1IF = 0; // clear timer1 flag
- TMR1IE = 1; // enable timer1 interrupt
- PEIE = 1; // enable peripheral interrupts
- GIE = 1; // enable all interrupts
- while (1)
- {
- if (ZERO_CROSS_PIN && !timerArmed) // if we have a pulse and the
- { // timer isnt armed, arm it
- delayFor = 4000; // in uS
- delayTicks = (2 * delayFor);
- TMR1 = (0xFFFF - delayTicks) + 1; // +1 to account for ffff->0 rollover
- T1CONbits.TMR1ON = 1; // arm timer1
- timerArmed = 1;
- }
- }
- return 0; // should never get here
- }
- void interrupt interrupt_handler(void)
- {
- if (TMR1IE && TMR1IF) // if TMR1 caused the interrupt
- {
- OUTPUT_PIN = 1; // fire!
- __delay_us(10); // for the triac to catch up
- OUTPUT_PIN = 0; // triac will continue to conduct until current drops
- timerArmed = 0; // timer not armed anymore
- TMR1IF = 0; // clear interrupt flag
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement