QSpice weird behavior when outputting a sine wave from c++ to an Op-Amp

Thread Starter

YoucefH

Joined Sep 17, 2024
11
I am using this setup to convert a sine signal from the X component to a square wave signal using an op amp
1.png

And it works as expected
2.png

Next i made the amplitude of the signal increase over time like so:

2_2.png

when the output of X1 is not connected to the op amp the sine wave goes to about -1v to +1v peak to peak at the end of the simulation
2_3.png

but when connected, the output of the op amp is ok, but the signal voltage goes bellow -400v and over +400v peak to peak
2_1.png

it gets weirder when i make the amplitude of the sine wave increase over time to some limit and then decrease back to 0 over time
when X1 is not connected to the op amp, i get this signal from it
3_1.png

but when connected i get the following
3_2.png

Am i doing something wrong ? because i expect the output from the op amp to be similar to the third image.
QSpice build sep 13 2024
 

Thread Starter

YoucefH

Joined Sep 17, 2024
11
You are using the amplifier with a gain of 100,000. It is working right. Do you need help reducing the gain?
I just want a similar output to the first one where i get a square wave signal with 0 to 5 volts amplitude. plus why connecting X1 component to the op amp changes its output (sig from X1) ?.
is not limiting the gain causes this issue ?
my thought is that using the op amp as a comparator with full gain will just give me a square wave of 0 and 5 volts
 

Thread Starter

YoucefH

Joined Sep 17, 2024
11
Yes
-----------------------
Connect "-" to "out" to get a gain of 1.
You might need a -5V supply for the amp to work correctly.
View attachment 331863
thank you for replying
i have Tried it and still gives the same result, this is a link for the qspice file and the cpp source code if you want to try it your self, and if possible i really want to just stick to positive voltages and ground.
 

MisterBill2

Joined Jan 23, 2018
28,626
In the real world you will not get 600 volts out of an op-amp powered by +- 5 volts and a gain of 100,000. Simulators are not so limited, it appears.
 

Alec_t

Joined Sep 17, 2013
15,171
when X1 is not connected to the op amp, i get this signal from it
That suggests to me that Qspice assumes some leakage path between the components, or else has a bug.
What happens when X1 is not connected and both opamp inputs are grounded?
 

Thread Starter

YoucefH

Joined Sep 17, 2024
11
I don't have that SPICE. Please show your schematic.
In the real world you will not get 600 volts out of an op-amp powered by +- 5 volts and a gain of 100,000. Simulators are not so limited, it appears.
I am not getting 600 volts out of the op amp , i am getting that from the output of X1 when connected to the op amp (X1 works fine otherwise) with no feedback loop.
 

Thread Starter

YoucefH

Joined Sep 17, 2024
11
I don't have that SPICE. Please show your schematic.
I posted the schematic in the first image, i don't know how to post it otherwise. and i don't think the problem is in the c++ code either but here it is anyway:
C++:
#include <cmath>
bool maxed = false;
const double max_amp = 0.4;

const double rate = 0.001;

const double freq = 4000.0;
const double PI = 3.1415926535;
double amp = 0.03;

extern "C" __declspec(dllexport) void cpp_x1(void **opaque, double t, union uData *data)
{
   double &out = data[0].d; // output
// Implement module evaluation code here:
   out = std::sin(2 * t * PI * freq) * amp;
    
   if(amp >= max_amp){
      maxed = true;
   }
   if(!maxed){
      amp += rate;
   }else{
      amp -= rate;
   }
   //clamp amp at 0
   if(amp <= 0){
      amp = 0;
   }
}
 

Thread Starter

YoucefH

Joined Sep 17, 2024
11
That suggests to me that Qspice assumes some leakage path between the components, or else has a bug.
What happens when X1 is not connected and both opamp inputs are grounded?
This the output with X1 is not connected to anything and both of the op-amp 's inputs grounded and v+ connected to +5v and v- connected to ground
4_1.png
 

ronsimpson

Joined Oct 7, 2019
4,903
I don't have your SPICE so I can't see.....
The schematic in post #1 is not how you use an op-amp.
1---You should not have the signal at a voltage near the power supply voltage. You have a +5 and 0V supplies. The signal should be around 2.5V to work correctly. If your signal is around 0V then you need a +5V and -5V supplies.
2---You need feedback. Connect the (-) input to the output. Do not leave it on ground.
 

Thread Starter

YoucefH

Joined Sep 17, 2024
11
I don't have your SPICE so I can't see.....
The schematic in post #1 is not how you use an op-amp.
1---You should not have the signal at a voltage near the power supply voltage. You have a +5 and 0V supplies. The signal should be around 2.5V to work correctly. If your signal is around 0V then you need a +5V and -5V supplies.
2---You need feedback. Connect the (-) input to the output. Do not leave it on ground.
You already told me that in your second reply and i replied to you that i have tried it but the issue still persists. am i missing something ??
- i am trying to make X1 to emulate a signal that is no more than a 1v peak to peak. do i need to amplify it first or what before feeding it to the op-amp ?
- also i want to use the op-amp as a comparator with full gain, so i don't understand why do i have to use a feedback loop (although i tried it and it didn't work). basically i want a square wave with uniform amplitude 5v (or near 5v) and 0v (or near 0 volts) so whenever the signal from X1 goes above 0 volts the op-amp should output 5 volts (or close) and whenever it goes to 0 volts or bellow the op-amp should output 0 volts (or close).
 

Alec_t

Joined Sep 17, 2013
15,171
This the output with X1 is not connected to anything and both of the op-amp 's inputs grounded and v+ connected to +5v and v- connected to ground
400mV peak is suspiciously high to be due to leakage. Does Qspice have any settings for default leakage or conductance?
 

BobTPH

Joined Jun 5, 2013
11,678
I, at least, have no idea what the model for that black box that seems to run C code does. For one thing it has no connection to ground, that seems suspicious.
 

Thread Starter

YoucefH

Joined Sep 17, 2024
11
400mV peak is suspiciously high to be due to leakage. Does Qspice have any settings for default leakage or conductance?
That signal from X1 is intentional, it suppose to go from 3mv to 400mv and back to 0v with the same frequency at 4khz, what is not normal is the output of the op-amp when fed up with the signal from X1.
note: everything works totally fine when the signal has a fixed amplitude.

this is the code that generates that signal from X1:

C++:
#include <cmath>
bool maxed = false;
const double max_amp = 0.4;

const double rate = 0.001;

const double freq = 4000.0;
const double PI = 3.1415926535;
double amp = 0.03;

extern "C" __declspec(dllexport) void cpp_x1(void **opaque, double t, union uData *data)
{
   double &out = data[0].d; // output
// Implement module evaluation code here:
   out = std::sin(2 * t * PI * freq) * amp;
    
   if(amp >= max_amp){
      maxed = true;
   }
   if(!maxed){
      amp += rate;
   }else{
      amp -= rate;
   }
   //clamp amp at 0
   if(amp <= 0){
      amp = 0;
   }
}
 

Thread Starter

YoucefH

Joined Sep 17, 2024
11
I, at least, have no idea what the model for that black box that seems to run C code does. For one thing it has no connection to ground, that seems suspicious.
In Qspice you can create custom c++ (or verilog) components. there is an option to add a ground pin to X1 , i tried it but it has no effect (at least it didn't fix the issue).
the logic of X1 is fairly simple it just generates a signal that goes from 3ms to 400ms and back to 0v at a fixed frequency of 4khz.
this the code for it:
C++:
#include <cmath>
bool maxed = false;
const double max_amp = 0.4;

const double rate = 0.001;

const double freq = 4000.0;
const double PI = 3.1415926535;
double amp = 0.03;

extern "C" __declspec(dllexport) void cpp_x1(void **opaque, double t, union uData *data)
{
   double &out = data[0].d; // output
// Implement module evaluation code here:
   out = std::sin(2 * t * PI * freq) * amp;
    
   if(amp >= max_amp){
      maxed = true;
   }
   if(!maxed){
      amp += rate;
   }else{
      amp -= rate;
   }
   //clamp amp at 0
   if(amp <= 0){
      amp = 0;
   }
}
note: everything works just fine when the signal amplitude is fixed.
 
Top