Skip to content

Instantly share code, notes, and snippets.

@wd5gnr
Last active June 11, 2021 17:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wd5gnr/0c699073c498ef80b3f0d6f3f1b98c5d to your computer and use it in GitHub Desktop.
Save wd5gnr/0c699073c498ef80b3f0d6f3f1b98c5d to your computer and use it in GitHub Desktop.
Falstad Simulator with Arduino A2D Example
#if 0 // simulation documentation
WARNING WARNING WARNING WARNING
Using any of the simulator's Save/Export functions will _NOT_
save this soure code! Before exiting the simulator, you must
save this text file somewhere.
Note that the schematic is encoded in text below. If you change the
schematic, you'll need to update the import text below to "save" it
with this file. However, the code doesn't care about that at all. Use
File | Export as Text... to regenerate the schematic text representation.
Steps for simulation:
1) Goto http://falstad.com/circuit/avr8js/
2) Paste this file into the right hand box
3) Copy everything between the lines of equal signs below.
Do not include the equal sign lines
4) From the simulator File menu, select Import from Text...
5) Paste the text into the import text box and import
6) Press the Run button over the source code;
not the Run button on the circuit diagram
================================
$ 1 0.000005 382.76258214399064 50 5 50 5e-11
418 240 256 112 256 0 1 40 5 0 0 0.5 pin\s9
r 336 256 400 256 0 680
c 400 256 400 320 0 0.000001 3.0448140406786646 0.001
a 448 240 576 240 8 15 -15 1000000 3.044783592842736 3.0448140406786646 100000
w 400 256 448 256 0
w 448 224 448 176 0
w 448 176 592 176 0
w 592 176 592 240 0
w 592 240 576 240 0
p 592 240 592 304 1 0 0
g 592 304 592 352 0 0
207 192 448 112 448 4 pin\s8
g 400 320 400 352 0 0
401 320 448 272 448 1 8\s15\s-15\s1000000\s3.044783592842736\s4.084\s100000 0\s20\s10000000000 0\s0
174 480 384 448 464 1 1000 0.18320000000000003 Analog Input
R 480 384 480 352 0 0 40 5 0 0 0.5
g 480 480 480 512 0 0
w 592 240 656 240 0
w 656 240 656 560 0
w 656 560 384 560 0
w 384 560 384 464 0
w 384 464 320 464 0
w 448 432 352 432 0
w 352 448 352 432 0
w 320 432 352 432 0
w 272 448 192 448 0
r 192 448 192 384 0 1000
R 192 384 192 352 0 0 40 5 0 0 0.5
368 352 400 352 352 0 0
207 208 528 112 528 4 A0
w 352 448 352 528 0
w 352 528 288 528 0
w 240 256 336 256 0
w 208 528 288 528 0
w 352 400 352 432 0
o 0 64 0 4098 5 0.0125 0 4 0 3 17 0 17 3
o 11 64 0 4099 5 0.00009765625 1 4 11 3 18 0 18 3
===========================
#endif
// I/O - the Comparator needs to be a "Labeled Node" (Outputs and Labels menu)
// the PWM output needs to be "External Voltage" (Inputs and Sources menu)
// We don't define A0 which is also a Labeled Node
#define COMPARATOR 8
#define PWMOUT 9
#define SETTLELOOPS 128
#define SAMPLOOP 5 // loops to wait after sample
#define convert convert0 // convert0 - simple, convert1 - binary search
void setup() {
Serial.begin(115200);
// add external voltage (Javascript) with name "pin n" to access output pins
// Labeled nodes work as inputs
pinMode(COMPARATOR, INPUT);
pinMode(PWMOUT, OUTPUT);
}
unsigned pwmval; // PWM duty cycle 0-255
unsigned loops = 0xFFFF; // # of PWM loops to settle before checking comparator
unsigned conversion; // result of conversion
int convert0() {
static unsigned v = 0; // test value
if (loops == 0xFFFF) // start conversion
{
loops = SETTLELOOPS; // # of loops before reading
pwmval = v = 0; // start at 0V
return 0;
}
if (loops == 0) // time for a reading?
{
if (digitalRead(COMPARATOR) == 1) // did we hit the voltage?
{
conversion = v; // remember result (really we are somewhere between v-1 and v)
loops = 0xFFFF; // stop measurement
pwmval = v = 0; // reset for next time
return 1; // signal loop that we have a valid answer
} else {
pwmval = ++v; // no match, try next value
loops = SETTLELOOPS;
return 0;
}
// never reached
return 0;
}
}
int convert1() {
static unsigned v = 0; // test value
static unsigned mask = 0x80;
if (loops == 0xFFFF) // start conversion
{
loops = SETTLELOOPS; // # of loops before reading
pwmval = v = mask = 0x80; // start at 1/2
return 0;
}
if (loops == 0) // time for a reading?
{
if (digitalRead(COMPARATOR) == 1) v &= ~mask; // clear this bit
mask >>= 1;
if (mask)
{
loops=SETTLELOOPS;
v|=mask;
pwmval=v;
return 0;
} else {
conversion = v;
loops = 0xFFFF;
pwmval = 0;
return 1;
}
}
// not reached
return 0;
}
void loop() {
static unsigned accum = 0;
static unsigned sampnow = 0; // zero means go now
int mode;
int v = 0;
if (sampnow == 0 && convert() == 1) {
// Conversion done
Serial.print(conversion);
Serial.print("-");
Serial.print(analogRead(0)>>2);
Serial.print(", ");
sampnow = SAMPLOOP;
}
// generate commanded PWM
accum += pwmval;
if (accum >= 0x100) v = 1;
accum &= 0XFF;
digitalWrite(PWMOUT, v);
if (loops != 0xFFFF) loops--;
if (sampnow) sampnow--;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment