CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

help regarding RMS measurement using 10 bit ADC

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
pad



Joined: 29 Nov 2007
Posts: 15

View user's profile Send private message

help regarding RMS measurement using 10 bit ADC
PostPosted: Mon Jul 12, 2010 11:19 am     Reply with quote

hi friends,
I am working with a program which measures three phase current (2.5V DC shift). I am getting accuracy of 5% to 7% with internal 8 bit ADC. If I try to work with 10 bit ADC, get an low RAM error as I make all 16bit signed variables to 32bit.

Controller - PIC 16F877A
CCS 4.020

Please look at the code,any suggestion/changes in existing code is helpful.

Code:

#include <16F877A.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES HS                       //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES NOWRT                    //Program memory not write protected

#use delay(clock=20000000)

#include <2by16_lcd_driver.c>
#include <math.h>
#include <stdlib.h>
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7,bits=8,parity=N)      //ERRORS,
#define ac_offset 87.52         //19.522 //0.02188//0.01953  5.5/256*4000 CTR 2000/0.5V ie 4000
#define dc_offset 128
//#define CTR 400

void adc_sample(void);
//void print_sample(void);
void RMS_R(void);
void RMS_Y(void);
void RMS_B(void);

signed int16 adc_R[32]  = {0};//adc sample input of R phase cap current.
signed int16 adc_Y[32]  = {0};//adc sample input of Y phase cap current.
signed int16 adc_B[32]  = {0};//adc sample input of B phase cap current.

unsigned int8 i = 0;
static float r_rms = 0;
static float y_rms = 0;
static float b_rms = 0;

////////////////////////////////////////////////////////////////////////////////
void adc_sample(void)
{
    output_high(PIN_B7);
    for(i=0;i<32;i++)                  // Start taking Samples after detecting rising edge of ZCD
    {
      set_adc_channel(0);              // IR, R phase current
      delay_us(20);
      adc_R[i] = read_adc();
      delay_us(10);
     
      set_adc_channel(1);              // IY, Y phase current
      delay_us(20);
      adc_Y[i] = read_adc();
      delay_us(10);
     
      set_adc_channel(2);              // IB, B phase current
      delay_us(20);
      adc_B[i] = read_adc();
      delay_us(10);
      delay_us(420);  /// this time gives exact 20 ms with 32 samples.
    } 
   output_low(PIN_B7);
}

////////////////////////////////////////////////////////////////////////////////

void RMS_R(void)
{
 signed int16 r_abs[32] =  {0};//to calculate absolute values
 // float r_abs[32] =  {0};//to calculate absolute values
  unsigned int32 sum = 0;
 // static float avg_rms = 0;
  for(i=0;i<32;i++)
  {
   r_abs[i] = abs(adc_R[i]-dc_offset);
   sum = sum + r_abs[i];
  }
  r_rms = (float)sum /32;
  r_rms *= ac_offset;
  r_rms += r_rms;
}

void RMS_Y(void)
{
  signed int16 y_abs[32] =  {0};//to calculate absolute values
  unsigned int32 sum = 0;
 // static float avg_rms = 0;
  for(i=0;i<32;i++)
  {
   y_abs[i] = abs(adc_Y[i]-dc_offset);
   sum = sum + y_abs[i];
  }
  y_rms = (float)sum /32;
  y_rms *= ac_offset;
  y_rms += y_rms;
}

void RMS_B(void)
{
  signed int16 b_abs[32] =  {0};//to calculate absolute values
  unsigned int32 sum = 0;
 // static float avg_rms = 0;
  for(i=0;i<32;i++)
  {
   b_abs[i] = abs(adc_B[i]-dc_offset);
   sum = sum + b_abs[i];
  }
  b_rms = (float)sum /32;
  b_rms *= ac_offset;
  b_rms += b_rms;
}

////////////////////////////////////////////////////////////////////////////////

void main()
{
   setup_adc_ports(AN0_AN1_AN2_AN3_AN4_AN5);
   setup_adc(ADC_CLOCK_INTERNAL);   //ADC_OFF
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   output_low(PIN_B7);
   lcd_init();
   delay_ms(5);
   lcd_init();
   delay_ms(5);
   
   lcd_gotoxy(1,1);                    //1st -position  1st -line
   lcd_putc(" CAPACITOR ");
   delay_ms(2);                        //delay_ms(5);
   lcd_gotoxy(1,2);                    //6th -position  2nd -line
   delay_ms(2);
   lcd_putc(" CURRENT");
   delay_ms(2);
   delay_ms(100);
  lcd_gotoxy(1,1);                             //x adress y line
  delay_ms(1);
  printf(lcd_putc, "                     ");   //clear LCD
  lcd_gotoxy(1,2);                             //x adress y line
  delay_ms(1);
  printf(lcd_putc,"                      ");    //clear LCD
  delay_ms(1);
  delay_ms(20);
   
   while(TRUE)
  {
    for(i=0;i<2;i++)
   {
    adc_sample();             // TAKE ADC SAMPLES AND COMPARE WITH REFERENCE HIGH AND LOW SAMPLES
     RMS_R();
     RMS_Y();
     RMS_B();
   } 
 
  lcd_gotoxy(1,1);                             //x adress y line
  delay_ms(1);
  printf(lcd_putc,"R%3.1f A,Y%3.1f A",r_rms/2.0,y_rms/2.0);  // divide by no.of adc cycle AVG
  delay_ms(1);
  lcd_gotoxy(1,2);                             //x adress y line
  delay_ms(1);
  printf(lcd_putc,"B%3.1f A",b_rms/2.0);   
  delay_ms(5);

    delay_ms(1500);
   
  } 
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jul 12, 2010 11:29 am     Reply with quote

Quote:
I get a low RAM error as I make all 16bit signed variables to 32bit.

Add the line shown in bold below, to tell the compiler to use all RAM
in the 16F-series PICs:
Quote:

#include <16F877A.h>
#device *=16
#device adc=8
pad



Joined: 29 Nov 2007
Posts: 15

View user's profile Send private message

PostPosted: Thu Jul 15, 2010 4:55 am     Reply with quote

Thanks for reply.
As I use 16F877A, low memory problem is there to remain. But is there any other way I can write the code so that I can take 3*32 samples of 10 bits and work around?
With 8 bit program its working nicely but with 10 bit ADC will get more accurate results.

Regards,
PAD
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group