Is possible to emulate an ADC in C++, with a sampling timer and a buffer for using the samples in a buffer?
I would assume so, based on the work others have demonstrated here on the forum. I’ll leave it there and hope others contribute a more useful answer.
Seems almost certainly possible. Post a part number for a chip that is similar to what you want and I’ll give it some thought.
Just an idea to implement textbook type waveform. Input allows 0V to FullScale, convert to output 0-255 (unsigned integer), and trigger by rising edge of clk.
Looking good. Things to consider:
Some (most?) ADC’s have an external reference voltage – sometimes VCC, often a separate reference pin. Maybe replace the FS param/attribute with an input port/reference voltage.
It would be possible for the input voltage being measured to be greater than the reference voltage or less than 0V. Maybe clip the min/max digitized value to 0-255.
I use FS parameter because I don’t want an extra voltage source in schematic, but you are correct that practically reference is generally an input pin. For min and max, the code included if… elseif… to prevent overflow.
I am only entry level C++ user and only try to demonstrate Qspice is possible for his request. May be after giordano gives more specify of what he is looking for, C++ expert in this community can give him a much better code.
The reason I asked is because if we emulate an ADC in C++, we could emulate a S-domain plant or a discret plant and a digital control loop in C++. In this way we could finding the correct parameters for the control loop in simulation. It certainly makes easy to test the plant and the digital control loop we could use in a DSP. We could verify stability or change discret components in the plant.
I implemented a digital controller using the C block. I used int variable to emulate the quantization effect, and the ADC gain with the behavioral voltage source. You can use a simple edge detection as an input to the C block to emulate the control fw, and the sample and hold effect (or use external delay function).
It worked for me.
You can have your plant in S-domain, a digital control loop with C++, run transient (.tran) or frequency response analysis (.bode). Mike Engelhardt provided every tool you need in Qspice.
You can check the following youtube channel:
Good information. Is it your site?
No, not mine but I like how the ADC is implemented