8
\$\begingroup\$

Super simple question here. Is it possible to use an if statement in a .param LTSpice directive? If so, what is the syntax? Shown below does not function.

enter image description here

Note that the .step function can not be used for my purposes, to the best of my knowledge, because I want to simulate a step response over time.

\$\endgroup\$
1
  • \$\begingroup\$ If it's about using an if() inside a .param, yes, it's possible. If it's about using the keyword time inside a .param, no. As I said back then, it is possible to use a .func for that. \$\endgroup\$ Apr 13, 2021 at 11:25

4 Answers 4

11
\$\begingroup\$

For those of you coming across this question several years late, it looks like this is indeed actually possible if instead of .param you use the .func directive.

Just do .func R(t) if(t>0.5,1,2) and R = R(time). LTSpice XVII Setup

And it works! Transient Simulation Results (Tested in LTSpice XVII)

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Brilliant! I’ve been using LTspice for 10 years and have never seen this before. \$\endgroup\$
    – winny
    Apr 13, 2021 at 11:50
  • 1
    \$\begingroup\$ Anyone looking at this answer and being excited will be interested in: ltwiki.org/?title=B_sources_%28complete_reference%29 \$\endgroup\$ Dec 9, 2021 at 4:52
  • \$\begingroup\$ If you have defined tolerance or power rating, LTSpice will complain about "unknown paramter "tol/pwr"". Just delete the value and that should fix it. Looks like a bug in field separation in GUI (groups.io/g/LTspice/topic/50267146 for reference) \$\endgroup\$
    – stiebrs
    Jan 24 at 17:40
4
\$\begingroup\$

From what I understand, the .PARAM directive is evaluated at startup. The answer to your literal question is thus no.

You can, however, put the equation as the resistance:

enter image description here

\$\endgroup\$
2
  • \$\begingroup\$ Thanks @pipe. This is the answer I feared. What if I'm evaluating eight other parameters based on my first? I'm actually trying to simulate the step response from a brushless DC motor, and only used this simple example to illustrate my question. I suppose I could calculate the results outside of LTSpice and put if statements in the model where appropriate. \$\endgroup\$ Jul 22, 2016 at 14:54
  • \$\begingroup\$ @BrianDohler Yeah, can't help with that I'm afraid, I hope you find a solution. It would be useful. \$\endgroup\$
    – pipe
    Jul 22, 2016 at 23:53
3
\$\begingroup\$

You can, put it in curly braces like so: .param R = {if(time>0.5,1,2)} and set the value of R1 to {R}.

You can also make more complex statements like this:

.param MODE 1 .param FREQ {if(MODE == 0, 1000, if(MODE == 1, 500, if(MODE == 2,250)))}.

This will select a FREQ based on the given MODE.

\$\endgroup\$
7
  • 1
    \$\begingroup\$ Did you try it? This does not work in my LTspice, but perhaps it's a new feature. I have't upgraded in a year. \$\endgroup\$
    – pipe
    Jul 31, 2016 at 8:46
  • \$\begingroup\$ I used it in LTSpice IV and LTSpice XVII to simulate a function generator. My voltage source is controlled like this: PULSE({Vinitial} {Von} [...] {Tperiod}) where each parameter is defined in a block of .params, like this: .param Vinitial = {if(MODE==2, 0, 1)}. What might be important is that .params in one text block are processed top to bottom, i.e. MODE needs to be defined before Vinitial. \$\endgroup\$
    – Tox
    Jul 31, 2016 at 12:33
  • \$\begingroup\$ Here's the schematic. \$\endgroup\$
    – Tox
    Jul 31, 2016 at 12:41
  • 1
    \$\begingroup\$ But you don't use IF(time>0.5,1,2) anywhere. time is a special variable, and the problem here is that time is not evaluated in a .PARAM statement. OP wants to have a time-dependent resistance without embedding it into the actual resistor. \$\endgroup\$
    – pipe
    Jul 31, 2016 at 14:24
  • 1
    \$\begingroup\$ @Tox You can forget about using time in a .param statement. time cannot be "stored" anywhere, since it's continuously changing. It can be used in .func declarations, though, directly or indirectly. What you meant with R=function(time) only applies to behavioural resistors, and thus it's an expression to be used as is inside the Value field (of the appropiate element). \$\endgroup\$ Aug 4, 2016 at 11:25
0
\$\begingroup\$

R=table(time,0.5,2,0.501.1)

R=table(time,t1,R1,t2,R2)

from 0 to t1 R value = R1; from t1 to t2 R changes lineally from R1 to R2, then R value will remain equal to R2 till the end

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.