You are on page 1of 3

24/08/2015

meettechniek.info/diyinstruments/arduinowattmetercodev10.txt

/************************************/
/***ArduinoWattmeterv1.0***/
/***Board:ArduinoNano3.0***/
/***By:FreddyAlferink***/
/***http://meettechniek.info***/
/***May2014***/
/************************************/
/***********Calibration&HardwareData***********/
floatVdiv=101.0;//Voltageconversionfactor
floatCdiv=10.0;//Currentconversionfactor
constbyteLCDlines=2;//LCD:Numberoflines
constbyteLCDwidth=16;//LCD:Numberofcharacterperline
/***************************************************/
constfloatadcSense=1.074219e3;//ADCconversionfactorvolt/bit(1.1V/1024)
floatVscale=1.0;//Voltagescale
floatCscale=1.0;//Currentscale
bytemuxCnt=4;//Analogmultiplexerstart
intnullVal=512;//Measurednullvalue
booleanoverFlowVolt=false;//Voltageoverflowflag
booleanoverFlowCurr=false;//Currentoverflowflag
unsignedintvCnt=0;//Valuescounter
/***Voltagefrequencymeasurement***/
intvfmUpperTh=10;//Upperthresholdvoltage
intvfmLowerTh=10;//Lowerthresholdvoltage
booleanvfmDir=false;//Directionvoltage
unsignedintvfmPeriods=7;//Periodscounter
unsignedintvfmPeriodsTime=1000;//Synchronizedwithperiodstime
unsignedintvfmTime=1000;//Timecounter
/***Currentfrequencymeasurement***/
intcfmUpperTh=10;//Upperthresholdcurrent
intcfmLowerTh=10;//Lowerthresholdcurrent
booleancfmDir=false;//Directioncurrent
unsignedintcfmPeriods=5;//Periodscounter
unsignedintcfmPeriodsTime=1000;//Synchronizedwithperiodstime
unsignedintcfmTime=1000;//Timecounter
/***Primaryaveraging***/
intprimCnt=0;//Primaryaveragingcounter
booleanprimReady=false;//Primaryaveragingcompleteflag
intadcVolt;//CopyADCvoltage
constunsignedintprimAvLength=64;//Numberofsamplesforprimaryaveraging
longprimMeanVolt=0;//CumulativeADCmeanvoltage
longprimMeanVoltCopy=0;//Averagedprimarymeanvoltage
longprimSquaredVolt=0;//CumulativeADCsquaredvoltage
longprimSquaredVoltCopy=0;//Averagedprimarysquaredvoltage
longprimMaxVolt=0;//HighestmeasuredADCvoltage
longprimMinVolt=0;//LowestmeasuredADCvoltage
intadcCurr;//CopyADCcurrent
longprimMeanCurr=0;//CumulativeADCmeancurrent
longprimMeanCurrCopy=0;//Averagedprimarycurrent
longprimSquaredCurr=0;//CumulativeADCsquaredcurrent
longprimSquaredCurrCopy=0;//Averagedprimarysquaredcurrent
longprimMaxCurr=0;//HighestmeasuredADCcurrent
longprimMinCurr=0;//LowestmeasuredADCcurrent
longprimMeanPow=0;//CumulativeADCmeanpower
longprimMeanPowCopy=0;//Averagedprimarymeanpower
longprimMaxPow=0;//HighestmeasuredADCpower
longprimMinPow=0;//LowestmeasuredADCpower
/***Secondaryaveraging***/
unsignedintsecArrCnt=0;
constunsignedintsecAvLength=50;
longsecMeanVoltArr[secAvLength];//MeanVoltagesecondaryaverageingarray
longsecSquaredVoltArr[secAvLength];//SquaredVoltagesecondaryaverageingarray
http://meettechniek.info/diyinstruments/arduinowattmetercodev10.txt

1/3

24/08/2015

meettechniek.info/diyinstruments/arduinowattmetercodev10.txt

longsecMeanCurrArr[secAvLength];//MeanCurrentsecondaryaverageingarray
longsecSquaredCurrArr[secAvLength];//SquaredCurrentsecondaryaverageingarray
longsecMeanPowArr[secAvLength];//RealPowersecondaryaverageingarray
longsecMeanVolt=0;//Resultsecondaryaveragingmeanvoltage
longsecSquaredVolt=0;//Resultsecondaryaveragingsquaredvoltage
longsecMeanCurr=0;//Resultsecondaryaveragingmeancurrent
longsecSquaredCurr=0;//Resultsecondaryaveragingsquaredcurrent
longsecMeanPow=0;//Resultsecondaryaveragingmeanpower
/***Cumulativevalues***/
bytetFlux[8];//64bitsintegratedADCvoltage
bytetCharge[8];//64bitsintegratedADCcurrent
bytetEnergy[8];//64bitsintegratedADCpower
unsignedintpreTimeCnt=0;//Timeprescaler
unsignedlongtimeCnt=0;//Timesecondscounter
/******/
floattotAverage=10.0;//Totalaveraginglength(primary*secondary)
floatfSample=10.0;//ADCchannelsamplefrequency
constbyteLCDlinePos[4]={0x80,0xC0,0x94,0xD4};//Firstcharpositionforeachline(as
DDRAMinstruction)
bytelineSelect=0;//Theselecteddsplayline
byteparamPointers[4]={0,7,13,18};//Parameterpointerforeachdisplayline
char*paramLabels[]={"Vmean","Vrms","Vsdef","Vmax","Vmin","Flux","f(V)","Imean
","Irms","Isdef","Imax","Imin","Charge","f(I)","Preal","S*","Q*","Pmax
","Pmin","Energy","\x01*","time"};
char*paramUnits[]=
{"V","V","V","V","V","Vs","Hz","A","A","A","A","A","C","Hz","W","VA","var","W","W","J","\x0
2","s"};
floatparamValues[]=
{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};
intparamRange[]={0,0,0,0,0,99,99,0,0,0,0,0,99,99,99,0,0,0,0,99,99,100};//
99=autorange,100=intnumber
intparamDigits[]={4,4,4,3,3,5,4,4,4,4,3,3,5,4,4,4,4,3,3,5,3,7};//numberof
digits
byteantiBounceCnt=0;//Buttonsantibouncecounter
booleanbuttonState=false;//Buttonispressed
unsignedintbuttonPushed=0;//Pressedbuttons
voidsetup(){
MCUCR&=0xEF;//Enablepullupresistors
DDRB&=0xE1;//ButtonsonD9...12areinputs
DDRB|=0x20;//OverflowLEDonD13isoutput
PORTB|=0x1E;//Pullupresistorforthebuttons
bitClear(PORTB,5);//OverflowLEDoff
initiateADC();//initiateADconverter
lcdInitiate();//initiateLCD
lcdCustomChars();//Makecustomcharacters
lcdWrite(true,LCDlinePos[0]);//LCDcursorfirstlineposition0
lcdPrintString("Universalpower");
lcdWrite(true,LCDlinePos[1]);//LCDcursorsecondlineposition0
lcdPrintString("meterv1.0");
for(intmd=0;md<100;md++){//Wait2seconds
muDelay(20000);
}
lcdWrite(true,LCDlinePos[0]);//LCDcursorfirstlineposition0
lcdPrintString("meettechniek");
lcdWrite(true,LCDlinePos[1]);//LCDcursorsecondlineposition0
lcdPrintString(".info");
for(intmd=0;md<100;md++){//Wait2seconds
muDelay(20000);
}
setProperties();//SetpropertiesandClearmeasuredvalues
updateParamLabels();//WriteParameterlabelstoLCD
updateParamValues();//WriteparametervaluestoLCD
sei();//setinterruptflag
http://meettechniek.info/diyinstruments/arduinowattmetercodev10.txt

2/3

24/08/2015

meettechniek.info/diyinstruments/arduinowattmetercodev10.txt

}
voidloop(){
if(primReady==true){
/***SecondaryavearagingMeanVolt***/
secMeanVolt=secMeanVoltArr[secArrCnt];//Subtractoldestvaluestrored
inarrayfromaverage
secMeanVolt+=primMeanVoltCopy;//Addnewestvaluetoaverage
secMeanVoltArr[secArrCnt]=primMeanVoltCopy;//Storenewestvalueinarray
/***SecondaryavearagingMeanSquaredVolt***/
secSquaredVolt=secSquaredVoltArr[secArrCnt];//thesameforsquaredvoltage
secSquaredVolt+=primSquaredVoltCopy;
secSquaredVoltArr[secArrCnt]=primSquaredVoltCopy;
/***SecondaryavearagingMeanCurrent***/
secMeanCurr=secMeanCurrArr[secArrCnt];//andmeancurrent
secMeanCurr+=primMeanCurrCopy;
secMeanCurrArr[secArrCnt]=primMeanCurrCopy;
/***SecondaryavearagingMeanSquaredCurrent***/
secSquaredCurr=secSquaredCurrArr[secArrCnt];//squaredcurrent
secSquaredCurr+=primSquaredCurrCopy;
secSquaredCurrArr[secArrCnt]=primSquaredCurrCopy;
/***SecondaryavearagingMeanPower***/
secMeanPow=secMeanPowArr[secArrCnt];//andpower
secMeanPow+=primMeanPowCopy;
secMeanPowArr[secArrCnt]=primMeanPowCopy;
/***ArrayPointer***/
secArrCnt++;//Increasesecondaryaveragingarray
pointer
if(secArrCnt>=secAvLength){
secArrCnt=0;
}
/***Cumulativevalues***/
extraLongAddLong(tFlux,primMeanVoltCopy);//Addprimaryaveragedvoltagetototal
Flux
extraLongAddLong(t

http://meettechniek.info/diyinstruments/arduinowattmetercodev10.txt

3/3

You might also like