/*******************************************************************************
Program for Power Factor measurement
Written by_ Engr. Mithun K. Das
E-mail: mithun060@gmail.com
MCU: PIC16F877A X-Tal: 8MHz
mikroC_v.6.6.2
*******************************************************************************/
unsigned char ch;
unsigned int adc_rd0;
unsigned int temp0=0,temp1=0;
unsigned max_point0 = 0;
unsigned int i,tlong0,tlong1;
// LCD module connections
sbit LCD_RS at RC2_bit;
sbit LCD_EN at RC3_bit;
sbit LCD_D4 at RC4_bit;
sbit LCD_D5 at RC5_bit;
sbit LCD_D6 at RC6_bit;
sbit LCD_D7 at RC7_bit;
sbit LCD_RS_Direction at TRISC2_bit;
sbit LCD_EN_Direction at TRISC3_bit;
sbit LCD_D4_Direction at TRISC4_bit;
sbit LCD_D5_Direction at TRISC5_bit;
sbit LCD_D6_Direction at TRISC6_bit;
sbit LCD_D7_Direction at TRISC7_bit;
// End LCD module connections
char message1[] = "PK:000V";
char message2[] = "RMS:000V";
char *freq = "000";
void Display_Freq(unsigned int freq2write)
{
freq[0] = (freq2write/100) + 48; // Extract tens digit
freq[1] = (freq2write/10)%10 + 48; // Extract tens digit
freq[2] = (freq2write/1)%10 + 48; // Extract ones digit
// Display Frequency on LCD
Lcd_Out(2, 11, freq);
Lcd_Out(2,14,"Hz");
}
void main()
{
TRISA = 0xFF;
TRISC = 0x00;
PORTC = 0x00;
PORTA = 0x00;
ADCON1=0x00;
Delay_ms(1000);
OPTION_REG = 0b00101000;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,"Measurement of");
Lcd_Out(2,1,"AC parameter ");
Delay_ms(2000);
Lcd_Cmd(_LCD_CLEAR);
while(1)
{
max_point0 = 0;
for(i=0;i<1000;i++)
{
ADCON0 = 0b00000001;
if(temp0 = ADC_Read(0),temp0>max_point0) //find the peak point
{
max_point0 = temp0;
}
}
max_point0 = abs(ceil((long)max_point0));
tlong0 = (long)max_point0*3.9543; // Voltage = Transformer ratio * 5/255 * voltage divider ratio
tlong1 = (long)max_point0*2.7961; // Voltage = 0.707 * Transformer ratio * 5/255 * voltage divider ratio
message1[3] = tlong0/100 + 48;
message1[4] = (tlong0/10)%10 +48;
message1[5] = (tlong0)%10 +48;
Lcd_Out(1,1,message1);
message2[4] = tlong1/100 + 48;
message2[5] = (tlong1/10)%10 +48;
message2[6] = (tlong1)%10 +48;
Lcd_Out(1,9,message2);
TMR0=0;
Delay_ms(985); // Delay 1 Sec
Lcd_Out(2,1,"FREQUENCY:");
Display_Freq(TMR0);
TMR0=0;
}// while
}// void main
//