Implimented Load detection module that auto cuts G1, G2 and G3 if values on the ADC_LOAD pins is outside parameters.
This commit is contained in:
parent
e4f19012b0
commit
eba8344e36
10
ADC.c
10
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;
|
||||
}
|
||||
|
||||
|
||||
|
|
3
config.h
3
config.h
|
@ -16,6 +16,9 @@
|
|||
|
||||
#include <stdint.h>
|
||||
|
||||
#define ADC_LOAD1 4
|
||||
#define ADC_LOAD2 5
|
||||
#define ADC_LOAD3 6
|
||||
|
||||
/**
|
||||
* @brief Positive Zero Crossing Trigger Value
|
||||
|
|
66
load.c
66
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);
|
||||
}
|
||||
|
||||
}
|
2
load.h
2
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);
|
||||
|
||||
|
|
6
main.c
6
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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -212,6 +212,8 @@
|
|||
<property key="memories.programmemory" value="true"/>
|
||||
<property key="memories.programmemory.ranges" value="0-7ff"/>
|
||||
<property key="poweroptions.powerenable" value="true"/>
|
||||
<property key="programmerToGoFilePath"
|
||||
value="/home/ronin/Documents/projects/freelance/laith_naaman/fg004a.X/debug/default/fg004a_ptg"/>
|
||||
<property key="programmerToGoImageName" value="fg004a_ptg"/>
|
||||
<property key="programoptions.donoteraseauxmem" value="false"/>
|
||||
<property key="programoptions.eraseb4program" value="true"/>
|
||||
|
@ -442,10 +444,10 @@
|
|||
<property key="communication.activationmode" value="nohv"/>
|
||||
<property key="communication.interface" value="updi"/>
|
||||
<property key="communication.interface.jtag" value="2wire"/>
|
||||
<property key="communication.speed" value="0.500"/>
|
||||
<property key="communication.speed" value="0.250"/>
|
||||
<property key="debugoptions.debug-startup" value="Use system settings"/>
|
||||
<property key="debugoptions.reset-behaviour" value="Use system settings"/>
|
||||
<property key="debugoptions.useswbreakpoints" value="false"/>
|
||||
<property key="debugoptions.useswbreakpoints" value="true"/>
|
||||
<property key="event.recorder.debugger.behavior" value="Running"/>
|
||||
<property key="event.recorder.enabled" value="false"/>
|
||||
<property key="event.recorder.scvd.files" value=""/>
|
||||
|
@ -465,6 +467,8 @@
|
|||
<property key="memories.programmemory" value="true"/>
|
||||
<property key="memories.programmemory.ranges" value="0-7ff"/>
|
||||
<property key="poweroptions.powerenable" value="true"/>
|
||||
<property key="programmerToGoFilePath"
|
||||
value="/home/ronin/Documents/projects/freelance/laith_naaman/fg004a.X/debug/attiny404/fg004a_ptg"/>
|
||||
<property key="programmerToGoImageName" value="fg004a_ptg"/>
|
||||
<property key="programoptions.donoteraseauxmem" value="false"/>
|
||||
<property key="programoptions.eraseb4program" value="true"/>
|
||||
|
@ -506,7 +510,7 @@
|
|||
<property key="communication.activationmode" value="nohv"/>
|
||||
<property key="communication.interface" value="updi"/>
|
||||
<property key="communication.interface.jtag" value="2wire"/>
|
||||
<property key="communication.speed" value="0.500"/>
|
||||
<property key="communication.speed" value="0.250"/>
|
||||
<property key="debugoptions.debug-startup" value="Use system settings"/>
|
||||
<property key="debugoptions.reset-behaviour" value="Use system settings"/>
|
||||
<property key="debugoptions.useswbreakpoints" value="false"/>
|
||||
|
|
|
@ -4,6 +4,8 @@
|
|||
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
|
||||
<group>
|
||||
<file>file:/home/ronin/Documents/projects/freelance/laith_naaman/fg004a.X/main.c</file>
|
||||
<file>file:/home/ronin/Documents/projects/freelance/laith_naaman/fg004a.X/load.c</file>
|
||||
<file>file:/home/ronin/Documents/projects/freelance/laith_naaman/fg004a.X/load.h</file>
|
||||
</group>
|
||||
</open-files>
|
||||
</project-private>
|
||||
|
|
|
@ -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
|
Loading…
Reference in New Issue