8

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.

Brian Dohler
  • 651
  • 1
  • 4
  • 11
  • 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](https://electronics.stackexchange.com/questions/247396/ltspice-param-if-statement#comment552497_248891), it is possible to use a `.func` for that. – a concerned citizen Apr 13 '21 at 11:25

4 Answers4

9

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)

abeta201
  • 226
  • 2
  • 4
  • 2
    Brilliant! I’ve been using LTspice for 10 years and have never seen this before. – winny Apr 13 '21 at 11:50
  • 1
    Anyone looking at this answer and being excited will be interested in: http://ltwiki.org/?title=B_sources_%28complete_reference%29 – Brian Dohler Dec 09 '21 at 04:52
4

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

pipe
  • 13,748
  • 5
  • 42
  • 72
  • 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. – Brian Dohler Jul 22 '16 at 14:54
  • @BrianDohler Yeah, can't help with that I'm afraid, I hope you find a solution. It would be useful. – pipe Jul 22 '16 at 23:53
2

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.

Tox
  • 193
  • 1
  • 8
  • 1
    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. – pipe Jul 31 '16 at 08:46
  • 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 `.param`s, like this: `.param Vinitial = {if(MODE==2, 0, 1)}`. What might be important is that `.param`s in one text block are processed top to bottom, i.e. `MODE` needs to be defined before `Vinitial`. – Tox Jul 31 '16 at 12:33
  • Here's the [schematic](http://www.webcodesign.de/LTSpiceFunctionGenerator.png). – Tox Jul 31 '16 at 12:41
  • 1
    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. – pipe Jul 31 '16 at 14:24
  • Oh darn, I've expected that `time` will be parsed. Hmm, I guess not because it constantly changes but parameters are only evaluated once at the beginning of the simulation. I don't think it'll work but might be worth a try: store `time` in a parameter (`.param T time`) and then use `T` rather than `time`. – Tox Aug 01 '16 at 09:42
  • 1
    @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). – a concerned citizen Aug 04 '16 at 11:25
  • Thanks for the confirmation, so it is as I expected. :/ That's a shame. – Tox Aug 05 '16 at 22:24
0

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