Clock

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

    parameter SI.Time startTime(start = 0) "Output = offset for time < startTime";
    parameter SI.Time period(final min = Modelica.Constants.small, start = 1) "Time for one period";
    parameter Real width(final min = Modelica.Constants.small, final max = 100, start = 50) "Width of pulses in % of period";
    D.Interfaces.DigitalOutput y "Connector of Digital output signal"
        annotation (Placement(transformation(extent = {
            {90, -10}, 
            {110, 10}})));
protected
    SI.Time t_i(final start = startTime, fixed = true) "Start time of current period";
    SI.Time t_width = 0.01 * (period * width);
equation
    when sample(startTime, period) then 
        t_i = time;
    end when;
    y = if not startTime <= time or t_i + t_width <= time then L.'0' else L.'1';

    annotation (
        Documentation(
            info = "<html>\n<p>\nThe clock source forms pulses between the  <em>'0'</em> value (forcing 0) and the <em>'1'</em> value (forcing 1).\nThe pulse length <em>width</em> is specified in percent of the period length <em>period</em>.\nThe number of periods is unlimited. The first pulse starts at startTime.\n</p>\n<p> The clock source is a special but often used variant of the pulse source.\n</p>\n</html>",
            revisions = "<html>\n<dl>\n<dt><em>September 18, 2004</em></dt>\n<dd>by Andre Schneider 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, 80}, 
                    {-18, 80}, 
                    {-18, 40}, 
                    {-6, 40}, 
                    {-6, 80}, 
                    {6, 80}, 
                    {6, 40}, 
                    {18, 40}, 
                    {18, 80}, 
                    {30, 80}, 
                    {30, 40}, 
                    {36, 40}})}));
end Clock;