/******************************************************************************************* Brultech Power Monitor - Main *******************************************************************************************/ /* Programmer: Allan Yates Email: allan@yates.ca Version: 1.0 */ /******************************************************************************************* Compiler Directives *******************************************************************************************/ #OUTPUT_SHIFT 1 #CATEGORY "4" // Device Interface #DEFAULT_VOLATILE #ENABLE_STACK_CHECKING #DEFINE_CONSTANT FALSE 0 #DEFINE_CONSTANT TRUE 1 #DEFINE_CONSTANT PWR_INIT 65535 #DEFINE_CONSTANT ON_INIT 255 #DEFINE_CONSTANT FORMATTED_LEN 8 #DEFINE_CONSTANT OUTPUT_ONCHANGE 0 #DEFINE_CONSTANT OUTPUT_FORCE 1 #DEFINE_CONSTANT MAX_INPUTS 5 #HELP_BEGIN Brultech Energy Monitor Load Sum This module will sum two or more analogue inputs, create a formatted string output in Watts along with an corresponding analogue output. A digital output is set high if the summed output exceeds the threshold parameter. A rising edge on the Refresh input will force the module outputs to be updated. Contact Allan Yates at allan@yates.ca with any questions, to report bugs, or to request enhancements. #HELP_END /******************************************************************************************* DIGITAL, ANALOG and SERIAL INPUTS and OUTPUTS *******************************************************************************************/ INTEGER_PARAMETER Threshold; DIGITAL_INPUT _SKIP_; DIGITAL_INPUT Refresh; ANALOG_INPUT PowerIn[MAX_INPUTS]; STRING_OUTPUT Power$; ANALOG_OUTPUT Power; DIGITAL_OUTPUT PowerOn; /******************************************************************************************* Global Variables *******************************************************************************************/ INTEGER CurPower; INTEGER CurPowerOn; INTEGER NumInputs; INTEGER Processing; /******************************************************************************************* Functions *******************************************************************************************/ STRING_FUNCTION FormatPower(INTEGER PowerVal) { STRING Formatted$[FORMATTED_LEN]; INTEGER Thousands; INTEGER Hundreds; if (PowerVal > 999) { Thousands = PowerVal / 1000; Hundreds = PowerVal - (Thousands * 1000); makestring(Formatted$,"%d,%03d",Thousands,Hundreds); } else { makestring(Formatted$,"%d",PowerVal); } Formatted$ = Formatted$ + " W"; return (Formatted$); } FUNCTION ProcessPower (INTEGER Force) { INTEGER NewOn; INTEGER NewPower; INTEGER Idx; NewPower = 0; for (Idx = 1 to NumInputs) NewPower = NewPower + PowerIn[Idx]; if ((NewPower <> CurPower) || Force) { Power$ = FormatPower(NewPower); CurPower = NewPower; Power = NewPower; NewOn = 0; IF (NewPower >= Threshold) NewOn = 1; IF ((NewOn <> CurPowerOn) || Force) { PowerOn = NewOn; CurPowerOn = NewOn; } } } FUNCTION RefreshOutputs () { ProcessPower(OUTPUT_FORCE); } /******************************************************************************************* Event Handlers *******************************************************************************************/ PUSH Refresh { RefreshOutputs(); } CHANGE PowerIn { if (Processing = TRUE) TERMINATEEVENT; Processing = TRUE; ProcessPower(OUTPUT_ONCHANGE); Processing = FALSE; } /******************************************************************************************* Main() *******************************************************************************************/ Function Main() { CurPower = PWR_INIT; PowerOn = ON_INIT; Processing = FALSE; for (NumInputs = MAX_INPUTS to 1 Step -1) if (IsSignalDefined(PowerIn[NumInputs])) Break; }