Zero cross detect - switching audio


Closed Thread
Results 1 to 20 of 20

Hybrid View

  1. #1
    Join Date
    Mar 2009
    Posts
    653

    Default Zero cross detect - switching audio

    So I'm wanting to have an audio switch that only actually switches at a zero crossing point (or very near to zero)...this will only involve audio upto about 1.5khz in frequency.

    My thinking here is to use a PIC's comparator ....ie toggling when zero points are 'crossed' (this will be a single supply circuit, 'zero' will actually be about 1/2 VCC), but how do I implement the associated code?

    At a high level, I reckon it'd be something along these lines...

    1. A switch is pressed
    2. wait until the comparator's output logic level is *not* what it was at the time of the switch being pressed (ie a zero cross has happened)
    3. raise the necessay control voltage to invoke the actual audio switch.

    .....now steps 1 & 3 I'm ok with, but how in code would I do step 2? Upon the switch being pressed, the comparator's output could either be high or low (it'd be random), I want to 'trap' quickly the opposite condition of what it is at the time of the switch being pressed.

    Ideas?
    Last edited by HankMcSpank; - 30th October 2011 at 12:37.

  2. #2
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default Re: Zero cross detect - switching audio

    All of the PIC inputs have protection diodes so all you need is a current limiting resistor. Look at the Microchip AN236 (p3& p20) which uses just a 5meg resistor to get ZC from the 120VAC powerline.

  3. #3
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    967


    Did you find this post helpful? Yes | No

    Default Re: Zero cross detect - switching audio

    Hi Hank

    I think you can get the comparator to interrupt you at every zero crossing. That is the easy part.

    What I suggest is setup the framework to get the zero crossing detector (ZCD) interrupts when the comparator threshold is breached.

    1 - enable the ZCD interrupts only when the switch is pressed.
    2 - On interrupt, do your audio switching thing which will be within a fraction of a mS away from the Zero crossing. That depends on whether you use the leading edge or trailing edge for trigger; disable the interrupt again so you have just 1 event per switch press
    3 - wait for switch release

    Hope that helps
    Jerson

  4. #4
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Zero cross detect - switching audio

    Hi Jerson,

    I anticipate the actual switch involved being pressed very rapidly (think 'teen on an arcade button'!) ...I'd be worried about the overhead of getting in/out of any interrupt *after* trapping the switch being pressed, so I was thinking of an IOC interrupt for capturing the switch being pressed & then somehow trapping the next comparator toggle in the IOC interrupt routine.

    At the minute the best I can come up with, is something along these (high level) lines...

    Code:
    Sw_Interrupt: 'IOC interrupt handler
    '
    stored_comparator_level = comparator_output_level   ' store the present comparator logic level nb: I've not delved to get the actual registers involved, this is high level stuff!
    '
    while comparator_output_level = stored_comparator_level  'keep looping until there's a mismatch
    wend   
    '
    'ok, to drop out of above wend and get to this point, then there must be a mismatch (a zero cross - comparator toggle) so let's continue  
    '                                                                   
    PortA.1 = 1 ' switch PortA.1 high immediately after comparator has toggled (ie the control voltage to the audio switch)
    ...blah blah
    Not even sure if you can use a while/wend like that?

    PS Dave - you've lost me...this has nothing to do with AC mains?! (I'm talking low level audio zero cross detect)
    Last edited by HankMcSpank; - 30th October 2011 at 14:44.

  5. #5
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: Zero cross detect - switching audio

    Hank that looks like good logic to me, but to make sure I get where you are heading -
    on switch press, call ISR,
    Inside ISR, wait for the signal to cross zero (in either direction, max time here would be .3333mS?)
    Crossed so do what needs to be done and leave.

    Yes you can use while/end like that.
    Don't forget to set/clear the IOC caller so as not to re-trigger as soon as you leave.
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  6. #6
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Zero cross detect - switching audio

    Quote Originally Posted by cncmachineguy View Post
    Inside ISR, wait for the signal to cross zero (in either direction, max time here would be .3333mS?)
    Hi Bert...thanks for the confirmation - yes, you understand what I'm trying to do ...what I would add at this point is the full sequence of events really needs to be like this...

    1. Switch gets pressed
    2. IOC interrrupt routine Entered

    hang around at this point, until....


    3. comparator toggles (zero cross) -> Audio switch control voltage get's 'enabled' as quickly as possible thereafter.

    hang around at this point, until....

    4.Switch gets released (finger removed!)
    5. comparator toggles (another zero cross)-> Audio switch control voltage get's 'disabled' as quickly as possible thereafter.
    6. exit the IOC switch interrupt routine.

    where did your .333mS number come from?

    I don't need to trap the initial switch press too quick (anything sub 4mS will likely be ok), but from the comparator flipping state to the audio switch control voltage changing needs to be as blisteringly fast as I can make it (as obviously, the longer this bit takes the 'further' the AC signal is going to be away from 'zero') ..... 333us seems like a long time!
    Last edited by HankMcSpank; - 30th October 2011 at 15:22.

  7. #7
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    967


    Did you find this post helpful? Yes | No

    Default Re: Zero cross detect - switching audio

    I don't think you can check comparator levels; that would involve reading analog levels(ADC). What you can do is set the comparator threshold. Then the comparator tells you 'Above' or "Below" threshold by its output. This is what you use to trigger the comparator interrupt.

    That's the reason I suggested you turn on the ZCD interrupt on switch press. Now, the very first int after switch press will be captured. You may need to clear the int flag before enabling the CMP interrupt so that you do not process the previous INT which has been flagged.

    So, the pseudo code would look like

    Code:
    SW_Interrupt:
        clear CMP interrupt pending flag
        enable the CMP interrupt
        clear SW interrupt flag(maybe an int on change)
        EXIT INT
    
    CMPINT:
        Do audio switch(maybe a toggle of the port involved)
        EXIT INT

  8. #8
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Zero cross detect - switching audio

    Quote Originally Posted by Jerson View Post
    I don't think you can check comparator levels; that would involve reading analog levels(ADC). What you can do is set the comparator threshold. Then the comparator tells you 'Above' or "Below" threshold by its output. This is what you use to trigger the comparator interrupt.
    [/code]
    But the comparator is only analogue on the input side (where I will preset the comparator threshold to be my circuit's ZC point ....about 1/2 VCC) ...it's output is digital & can therefore be read (I think), this from the associated PIC's datasheet (16f1828)...


    18.2.2 COMPARATOR OUTPUT SELECTION
    The output of the comparator can be monitored by reading either the CxOUT bit of the CMxCON0 register or the MCxOUT bit of the CMOUT register.



    Sound reasonable?




    Last edited by HankMcSpank; - 31st October 2011 at 09:51.

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