PDA

View Full Version : Phase angle differences



fanman
- 18th June 2011, 13:07
Hi guys,

How can I measure the phase angle difference between a sine wave and a triggered pulse?

I thought about doing it like this

Loop:
t_sine = time between sine wave peaks.
t_pulse = time pulse is detected
phase angle = 360 x (t_pulse / t_sine)
reset time when phase angle calculated
Goto Loop

I'm not sure how can I measure the times accurately? The frequency of the sine wave is in the order of 50Hz. Is there a better way of doing this?

thanks

M.

HankMcSpank
- 18th June 2011, 15:38
There are a few ways to skin that cat, first one I thought of....

Arrange it so your sine so it 'sits' on exactly half your PIC's VCC.

You can now detect zero (either in software or with a PIC comparator)......this will yield you the frequency (ie start/stop a timer when the sine crosses zero).

Now all you need to do is tuck away the same timer value when the pulse arrives ..and apply your formula.

BobEdge
- 19th June 2011, 03:20
Hi,
I have just done something very similar using DT's interrupt routines. When I get back to work I will post the code.

Regards
Bob...

fanman
- 19th June 2011, 07:03
Hi,
I have just done something very similar using DT's interrupt routines. When I get back to work I will post the code.

Regards
Bob...

Bob that would be great look forward to seeing how this can be done.

cheers

mark

BobEdge
- 24th June 2011, 11:57
Hi Fanman,

Here is a little code that uses an interrupt to start a timer and measure the period of a sine wave. It is written for a PIC18F1220.



define OSC 20
'channel 0 analog all others digital
TRISA = 255
ADCON0 = %00000001 'Vref = Vss Vdd, turn on A/D
ADCON1 = %11111110 'Only Channel 0 analog
ADCON2 = %10001010 'Right justified, 2 TAD, TOSC/32 (for 20MHz)

INCLUDE "DT_INTS-18.bas" ' Base Interrupt System
INCLUDE "ReEnterPBP-18.bas" ' Include if using PBP interrupts

badweld var portb.3
prim var portb.0
prim1 var portb.1
bren var portb.2
thy3 var portb.6
thy2 var portb.5
thy1 var portb.4

input badweld
input prim
input bren
high thy3
high thy2
high thy1
input prim1

angletime var word
temp var word
period var word
period1 var word
period2 var word
period3 var word
period4 var word
sixtydeg var word
thirtydeg var word
multiplier var word
decimal1 var word
decimal2 var word
adresult var word
x var byte 'for next loop variable
rot var bit 'rotation 1 = forward 0 = reverse
zeroflag var bit 'flag set to 1 at zero crossing


pause 1000 'give time for supply to settle
gosub rotation

'set up timer
T0CON = %00000000 '1/2 prescale fosc/4 not running
TMR0H = 0 ;Clear registers
TMR0L = 0


ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler INT0_INT, _ZERO, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM

@ INT_ENABLE INT0_INT ; enable external (INT) interrupts

goto cycle
'-------Interrupt Service Routine------------------
ZERO:
@ bcf T0CON,TMR0ON ; Switch off timer
period.lowbyte = TMR0L 'Record Result
period.highbyte = TMR0H
@ clrf TMR0H ; Reset registers to zero
@ clrf TMR0L
@ bsf T0CON,TMR0ON ;Start timer
period1 = period2 'Move values up the stack
period2 = period3
period3 = period4
period4 = (period / 5) 'Put new reading at bottom of stack
period = (period1 + period2 + period3 + period4) / 2 'scale period value in micro seconds
sixtydeg = period / 6
thirtydeg = sixtydeg / 2
multiplier = (period / 4) / 859
decimal1 = (period / 4) // 859 DIg 0
decimal2 = (period / 4) // 859 dig 1
zeroflag = 1 'Set zero crossing flag


You could do something similar, but you would start 2 timers. One to measure the period of the sinewave, and another to time from the start of the sine wave till your other pulse. Then calculating the phase angle would be quite easy. If you use the "PIC Timer Calculator" this makes using the timers fairly easy.

I would suggest using the interrupt for timing the period, and starting both timers, and just use PBP to stop the second timer, and do the maths etc.

The input is a H11L1 opto isolator, resistor, and diode in series across the mains. I am measuring 3 phase at 415 volts, I use 33K 14W resistor. The input goes high roughly 500uS before actual zero crossing, but this can be compensated for in the maths.

Bob...

BobEdge
- 24th June 2011, 15:08
By the way here is a link to DT's interrupts page: http://darreltaylor.com/DT_INTS-18/home.html That is for PIC18, PIC16 devices can be found through a link at the top left of the page.

and here is the pic timer calculator : http://www.mikroe.com/forum/viewtopic.php?f=10&t=15663&start=15

Well worth a look, makes using interrupts very easy, and the pic timer calculator opend up a whole new world for me. I was afraid to try either before I found out about these two things.

Regards
Bob

HankMcSpank
- 25th June 2011, 11:50
Here's a related thread of mine where I shamelessly display my woeful grasp of' all things pic', but a great group effort at steering me to the required outcome...

http://www.picbasic.co.uk/forum/archive/index.php/t-13690.html