#define DC_Offset1 2042
#define DC_Offset2 2042 //for Analog Pin
#define DC_Offset3 2042 //For analog pin
#define OPV 5.035 //operating controller voltage
#define Gain 244.442 //gain = (OPV)* Fluke Voltage measurement at the end of OPAMP (Converted DC vlotage)
#define Mul_fact_B 0.325 //Multifiaon Factor = (gain*OPV)/4096 //here 4096 is for 12 bit adc 2^12 (Offset max expected)
#define Mul_fact_Y 0.300
#define Mul_fact_R 0.330
void Adc_Init(void)
{
ADPCFG = 0xFFFF; //configuring channels
ADPCFGbits.PCFG13 =0; //Blue
ADPCFGbits.PCFG14 =0; //Yellow
ADPCFGbits.PCFG15 =0; // Red
ADCHS = 0;
ADCON3bits.ADCS = 31; //A/D Conversion Clock Select bits.
ADCON3bits.SAMC = 1; //sample for 1 Tad cycles
ADCON3bits.ADRC = 0; //Clock derived from system clock
ADCSSL = 0x0000;
ADCSSLbits.CSSL13 =1;
ADCSSLbits.CSSL14 =1;
ADCSSLbits.CSSL15 =1;
ADCON2bits.ALTS=0; //0 = Always use MUX A input multiplexer settings, (bit 0)
ADCON2bits.BUFM=0; //0 means Buffer configured as one 16-word buffer (bit 1)
ADCON2bits.SMPI=2; //Sample/Convert Sequences Per Interrupt Selection bits (bit 5-2)
ADCON2bits.CSCNA=1; //1= scan i/p, 0= dont scan (bit 10)
ADCON2bits.VCFG=0; //Voltage ref Config (bit 15-13)
ADCON2bits.BUFS=0; //0 means BUF select any one of set 8-16 resgisters,0r 0-7 (bit 7)
ADCON1bits.ADON=0; //on or off A/D (BIT 15)
ADCON1bits.ADSIDL=0; //IDEAL MODE RUN BIT(BIT 13)
ADCON1bits.FORM=0; //OUTPUT FORM 00 (16 BIT UNSIGNED INT)
ADCON1bits.SSRC=7; //111 = Internal counter ends sampling and starts conversion (auto convert).
ADCON1bits.ASAM=1; //1 is for auto sample,0 Sampling begins when SAMP bit set (BIT 2)
ADCON1bits.SAMP=0; //ASAM = 0, writing ‘1’ to this bit will start sampling (BIT 1)
ADCON1bits.DONE=0; //Done Coversion (BIT 0)
ADCON1bits.ADON=1;
}
void Display(unsigned long int root1,unsigned long int root2,unsigned long int root3)
{
Mean_sqrt1 = sqrt(root1/Samples); //applying mean and square root to the result of value1
Mean_sqrt2 = sqrt(root2/Samples); //applying mean and square root to the result of value2
Mean_sqrt3 = sqrt(root3/Samples); //applying mean and square root to the result of value3
digi_result1 = ((float)Mean_sqrt1*Mul_fact_B);
digi_result2 = ((float)Mean_sqrt2*Mul_fact_Y);
digi_result3 = ((float)Mean_sqrt3*Mul_fact_R);
printf("\nBLUE = %.2f V \t", (digi_result1));
printf("\tYELLOW = %.2f V \t", (digi_result2));
printf("\tRED = %.2f V", (digi_result3));
digi_result1=0; digi_result2=0; digi_result3=0;
int main()
{
Adc_Init();
while(1)
{
while(!ADCON1bits.DONE);
buf_read1 = ((signed int)ADCBUF0 - DC_Offset1); //To get the exact DC
buf_read2 = ((signed int)ADCBUF1 - DC_Offset2); //To get the exact DC
buf_read3 = ((signed int)ADCBUF2 - DC_Offset3); //To get the exact DC
////////To get RMS (ROOT MEAN SQUARE)//////////////////////
root1 += pow(abs(buf_read1),2); //applying root to the buf0 value
root2 += pow(abs(buf_read2),2); //applying root to the buf1 value
root3 += pow(abs(buf_read3),2); //applying root to the buf2 value
j++;
if(j >(Samples-1))
{
Display(root1,root2,root3);
root1 =0,root2=0,root3=0;
j=0;
}
}
}