Hot!RMS calculation problem

Author
KMDN
Super Member
  • Total Posts : 258
  • Reward points : 0
  • Joined: 2014/03/08 03:40:44
  • Location: 0
  • Status: offline
2018/03/13 10:59:30 (permalink)
0

RMS calculation problem

I am trying to read RMS value of a half wave signal using dspic30f3011. I am getting correct value at my LCD display, but problem is that It's taking too much time to update value I want to scan analog value at every 500 ms.
 
I am using 10 MHz crystal. 
 
 

unsigned char ADC_buffer [5];
unsigned long ADCValue;
unsigned int *ADC16Ptr;
float ADC_float = 0;
double ADC_average[];



// int ii ,jj , kk ;
static int ii = 0, jj = 0, kk = 0;


 void ADC_AN2(void)
{
float maxima[1];
float average[10];
float maxima_average;

ADPCFG = 0b0000000000000000; // all PORTB = Digital; RB2 = analog
ADCON1 = 0x00E0; // SSRC bit = 111 implies internal
// counter ends sampling and starts
// converting.
ADCHS = 0x0005; // Connect RB2/AN2 as CH0 input
// in this example RB2/AN2 is the input
ADCSSL = 0;
ADCON3 = 0x0203; // Sample time = 2Tad, Tad = 95.24 ns @ 21 MIPS
// which will give 1 / (14 * 95.24 ns) = 750 ksps
ADCON2 = 0b111111000000100; // Select external VREF+ and VREF- pins
// Interrupt after every 2 samples
ADCON1bits.ADON = 1; // turn ADC ON



ADCValue = 0; // clear value
ADC16Ptr = &ADCBUF0; // initialize ADCBUF pointer
IFS0bits.ADIF = 0; // clear ADC interrupt flag
ADCON1bits.ASAM = 1; // auto start sampling
// for 31Tad then go to conversion
while (!IFS0bits.ADIF); // conversion done?
ADCON1bits.ASAM = 0; // yes then stop sample/convert
//for (ii = 0; ii <15; ii++) // average the 2 ADC value
{
ADCValue = ADCValue+ *ADC16Ptr++;
}
//ADCValue = ADCValue >> 1;

ADC_float = (float)(ADCValue);

//i=0;

dis_cmd(0x80);
   sprintf(ADC_buffer,"%f",ADC_float);
for(ii=0;ii<3;ii++)
{
dis_data(ADC_buffer[ii]);


}
*/

// I1 SHUNT


 ADPCFG = 0xFFFB; // all PORTB = Digital; RB2 = analog
 ADCON1 = 0x00E0; // SSRC bit = 111 implies internal
// counter ends sampling and starts
// converting.
 ADCHS = 0x0002; // Connect RB2/AN2 as CH0 input ..
// in this example RB2/AN2 is the input
 ADCSSL = 0;
 ADCON3 = 0x0113; // Sample time = 15Tad, Tad = internal Tcy/2
 ADCON2 = 0x0004; // Interrupt after every 2 samples
 ADCON2bits.SMPI = 15;

//for(kk=0;kk<=10;kk++)
{
for(jj=0;jj<=10;jj++)
{
 ADCON1bits.ADON = 1; // turn ADC ON



ADCValue = 0; // clear value
ADC16Ptr = &ADCBUF0; // initialize ADCBUF pointer
IFS0bits.ADIF = 0; // clear ADC interrupt flag
 ADCON1bits.ASAM = 1; // auto start sampling
// for 31Tad then go to conversion
while (!IFS0bits.ADIF); // conversion done?
ADCON1bits.ASAM = 0; // yes then stop sample/convert
for (ii = 0; ii < 2; ii++) // average the 2 ADC value
ADCValue = ADCValue + *ADC16Ptr++;
ADCValue = ADCValue >> 1;

dis_cmd(0x80);

ADC_float = (float)((ADCValue)*(5.03/1023)*4);

maxima_average = maxima_average + ADC_float;
}
average[kk] = maxima_average/10;
average[kk] = average[kk]*average[kk];
kk++;
if(kk>=10)
{
kk=0;

for(kk=0;kk<=10;kk++)
average[0] = average[0] + average[kk];

average[0] = average[0]/10;

average[0] = sqrt(average[0]);


//average[kk] = (average[kk]/1.417);
sprintf(ADC_buffer,"%f",average[0]);
kk=0;
average[0]=0;
maxima_average = 0;
for(ii=0;ii<4;ii++)
{
dis_data(ADC_buffer[ii]);
//dis_cmd(0x01);
}


}
//if(average[0]<average[1])




////dis_cmd(0x01);


}

}

#1

4 Replies Related Threads

    rodims
    Super Member
    • Total Posts : 1292
    • Reward points : 0
    • Joined: 2009/02/10 11:08:59
    • Location: 51.9627, 7.6262
    • Status: offline
    Re: RMS calculation problem 2018/03/13 13:25:41 (permalink)
    5 (2)
    Since you do not provide enough information it is not possible to see whether your display hardware and interfacing  is just slow, or whether you have written ineffective code (update everything instead of only updating relevant positions)
     
    But let's assume it's just awfully slow and each call takes longer than 125 ms (total 500 ms, you call in in a loop 4 x times)
     
    I am getting correct value at my LCD display, but problem is that It's taking too much time to update value I want to scan analog value at every 500 ms.

     
    And what is your question here ? What has your data acquisition rate to do with your display speed ?
    Either reduce the amount of data which you display on your LCD, or lower the display update rate.
    Why do you want to update the display with 2 Hz rate ?
     
     
    #2
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 2586
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: RMS calculation problem 2018/03/13 14:47:41 (permalink)
    5 (1)
    You've got unnecessary delays in your lcd functions.

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #3
    KMDN
    Super Member
    • Total Posts : 258
    • Reward points : 0
    • Joined: 2014/03/08 03:40:44
    • Location: 0
    • Status: offline
    Re: RMS calculation problem 2018/03/13 18:59:11 (permalink)
    0
    Please find my LCD code.
     
    I do't think LCD delay will create problem, because data get updated after 4 to 5 min, my LCD is not that slow.
     
    after taking samples, I go for calculation of averaging, squaring, then again averring and then square root and after completing of all above cycle it start displaying a data, when LCD start displaying data it will update under 200 ms but it's take time 4 to 5 sec for updating new data. 
    #4
    rodims
    Super Member
    • Total Posts : 1292
    • Reward points : 0
    • Joined: 2009/02/10 11:08:59
    • Location: 51.9627, 7.6262
    • Status: offline
    Re: RMS calculation problem 2018/03/14 03:19:20 (permalink)
    0
     
    GortYou've got unnecessary delays in your lcd functions.

     
    Gort seems to be a clairvoyant :-)
     
    kmdnI do't think LCD delay will create problem, because data get updated after 4 to 5 min

    minutes ? or seconds ? Anyway, I don't understand why you think this would explain that it is NOT a problem. 
     
    For each and every character you add a delay of 10 ms.
    delay_ms ( 10 );
    If you multiply that be the number of characters which you output, your result is ?
    post edited by rodims - 2018/03/14 03:21:39
    #5
    Jump to:
    © 2018 APG vNext Commercial Version 4.5