//**************************************************************************
// JJ18-27 Watt Hour Meter, Phase 1		   		           		   S215  10 1018
// *************************************************************************

#include "s215-header17-f45.h" //CHANGE TO s215-header17-f46.h IF VERSION D
							   //  BOARD.
#pragma code
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
//Global Variables
float
	ccm = 1.0,				//Charging (solar panel) current ADC multiplier
	lcm = 1.0,				//Load current ADC multiplier
	bveslope = 0.0464, 		//Battery voltage-equation slope
	bveintercept = -8.1605;	//Battery voltage-equation y intercept
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
void main (void)
{
unsigned char wo;	//Which one.
float
	spwh = 0,	//Solar panel watt hours.
	ldwh = 0,	//Load watt hours.
	bv = 12,	//Battery voltage. (Start with 12 V. so running average
				//   won't take so long to settle down.)
	td,			//Number to be displayed.
	//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
	cf = 1. / 1.;	//Correction factor = actual time/displayed time
	//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

InitPorts();
while (1==1)
{
//|||||||||||||||||||||||||| Main Loop |||||||||||||||||||||||||||||||||||||
Delay10ms(100);							//One second.
bv = Getcv(0);							//0 selects battery voltage.
spwh = spwh + Getcv(1)*1000.*12.*0.000277778 * cf;	//1 selects solar panel
										// current. 1 A. max.
ldwh = ldwh + Getcv(2)*1000.*12.*0.000277778 * cf;	//2 selects load 
										// current. 1 A. max.
if (PB == 1)
{
	spwh = 0;
	ldwh = 0;
}
wo = SWITCHES & 0b00000011;
if (wo == 0)			//Battery voltage.
	td = bv;
if (wo == 1)			//Solar panel watt hours.
	td = spwh;
if (wo == 2)			//Load watt hours.
	td = ldwh;
if (wo == 3)			//Watt hours of battery charging.
	td = spwh - ldwh;	
DispFP(td, 3);
//===========================================================
//|||||||||||||||||||||||| End of Main Loop ||||||||||||||||||||||||||||||||		
}
}
