I want to make a square wave generator with an LM311.
I have a variable sine wave source from 1Hz to 16kHz. The amplitude can vary between 0.5V to 11.4V.
I tried to use the comparator circuit below but couldn't get an acceptable result.
Sine wave configuration:
Blue is the output of the comparator, yellow is the sine wave:
Edit: After I added the pull-up resistor and made the supply bipolar (+12/-12V) I get the result below which is quite fine but has little distortions such as spikes. Peak values are on the other hand are not even near to the desired output. The wave oscillates between -11V and 750mV
Edit2: In real circuit I get the result below. What should I do to flatten the wave? Why is this happening?