Help with using op amp as a sine-to-square converter!


Closed Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2009
    Location
    Huntsville, AL
    Posts
    32

    Default Help with using op amp as a sine-to-square converter!

    I'm developing an audio frequency radio modem, and have a problem I was hoping some of the gurus here could help me with. I greatly appreciate any help that can be provided!

    In short, the modem will be used across a wide temperature range. To prevent oscillator drift, I've include the ability for it to 'tune' itself. The sine wave output can't be used with the count function (it doesn't reach the threshold voltage required for a high-to-low transition). So to calibrate the frequency, an op amp is fed the sine wave in one input and a 2.5V reference is fed into the other input to construct a square wave. The "count" function is used on the square wav. The audio frequencies I'm sampling are between 1.5 and 3 kHz. I'm using a 16F628a with a 20MHz xtal. 'Tuning' is implemented by adjustment of digital pots that act as trimmers in the oscillator.

    Attached is a basic schematic of how I'm implementing it.

    It starts off working great, but after a few minutes, sometimes it starts returning count values of 0. My questions are: have I correctly implemented the sine-to-square wav converter for use with the 16F628a? Is porta.2 have some quirks that make it bad for use with the count function? Are the resistance values I'm using on either side of the op amp satisfactory?

    Any help is greatly appreciated!

    Thanks in advance!
    Dave
    Attached Images Attached Images  

  2. #2


    Did you find this post helpful? Yes | No

    Default

    The first thing I would do is check your input sine wave and make sure it always exceeds the 2.5V ref level with enough margin for the op-amp. That would be the first thing to suspect when doing this kind of conversion. Another thing to check is to make sure your sine wave offset level does not drift over time. Also check the response of the op-amp over your frequency range.
    Tim Barr

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


    Did you find this post helpful? Yes | No

    Default

    You need a resistor between Opamp output and inverting input to tame the offset drift getting amplified and saturating over time.

  4. #4
    Join Date
    Jul 2005
    Posts
    78


    Did you find this post helpful? Yes | No

    Default

    Well, since I speak both analog and digital as my dual native languages....

    The circuit you propose looks OK on the surface. You are using an op amp in a comparator mode, and as your inputs are greatly larger then any offset voltages it should be stable, so no other parts needed. A brief look at the data sheet make it look like a good choice (my real concern would be are you paying too much for it, you don't need a very refined pricey amp here).

    I am assuming you do not have an oscilloscope to check things (or you would have mentioned what you've seen). I would be interested in how sure you are of the sine wave input, if the DC level changes from what you think it is then you would see the symptom you describe.

    One thing to try is to make the sine wave make it's own DC reference: on the left side of your schematic you have a brutally locked in DC divider (10uF is huge for that app). But given your 10K R4 and your 10uF C3 then toss out R7/R5 and replace V1/+5V with the sine wave: this is then a low pass filter that gives you the DC level of the sine wave, and will track slow changes too (it has a cut-off of about 16 Hz).

    You don't show any power supply bypassing, be liberal with some 0.1uF ceramic caps and try to sprinkle one on every component IC, especially the PIC and the op amp.

  5. #5
    Join Date
    Jul 2005
    Posts
    78


    Did you find this post helpful? Yes | No

    Default

    One thing to keep in mind with the revision I described is it is incredibly sensitive to noise if the signal goes away. It will chatter at some high frequency, so you either need to code against that or somehow just know when no signal is there so you ignore the input.

    Or... you could DC couple the sine signal to the comparator but use the comparator as an amplifier. Feed the amp output direct to a digital input. That should chop the sine wave into digital form while also driving any noise problems away (you either get a square wave or nothing out of it). Input R of 1K, input C of .01uf and a 10K feedback R should get you there with a gain of 10. Increase the feedback if your output signal isn't enough. (Ask me if you need this drawn up.)

    A quick check of the 16F628a data sheet shows RA2 is also AN2, so make sure you shut off the comparator function (the default) so it works as a digital input.

    I've used schemes such as this in a few products to measure a frequency. What I did there was use the signal to increment one of the counters while another timer would be set up a roll-over interrupt off the internal PIC clock. To start, I would clear both counters and enable the interrupt. When the interrupt occurs read how many counts I got in the first counter and reset it. That way every interrupt time I get a frequency measurement of cycles (counts) per unit time.

  6. #6
    Join Date
    Jan 2009
    Location
    Huntsville, AL
    Posts
    32


    Did you find this post helpful? Yes | No

    Default

    Thanks for all the good comments and suggestions!

    The oscillator is a wein-bridge oscillator, and DC drift could be the cause of it. I'll be hooking up an oscilloscope to check this weekend. Using the sine wave as it's own DC reference may be the solution.

    I'll be trying that this weekend and I'll post the results.

    Again, thanks for the suggestions!
    Dave

Similar Threads

  1. To Generate Sine Wave Using PCPWM of PIC18F4331 (Issue)
    By Cyborg in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 22nd March 2014, 13:39
  2. Serout2/serin2 Pbp Problem
    By SOMRU in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 11th December 2006, 19:55
  3. square to sine wave help
    By TONIGALEA in forum General
    Replies: 6
    Last Post: - 2nd July 2006, 09:32

Members who have read this thread : 1

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