/*
 * File: Merici_jednotka.c
 *
 * Real-Time Workshop code generated for Simulink model Merici_jednotka.
 *
 * Model version                        : 1.194
 * Real-Time Workshop file version      : 7.3  (R2009a)  15-Jan-2009
 * Real-Time Workshop file generated on : Tue May 21 16:17:24 2013
 * TLC version                          : 7.3 (Jan 18 2009)
 * C source code generated on           : Tue May 21 16:17:25 2013
 *--------------------------------------------------------------
 *   Embedded Coder for Microchip dsPIC family.                 |
 *   Generate .c and .h files from your Matlab/simulink model   |
 *   and compile the diagram to .hex and .coff file that can be |
 *   downloaded directly into the microcontroller               |
 *                                                              |
 * Licence Accorded to Ing. Robert Grepl, Ph.D.                 |
 *                     Technical University Brno                |
 *                                                              |
 *   Written by Lubin KERHUEL -  http://www.kerhuel.eu          |
 *  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
 *   Version 3.5                               27-Apr-2011      |
 *   For Matlab 7.8             R2009a                          |
 *--------------------------------------------------------------
 */

#include "Merici_jednotka.h"
#include "Merici_jednotka_private.h"

/* Named constants for Stateflow: '<Root>/Chart' */
#define Merici_jednotka_IN_off         (1)
#define Merici_jednotka_IN_on          (2)

/* Block signals (auto storage) */
BlockIO_Merici_jednotka Merici_jednotka_B;

/* Block states (auto storage) */
D_Work_Merici_jednotka Merici_jednotka_DWork;
real_T uMultiWord2Double(const uint32_T u1[], int16_T n1)
{
  real_T y;
  int16_T i;
  int16_T exp_0;
  int16_T loop_ub;
  y = 0.0;
  exp_0 = 0;
  loop_ub = n1 - 1;
  for (i = 0; i <= loop_ub; i = i + 1) {
    y = y + ldexp((real_T)u1[i], exp_0);
    exp_0 = exp_0 + 32;
  }

  return y;
}

void uMultiWordMul(const uint32_T u1[], int16_T n1, const uint32_T u2[], int16_T
                   n2, uint32_T y[], int16_T n)
{
  int16_T i;
  int16_T j;
  int16_T k;
  int16_T nj;
  uint32_T u1i;
  uint32_T yk;
  uint32_T a1;
  uint32_T a0;
  uint32_T b1;
  uint32_T w10;
  uint32_T w01;
  uint32_T cb;
  int16_T loop_ub;
  int16_T loop_ub_0;
  int16_T loop_ub_1;

  /* Initialize output to zero */
  loop_ub = n - 1;
  for (k = 0; k <= loop_ub; k = k + 1) {
    y[k] = 0UL;
  }

  loop_ub_1 = n1 - 1;
  for (i = 0; i <= loop_ub_1; i = i + 1) {
    cb = 0UL;
    u1i = u1[i];
    a1 = u1i >> 16U;
    a0 = u1i & 65535UL;
    k = n - i;
    nj = n2 <= k ? n2 : k;
    k = i;
    loop_ub_0 = nj - 1;
    for (j = 0; j <= loop_ub_0; j = j + 1) {
      yk = y[k];
      u1i = u2[j];
      b1 = u1i >> 16U;
      u1i = u1i & 65535UL;
      w10 = a1 * u1i;
      w01 = a0 * b1;
      yk = yk + cb;
      cb = yk < cb ? 1UL : 0UL;
      u1i = a0 * u1i;
      yk = yk + u1i;
      cb = (yk < u1i ? 1UL : 0UL) + cb;
      u1i = w10 << 16U;
      yk = yk + u1i;
      cb = (yk < u1i ? 1UL : 0UL) + cb;
      u1i = w01 << 16U;
      yk = yk + u1i;
      cb = (yk < u1i ? 1UL : 0UL) + cb;
      y[k] = yk;
      cb = (w10 >> 16U) + cb;
      cb = (w01 >> 16U) + cb;
      cb = a1 * b1 + cb;
      k = k + 1;
    }

    if (k < n) {
      y[k] = cb;
    }
  }
}

/* Model step function */
void Merici_jednotka_step(void)
{
  {
    int16_T eml_t;
    real_T rtb_Gain5;
    int16_T rtb_DataTypeConversion2;
    int16_T rtb_T1;
    int16_T rtb_T2;
    int16_T rtb_H1;
    uint8_T rtb_Switch;
    int16_T rtb_Gain_k;
    int16_T rtb_Gain1;
    int16_T rtb_Gain2_i;
    uint32_T rtb_UnitDelay3;
    uint32_T rtb_UnitDelay2;
    uint32_T rtb_UnitDelay;
    uint32_T rtb_UnitDelay1;
    uint32_T rtb_Gain4;
    real_T tmp;
    uint64m_T tmp_0;
    uint32_T tmp_1;
    uint32_T tmp_2;

    /* S-Function "UART_RX" Block: <Root>/Rx Input */
    {
      if (U1STAbits.URXDA != 0)
        _U1RXIF = 1;
      if (RxPtrHaut_Uart1 != RxPtrBas_Uart1) {
        Merici_jednotka_B.RxInput = (uint8_T) RxTab_Uart1[RxPtrBas_Uart1 ++];/* Use only the 8 low bytes or RxReg */
        RxPtrBas_Uart1 &= (Rx_BUFF_SIZE_Uart1-1);
      } else {
        Merici_jednotka_B.RxInput = 0;
      }
    }

    /* S-Function "dsPIC_Digital_Input" Block: <S1>/A1 */
    Merici_jednotka_B.A1 = PORTBbits.RB3;

    /* Gain: '<S1>/Gain' incorporates:
     *  DataTypeConversion: '<S1>/Data Type Conversion1'
     */
    rtb_Gain_k = Merici_jednotka_P.Gain_Gain_g * Merici_jednotka_B.A1;

    /* S-Function "dsPIC_Digital_Input" Block: <S1>/A2 */
    Merici_jednotka_B.A2 = PORTBbits.RB6;

    /* Gain: '<S1>/Gain1' incorporates:
     *  DataTypeConversion: '<S1>/Data Type Conversion2'
     */
    rtb_Gain1 = Merici_jednotka_P.Gain1_Gain_i * Merici_jednotka_B.A2;

    /* S-Function "dsPIC_Digital_Input" Block: <S1>/A3 */
    Merici_jednotka_B.A3 = PORTBbits.RB7;

    /* Gain: '<S1>/Gain2' incorporates:
     *  DataTypeConversion: '<S1>/Data Type Conversion3'
     */
    rtb_Gain2_i = Merici_jednotka_P.Gain2_Gain_d * Merici_jednotka_B.A3;

    /* S-Function "dsPIC_Digital_Input" Block: <S1>/A4 */
    Merici_jednotka_B.A4 = PORTCbits.RC15;

    /* Stateflow: '<Root>/Chart' incorporates:
     *  Constant: '<S1>/Constant'
     *  DataTypeConversion: '<S1>/Data Type Conversion4'
     *  Gain: '<S1>/Gain3'
     *  RelationalOperator: '<Root>/Relational Operator'
     *  Sum: '<S1>/Add'
     */
    /* Gateway: Chart */
    /* During: Chart */
    if (Merici_jednotka_DWork.temporalCounter_i1 < 7) {
      Merici_jednotka_DWork.temporalCounter_i1 = (uint8_T)
        (Merici_jednotka_DWork.temporalCounter_i1 + 1);
    }

    if (Merici_jednotka_DWork.is_active_c1_Merici_jednotka == 0) {
      /* Entry: Chart */
      Merici_jednotka_DWork.is_active_c1_Merici_jednotka = 1U;

      /* Transition: '<S2>:10' */
      /* Entry 'off': '<S2>:1' */
      Merici_jednotka_DWork.is_c1_Merici_jednotka = (uint8_T)
        Merici_jednotka_IN_off;
      Merici_jednotka_B.y = false;
    } else {
      switch (Merici_jednotka_DWork.is_c1_Merici_jednotka) {
       case Merici_jednotka_IN_off:
        /* During 'off': '<S2>:1' */
        if (((uint8_T)(uint16_T)(((((int16_T)((uint16_T)rtb_Gain_k >> 7) +
                 (int16_T)((uint16_T)rtb_Gain1 >> 6)) + (int16_T)((uint16_T)
                 rtb_Gain2_i >> 5)) + (int16_T)((uint16_T)
                (Merici_jednotka_P.Gain3_Gain * Merici_jednotka_B.A4) >> 4)) +
              Merici_jednotka_P.Constant_Value) == Merici_jednotka_B.RxInput) ==
            1) {
          /* Transition: '<S2>:8' */
          /* Exit 'off': '<S2>:1' */
          /* Entry 'on': '<S2>:2' */
          Merici_jednotka_DWork.is_c1_Merici_jednotka = (uint8_T)
            Merici_jednotka_IN_on;
          Merici_jednotka_DWork.temporalCounter_i1 = 0U;
          Merici_jednotka_B.y = true;
        }
        break;

       case Merici_jednotka_IN_on:
        /* During 'on': '<S2>:2' */
        if (Merici_jednotka_DWork.temporalCounter_i1 >= 4) {
          /* Transition: '<S2>:9' */
          /* Exit 'on': '<S2>:2' */
          /* Entry 'off': '<S2>:1' */
          Merici_jednotka_DWork.is_c1_Merici_jednotka = (uint8_T)
            Merici_jednotka_IN_off;
          Merici_jednotka_B.y = false;
        }
        break;

       default:
        /* Transition: '<S2>:10' */
        /* Entry 'off': '<S2>:1' */
        Merici_jednotka_DWork.is_c1_Merici_jednotka = (uint8_T)
          Merici_jednotka_IN_off;
        Merici_jednotka_B.y = false;
        break;
      }
    }

    /* S-Function "dsPIC_Digital_OutputWrite" Block: <Root>/Digital Output Write */
    LATDbits.LATD0 = Merici_jednotka_B.y;

    /* UnitDelay: '<S7>/Unit Delay3' */
    rtb_UnitDelay3 = Merici_jednotka_DWork.UnitDelay3_DSTATE;

    /* UnitDelay: '<S7>/Unit Delay2' */
    rtb_UnitDelay2 = Merici_jednotka_DWork.UnitDelay2_DSTATE;

    /* UnitDelay: '<S7>/Unit Delay' */
    rtb_UnitDelay = Merici_jednotka_DWork.UnitDelay_DSTATE;

    /* UnitDelay: '<S7>/Unit Delay1' */
    rtb_UnitDelay1 = Merici_jednotka_DWork.UnitDelay1_DSTATE;

    /* Gain: '<S4>/Gain5' incorporates:
     *  Constant: '<S4>/Constant2'
     *  Gain: '<S7>/Gain'
     *  Sum: '<S4>/Subtract'
     *  Sum: '<S7>/Add1'
     *  UnitDelay: '<S7>/Unit Delay4'
     */
    tmp_1 = Merici_jednotka_P.Gain_Gain;
    tmp_2 = (((Merici_jednotka_DWork.UnitDelay4_DSTATE +
               Merici_jednotka_DWork.UnitDelay3_DSTATE) +
              Merici_jednotka_DWork.UnitDelay2_DSTATE) +
             Merici_jednotka_DWork.UnitDelay_DSTATE) +
      Merici_jednotka_DWork.UnitDelay1_DSTATE;
    uMultiWordMul(&tmp_1, 1, &tmp_2, 1, &tmp_0.chunks[0U], 2);
    rtb_Gain5 = (ldexp(uMultiWord2Double(&tmp_0.chunks[0U], 2), (int16_T)-50L) -
                 Merici_jednotka_P.Constant2_Value) *
      Merici_jednotka_P.Gain5_Gain;

    /* DataTypeConversion: '<S4>/Data Type Conversion1' incorporates:
     *  Gain: '<S4>/Gain1'
     */
    tmp = Merici_jednotka_P.Gain1_Gain * rtb_Gain5;
    if (rtIsNaN(tmp) || rtIsInf(tmp)) {
      tmp = 0.0;
    } else {
      tmp = fmod(floor(tmp), 65536.0);
    }

    rtb_DataTypeConversion2 = tmp < 0.0 ? -((int16_T)(uint16_T)(-tmp)) :
      (int16_T)(uint16_T)tmp;

    /* Saturate: '<Root>/Saturation' */
    rtb_Gain_k = rt_SATURATE(rtb_DataTypeConversion2,
      Merici_jednotka_P.Saturation_LowerSat,
      Merici_jednotka_P.Saturation_UpperSat);

    /* Sum: '<S4>/Subtract2' incorporates:
     *  Constant: '<S4>/Constant12'
     *  Gain: '<S4>/Gain6'
     */
    rtb_Gain5 = Merici_jednotka_P.Constant12_Value -
      Merici_jednotka_P.Gain6_Gain * rtb_Gain5;

    /* S-Function "ADC" Block: <Root>/ADC  */
    Merici_jednotka_B.Teplota = ADCBUF0;
    Merici_jednotka_B.Vlhkost = ADCBUF1;
    Merici_jednotka_B.Fotorezistor = ADCBUF2;

    /* DataTypeConversion: '<S4>/Data Type Conversion2' incorporates:
     *  Constant: '<S4>/Constant1'
     *  Gain: '<S4>/Gain2'
     *  Gain: '<S4>/Gain7'
     *  Product: '<S4>/Divide'
     *  Sum: '<S4>/Subtract1'
     */
    tmp = (ldexp((real_T)((uint32_T)Merici_jednotka_P.Gain2_Gain * (uint32_T)
                          Merici_jednotka_B.Vlhkost), (int16_T)-26L) -
           Merici_jednotka_P.Constant1_Value) * Merici_jednotka_P.Gain7_Gain *
      (1.0 / rtb_Gain5);
    if (rtIsNaN(tmp) || rtIsInf(tmp)) {
      tmp = 0.0;
    } else {
      tmp = fmod(floor(tmp), 65536.0);
    }

    rtb_DataTypeConversion2 = tmp < 0.0 ? -((int16_T)(uint16_T)(-tmp)) :
      (int16_T)(uint16_T)tmp;

    /* Saturate: '<Root>/Saturation1' */
    rtb_DataTypeConversion2 = rt_SATURATE(rtb_DataTypeConversion2,
      Merici_jednotka_P.Saturation1_LowerSat,
      Merici_jednotka_P.Saturation1_UpperSat);

    /* Switch: '<S4>/Switch' incorporates:
     *  Constant: '<S4>/Constant13'
     *  Constant: '<S4>/Constant14'
     */
    if (Merici_jednotka_B.Fotorezistor > Merici_jednotka_P.Switch_Threshold) {
      rtb_Switch = Merici_jednotka_P.Constant13_Value;
    } else {
      rtb_Switch = Merici_jednotka_P.Constant14_Value;
    }

    /* Embedded MATLAB: '<Root>/Embedded MATLAB Function' */
    /* Embedded MATLAB Function 'Embedded MATLAB Function': '<S3>:1' */
    if (rtb_Gain_k < 0) {
      /* '<S3>:1:3' */
      /* '<S3>:1:4' */
      rtb_Gain5 = 45.0;
    } else {
      /* '<S3>:1:6' */
      rtb_Gain5 = 43.0;
    }

    /* '<S3>:1:9' */
    if (rtb_Gain_k < 0) {
      if (rtb_Gain_k <= MIN_int16_T) {
        rtb_Gain1 = MAX_int16_T;
      } else {
        rtb_Gain1 = -rtb_Gain_k;
      }
    } else {
      rtb_Gain1 = rtb_Gain_k;
    }

    /*  prevod teploty */
    if ((rtb_Gain1 >= 100) && (rtb_Gain1 < 200)) {
      /* '<S3>:1:11' */
      /* '<S3>:1:12' */
      rtb_T1 = 49;

      /* '<S3>:1:13' */
      eml_t = 100;
    } else if ((rtb_Gain1 >= 200) && (rtb_Gain1 < 300)) {
      /* '<S3>:1:14' */
      /* '<S3>:1:15' */
      rtb_T1 = 50;

      /* '<S3>:1:16' */
      eml_t = 200;
    } else if ((rtb_Gain1 > 300) && (rtb_Gain1 < 400)) {
      /* '<S3>:1:17' */
      /* '<S3>:1:18' */
      rtb_T1 = 51;

      /* '<S3>:1:19' */
      eml_t = 300;
    } else if ((rtb_Gain1 >= 400) && (rtb_Gain1 < 500)) {
      /* '<S3>:1:20' */
      /* '<S3>:1:21' */
      rtb_T1 = 52;

      /* '<S3>:1:22' */
      eml_t = 400;
    } else if ((rtb_Gain1 >= 500) && (rtb_Gain1 < 600)) {
      /* '<S3>:1:23' */
      /* '<S3>:1:24' */
      rtb_T1 = 53;

      /* '<S3>:1:25' */
      eml_t = 500;
    } else if ((rtb_Gain1 >= 600) && (rtb_Gain1 < 700)) {
      /* '<S3>:1:26' */
      /* '<S3>:1:27' */
      rtb_T1 = 54;

      /* '<S3>:1:28' */
      eml_t = 600;
    } else if ((rtb_Gain1 >= 700) && (rtb_Gain1 < 800)) {
      /* '<S3>:1:29' */
      /* '<S3>:1:30' */
      rtb_T1 = 55;

      /* '<S3>:1:31' */
      eml_t = 700;
    } else if ((rtb_Gain1 >= 800) && (rtb_Gain1 < 900)) {
      /* '<S3>:1:32' */
      /* '<S3>:1:33' */
      rtb_T1 = 56;

      /* '<S3>:1:34' */
      eml_t = 800;
    } else if (rtb_Gain1 >= 900) {
      /* '<S3>:1:35' */
      /* '<S3>:1:36' */
      rtb_T1 = 57;

      /* '<S3>:1:37' */
      eml_t = 900;
    } else {
      /* '<S3>:1:39' */
      rtb_T1 = 48;

      /* '<S3>:1:40' */
      eml_t = 0;
    }

    /* '<S3>:1:43' */
    tmp = (real_T)(rtb_Gain1 - eml_t);
    tmp = tmp < 0.0 ? ceil(tmp - 0.5) : floor(tmp + 0.5);
    if (tmp < 32768.0) {
      if (tmp >= -32768.0) {
        rtb_Gain2_i = (int16_T)tmp;
      } else {
        rtb_Gain2_i = MIN_int16_T;
      }
    } else {
      rtb_Gain2_i = MAX_int16_T;
    }

    if ((rtb_Gain2_i >= 10) && (rtb_Gain2_i < 20)) {
      /* '<S3>:1:45' */
      /* '<S3>:1:46' */
      rtb_T2 = 49;

      /* '<S3>:1:47' */
      eml_t = 10;
    } else if ((rtb_Gain2_i >= 20) && (rtb_Gain2_i < 30)) {
      /* '<S3>:1:48' */
      /* '<S3>:1:49' */
      rtb_T2 = 50;

      /* '<S3>:1:50' */
      eml_t = 20;
    } else if ((rtb_Gain2_i >= 30) && (rtb_Gain2_i < 40)) {
      /* '<S3>:1:51' */
      /* '<S3>:1:52' */
      rtb_T2 = 51;

      /* '<S3>:1:53' */
      eml_t = 30;
    } else if ((rtb_Gain2_i >= 40) && (rtb_Gain2_i < 50)) {
      /* '<S3>:1:54' */
      /* '<S3>:1:55' */
      rtb_T2 = 52;

      /* '<S3>:1:56' */
      eml_t = 40;
    } else if ((rtb_Gain2_i >= 50) && (rtb_Gain2_i < 60)) {
      /* '<S3>:1:57' */
      /* '<S3>:1:58' */
      rtb_T2 = 53;

      /* '<S3>:1:59' */
      eml_t = 50;
    } else if ((rtb_Gain2_i >= 60) && (rtb_Gain2_i < 70)) {
      /* '<S3>:1:60' */
      /* '<S3>:1:61' */
      rtb_T2 = 54;

      /* '<S3>:1:62' */
      eml_t = 60;
    } else if ((rtb_Gain2_i >= 70) && (rtb_Gain2_i < 80)) {
      /* '<S3>:1:63' */
      /* '<S3>:1:64' */
      rtb_T2 = 55;

      /* '<S3>:1:65' */
      eml_t = 70;
    } else if ((rtb_Gain1 >= 80) && (rtb_Gain1 < 90)) {
      /* '<S3>:1:66' */
      /* '<S3>:1:67' */
      rtb_T2 = 56;

      /* '<S3>:1:68' */
      eml_t = 80;
    } else if (rtb_Gain_k >= 90) {
      /* '<S3>:1:69' */
      /* '<S3>:1:70' */
      rtb_T2 = 57;

      /* '<S3>:1:71' */
      eml_t = 90;
    } else {
      /* '<S3>:1:73' */
      rtb_T2 = 48;

      /* '<S3>:1:74' */
      eml_t = 0;
    }

    /* '<S3>:1:77' */
    tmp = (real_T)((int32_T)rtb_Gain2_i - (int32_T)eml_t);
    tmp = tmp < 0.0 ? ceil(tmp - 0.5) : floor(tmp + 0.5);
    if (tmp < 32768.0) {
      if (tmp >= -32768.0) {
        rtb_Gain_k = (int16_T)tmp;
      } else {
        rtb_Gain_k = MIN_int16_T;
      }
    } else {
      rtb_Gain_k = MAX_int16_T;
    }

    if (rtb_Gain_k == 1) {
      /* '<S3>:1:79' */
      /* '<S3>:1:80' */
      rtb_Gain2_i = 49;
    } else if (rtb_Gain_k == 2) {
      /* '<S3>:1:81' */
      /* '<S3>:1:82' */
      rtb_Gain2_i = 50;
    } else if (rtb_Gain_k == 3) {
      /* '<S3>:1:83' */
      /* '<S3>:1:84' */
      rtb_Gain2_i = 51;
    } else if (rtb_Gain_k == 4) {
      /* '<S3>:1:85' */
      /* '<S3>:1:86' */
      rtb_Gain2_i = 52;
    } else if (rtb_Gain_k == 5) {
      /* '<S3>:1:87' */
      /* '<S3>:1:88' */
      rtb_Gain2_i = 53;
    } else if (rtb_Gain_k == 6) {
      /* '<S3>:1:89' */
      /* '<S3>:1:90' */
      rtb_Gain2_i = 54;
    } else if (rtb_Gain_k == 7) {
      /* '<S3>:1:91' */
      /* '<S3>:1:92' */
      rtb_Gain2_i = 55;
    } else if (rtb_Gain_k == 8) {
      /* '<S3>:1:93' */
      /* '<S3>:1:94' */
      rtb_Gain2_i = 56;
    } else if (rtb_Gain_k == 9) {
      /* '<S3>:1:95' */
      /* '<S3>:1:96' */
      rtb_Gain2_i = 57;
    } else {
      /* '<S3>:1:98' */
      rtb_Gain2_i = 48;
    }

    /*  prevod vlhkosti */
    if ((rtb_DataTypeConversion2 >= 10) && (rtb_DataTypeConversion2 < 20)) {
      /* '<S3>:1:102' */
      /* '<S3>:1:103' */
      rtb_H1 = 49;

      /* '<S3>:1:104' */
      eml_t = 10;
    } else if ((rtb_DataTypeConversion2 >= 20) && (rtb_DataTypeConversion2 < 30))
    {
      /* '<S3>:1:105' */
      /* '<S3>:1:106' */
      rtb_H1 = 50;

      /* '<S3>:1:107' */
      eml_t = 20;
    } else if ((rtb_DataTypeConversion2 >= 30) && (rtb_DataTypeConversion2 < 40))
    {
      /* '<S3>:1:108' */
      /* '<S3>:1:109' */
      rtb_H1 = 51;

      /* '<S3>:1:110' */
      eml_t = 30;
    } else if ((rtb_DataTypeConversion2 >= 40) && (rtb_DataTypeConversion2 < 50))
    {
      /* '<S3>:1:111' */
      /* '<S3>:1:112' */
      rtb_H1 = 52;

      /* '<S3>:1:113' */
      eml_t = 40;
    } else if ((rtb_DataTypeConversion2 >= 50) && (rtb_DataTypeConversion2 < 60))
    {
      /* '<S3>:1:114' */
      /* '<S3>:1:115' */
      rtb_H1 = 53;

      /* '<S3>:1:116' */
      eml_t = 50;
    } else if ((rtb_DataTypeConversion2 >= 60) && (rtb_DataTypeConversion2 < 70))
    {
      /* '<S3>:1:117' */
      /* '<S3>:1:118' */
      rtb_H1 = 54;

      /* '<S3>:1:119' */
      eml_t = 60;
    } else if ((rtb_DataTypeConversion2 >= 70) && (rtb_DataTypeConversion2 < 80))
    {
      /* '<S3>:1:120' */
      /* '<S3>:1:121' */
      rtb_H1 = 55;

      /* '<S3>:1:122' */
      eml_t = 70;
    } else if ((rtb_DataTypeConversion2 >= 80) && (rtb_DataTypeConversion2 < 90))
    {
      /* '<S3>:1:123' */
      /* '<S3>:1:124' */
      rtb_H1 = 56;

      /* '<S3>:1:125' */
      eml_t = 80;
    } else if (rtb_DataTypeConversion2 >= 90) {
      /* '<S3>:1:126' */
      /* '<S3>:1:127' */
      rtb_H1 = 57;

      /* '<S3>:1:128' */
      eml_t = 90;
    } else {
      /* '<S3>:1:131' */
      rtb_H1 = 48;

      /* '<S3>:1:132' */
      eml_t = 0;
    }

    /* '<S3>:1:135' */
    tmp = (real_T)((int32_T)rtb_DataTypeConversion2 - (int32_T)eml_t);
    tmp = tmp < 0.0 ? ceil(tmp - 0.5) : floor(tmp + 0.5);
    if (tmp < 32768.0) {
      if (tmp >= -32768.0) {
        rtb_Gain_k = (int16_T)tmp;
      } else {
        rtb_Gain_k = MIN_int16_T;
      }
    } else {
      rtb_Gain_k = MAX_int16_T;
    }

    if (rtb_Gain_k == 1) {
      /* '<S3>:1:137' */
      /* '<S3>:1:138' */
      rtb_Gain_k = 49;
    } else if (rtb_Gain_k == 2) {
      /* '<S3>:1:139' */
      /* '<S3>:1:140' */
      rtb_Gain_k = 50;
    } else if (rtb_Gain_k == 3) {
      /* '<S3>:1:141' */
      /* '<S3>:1:142' */
      rtb_Gain_k = 51;
    } else if (rtb_Gain_k == 4) {
      /* '<S3>:1:143' */
      /* '<S3>:1:144' */
      rtb_Gain_k = 52;
    } else if (rtb_Gain_k == 5) {
      /* '<S3>:1:145' */
      /* '<S3>:1:146' */
      rtb_Gain_k = 53;
    } else if (rtb_Gain_k == 6) {
      /* '<S3>:1:147' */
      /* '<S3>:1:148' */
      rtb_Gain_k = 54;
    } else if (rtb_Gain_k == 7) {
      /* '<S3>:1:149' */
      /* '<S3>:1:150' */
      rtb_Gain_k = 55;
    } else if (rtb_Gain_k == 8) {
      /* '<S3>:1:151' */
      /* '<S3>:1:152' */
      rtb_Gain_k = 56;
    } else if (rtb_Gain_k == 9) {
      /* '<S3>:1:153' */
      /* '<S3>:1:154' */
      rtb_Gain_k = 57;
    } else {
      /* '<S3>:1:156' */
      rtb_Gain_k = 48;
    }

    /*  prevod svetla */
    if (rtb_Switch == 1) {
      /* '<S3>:1:161' */
      /* '<S3>:1:162' */
      rtb_Gain1 = 49;
    } else {
      /* '<S3>:1:164' */
      rtb_Gain1 = 48;
    }

    /* DataTypeConversion: '<Root>/Data Type Conversion8' */
    tmp = fmod((real_T)(int16_T)rtb_Gain5, 256.0);
    Merici_jednotka_B.DataTypeConversion8 = tmp < 0.0 ? (uint8_T)(-((uint8_T)
      (-tmp))) : (uint8_T)tmp;

    /* DataTypeConversion: '<Root>/Data Type Conversion1' */
    tmp = fmod((real_T)rtb_T1, 256.0);
    Merici_jednotka_B.DataTypeConversion1 = tmp < 0.0 ? (uint8_T)(-((uint8_T)
      (-tmp))) : (uint8_T)tmp;

    /* DataTypeConversion: '<Root>/Data Type Conversion2' */
    tmp = fmod((real_T)rtb_T2, 256.0);
    Merici_jednotka_B.DataTypeConversion2 = tmp < 0.0 ? (uint8_T)(-((uint8_T)
      (-tmp))) : (uint8_T)tmp;

    /* DataTypeConversion: '<Root>/Data Type Conversion3' */
    tmp = fmod((real_T)rtb_Gain2_i, 256.0);
    Merici_jednotka_B.DataTypeConversion3 = tmp < 0.0 ? (uint8_T)(-((uint8_T)
      (-tmp))) : (uint8_T)tmp;

    /* DataTypeConversion: '<Root>/Data Type Conversion4' */
    tmp = fmod((real_T)rtb_H1, 256.0);
    Merici_jednotka_B.DataTypeConversion4 = tmp < 0.0 ? (uint8_T)(-((uint8_T)
      (-tmp))) : (uint8_T)tmp;

    /* DataTypeConversion: '<Root>/Data Type Conversion5' */
    tmp = fmod((real_T)rtb_Gain_k, 256.0);
    Merici_jednotka_B.DataTypeConversion5 = tmp < 0.0 ? (uint8_T)(-((uint8_T)
      (-tmp))) : (uint8_T)tmp;

    /* DataTypeConversion: '<Root>/Data Type Conversion7' */
    tmp = fmod((real_T)rtb_Gain1, 256.0);
    Merici_jednotka_B.DataTypeConversion7 = tmp < 0.0 ? (uint8_T)(-((uint8_T)
      (-tmp))) : (uint8_T)tmp;

    /* Outputs for enable SubSystem: '<Root>/TX odeslani' incorporates:
     *  EnablePort: '<S6>/Enable'
     */
    if (Merici_jednotka_B.y) {
      /* S-Function "UART_Tx" Block: <S6>/Tx Output */
      {
        uint8_T Tmp;
        do {
          Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 128	*/
          Tmp = Tmp & (128-1);
        } while (Tmp == 0);

        TxTab_Uart1[TxPtrHaut_Uart1] = Merici_jednotka_P.START_Value;
        TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (128-1);
        _U1TXIF = 1;
        _U1TXIE = 1;
      }

      /* S-Function "UART_Tx" Block: <S6>/Tx Output1 */
      {
        uint8_T Tmp;
        do {
          Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 128	*/
          Tmp = Tmp & (128-1);
        } while (Tmp == 0);

        TxTab_Uart1[TxPtrHaut_Uart1] = Merici_jednotka_P.Constant1_Value_i;
        TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (128-1);
        _U1TXIF = 1;
        _U1TXIE = 1;
      }

      /* S-Function "UART_Tx" Block: <S6>/Tx Output2 */
      {
        uint8_T Tmp;
        do {
          Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 128	*/
          Tmp = Tmp & (128-1);
        } while (Tmp == 0);

        TxTab_Uart1[TxPtrHaut_Uart1] = Merici_jednotka_P.Constant7_Value;
        TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (128-1);
        _U1TXIF = 1;
        _U1TXIE = 1;
      }

      /* S-Function "UART_Tx" Block: <S6>/Tx Output3 */
      {
        uint8_T Tmp;
        do {
          Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 128	*/
          Tmp = Tmp & (128-1);
        } while (Tmp == 0);

        TxTab_Uart1[TxPtrHaut_Uart1] = Merici_jednotka_B.DataTypeConversion8;
        TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (128-1);
        _U1TXIF = 1;
        _U1TXIE = 1;
      }

      /* S-Function "UART_Tx" Block: <S6>/Tx Output4 */
      {
        uint8_T Tmp;
        do {
          Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 128	*/
          Tmp = Tmp & (128-1);
        } while (Tmp == 0);

        TxTab_Uart1[TxPtrHaut_Uart1] = Merici_jednotka_B.DataTypeConversion1;
        TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (128-1);
        _U1TXIF = 1;
        _U1TXIE = 1;
      }

      /* S-Function "UART_Tx" Block: <S6>/Tx Output5 */
      {
        uint8_T Tmp;
        do {
          Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 128	*/
          Tmp = Tmp & (128-1);
        } while (Tmp == 0);

        TxTab_Uart1[TxPtrHaut_Uart1] = Merici_jednotka_B.DataTypeConversion2;
        TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (128-1);
        _U1TXIF = 1;
        _U1TXIE = 1;
      }

      /* S-Function "UART_Tx" Block: <S6>/Tx Output6 */
      {
        uint8_T Tmp;
        do {
          Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 128	*/
          Tmp = Tmp & (128-1);
        } while (Tmp == 0);

        TxTab_Uart1[TxPtrHaut_Uart1] = Merici_jednotka_P.Constant2_Value_a;
        TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (128-1);
        _U1TXIF = 1;
        _U1TXIE = 1;
      }

      /* S-Function "UART_Tx" Block: <S6>/Tx Output7 */
      {
        uint8_T Tmp;
        do {
          Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 128	*/
          Tmp = Tmp & (128-1);
        } while (Tmp == 0);

        TxTab_Uart1[TxPtrHaut_Uart1] = Merici_jednotka_B.DataTypeConversion3;
        TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (128-1);
        _U1TXIF = 1;
        _U1TXIE = 1;
      }

      /* S-Function "UART_Tx" Block: <S6>/Tx Output8 */
      {
        uint8_T Tmp;
        do {
          Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 128	*/
          Tmp = Tmp & (128-1);
        } while (Tmp == 0);

        TxTab_Uart1[TxPtrHaut_Uart1] = Merici_jednotka_P.Constant3_Value;
        TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (128-1);
        _U1TXIF = 1;
        _U1TXIE = 1;
      }

      /* S-Function "UART_Tx" Block: <S6>/Tx Output9 */
      {
        uint8_T Tmp;
        do {
          Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 128	*/
          Tmp = Tmp & (128-1);
        } while (Tmp == 0);

        TxTab_Uart1[TxPtrHaut_Uart1] = Merici_jednotka_P.Constant8_Value;
        TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (128-1);
        _U1TXIF = 1;
        _U1TXIE = 1;
      }

      /* S-Function "UART_Tx" Block: <S6>/Tx Output10 */
      {
        uint8_T Tmp;
        do {
          Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 128	*/
          Tmp = Tmp & (128-1);
        } while (Tmp == 0);

        TxTab_Uart1[TxPtrHaut_Uart1] = Merici_jednotka_B.DataTypeConversion4;
        TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (128-1);
        _U1TXIF = 1;
        _U1TXIE = 1;
      }

      /* S-Function "UART_Tx" Block: <S6>/Tx Output11 */
      {
        uint8_T Tmp;
        do {
          Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 128	*/
          Tmp = Tmp & (128-1);
        } while (Tmp == 0);

        TxTab_Uart1[TxPtrHaut_Uart1] = Merici_jednotka_B.DataTypeConversion5;
        TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (128-1);
        _U1TXIF = 1;
        _U1TXIE = 1;
      }

      /* S-Function "UART_Tx" Block: <S6>/Tx Output12 */
      {
        uint8_T Tmp;
        do {
          Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 128	*/
          Tmp = Tmp & (128-1);
        } while (Tmp == 0);

        TxTab_Uart1[TxPtrHaut_Uart1] = Merici_jednotka_P.Constant4_Value;
        TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (128-1);
        _U1TXIF = 1;
        _U1TXIE = 1;
      }

      /* S-Function "UART_Tx" Block: <S6>/Tx Output13 */
      {
        uint8_T Tmp;
        do {
          Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 128	*/
          Tmp = Tmp & (128-1);
        } while (Tmp == 0);

        TxTab_Uart1[TxPtrHaut_Uart1] = Merici_jednotka_P.Constant6_Value;
        TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (128-1);
        _U1TXIF = 1;
        _U1TXIE = 1;
      }

      /* S-Function "UART_Tx" Block: <S6>/Tx Output14 */
      {
        uint8_T Tmp;
        do {
          Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 128	*/
          Tmp = Tmp & (128-1);
        } while (Tmp == 0);

        TxTab_Uart1[TxPtrHaut_Uart1] = Merici_jednotka_P.Constant9_Value;
        TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (128-1);
        _U1TXIF = 1;
        _U1TXIE = 1;
      }

      /* S-Function "UART_Tx" Block: <S6>/Tx Output15 */
      {
        uint8_T Tmp;
        do {
          Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 128	*/
          Tmp = Tmp & (128-1);
        } while (Tmp == 0);

        TxTab_Uart1[TxPtrHaut_Uart1] = Merici_jednotka_B.DataTypeConversion7;
        TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (128-1);
        _U1TXIF = 1;
        _U1TXIE = 1;
      }

      /* S-Function "UART_Tx" Block: <S6>/Tx Output16 */
      {
        uint8_T Tmp;
        do {
          Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 128	*/
          Tmp = Tmp & (128-1);
        } while (Tmp == 0);

        TxTab_Uart1[TxPtrHaut_Uart1] = Merici_jednotka_P.Constant5_Value;
        TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (128-1);
        _U1TXIF = 1;
        _U1TXIE = 1;
      }
    }

    /* end of Outputs for SubSystem: '<Root>/TX odeslani' */

    /* Gain: '<S4>/Gain4' */
    rtb_Gain4 = (uint32_T)Merici_jednotka_P.Gain4_Gain * (uint32_T)
      Merici_jednotka_B.Teplota;

    /* DiscretePulseGenerator: '<Root>/Pulse Generator' */
    rtb_Gain5 = ((real_T)Merici_jednotka_DWork.clockTickCounter <
                 Merici_jednotka_P.PulseGenerator_Duty) &&
      (Merici_jednotka_DWork.clockTickCounter >= 0L) ?
      Merici_jednotka_P.PulseGenerator_Amp : 0.0;
    if ((real_T)Merici_jednotka_DWork.clockTickCounter >=
        Merici_jednotka_P.PulseGenerator_Period - 1.0) {
      Merici_jednotka_DWork.clockTickCounter = 0L;
    } else {
      Merici_jednotka_DWork.clockTickCounter =
        Merici_jednotka_DWork.clockTickCounter + 1L;
    }

    /* DataTypeConversion: '<Root>/Data Type Conversion' */
    Merici_jednotka_B.DataTypeConversion = (rtb_Gain5 != 0.0);

    /* S-Function "dsPIC_Digital_OutputWrite" Block: <Root>/na LED */
    LATBbits.LATB4 = Merici_jednotka_B.DataTypeConversion;

    /* Update for UnitDelay: '<S7>/Unit Delay4' */
    Merici_jednotka_DWork.UnitDelay4_DSTATE = rtb_UnitDelay3;

    /* Update for UnitDelay: '<S7>/Unit Delay3' */
    Merici_jednotka_DWork.UnitDelay3_DSTATE = rtb_UnitDelay2;

    /* Update for UnitDelay: '<S7>/Unit Delay2' */
    Merici_jednotka_DWork.UnitDelay2_DSTATE = rtb_UnitDelay;

    /* Update for UnitDelay: '<S7>/Unit Delay' */
    Merici_jednotka_DWork.UnitDelay_DSTATE = rtb_UnitDelay1;

    /* Update for UnitDelay: '<S7>/Unit Delay1' */
    Merici_jednotka_DWork.UnitDelay1_DSTATE = rtb_Gain4;
  }
}

/* Model initialize function */
void Merici_jednotka_initialize(boolean_T firstTime)
{
  (void)firstTime;

  /* Registration code */

  /* initialize non-finites */
  rt_InitInfAndNaN(sizeof(real_T));

  /* block I/O */
  (void) memset(((void *) &Merici_jednotka_B),0,
                sizeof(BlockIO_Merici_jednotka));

  /* states (dwork) */
  (void) memset((void *)&Merici_jednotka_DWork, 0,
                sizeof(D_Work_Merici_jednotka));

  /* S-Function "dsPIC_MASTER" initialization Block: <Root>/Master */
  /* Solver mode : SingleTasking */
  /* CONFIG TIMER 1 for scheduling steps 	*/
  ConfigIntTimer1(T1_INT_PRIOR_0 & T1_INT_OFF);
  T1CON = 0x8010 ;                     /* T1_PS_1_8	*/
  PR1 = 9212;

  /* Configuration TRIS */
  TRISB = 65519;
  TRISD = 65534;

  /* Configuration ADCHS */
  ADPCFG = 216U;

  /* S-Function "dsPIC_UART_config" initialization Block: <Root>/UART Configuration */
  /* set priority */
  _U1RXIP = 0x0007 & 2;
  _U1TXIP = 0x0007 & 1;

  /* enable/disable interrupt */
  _U1RXIE = (3 != 0);
  _U1TXIE = 0;
  U1MODE = 0x8080 + 1024;
  U1STA = 0x0 + 1 + 1024 + 192 + (1<<12);
  U1BRG = 47;
  TxPtrHaut_Uart1 = 0;
  TxPtrBas_Uart1 = 0;
  RxPtrHaut_Uart1 = 0;
  RxPtrBas_Uart1 = 0;
  _U1TXIF = 0;

  /* S-Function "dsPIC_ADC" initialization Block: <Root>/ADC  */
  /* Initialise ADC converter */
  /* 0 mean each 1 conversion */
#define NoSamplesADC                   2
#define ADCSval                        9
#define SAMCval                        (1 << 8)
#define OutFormatBitsval               (0 << 8)
#define VoltRef                        (0 << 13)

  /* Use internal counter Trigger, continuous simulatenous sampling */
  ADCON1 = 0x0004 | (7 << 5) | (1 << 10)| OutFormatBitsval;
  ADCON2 = 0x0400 | (NoSamplesADC << 2) | VoltRef;
  ADCSSL = 7;
  ADCON3 = (ADCSval | SAMCval) & 0xff7f;
  ADCHS = 0x0000;
  IPC2 = (IPC2 | (6 << 12));           /*Interrupt Priority : 6*/
  _ADIF = 0;

  /* We set _ADIE = 1 in the main file if necessary*/
  /* Clear Off Any Interrupt Due To Configuration */
  /* Turn on ADC Module */
  ADCON1bits.ADON= 1;

  /* Start for DiscretePulseGenerator: '<Root>/Pulse Generator' */
  Merici_jednotka_DWork.clockTickCounter = 0L;

  /* InitializeConditions for Stateflow: '<Root>/Chart' */
  Merici_jednotka_DWork.is_active_c1_Merici_jednotka = 0U;
  Merici_jednotka_DWork.is_c1_Merici_jednotka = 0U;
  Merici_jednotka_B.y = false;

  /* InitializeConditions for UnitDelay: '<S7>/Unit Delay4' */
  Merici_jednotka_DWork.UnitDelay4_DSTATE = Merici_jednotka_P.UnitDelay4_X0;

  /* InitializeConditions for UnitDelay: '<S7>/Unit Delay3' */
  Merici_jednotka_DWork.UnitDelay3_DSTATE = Merici_jednotka_P.UnitDelay3_X0;

  /* InitializeConditions for UnitDelay: '<S7>/Unit Delay2' */
  Merici_jednotka_DWork.UnitDelay2_DSTATE = Merici_jednotka_P.UnitDelay2_X0;

  /* InitializeConditions for UnitDelay: '<S7>/Unit Delay' */
  Merici_jednotka_DWork.UnitDelay_DSTATE = Merici_jednotka_P.UnitDelay_X0;

  /* InitializeConditions for UnitDelay: '<S7>/Unit Delay1' */
  Merici_jednotka_DWork.UnitDelay1_DSTATE = Merici_jednotka_P.UnitDelay1_X0;
}

/* File trailer for Real-Time Workshop generated code.
 *
 * [EOF]
 */
