Pulse

model Pulse "Digital Pulse Source"
    import D = Modelica.Electrical.Digital;
    import L = Modelica.Electrical.Digital.Interfaces.Logic;

    parameter Real width(final min = Modelica.Constants.small, final max = 100, start = 50) "Widths of pulses in % of periods";
    parameter SI.Time period(final min = Modelica.Constants.small, start = 1) "Time for one period";
    parameter SI.Time startTime(start = 0) "Output = quiet for time < startTime";
    parameter D.Interfaces.Logic pulse(start = L.'0') "pulsed value";
    parameter D.Interfaces.Logic quiet(start = L.'1') "quiet value";
    SI.Time T0(final start = startTime, fixed = true) "Start time of current period";
    parameter Integer nperiod(start = -1) "Number of periods (< 0 means infinite number of periods)";
    Integer np(start = 0, fixed = true);
    D.Interfaces.DigitalOutput y annotation (Placement(transformation(extent = {
        {90, -10}, 
        {110, 10}})));
protected
    Boolean sampling;
equation
    if sampling then 
        y = if time < startTime or T0 + 0.01 * (width * period) <= time then quiet else pulse;
    else 
        y = quiet;
    end if;
    when sampling and sample(startTime, period) then 
        T0 = time;
        np = if 0 < nperiod then pre(np) + 1 else pre(np);
    end when;
    sampling = nperiod <> 0 and (pre(np) <= nperiod or nperiod < 0);

    annotation (
        Documentation(
            info = "<html>\n<p>\nThe pulse source forms pulses between the  <em>quiet</em> value and the <em>pulse</em> value.\nThe pulse length <em>width</em> is specified in percent of the period length <em>period</em>.\nThe number of periods is specified by <em>nperiod</em>. If <em>nperiod</em> is less than zero,\nthe number of periods is unlimited.\n</p>\n<p>\nTo specify the logic value parameters, the integer code has to be used.\n</p>\n<p><strong>Code Table</strong></p>\n<table border=1 cellspacing=0 cellpadding=2>\n  <tr><td><strong>Logic value</strong></td>\n      <td><strong>Integer code</strong></td>\n      <td><strong>Meaning</strong></td>\n  </tr>\n\n  <tr><td>'U'</td> <td>1</td> <td>Uninitialized</td></tr>\n  <tr><td>'X'</td> <td>2</td> <td>Forcing Unknown</td></tr>\n  <tr><td>'0'</td> <td>3</td> <td>Forcing 0</td></tr>\n  <tr><td>'1'</td> <td>4</td> <td>Forcing 1</td></tr>\n  <tr><td>'Z'</td> <td>5</td> <td>High Impedance</td></tr>\n  <tr><td>'W'</td> <td>6</td> <td>Weak Unknown</td></tr>\n  <tr><td>'L'</td> <td>7</td> <td>Weak 0</td></tr>\n  <tr><td>'H'</td> <td>8</td> <td>Weak 1</td></tr>\n  <tr><td>'-'</td> <td>9</td> <td>Do not care</td></tr>\n</table>\n<p>\nIf the logic values are imported by<br><strong>import L = Digital.Interfaces.Logic;</strong><br>\nthey can be used to specify the parameter, e.g., <strong>L.'0'</strong> for forcing 0.\n</p>\n</html>",
            revisions = "<html>\n<dl>\n<dt><em>September 2, 2003</em></dt>\n<dd>by Christoph Clauss initially modelled.</dd>\n</dl>\n</html>"),
        Icon(
            coordinateSystem(
                preserveAspectRatio = true,
                extent = {
                    {-100, -100}, 
                    {100, 100}}),
            graphics = {
                Rectangle(
                    extent = {
                        {-50, 100}, 
                        {50, -100}},
                    lineThickness = 0.5,
                    fillColor = {213, 255, 170},
                    fillPattern = FillPattern.Solid), 
                Text(
                    extent = {
                        {152, -160}, 
                        {-148, -100}},
                    lineColor = {0, 0, 255},
                    textString = "%name"), 
                Line(
                    points = {
                        {50, 0}, 
                        {90, 0}},
                    color = {127, 0, 127}), 
                Line(points = {
                    {-36, 40}, 
                    {-30, 40}, 
                    {-30, 40}, 
                    {-18, 40}, 
                    {-18, 40}, 
                    {-14, 40}, 
                    {-14, 80}, 
                    {14, 80}, 
                    {14, 40}, 
                    {14, 40}, 
                    {14, 40}, 
                    {28, 40}, 
                    {30, 40}, 
                    {36, 40}})}));
end Pulse;