Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Measure the time difference between rising edges of two different square waves PIC16F877A

Status
Not open for further replies.
I am trying to measure the time difference between the rising or the falling edge of two square waves, that i get from the Zero Crossing Detectors.
I am trying to measure phase difference between Current and Voltages of an A.C. Circuit. Getting two inputs from Voltage and Potential transformers.
Someone told me to use CCP to measure the time but that can measure time difference between the events from input on single pin.
But I have two inputs from C.T. and P.T.
I have to measure the power factor of A.C. Circuit, but not finding a proper way to do that.
I tried this but not getting the correct output

void main() {
//Let the Module start up
Wait(500);
LCDInit(LS_CURSOR_OFF);
LCDClear();
LCDClear();
///******************** PORT Initilization **********************************
PORTD = 0X00;
TRISD = 0X00;
while (1) {
time=0;
LCDWriteStringXY(0,2,"Frequency:=");
if(PORTCbits.RC6 == 1)
{

while(PORTCbits.RC7 != 1){
time = time+1;
Delay_us(1);
RD0 = ~RD0; // Toggle RA0 pin
}
LCDWriteIntXY(6,0,cos(time*0.9),3);
}
Delay_ms(50);
/*OPTION_REG = 0b00101000; // TOCS=1 for Counter mode, PSA=1 for 1:1 prescaler
// Display Frequency
TMR0=0;
Delay_ms(1000);
LCDWriteStringXY(0,0,"Frequency:=");
LCDWriteIntXY(12,0,TMR0,2);
LCDWriteStringXY(14,0,"Hz");
////////////////////////////////////*/

}
}
 
Last edited:
Can't you use logic to bring both waves into the same pin... Then you can use the CCP module.

Or!! If you use the PORTB interrupt on change you can read one change, Then the other.

If you use the pic18f4520 ( pin compatible ) you can use int0 and int1.

I've just realized this has been posted in the wrong place...
 
If i some how manage to bring them to one pin i wont be able to identify the rising edge of either wave separately. I thought there may be some way CCP can be used. Otherwise i have plan that in ISR i would start timer and wait until i get rising edge on some pin and measure time
 
hi,
Use RB0 with INT and RB4 with interrupt on change, use the INT flags to test the interrupting source.
E

EDIT:
Corrected RB1 to RB4
 
I don't know why I assumed a pic16f877a!!!! EDIT.... oops.... it was in the title of the thread...

Even so !!
C:
while(PORTCbits.RC7 != 1){
   time = time+1;
   Delay_us(1);
   RD0 = ~RD0; // Toggle RA0 pin
}
The delay will not be correct.... The while statement will take over 7 clock cycles... If time is an int... another 4.. then the toggle... So realistically much more than a uS... What chip and crystal are you using?
 
I have options if you want to suggest but i can go for 16f877a with 8 MHZ or 18f452 with 8 mhz. I can change crystal frequency no big deal
 
Your best way is to poll a pin (square wave 1) once triggered just run the timer at 1:1 until the int0 (second square wave) and read the timer.... There you will have @4Mhz timer * 1uS and with 8Mhz run the timer at 1:2... and you will have the same timings...
 
I am trying to measure the time difference between the rising or the falling edge of two square waves, that i get from the Zero Crossing Detectors.
I am trying to measure phase difference between Current and Voltages of an A.C. Circuit. Getting two inputs from Voltage and Potential transformers.

I don't know if this helps or not, but I'm working on a project where I have to measure the time between pulses. I'm using separate pins, but there is nothing that would keep it from working on the same pin. I'm using the change interrupt to record a timer value when the pin goes low (in my case). It will work with a pin going high too.

So, I set up a 32 bit timer running at 4 MHz. Every time I get an interrupt, I save the timer value. Of course you have to correct for timer rollover, but that's easy to do.

Brad
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top