* Z:\home\khalils\Downloads\files\data\raw_asc\355ef60b4f4b_Digital_PWM1.asc
* Generated by LTspice 26.0.1 for Windows.
L1 N004 N005 120m Rser=20
M1 N001 N003 N004 N004 Si4850EY
V1 period 0 PULSE(0 1 0 10n 10n {MIN_ON} {PERIOD})
V2 N001 0 14
R2 N002 N003 50
D2 0 N004 MURS320
Rsense N005 0 0.5
E1 N002 N004 pwm 0 5
A1 period pwmres 0 0 0 0 pwm 0 SRFLOP td=10n
B2 Icoil 0 V=V(Vsense)*2
B3 error 0 V=V(target)-V(Icoil)
B5 pid 0 V={K1}*V(error) + V(i_error)
B6 pid_clamp 0 V=limit({MIN_DUTY}, V(pid), {MAX_DUTY})
V3 target 0 PULSE(0.2 0.4 0 10n 20n 40m 80m)
B7 pwmres 0 V=delay(V(period),V(pwm_val)*{PERIOD})
R1 Vsense N005 10k
C1 Vsense 0 100n
B1 i_error 0 V=idt({K2}*V(elim)/{PERIOD})
S1 error elim N008 0 SW1
R3 elim 0 1k
B9 N006 0 V=IF( (V(i_error)>{INT_MIN} & V(i_error) < {INT_MAX}) , 1, 0)
B8 N007 0 V=IF( (V(i_error)<={INT_MIN} & V(error) > 0 ), 1, 0)
B10 N009 0 V=IF( (V(i_error)>={INT_MAX} & V(error) < 0) , 1, 0)
A2 N009 0 N007 0 N006 0 N008 0 OR
B4 pwm_val 0 V=int(V(pid_clamp)*PWM_RES+0.5)/PWM_RES
.model D D
.lib C:\users\khalils\AppData\Local\LTspice\lib\cmp\standard.dio
.model NMOS NMOS
.model PMOS PMOS
.lib C:\users\khalils\AppData\Local\LTspice\lib\cmp\standard.mos
.tran 0 80m 0 {MIN_TSTEP}
* Pseudo Code of Software Control Loop\n// Interrupt loop \n \n// ADC \nicoil = 2* Vsense\n// Error \nerror = Itarget - Icoil\n// Integrator \ni_error = i_error+k2*error\ni_error = limit(-1, i_error, 1)\n// PI(D) sum \npid = k1 * error + i_error\n// Clamping \npid_clamp = limit(min, pid, max)\n// PWM quantization \npwm = int(pid_clamp*PWM_RES+0.5)\n// Write to PWM
.param PERIOD=100u
.param MIN_DUTY=0.01
.param MAX_DUTY=1-2*MIN_DUTY
.param MIN_ON=MIN_DUTY*PERIOD
.param PWM_RES=256
.param MIN_TSTEP=0.2*PERIOD*MIN(MIN_DUTY,1/PWM_RES)
* PI(D) parameters
.param K1=25
.param K2=0.5
.param INT_MIN=-0.1
.param INT_MAX=1
* Limiter:\n0...1 = 0...100% duty cycle
* RC-Filter
* Integral
* Proportional
.MODEL SW1 SW(Ron=1m, Roff=1G Vt=0.5 Vh=0)
* Digital PI(D) Controller with digital PWM\n \nHelmut Sennewald 4/18/2004
* Digital PWM inside uC
* Sum of Control
* Integrator with Limiter
* Quantization
* Target current of coil
* Watch V(Icoil) and V(pwm_val)\n \nThe used integrator is clamped to INT_MIN and INT_MAX.
.backanno
.end

* LTspice schematic (.asc):
Version 4
SHEET 1 1232 1612
WIRE 112 272 112 176
WIRE 112 -48 112 -112
WIRE 112 -112 336 -112
WIRE 336 -112 336 -80
WIRE 336 0 336 32
WIRE 64 32 -48 32
WIRE -1088 80 -1088 192
WIRE -1088 320 -1088 272
WIRE 112 176 112 48
WIRE -128 32 -256 32
WIRE 112 352 112 416
WIRE 352 560 112 560
WIRE 112 560 112 576
WIRE 112 176 352 176
WIRE 352 176 352 320
WIRE 112 528 112 560
WIRE 352 384 352 560
WIRE 112 416 48 416
WIRE 112 416 112 448
WIRE -256 176 112 176
WIRE -256 64 -256 32
WIRE -256 144 -256 176
WIRE -304 128 -304 160
WIRE -304 80 -448 80
WIRE -1088 80 -1056 80
WIRE -1056 80 -720 80
WIRE -448 80 -624 80
WIRE -720 128 -848 128
WIRE -1104 800 -1104 752
WIRE -1104 752 -1024 752
WIRE -1104 880 -1104 912
WIRE -800 800 -800 752
WIRE -800 752 -736 752
WIRE -800 880 -800 912
WIRE -1120 1136 -1120 1168
WIRE -1120 1056 -1120 1008
WIRE -1120 1008 -1056 1008
WIRE -1120 1408 -1120 1440
WIRE -1120 1328 -1120 1280
WIRE -1120 1280 -1040 1280
WIRE -1104 464 -1104 512
WIRE -1104 624 -1104 592
WIRE -1104 464 -1072 464
WIRE -896 256 -896 320
WIRE -896 176 -896 128
WIRE -848 128 -896 128
WIRE -32 416 -80 416
WIRE -80 464 -80 416
WIRE -80 416 -112 416
WIRE -80 528 -80 576
WIRE 128 880 128 832
WIRE 128 832 192 832
WIRE 128 960 128 992
WIRE -80 864 -80 832
WIRE -80 832 -192 832
WIRE -80 944 -80 992
WIRE -208 880 -208 1120
WIRE -256 880 -256 912
WIRE -272 832 -336 832
WIRE -336 832 -368 832
WIRE -80 832 -32 832
WIRE -32 832 16 832
WIRE 368 1184 368 1216
WIRE 224 1280 224 1312
WIRE 64 1360 64 1392
WIRE -128 1120 -208 1120
WIRE -64 1072 368 1072
WIRE -64 1104 224 1104
WIRE 224 1104 224 1200
WIRE 64 1280 64 1136
WIRE 64 1136 -64 1136
WIRE 368 1072 368 1104
WIRE -624 1392 -624 1440
WIRE -624 1312 -624 1264
WIRE -624 1264 -560 1264
FLAG 336 32 0
FLAG 112 576 0
FLAG -112 416 Vsense
IOPIN -112 416 Out
FLAG -1056 80 period
FLAG -304 160 0
FLAG -448 80 pwm
FLAG -1088 320 0
FLAG -1104 912 0
FLAG -1024 752 Icoil
IOPIN -1024 752 Out
FLAG -800 912 0
FLAG -736 752 error
IOPIN -736 752 Out
FLAG -1120 1168 0
FLAG -1056 1008 pid
IOPIN -1056 1008 Out
FLAG -1120 1440 0
FLAG -1040 1280 pid_clamp
IOPIN -1040 1280 Out
FLAG -1072 464 target
IOPIN -1072 464 Out
FLAG -1104 624 0
FLAG -896 320 0
FLAG -848 128 pwmres
FLAG -80 576 0
FLAG 128 992 0
FLAG 192 832 i_error
IOPIN 192 832 Out
FLAG -80 992 0
FLAG -256 912 0
FLAG -336 832 error
FLAG -32 832 elim
FLAG 368 1216 0
FLAG 224 1312 0
FLAG 64 1392 0
FLAG -624 1440 0
FLAG -560 1264 pwm_val
IOPIN -560 1264 Out
SYMBOL ind2 96 256 R0
WINDOW 39 36 108 Left 0
SYMATTR SpiceLine Rser=20
SYMATTR InstName L1
SYMATTR Value 120m
SYMATTR Type ind
SYMBOL nmos 64 -48 R0
SYMATTR InstName M1
SYMATTR Value Si4850EY
SYMBOL voltage -1088 176 R0
WINDOW 3 -28 188 Left 0
WINDOW 123 0 0 Left 0
WINDOW 39 0 0 Left 0
SYMATTR Value PULSE(0 1 0 10n 10n {MIN_ON} {PERIOD})
SYMATTR InstName V1
SYMBOL voltage 336 -96 R0
SYMATTR InstName V2
SYMATTR Value 14
SYMBOL res -144 48 R270
WINDOW 0 32 56 VTop 0
WINDOW 3 0 56 VBottom 0
SYMATTR InstName R2
SYMATTR Value 50
SYMBOL schottky 368 384 R180
WINDOW 0 24 72 Left 0
WINDOW 3 24 0 Left 0
SYMATTR InstName D2
SYMATTR Value MURS320
SYMATTR Description Diode
SYMATTR Type diode
SYMBOL res 96 432 R0
SYMATTR InstName Rsense
SYMATTR Value 0.5
SYMBOL e -256 48 R0
SYMATTR InstName E1
SYMATTR Value 5
SYMBOL Digital\\srflop -672 32 R0
WINDOW 3 -40 28 Left 0
SYMATTR Value td=10n
SYMATTR InstName A1
SYMBOL bv -1104 784 R0
SYMATTR InstName B2
SYMATTR Value V=V(Vsense)*2
SYMBOL bv -800 784 R0
SYMATTR InstName B3
SYMATTR Value V=V(target)-V(Icoil)
SYMBOL bv -1120 1040 R0
SYMATTR InstName B5
SYMATTR Value V={K1}*V(error) + V(i_error)
SYMBOL bv -1120 1312 R0
SYMATTR InstName B6
SYMATTR Value V=limit({MIN_DUTY}, V(pid), {MAX_DUTY})
SYMBOL voltage -1104 496 R0
WINDOW 3 25 91 Left 0
WINDOW 123 0 0 Left 0
WINDOW 39 0 0 Left 0
SYMATTR Value PULSE(0.2 0.4 0 10n 20n 40m 80m)
SYMATTR InstName V3
SYMBOL bv -896 160 R0
SYMATTR InstName B7
SYMATTR Value V=delay(V(period),V(pwm_val)*{PERIOD})
SYMBOL res -48 432 R270
WINDOW 0 32 56 VTop 0
WINDOW 3 0 56 VBottom 0
SYMATTR InstName R1
SYMATTR Value 10k
SYMBOL cap -96 464 R0
SYMATTR InstName C1
SYMATTR Value 100n
SYMBOL bv 128 864 R0
SYMATTR InstName B1
SYMATTR Value V=idt({K2}*V(elim)/{PERIOD})
SYMBOL sw -288 832 R270
SYMATTR InstName S1
SYMATTR Value SW1
SYMBOL res -96 848 R0
SYMATTR InstName R3
SYMATTR Value 1k
SYMBOL bv 368 1088 R0
SYMATTR InstName B9
SYMATTR Value V=IF( (V(i_error)>{INT_MIN} & V(i_error) < {INT_MAX}) , 1, 0)
SYMBOL bv 224 1184 R0
SYMATTR InstName B8
SYMATTR Value V=IF( (V(i_error)<={INT_MIN} & V(error) > 0 ), 1, 0)
SYMBOL bv 64 1264 R0
SYMATTR InstName B10
SYMATTR Value V=IF( (V(i_error)>={INT_MAX} & V(error) < 0) , 1, 0)
SYMBOL Digital\\or -96 1168 R180
SYMATTR InstName A2
SYMBOL bv -624 1296 R0
SYMATTR InstName B4
SYMATTR Value V=int(V(pid_clamp)*PWM_RES+0.5)/PWM_RES
TEXT -1120 -400 Left 0 !.tran 0 80m 0 {MIN_TSTEP}
TEXT -336 -592 Left 0 ;Pseudo Code of Software Control Loop\n// Interrupt loop \n \n// ADC \nicoil = 2* Vsense\n// Error \nerror = Itarget - Icoil\n// Integrator \ni_error = i_error+k2*error\ni_error = limit(-1, i_error, 1)\n// PI(D)  sum \npid = k1 * error + i_error\n// Clamping \npid_clamp = limit(min, pid, max)\n// PWM quantization \npwm = int(pid_clamp*PWM_RES+0.5)\n// Write to PWM
TEXT -1128 -360 Left 0 !.param PERIOD=100u\n.param MIN_DUTY=0.01\n.param MAX_DUTY=1-2*MIN_DUTY\n.param MIN_ON=MIN_DUTY*PERIOD\n.param PWM_RES=256\n.param MIN_TSTEP=0.2*PERIOD*MIN(MIN_DUTY,1/PWM_RES)\n \n* PI(D) parameters\n.param K1=25\n.param K2=0.5\n.param INT_MIN=-0.1\n.param INT_MAX=1
TEXT -1008 1336 Left 0 ;Limiter:\n0...1 = 0...100% duty cycle
TEXT -72 344 Left 0 ;RC-Filter
TEXT 208 888 Left 0 ;Integral
TEXT -712 824 Left 0 ;Proportional
TEXT -320 736 Left 0 !.MODEL SW1 SW(Ron=1m, Roff=1G Vt=0.5 Vh=0)
TEXT -1104 -624 Left 0 ;Digital PI(D) Controller with digital PWM\n \nHelmut Sennewald    4/18/2004
TEXT -1008 16 Left 0 ;Digital PWM inside uC
TEXT -984 1080 Left 0 ;Sum of Control
TEXT 528 736 Left 0 ;Integrator with Limiter
TEXT -560 1320 Left 0 ;Quantization
TEXT -1024 520 Left 0 ;Target current of coil
TEXT -1104 -504 Left 0 ;Watch V(Icoil) and V(pwm_val)\n \nThe used integrator is clamped to INT_MIN and INT_MAX.
RECTANGLE Normal -368 400 -1168 -16 2
RECTANGLE Normal 1024 1440 -400 688 2