diff --git a/ADC.c b/ADC.c index 42d49ea..e59f6cc 100644 --- a/ADC.c +++ b/ADC.c @@ -9,6 +9,8 @@ #include "RegEdit.h" #include "avr/io.h" +static uint8_t porta_out; +static uint8_t porta_dir; static bool IsInvalidPin(uint8_t pin_num){ if(pin_num > 7){ @@ -20,6 +22,10 @@ static bool IsInvalidPin(uint8_t pin_num){ void ADC_Init(uint8_t pin_num) { + //Save the porta status. + porta_out = PORTA.OUT; + porta_dir = PORTA.DIR; + if(IsInvalidPin(pin_num)){return;} //set the direction to input @@ -70,6 +76,10 @@ void ADC_Disable() //Clear the enable ADC flag RegEdit_ClearBit((void *) &ADC0.CTRLA, 0); + + //Restore the port registers + PORTA.OUT = porta_out; + PORTA.DIR = porta_dir; } diff --git a/config.h b/config.h index 2f41697..8e9d2c0 100644 --- a/config.h +++ b/config.h @@ -16,6 +16,9 @@ #include +#define ADC_LOAD1 4 +#define ADC_LOAD2 5 +#define ADC_LOAD3 6 /** * @brief Positive Zero Crossing Trigger Value diff --git a/load.c b/load.c index 4330af5..1f3f501 100644 --- a/load.c +++ b/load.c @@ -4,60 +4,40 @@ #include "RegEdit.h" -#define ADC_LOAD1 4 -#define ADC_LOAD2 5 -#define ADC_LOAD3 6 - #define G1 1 #define G2 3 #define G3 2 #define SAMPLE_QTY 32 - -uint16_t read_load(uint8_t pin_num) +void Load_HandleLoadPortA(uint8_t adc_pin, uint8_t out_pin) { - ADC_Init(pin_num); - ADC_Enable(pin_num); - uint16_t value = ADC_ReadValue(pin_num); + ADC_Init(adc_pin); + ADC_Enable(adc_pin); + uint16_t val; + for(int i = 0; i < 32; i++){ + val += ADC_ReadValue(adc_pin); + } + val /= 32; ADC_Disable(); - return value; + if(val < 527 || val > 1000){ + RegEdit_ClearBit((void *) &PORTA.OUT, out_pin); + RegEdit_ClearBit((void *) &PORTA.DIR, out_pin); + } } - -void Load_HandlePinLoads(void) +void Load_HandleLoadPortB(uint8_t adc_pin, uint8_t out_pin) { - //saving the state of the port A output. - uint8_t porta_out_state = PORTA.OUT; - uint8_t portb_out_state = PORTB.OUT; - - uint16_t load_one, load_two, load_three; - load_one = 0; - load_two = 0; - load_three = 0; - - - for(int i = 0; i < SAMPLE_QTY; i++){ - load_one += read_load(ADC_LOAD1); - load_one += read_load(ADC_LOAD2); - load_one += read_load(ADC_LOAD3); + ADC_Init(adc_pin); + ADC_Enable(adc_pin); + uint16_t val; + for(int i = 0; i < 32; i++){ + val = ADC_ReadValue(adc_pin); } - load_one /= SAMPLE_QTY; - load_two /= SAMPLE_QTY; - load_three /= SAMPLE_QTY; - - //load the backed up state(just in case) - RegEdit_SetNum((void *) &PORTA.OUT, porta_out_state); - RegEdit_SetNum((void *) &PORTB.OUT, portb_out_state); - - if(load_one < LOWTHRESH || load_one > HIGHTHRESH){ - RegEdit_ClearBit((void *) &PORTA.OUT, G1); + + ADC_Disable(); + if(val < 527 || val > 1000){ + RegEdit_ClearBit((void *) &PORTB.OUT, out_pin); + RegEdit_ClearBit((void *) &PORTB.DIR, out_pin); } - if(load_two < LOWTHRESH || load_two > HIGHTHRESH){ - RegEdit_ClearBit((void *) &PORTB.OUT, G2); - } - if(load_three < LOWTHRESH || load_three > HIGHTHRESH){ - RegEdit_ClearBit((void *) &PORTB.OUT, G3); - } - } \ No newline at end of file diff --git a/load.h b/load.h index 606977b..1b3112f 100644 --- a/load.h +++ b/load.h @@ -22,6 +22,8 @@ */ #define HIGHTHRESH 1000 +void Load_HandleLoadPortA(uint8_t adc_pin, uint8_t out_pin); +void Load_HandleLoadPortB(uint8_t adc_pin, uint8_t out_pin); void Load_HandlePinLoads(void); diff --git a/main.c b/main.c index 300e13c..d3460fe 100644 --- a/main.c +++ b/main.c @@ -51,7 +51,11 @@ int main(int argc, char **argv) ZCD_Poll(); TriacOut_SetupPins(); TriacOut_SetAllHigh(); - Load_HandlePinLoads(); + + Load_HandleLoadPortA(ADC_LOAD1, 1); + Load_HandleLoadPortB(ADC_LOAD2, 3); + Load_HandleLoadPortB(ADC_LOAD3, 2); + while(true){ ; //Do nothing until new Power cycle/reset occurs } diff --git a/nbproject/Makefile-genesis.properties b/nbproject/Makefile-genesis.properties index 052a98d..fe30d3b 100644 --- a/nbproject/Makefile-genesis.properties +++ b/nbproject/Makefile-genesis.properties @@ -1,11 +1,11 @@ # -#Tue Jul 02 14:50:32 PDT 2024 +#Tue Jul 02 16:53:25 PDT 2024 attiny404.com-microchip-mplab-mdbcore-PK5Tool-PK5ToolImpl.md5=8ed9aa4326bfc0c1a849e697826741b7 attiny404.languagetoolchain.version=2.46 attiny404.com-microchip-mplab-nbide-toolchain-xc8-XC8LanguageToolchain.md5=bf89cdcdd6c0a49174fe4b605ef2b42d conf.ids=,attiny404 host.id=2ov5-ff4p-rv -configurations-xml=b81754b515c9768dda4d05124e37fb9b +configurations-xml=612d34b4ce82b1d02e7fec601f793556 com-microchip-mplab-nbide-embedded-makeproject-MakeProject.md5=f612087c95360c842296d189edfe3321 attiny404.languagetoolchain.dir=/opt/microchip/xc8/v2.46/bin proj.dir=/home/ronin/Documents/projects/freelance/laith_naaman/fg004a.X diff --git a/nbproject/configurations.xml b/nbproject/configurations.xml index f2dea4b..57742c3 100644 --- a/nbproject/configurations.xml +++ b/nbproject/configurations.xml @@ -212,6 +212,8 @@ + @@ -442,10 +444,10 @@ - + - + @@ -465,6 +467,8 @@ + @@ -506,7 +510,7 @@ - + diff --git a/nbproject/private/private.xml b/nbproject/private/private.xml index e77a58e..63881df 100644 --- a/nbproject/private/private.xml +++ b/nbproject/private/private.xml @@ -4,6 +4,8 @@ file:/home/ronin/Documents/projects/freelance/laith_naaman/fg004a.X/main.c + file:/home/ronin/Documents/projects/freelance/laith_naaman/fg004a.X/load.c + file:/home/ronin/Documents/projects/freelance/laith_naaman/fg004a.X/load.h diff --git a/queuelogs/debugtool.txt b/queuelogs/debugtool.txt new file mode 100644 index 0000000..2f5a2c5 --- /dev/null +++ b/queuelogs/debugtool.txt @@ -0,0 +1,22 @@ +37 1719963429078 GEN4_CONNECT_DIAGNOSTICS:Removing plug and play observer com.microchip.mplab.mdbcore.RI4Tool.RI4ToolBase$RI4PlugNPlayObserver@526a24e0 +37 1719963429078 GEN4_CONNECT_DIAGNOSTICS:Begin PlugNPlayObservers list dump: +37 1719963429078 GEN4_CONNECT_DIAGNOSTICS:End PlugNPlayObservers list dump: + +4715 1719963429080 GEN4_TO_CONTROLLER_LOG:GEN4_DEBUG_INT_LOG_ATtiny404 Pre:SetSoftwareBreakpoint +4715 1719963429080 GEN4_TO_CONTROLLER_LOG:GEN4_DEBUG_INT_LOG_ATtiny404 Payload +4715 1719963429080 GEN4_TO_CONTROLLER_LOG:GEN4_DEBUG_INT_LOG_ATtiny404 ffffffffffffffff +4715 1719963429080 GEN4_TO_CONTROLLER_LOG:GEN4_DEBUG_INT_LOG_ATtiny404 f7b9 +4715 1719963429080 GEN4_TO_CONTROLLER_LOG:GEN4_DEBUG_INT_LOG_ATtiny404 0 +4715 1719963429080 GEN4_TO_CONTROLLER_LOG:GEN4_DEBUG_INT_LOG_ATtiny404 Payload End +4715 1719963429080 XXXX 1719963429080, Thread 4715: XXXX 1719963429080, Entering Thread 4715: Commands::runScriptWithUpload, script name = ReadProgmem +4715 1719963429080 XXXX 1719963429080, Thread 4715: XXXX 1719963429080, Entering Thread 4715: ToolCommUsb::readTransfer, sideData is 0x59 bytes long, transferDataLength is 0x40 bytes long +4715 1719963429081 XXXX 1719963429081, Thread 4715: com.Send returned false +4715 1719963429081 XXXX 1719963429081, Thread 4715: readTransfer: exception after writeHeaderAndGetAck null +4715 1719963429081 XXXX 1719963429081, Thread 4715: XXXX 1719963429081, Entering Thread 4715: ToolCommUsb::recoverFromProtocolError, type = NO_SCRIPT +4715 1719963429081 XXXX 1719963429081, Thread 4715: Send Abort Script Engine Command +4715 1719963429081 XXXX 1719963429081, Thread 4715: XXXX 1719963429081, Entering Thread 4715: ToolCommUsb::killScriptEngine +4715 1719963429081 XXXX 1719963429081, Thread 4715: com.Send returned false +4715 1719963429081 XXXX 1719963429081, Thread 4715: Failed sending Abort Script Engine Command. Set nuclear option = true +4715 1719963429081 XXXX 1719963429081, Thread 4715: About to nuke +4715 1719963429081 XXXX 1719963429081, Thread 4715: XXXX 1719963429081, Entering Thread 4715: ToolCommUsb::nuclearOption +4715 1719963429081 XXXX 1719963429081, Thread 4715: com.Send returned false