Hi B O E,
Thank you for your reply! I have implemented the concept you have mentioned, however, it doesn't fulfil the purpose.
I would explain the concept and the code I have written underneath:
--------------------------------------------------------------------------------
---------------------
The purpose of the block is:
1. To change the state to 0 or 1 if the input is detected to be vthmax (Maximum threshold) or vthmin (Minimum threshold) at rising and falling edges respectively.
2. In case, the input reaches the maximum of minimum thresholds too FAST, it should wait for a minimum time "Tmin" before changing the respective states.
3. In case, the input reaches the maximum and minimum thresholds too SLOW, the states should be changed after a time "Tmax" after the last crossing event.
In order to fulfil the above conditions, I have written this code:--------------------------------------------------------------------------------
---------------------------
Code:`include "constants.vams"
`include "disciplines.vams"
module autoswitch(in, out);
input in;
output out;
voltage in, out;
parameter real tmin = 20u; //Minimum time before the state changes its
transition
parameter real tmax = 20u;
parameter real tt = 1n; //Output transition time
parameter real startval = 0; //Low Voltage level
parameter real endval = 2.5; //High Voltage level
parameter real vthmin = 1.182m from (inf :inf); //Min. voltage from input should be closed [V]
parameter real vthmax = 97.32m from (inf:inf); //Max. voltage from input after which system should be open [V]
real vout, tstartrise, tstoprise, tstartfall, tstopfall, crossrise, crossfall;
integer state, state_d, crossing;
analog begin
@(initial_step) begin
vout = 0;
state = 0;
state_d = 0;
tstoprise = 0;
tstartrise = 0;
tstartfall = 0;
tstopfall = 0;
crossing = 0;
crossrise = 0;
crossfall = 0;
end
state_d = transition(state, tmin, 1n); // Generating a replica of the output signal delayed by tmin units for comparison of thresholds
@(cross(V(in) vthmin, 1)) begin
$strobe("Falling edge detected. Change state to 1. \n abs_time=%g", $abstime);
crossrise = $abstime;
if (state_d == 1) begin //Change the state
crossing = 1;
vout = endval;
state = 1;
$strobe("Falling edge detected. Change state to 1. \n abs_time=%g",$abstime);
tstartrise = $abstime;
tstoprise = tstartrise +tmax;
end
else begin
if (state_d == 0) begin //Resume state
crossing = 0;
vout = startval;
state = 0;
$strobe("Resume low state");
end
end
end
@(timer(tstoprise)) begin //At "tstoprise" after every falling edge event, if threshold is less than vthmin, resume state else change the state
tstoprise = tstartfall;
if (V(in) > vthmin) begin
crossing = 1;
vout = startval;
state = 0;
end
end
@(cross(V(in) vthmax, 1)) begin
$strobe("Rising edge detected. Change state to 1. \n abs_time=%g", $abstime);
crossfall = $abstime;
if (state_d == 0) begin
crossing = 1;
state = 0;
vout = startval;
$strobe("Rising edge detected. Change state to 0. \n abs_time=%g", $abstime);
tstartfall = $abstime;
tstopfall = tstartfall +tmax;
end
else begin
if (state_d == 1) begin
crossing = 0;
vout = endval;
state = 1;
$strobe("Resume high state");
end
end
end
@(timer(tstopfall)) begin
tstopfall = tstartrise;
if (V(in) < vthmax) begin
crossing = 1;
vout = endval;
state = 1;
end
end
V(out) <+ transition(vout, 5n, 1n);
end
endmodule
--------------------------------------------------------------------------------
-------------------------------
I would be grateful to know if the above conditions would be fulfilled by the following code.
Kind Regards,
Omnidroid