Measuring audio phase shift through a circuit with a PIC


Closed Thread
Results 1 to 40 of 50

Hybrid View

  1. #1
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Jerson View Post
    Ok, in the case of rollover, let us assume the first reading was 65500. Assume a phase shift of 500 counts. The new value is 66000. After rounding to 16 bits, you get 464 (66000-65536). So, current reading 464 - previous reading 65500 will give you your phase counts as 464-65500 = 01F4 in 16 bits (use the F5/F6 buttons on calc to see the Hex/Dec values). So, for an error of 1 count, you still get the same phase diff.
    But how do you know the timer has rolled over or not? (ie using you analagy how are you counting laps & intra laps)

    For example, the frequency might change & therefore you get a a first reading count of 460 (vs 65500 in your example). So how do you know if a reading of 464 relates to the frequency changing massively, or is a result of the timer rolling over? (surely you have to track overflows too? I tried this, but the PIC baulked when I programmed it with my code! Too many interrupts arriving close together possibly?)

  2. #2
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    970


    Did you find this post helpful? Yes | No

    Default

    Yes, you need to track overflows in your code - too low frequency. Of course, the same may not be true for the phase. Tracking the overflow needs to be done. I did not have to use such a thing since my frequency in was fixed at 50Hz. However, I can propose, you could use the timer start/stop method and track the timer overflow to indicate a very low frequency or too large values of time coming in.

    Possible implementation
    Comp1 int
    if T1 has overflowed, you have yourself an error condition here. Freq Too Low
    FreqCounts = T1
    T1 = 0

    Comp2 int
    PhaseCounts = T1

    Again, the 'stick in the mud' is T1 being set to 0. But, here, you get your freqcounts and phasecounts directly.!!!

    The time from one zero crossing (T1=0) to the next zero crossing on the same Comparator is the frequency counts. Read this, check for overflow and Set T1 = 0.

    The time from the zero crossing of Comp1 to the zero crossing of Comp2 is the phase counts.

    Oh, and you need to run your PIC at 20MHz just so you have more time in your ISRs. A helpful suggestion would be to write your ISR in ASM no matter how difficult it seems; PBP adds overhead to the code you write and its better to exit the ISR as quickly as you can to let other blocks of code to run.
    Last edited by Jerson; - 13th September 2010 at 02:58.

  3. #3
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Thanks Jerson....I'll study that later (it's 2.15am here in Lodnon!)

    Actually, this looks like it might have some signs of doing what I want...

    Code:
    'Comparator1 Interrupt handler+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++        
    Comp1_Int:
    @ INT_DISABLE  CMP1_INT
            Comp1Time.Lowbyte = TMR1L 'puts the timer's low byte in MyTime's lower 8 bits 
            Comp1Time.Highbyte = TMR1H 'puts the timer's high byte in MyTime's upper 8 bits
            TMR1H = 0 'Set the high part of the timer value to 0
            TMR1L = 0 'Set the low part of the timer value to 0
    @ INT_ENABLE  CMP2_INT
            'Comp1Time =Comp1Time +4 
            'dummy = b * c       '5,000,000 
            'frequency = div32 Comp1time
            'frequency =frequency/2
            'HSEROUT ["mytime = ", dec mytime,9, dec frequency/10,".",dec frequency//10,"Hz",13, 10]
              
    @ INT_RETURN
    
    'Comparator2 Interrupt handler+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    Comp2_Int:
    @ INT_DISABLE  CMP2_INT
            Comp2Time.Lowbyte = TMR1L 'puts the timer's low byte in MyTime's lower 8 bits 
            Comp2Time.Highbyte = TMR1H 'puts the timer's high byte in MyTime's upper 8 bits
            TMR1H = 0 'Set the high part of the timer value to 0
            TMR1L = 0 'Set the low part of the timer value to 0  
    @ INT_ENABLE  CMP1_INT   
    @ INT_RETURN
    What's getting me bogged down is something as simple as setting my config correct for a 20Mhz external resonator. (the first time I've used an external clock)

    I'm putting a 1Khz test signal into comparator1 input pin- the handler it's registering 2500 timer1 'counts' between successive compartor1 interrupts - that doesn't seem correct for a 20Mhz external clock?

    at 1Khz, the actual physical time between interrupts should be 1ms - & I'm seeiing a count of 2500 for comparator 1 interrupts.....

    therefore 1ms divided by 2500 is 0.0000004 seconds ....or in frequency is 2500000Hz (2.5Mhz - that's not 20Mhz???).

    here's my settings...

    Code:
    @ __CONFIG _FCMEN_OFF & _HS_OSC & _WDT_OFF & _MCLRE_OFF & _CP_OFF & _IESO_OFF & _BOR_OFF & _PWRTE_OFF
    
    DEFINE OSC 20        ' set Oscillator at 20Mhz.
    OSCCON.0 = 0
    what am I setting wrong? (I'm using MPASM assembler)

  4. #4
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    970


    Did you find this post helpful? Yes | No

    Default

    Hi Hank

    Is it possible you got your T1 prescaler(T1CON) set to 1:8 ? That will explain the counts you see. 20e6 / 8 / 1000 = 2500 counts. You haven't mentioned which pic you use.

    I've never used a resonator on my circuits, but, you could try with HS / XT options. Most likely the XT might work.

    Regards

  5. #5
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Jerson View Post
    Hi Hank

    Is it possible you got your T1 prescaler(T1CON) set to 1:8 ? That will explain the counts you see. 20e6 / 8 / 1000 = 2500 counts. You haven't mentioned which pic you use.

    I've never used a resonator on my circuits, but, you could try with HS / XT options. Most likely the XT might work.

    Regards
    Thanks for bearing with me....and keep those ideas coming! (to my knowledge there are no prescalers set anyhere in my code)

    Ok, I think I know (in part) what the problem is....& I throw myself open to public ridicule.

    My signal path is this...

    Signal 1 -> Audio preamp->schmitt trigger-> comparator1 input pin

    Signal2 -> Audio preamp-> all pass filter-> schmitt trigger-> comparator2 input pin

    When dealing with phase shift - it's easier on the eye to work with sine waves on a scope (for me at least)....*but*, doh, I was aligning aformetnioned 'zer degrees phase shift' in sine wave form as viewed on the scope. Alas, it appears that becuase each of the signals is of different amplitude going into the schmitt tirrger, the schmitt triggers are erhm triggering at slightly different times wrt to one another - which translates itself into phase shift! (now I'm pretty sure I was checking at the input to the pic comparators the other night, so I'm bemused how this has only come to light)

    I therefore need to modify my circuit to ensure that the signals feeding into the shcmitt are of exactly the same amplitude (preferable amplified to nice sharp edged square wavea) ....this should ensure they trigger at the same point

    Ok, something else I've established tonight ....if I feed signal1 into both schmitt triggers & ultimately into both comparators (so, absolutely no phase shift in play whatsoever then!)....I get a constant count of 29....this stays at 29 no matter what the input frequency - would it therefore be safe to assume that that's how many clock cycles it takes for all of the interrupt routines, storing the counts etc to work?


    So therefore my new plan of attack is.....rejig my 'feed' circuitry to ensure an identical signal feeds amplitude into both schmitt triggers....and then also subtract 29 from the comp2 count. (at my upper frequency of 1.5Khz...I get a comp1 count of about 3000...therefore 1%, hich in phase terms is an error of about 3.6 degrees - not ideal. )

    Would using any other form of interrupts/hardware get this 'floor' count of 29 down? (faster processor, higher frequency oscillator - 40Mhz?)
    Last edited by HankMcSpank; - 16th September 2010 at 01:07.

  6. #6
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Ok, so I impemented my plan (square up the source signa, make them the same amplitude ....then subtract 29 from the final comp2 count).

    Works...

    (green trace is comp1 input, yellow trace is the phase lagged signal into comp2)








    Thanks for all your help........"I have exorcised the demons this code is clear!"
    Last edited by HankMcSpank; - 16th September 2010 at 23:17.

  7. #7
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240


    Did you find this post helpful? Yes | No

    Default

    Excellent Job. Congratulations!!!
    Thanks and Regards;
    Gadelhas

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts