diff --git a/src/main.c b/src/main.c index d5d8b91..d707f5b 100644 --- a/src/main.c +++ b/src/main.c @@ -96,6 +96,48 @@ static void setup_relays(Relay *arr) { } } +void zerocross_relay_enable(Relay *relay) { + uint16_t delay_value = 0; + if(relay->break_time < 83){ + delay_value = (83 - relay->make_time) * 100; + } + else{ + delay_value = (166 - relay->make_time) * 100; + } + + ZCD_Poll(); + _delay_us(delay_value); + Relay_Enable(relay); +} + +void check_load(Relay *relay, uint8_t load_pin) { + //get the ADC value + ADC_Init(load_pin); + ADC_Enable(); + ADC_SetPin(load_pin); + uint16_t val = ADC_ReadValue(load_pin); + ADC_Disable(); + + + //check if it's valid. + if (val > LOWTHRESH && val < HIGHTHRESH) { + return; + } + + uint16_t delay_value = 0; + if(relay->break_time < 83){ + delay_value = (83 - relay->break_time) * 100; + } + else{ + delay_value = (166 - relay->break_time) * 100; + } + ZCD_Poll(); + _delay_us(delay_value); + + Relay_Disable(relay); + Relay_DisableForPowerCycle(relay); +} + int main(int argc, char **argv) { // startup delay prevents fast toggles of relays on reset signals _delay_ms(500); @@ -106,24 +148,20 @@ int main(int argc, char **argv) { // Only setup the AC relay after testing of the other relays is done. setup_ac_relay(&ac_relay); + Relay_Enable(&ac_relay); ADC_Setup(); - for (int i = 0; i < GatePulsesQty; i++) { - ZCD_Poll(); - _delay_us(Tau); - // TriacOut_SetAllHigh(); // Only G1 exists in High power mode - // TriacOut_PulsePins(GatePulses[i]); + + + for(int i = 0; i < N_RELAYS; i++){ + zerocross_relay_enable(&relay_array[i]); } - Relay_Enable(&ac_relay); - _delay_ms(500); - Relay_Disable(&ac_relay); while (true) { - // Enable pins are enabled(set high) if the ADCLOAD value is valid. - Load_HandleLoadPortA(ADC_LOAD1, RELAY0_ENPIN); - Load_HandleLoadPortB(ADC_LOAD2, RELAY1_ENPIN); - Load_HandleLoadPortB(ADC_LOAD3, RELAY2_ENPIN); + check_load(&relay_array[0], ADC_LOAD1); + check_load(&relay_array[1], ADC_LOAD2); + check_load(&relay_array[2], ADC_LOAD3); } // Setup for Infinite Loop