Anda di halaman 1dari 3

include <16F877A.

h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz fo
r 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 20
00/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 r
ising 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 HIG
H 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);
}
}

Anda mungkin juga menyukai