PDA

View Full Version : Help with using op amp as a sine-to-square converter!



achilles03
- 11th March 2009, 15:58
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

falingtrea
- 11th March 2009, 17:24
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.

Jerson
- 11th March 2009, 17:25
You need a resistor between Opamp output and inverting input to tame the offset drift getting amplified and saturating over time.

ErnieM
- 11th March 2009, 18:52
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.

ErnieM
- 12th March 2009, 13:19
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.

achilles03
- 12th March 2009, 16:06
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