
#include <p18cxxx.h>
#include <delays.h>
#include <usart.h>
#include <stdio.h>
#include <timers.h>

#include "stpm10.h"


//////////////////////// DEFINICE HW /////////////////////////////////////////


#define LED_POM_ON()		{LATCbits.LATC0=0;}
#define LED_POM_OFF()		{LATCbits.LATC0=1;}


//////////////////////// OVLADANI SERIOVEHO PORTU ////////////////////////////




/////////////////////// INICIALIZACE HW //////////////////////////////////////

void initHardware(void) {
    TRISA = 0b00000000;
    TRISB = 0b11000000;
    TRISC = 0b10010000;
}


/////////////////////// VYPIS DAT ///////////////////////////////////////////

void printFloat(float fInput)	{
	//Convert number from float to fixed point for display.
	//The number is converted to two parts.
	long lWhole=(long)((float)fInput);
	long ulPart=(long)((float)fInput*1000)-lWhole*1000;

	if (ulPart<0) {
		ulPart *= (-1);
		if (lWhole==0) printf((const rom far char *)"-");
	} 

	printf( (const rom far char *)"%7li,%.3li\r\n",lWhole,ulPart);
}
		
void printDF(stpmEnergyCounter ec) {
	if (ec.dec>=0) {
		printf( (const rom far char *)" %6ld,%.6ld\r\n",ec.dec,ec.frac);
	} else {
		//zaporne hodnoty
		if (ec.dec==-1) {
			printf( (const rom far char *)"-");
		}
		printf( (const rom far char *)"%6ld,%.6ld\r\n",ec.dec+1,1000000-ec.frac);
	}
}
		



/////////////////////// HLAVNI SMYCKA ////////////////////////////////////////

void main(void) {
	int pocetMereni = 0;
	unsigned int cas = 0;
    initHardware();
	ANCON0=0;
	ANCON1=0;
	
	
	Open1USART(USART_TX_INT_OFF &
	USART_RX_INT_OFF &
	USART_ASYNCH_MODE &
	USART_EIGHT_BIT &
	USART_CONT_RX &
	USART_BRGH_HIGH,51);
	

	RCONbits.IPEN = 1;
	IPR1bits.RCIP = 1;
	INTCONbits.GIEH = 1;

	stdout = _H_USART;

	printf( (const rom far char *)"STPM10.c - ukazkovy program\r\n");
	printf( (const rom far char *)"---------------------------\r\n");



	T0CON = 0b10000111;

	initSTPM();
	
	writeSTPMBit(1,23,1);

	initDefaultCalibrations();



	WriteTimer0(0);


	while (1) {
	//	Delay10KTCYx(40);
		LED_POM_ON();

		

		pocetMereni++;
		if (pocetMereni>100) {
			pocetMereni=0;
			reinitializeMaxMeasurements(REI_ALL);
		}

		cas = ReadTimer0();
		WriteTimer0(0);

		printf( (const rom far char *)"=== Prubezne mereni (%d) ===\r\n",pocetMereni);

		printf( (const rom far char *)"Cas cyklu [ms]: %lu\r\n",(unsigned long)cas*64/1000);

		processSTPM((unsigned long)cas*64); //cas v us

		LED_POM_OFF();

		if ((pocetMereni%10) == 0) {

			computeSTPMResults(1); 
		
			printf( (const rom far char *)"=== Vysledky mereni pro blok 1 ===\r\n");	

			//vypis hodnot
			printf( (const rom far char *)"Frekvence [Hz]:     ");
			printFloat(stpmResult[1] . freq);
			printf( (const rom far char *)"Napeti [Vrms]:      ");
			printFloat(stpmResult[1] . uRMS);
			printf( (const rom far char *)"Proud [Arms]:       ");
			printFloat(stpmResult[1] . iRMS);
			printf( (const rom far char *)"Napeti [Vpeak]:     ");
			printFloat(stpmResult[1] . uMax);
			printf( (const rom far char *)"Proud [Apeak]:      ");
			printFloat(stpmResult[1] . iMax);
	
			printf( (const rom far char *)"Cinny vykon  [W]:   ");
			printFloat(stpmResult[1] . actPwr);
			printf( (const rom far char *)"Zdanl. vykon [VA]:  ");
			printFloat(stpmResult[1] . appPwr);
			printf( (const rom far char *)"Jalovy vykon [VAr]: ");
			printFloat(stpmResult[1] . reaPwr);
			printf( (const rom far char *)"Vykon v 1.h. [W]:   ");
			printFloat(stpmResult[1] . t1Pwr);
			printf( (const rom far char *)"Power factor [-]:   ");
			printFloat(getPowerFactor(&stpmResult[1]));
	
			if (pocetMereni>30) {
				printf( (const rom far char *)"Crest factor U [-]: ");
				printFloat(getUCrestFactor(&stpmResult[1]));
		
				printf( (const rom far char *)"Crest factor I [-]: ");
				printFloat(getICrestFactor(&stpmResult[1]));
			}
	
			printf( (const rom far char *)"Spotreba [-]:       ");
			printDF(stpmResult[1] . actEnergy);
	
			printf( (const rom far char *)"Spotreba [Ws]:      ");
			printFloat(getEnergyWs(1, stpmResult[1].actEnergy, TD_ACT));
	
			printf( (const rom far char *)"Spotreba [Wh]:      ");
			printFloat(getEnergyWh(1, stpmResult[1].actEnergy, TD_ACT));
	
	
			printf( (const rom far char *)"Zdanl. energie [-]: ");
			printDF(stpmResult[1] . appEnergy);
	
			printf( (const rom far char *)"Zdanl. energie[VAs]:");
			printFloat(getEnergyWs(1, stpmResult[1].appEnergy, TD_ACT));
	
			printf( (const rom far char *)"Zdanl. energie[VAh]:");
			printFloat(getEnergyWh(1, stpmResult[1].appEnergy, TD_ACT));
	
	
			printf( (const rom far char *)"Jal. energie [-]:   ");
			printDF(stpmResult[1] . reaEnergy);
	
			printf( (const rom far char *)"Energie v 1.h. [-]: ");
			printDF(stpmResult[1] . t1Energy);
		}
    }
    
	return;
} 