Skip to content

Instantly share code, notes, and snippets.

@chaeplin
Forked from me-no-dev/ZeroCrossTriac.ino
Created April 15, 2016 23:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chaeplin/709bbf37dae5b98c2cdefec70c79d5e7 to your computer and use it in GitHub Desktop.
Save chaeplin/709bbf37dae5b98c2cdefec70c79d5e7 to your computer and use it in GitHub Desktop.
#define TRIAC_PIN 5
#define ZERO_CROSS_PIN 4
#define DEBOUNCE_TIME 9000 //9ms - 10ms is the pulse period
static uint32_t lastPulse = 0;
static uint16_t period = 5000; //5ms - 50% for each half wave
void ICACHE_RAM_ATTR onTimerISR(){
if(GPIP(TRIAC_PIN)){//OUTPUT is HIGH
GPOC = (1 << TRIAC_PIN);//low
timer1_write(60);//12us
} else GPOS = (1 << TRIAC_PIN);//high
}
void ICACHE_RAM_ATTR onPinISR(){
uint32_t now = micros();
if(now - lastPulse < DEBOUNCE_TIME) return;
lastPulse = now;
GPOS = (1 << TRIAC_PIN);//high
timer1_write(period * 5);
}
void setup(){
pinMode(TRIAC_PIN, OUTPUT);
pinMode(ZERO_CROSS_PIN, INPUT);
GPOS = (1 << TRIAC_PIN);//high
timer1_attachInterrupt(onTimerISR);
timer1_enable(TIM_DIV16, TIM_EDGE, TIM_SINGLE);
attachInterrupt(ZERO_CROSS_PIN, onPinISR, RISING);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment