karan123
Member level 3
Hello All,
I am working on Project of Measurement of Power factor with PIC18F4520 MCU with External
Crystal Osc 4 MHz and MPLAB XC8. [Capture mode, every rising edge]
My Voltage ZCD and Current ZCD are working fine as checked on CRO.
Can any body give me given how to convert
Time value to cos (phi) in C. I am doing as below:
I am working on Project of Measurement of Power factor with PIC18F4520 MCU with External
Crystal Osc 4 MHz and MPLAB XC8. [Capture mode, every rising edge]
My Voltage ZCD and Current ZCD are working fine as checked on CRO.
Can any body give me given how to convert
Time value to cos (phi) in C. I am doing as below:
Code:
void Capture_init()
{
/*
CCPxM3:CCPxM0
[B]0101= Capture mode, every rising edge[/B]
*/
CCP1CON=0x05;
CCP2CON=0x05;
/*
bit 5-4 T1CKPS1:T1CKPS0:Timer1 Input Clock Prescale Select bits
[B] 11= 1:8 Prescale value[/B]
bit 2 T1SYNC:Timer1 External Clock Input Synchronization Select bit
[B] When TMR1CS = 1:[/B]
*/
T1CON=0x34;
TMR1H=0;
TMR1L=0;
while(1)
{
for(i=0;i<10;i++)
{
CCP2IF=0;
while(CCP2IF==0);
TMR1ON=1;
CCP1IF=0;
while(CCP1IF==0);
TMR1ON=0;
time=CCPR1H;
time=(time<<8)+CCPR1L;
avg=avg+time;
}
avg=avg/10;
cmd(0xC0) ;
printf("%d",avg); // Display
[B] // Clear Variable to Next Round[/B]
TMR1H=0;
TMR1L=0;
TMR1IF=0;avg=0;
}
}