From f578a4ec284a92dce5ae445800efed392337543f Mon Sep 17 00:00:00 2001 From: jakeg00dwin Date: Thu, 5 Sep 2024 13:29:07 -0700 Subject: [PATCH] Included the ADC and ZCD headers. Also added function for using the AC relay. --- src/main.c | 82 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 63 insertions(+), 19 deletions(-) diff --git a/src/main.c b/src/main.c index 75d5045..82a8281 100644 --- a/src/main.c +++ b/src/main.c @@ -9,17 +9,23 @@ * extracted into separate source files and headers for configuration. */ +#ifndef __AVR_ATtiny404__ +#define __AVR_ATtiny404__ +#endif + #include #define F_CPU 3333333UL // This can prevent issues with utils/delay.h library with the gcc toolchain #define __DELAY_BACKWARD_COMPATIBLE__ +#include "ADC.h" #include "RegEdit.h" #include "Relays.h" #include "SuperLoop.h" #include "config.h" #include "timer.h" +#include "zero_cross_detection.h" #include /* Required header file */ #include #include @@ -33,55 +39,93 @@ #define RELAY2_ENPIN 2 // PORTB #define RELAY2_INPIN 0 // PORTB +#define RELAYAC_ENPIN 1 // PORTB + #define N_RELAYS 3 // Set the function pointer for the delay func void (*Delay_MicroSeconds)(double us) = _delay_us; -void setup_relays(Relay *arr) { +static void setup_ac_relay(Relay *ac) { + // setup the first relay + ac->output_port = &PORTB.OUT; + ac->output_pin = RELAYAC_ENPIN; + ac->disabled_fpc = false; + + // Set the directions for the input and output pins/ports + PORTB.DIR |= (1 << RELAYAC_ENPIN); +} + +static void setup_relays(Relay *arr) { // setup the first relay arr[0].output_port = &PORTA.OUT; arr[0].output_pin = RELAY0_ENPIN; arr[0].input_port = &PORTA.IN; arr[0].input_pin = RELAY0_INPIN; - arr[0].disabled_fpc = false; // Set the directions for the input and output pins/ports PORTA.DIR |= (1 << RELAY0_ENPIN); PORTA.DIR &= ~(1 << RELAY0_INPIN); - // setup the second relay arr[1].output_port = &PORTB.OUT; arr[1].output_pin = RELAY1_ENPIN; arr[1].input_port = &PORTA.IN; arr[1].input_pin = RELAY1_INPIN; - arr[1].disabled_fpc = false; // Set the directions for the input and output pins/ports PORTB.DIR |= (1 << RELAY1_ENPIN); - PORTA.DIR &= ~(1 << RELAY0_INPIN); - - //for (int i = 0; i < N_RELAYS; i++) { - //Relay_MeasureMakeTime(&arr[0]); - //_delay_ms(5); - //Relay_MeasureBreakTime(&arr[0]); - //} - + PORTA.DIR &= ~(1 << RELAY1_INPIN); + + // setup the third relay + arr[2].output_port = &PORTB.OUT; + arr[2].output_pin = RELAY2_ENPIN; + arr[2].input_port = &PORTB.IN; + arr[2].input_pin = RELAY2_INPIN; + + // Set the directions for the input and output pins/ports + PORTB.DIR |= (1 << RELAY2_ENPIN); + PORTB.DIR &= ~(1 << RELAY2_INPIN); + + for (int i = 0; i < N_RELAYS; i++) { + arr[i].disabled_fpc = false; + Relay_MeasureMakeTime(&arr[i]); + _delay_ms(100); + Relay_MeasureBreakTime(&arr[i]); + } } int main(int argc, char **argv) { // startup delay prevents fast toggles of relays on reset signals _delay_ms(500); - Relay relay_array[N_RELAYS]; - setup_relays(relay_array); + Relay relay_array[N_RELAYS]; + Relay ac_relay; - Relay_MeasureMakeTime(&relay_array[1]); - _delay_ms(500); - Relay_MeasureBreakTime(&relay_array[1]); + setup_relays(relay_array); + // Only setup the AC relay after testing of the other relays is done. + setup_ac_relay(&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]); + } + + 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, EN1); + // Load_HandleLoadPortB(ADC_LOAD2, EN2); + // Load_HandleLoadPortB(ADC_LOAD3, EN3); + } // Setup for Infinite Loop - SuperLoop_SetIterations(0); - SuperLoop_Run(); + // SuperLoop_SetIterations(0); + // SuperLoop_Run(); }