/*
 * File: Vejlupek_CAR4_dsPIC33_ACU44_MainBoard_direct.c
 *
 * Real-Time Workshop code generated for Simulink model Vejlupek_CAR4_dsPIC33_ACU44_MainBoard_direct.
 *
 * Model version                        : 1.823
 * Real-Time Workshop file version      : 7.3  (R2009a)  15-Jan-2009
 * Real-Time Workshop file generated on : Wed Apr 14 04:14:00 2010
 * TLC version                          : 7.3 (Jan 18 2009)
 * C source code generated on           : Wed Apr 14 04:14:01 2010
 *--------------------------------------------------------------
 *   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.1                               26-Nov-2009      |
 *   For Matlab 7.8             R2009a                          |
 *--------------------------------------------------------------
 */

#include "Vejlupek_CAR4_dsPIC33_ACU44_MainBoard_direct.h"
#include "Vejlupek_CAR4_dsPIC33_ACU44_MainBoard_direct_private.h"

/* Exported block states */
int16_T Motor_Momentum;                /* '<Root>/Int16: Motor Momentum' */
int16_T Steering_Front;                /* '<Root>/Int16: Steering Front' */
int16_T Steering_Rear;                 /* '<Root>/Int16: Steering Rear' */
uint16_T Buttons;                      /* '<Root>/Uint16: Buttons' */
uint16_T Current_M3;                   /* '<Root>/Uint16: Current M3' */
uint16_T Current_M4;                   /* '<Root>/Uint16: Current M4' */
uint16_T ct_turnsE1;                   /* '<Root>/Uint16: Encoder 1' */
uint16_T ct_turnsE2;                   /* '<Root>/Uint16: Encoder 2' */
uint16_T ct_turnsE3;                   /* '<Root>/Uint16: Encoder 3' */
uint16_T ct_turnsE4;                   /* '<Root>/Uint16: Encoder 4' */
boolean_T DISABLE;                     /* '<Root>/Boolean: DISABLE' */

/* Block signals (auto storage) */
BlockIO_Vejlupek_CAR4_dsPIC33_A Vejlupek_CAR4_dsPIC33_ACU44_M_B;

/* Block states (auto storage) */
D_Work_Vejlupek_CAR4_dsPIC33_AC Vejlupek_CAR4_dsPIC33_ACU_DWork;

/* Previous zero-crossings (trigger) states */
PrevZCSigStates_Vejlupek_CAR4_d Vejlupek_CAR4_ds_PrevZCSigState;

/* Real-time model */
RT_MODEL_Vejlupek_CAR4_dsPIC33_ Vejlupek_CAR4_dsPIC33_ACU44__M_;
RT_MODEL_Vejlupek_CAR4_dsPIC33_ *Vejlupek_CAR4_dsPIC33_ACU44__M =
  &Vejlupek_CAR4_dsPIC33_ACU44__M_;
static void rate_monotonic_scheduler(void);

/* Set which subrates need to run this base step (base rate always runs).
 * This function must be called prior to calling the model step function
 * in order to "remember" which rates need to run this base step.  The
 * buffering of events allows for overlapping preemption.
 */
void Vejlupek_CAR4_dsPIC33_ACU44_MainBoard_direct_SetEventsForThisBaseStep
  (boolean_T *eventFlags)
{
  /* Task runs when its counter is zero, computed via rtmStepTask macro */
  eventFlags[1] = ((boolean_T)rtmStepTask(Vejlupek_CAR4_dsPIC33_ACU44__M, 1));
  eventFlags[2] = ((boolean_T)rtmStepTask(Vejlupek_CAR4_dsPIC33_ACU44__M, 2));
  eventFlags[3] = ((boolean_T)rtmStepTask(Vejlupek_CAR4_dsPIC33_ACU44__M, 3));
}

/* This function updates active task flag for each subrate
 * and rate transition flags for tasks that exchagne data.
 * The function assumes rate-monotonic multitasking scheduler.
 * The function must be called at model base rate so that
 * the generated code self-manages all its subrates and rate
 * transition flags.
 */
static void rate_monotonic_scheduler(void)
{
  /* To ensure a safe and deterministic data transfer between two rates,
   * data is transferred at the priority of a fast task and the frequency
   * of the slow task.  The following flags indicate when the data transfer
   * happens.  That is, a rate interaction flag is set true when both rates
   * will run, and false otherwise.
   */

  /* tid 2 shares data with slower tid rate: 3 */
  if (Vejlupek_CAR4_dsPIC33_ACU44__M->Timing.TaskCounters.TID[2] == 0) {
    Vejlupek_CAR4_dsPIC33_ACU44__M->Timing.RateInteraction.TID2_3 =
      (Vejlupek_CAR4_dsPIC33_ACU44__M->Timing.TaskCounters.TID[3] == 0);
  }

  /* Compute which subrates run during the next base time step.  Subrates
   * are an integer multiple of the base rate counter.  Therefore, the subtask
   * counter is reset when it reaches its limit (zero means run).
   */
  if (++Vejlupek_CAR4_dsPIC33_ACU44__M->Timing.TaskCounters.TID[1] == 2) {/* Sample time: [0.002s, 0.0s] */
    Vejlupek_CAR4_dsPIC33_ACU44__M->Timing.TaskCounters.TID[1] = 0;
  }

  if (++Vejlupek_CAR4_dsPIC33_ACU44__M->Timing.TaskCounters.TID[2] == 5) {/* Sample time: [0.005s, 0.0s] */
    Vejlupek_CAR4_dsPIC33_ACU44__M->Timing.TaskCounters.TID[2] = 0;
  }

  if (++Vejlupek_CAR4_dsPIC33_ACU44__M->Timing.TaskCounters.TID[3] == 10) {/* Sample time: [0.01s, 0.0s] */
    Vejlupek_CAR4_dsPIC33_ACU44__M->Timing.TaskCounters.TID[3] = 0;
  }
}

/* Model step function for TID0 */
void Vejlupek_CAR4_dsPIC33_ACU44_MainBoard_direct_step0(void) /* Sample time: [0.001s, 0.0s] */
{
  {                                    /* Sample time: [0.001s, 0.0s] */
    rate_monotonic_scheduler();
  }

  {
    boolean_T rtb_Compare_f;
    uint8_T rtb_UnitDelay1;
    uint16_T rtb_DataTypeConversion3;
    uint8_T rtb_UnitDelay2;
    int16_T rtb_DataTypeConversion1;
    uint8_T rtb_UnitDelay3;
    int16_T rtb_DataTypeConversion2;
    uint8_T rtb_UnitDelay4;
    boolean_T rtb_CRCCheck1;
    uint8_T rtb_UnitDelay5;
    int16_T rtb_DataTypeConversion4;
    uint8_T rtb_UnitDelay6;
    int16_T rtb_DataTypeConversion5;
    uint8_T rtb_UnitDelay7;
    int16_T rtb_DataTypeConversion6;
    int16_T rtb_MultiportSwitch;

    /* RelationalOperator: '<S77>/Compare' incorporates:
     *  Constant: '<S77>/Constant'
     *  UnitDelay: '<S14>/Unit Delay'
     */
    rtb_Compare_f = (Vejlupek_CAR4_dsPIC33_ACU_DWork.UnitDelay_DSTATE == 254);

    /* UnitDelay: '<S14>/Unit Delay1' */
    rtb_UnitDelay1 = Vejlupek_CAR4_dsPIC33_ACU_DWork.UnitDelay1_DSTATE;

    /* DataTypeConversion: '<S14>/Data Type Conversion3' */
    rtb_DataTypeConversion3 = (uint16_T)
      Vejlupek_CAR4_dsPIC33_ACU_DWork.UnitDelay1_DSTATE;

    /* UnitDelay: '<S14>/Unit Delay2' */
    rtb_UnitDelay2 = Vejlupek_CAR4_dsPIC33_ACU_DWork.UnitDelay2_DSTATE;

    /* DataTypeConversion: '<S14>/Data Type Conversion1' */
    rtb_DataTypeConversion1 = (int16_T)
      Vejlupek_CAR4_dsPIC33_ACU_DWork.UnitDelay2_DSTATE;

    /* UnitDelay: '<S14>/Unit Delay3' */
    rtb_UnitDelay3 = Vejlupek_CAR4_dsPIC33_ACU_DWork.UnitDelay3_DSTATE;

    /* DataTypeConversion: '<S14>/Data Type Conversion2' */
    rtb_DataTypeConversion2 = (int16_T)
      Vejlupek_CAR4_dsPIC33_ACU_DWork.UnitDelay3_DSTATE;

    /* UnitDelay: '<S14>/Unit Delay4' */
    rtb_UnitDelay4 = Vejlupek_CAR4_dsPIC33_ACU_DWork.UnitDelay4_DSTATE;

    /* RelationalOperator: '<S14>/CRC Check 1' incorporates:
     *  S-Function (sfix_bitop): '<S14>/null lower 4 bites5'
     */
    rtb_CRCCheck1 = ((uint16_T)((int16_T)rtb_DataTypeConversion3 ^
      rtb_DataTypeConversion1 ^ rtb_DataTypeConversion2) == (uint16_T)
                     Vejlupek_CAR4_dsPIC33_ACU_DWork.UnitDelay4_DSTATE);

    /* UnitDelay: '<S14>/Unit Delay5' */
    rtb_UnitDelay5 = Vejlupek_CAR4_dsPIC33_ACU_DWork.UnitDelay5_DSTATE;

    /* DataTypeConversion: '<S14>/Data Type Conversion4' */
    rtb_DataTypeConversion4 = (int16_T)
      Vejlupek_CAR4_dsPIC33_ACU_DWork.UnitDelay5_DSTATE;

    /* UnitDelay: '<S14>/Unit Delay6' */
    rtb_UnitDelay6 = Vejlupek_CAR4_dsPIC33_ACU_DWork.UnitDelay6_DSTATE;

    /* DataTypeConversion: '<S14>/Data Type Conversion5' */
    rtb_DataTypeConversion5 = (int16_T)
      Vejlupek_CAR4_dsPIC33_ACU_DWork.UnitDelay6_DSTATE;

    /* UnitDelay: '<S14>/Unit Delay7' */
    rtb_UnitDelay7 = Vejlupek_CAR4_dsPIC33_ACU_DWork.UnitDelay7_DSTATE;

    /* DataTypeConversion: '<S14>/Data Type Conversion6' */
    rtb_DataTypeConversion6 = (int16_T)
      Vejlupek_CAR4_dsPIC33_ACU_DWork.UnitDelay7_DSTATE;

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

    /* Logic: '<S14>/Logical Operator' incorporates:
     *  RelationalOperator: '<S14>/CRC Check 2'
     *  S-Function (sfix_bitop): '<S14>/null lower 4 bites1'
     */
    rtb_Compare_f = (rtb_Compare_f && rtb_CRCCheck1 && ((uint16_T)
      (rtb_DataTypeConversion4 ^ rtb_DataTypeConversion5 ^
       rtb_DataTypeConversion6) == (uint16_T)
      Vejlupek_CAR4_dsPIC33_ACU44_M_B.RxUART1));

    /* MultiPortSwitch: '<S74>/Multiport Switch' incorporates:
     *  ArithShift: '<S69>/shift'
     *  Constant: '<S14>/Constant1'
     *  S-Function (sfix_bitop): '<S14>/Null lower 4 bites_12b1'
     *  S-Function (sfix_bitop): '<S14>/null lower 4 bites2'
     *  Sum: '<S14>/Sum1'
     *  UnitDelay: '<S74>/Memory_4'
     */
    if (rtb_Compare_f == 0) {
      rtb_MultiportSwitch = Vejlupek_CAR4_dsPIC33_ACU_DWork.Memory_4_DSTATE;
    } else {
      rtb_MultiportSwitch = ((rtb_DataTypeConversion1 & 240) << 4 | (int16_T)
        rtb_DataTypeConversion3) - 2048;
    }

    /* MultiPortSwitch: '<S73>/Multiport Switch' incorporates:
     *  ArithShift: '<S70>/shift'
     *  Constant: '<S14>/Constant2'
     *  S-Function (sfix_bitop): '<S14>/Get Lower 4 bites2'
     *  S-Function (sfix_bitop): '<S14>/null lower 4 bites3'
     *  Sum: '<S14>/Sum2'
     *  UnitDelay: '<S73>/Memory_4'
     */
    if (rtb_Compare_f == 0) {
      rtb_DataTypeConversion2 =
        Vejlupek_CAR4_dsPIC33_ACU_DWork.Memory_4_DSTATE_n;
    } else {
      rtb_DataTypeConversion2 = ((rtb_DataTypeConversion1 & 15) |
        rtb_DataTypeConversion2 << 4) - 2048;
    }

    /* MultiPortSwitch: '<S75>/Multiport Switch' incorporates:
     *  ArithShift: '<S71>/shift'
     *  Constant: '<S14>/Constant3'
     *  S-Function (sfix_bitop): '<S14>/Null lower 4 bites_12b2'
     *  S-Function (sfix_bitop): '<S14>/null lower 4 bites4'
     *  Sum: '<S14>/Sum3'
     *  UnitDelay: '<S75>/Memory_4'
     */
    if (rtb_Compare_f == 0) {
      rtb_DataTypeConversion1 =
        Vejlupek_CAR4_dsPIC33_ACU_DWork.Memory_4_DSTATE_i;
    } else {
      rtb_DataTypeConversion1 = ((rtb_DataTypeConversion5 & 240) << 4 |
        rtb_DataTypeConversion4) - 2048;
    }

    /* MultiPortSwitch: '<S76>/Multiport Switch' incorporates:
     *  ArithShift: '<S72>/shift'
     *  S-Function (sfix_bitop): '<S14>/Get Lower 4 bites3'
     *  S-Function (sfix_bitop): '<S14>/null lower 4 bites6'
     *  UnitDelay: '<S76>/Memory_4'
     */
    if (rtb_Compare_f == 0) {
      rtb_DataTypeConversion3 =
        Vejlupek_CAR4_dsPIC33_ACU_DWork.Memory_4_DSTATE_f;
    } else {
      rtb_DataTypeConversion3 = (uint16_T)((rtb_DataTypeConversion5 & 15) |
        rtb_DataTypeConversion6 << 4);
    }

    /* Outputs for trigger SubSystem: '<S14>/Memory Write  If Valid data received' incorporates:
     *  TriggerPort: '<S68>/Trigger'
     */
    if (rtb_Compare_f &&
        (Vejlupek_CAR4_ds_PrevZCSigState.MemoryWriteIfValiddatareceived_ !=
         POS_ZCSIG)) {
      /* DataStoreWrite: '<S68>/Data Store Write' */
      Steering_Front = rtb_MultiportSwitch;

      /* DataStoreWrite: '<S68>/Data Store Write1' */
      Steering_Rear = rtb_DataTypeConversion2;

      /* DataStoreWrite: '<S68>/Data Store Write2' */
      Motor_Momentum = rtb_DataTypeConversion1;

      /* DataStoreWrite: '<S68>/Data Store Write3' */
      Buttons = rtb_DataTypeConversion3;
    }

    Vejlupek_CAR4_ds_PrevZCSigState.MemoryWriteIfValiddatareceived_ =
      rtb_Compare_f ? POS_ZCSIG : ZERO_ZCSIG;

    /* end of Outputs for SubSystem: '<S14>/Memory Write  If Valid data received' */

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

    /* Update for UnitDelay: '<S14>/Unit Delay1' */
    Vejlupek_CAR4_dsPIC33_ACU_DWork.UnitDelay1_DSTATE = rtb_UnitDelay2;

    /* Update for UnitDelay: '<S14>/Unit Delay2' */
    Vejlupek_CAR4_dsPIC33_ACU_DWork.UnitDelay2_DSTATE = rtb_UnitDelay3;

    /* Update for UnitDelay: '<S14>/Unit Delay3' */
    Vejlupek_CAR4_dsPIC33_ACU_DWork.UnitDelay3_DSTATE = rtb_UnitDelay4;

    /* Update for UnitDelay: '<S14>/Unit Delay4' */
    Vejlupek_CAR4_dsPIC33_ACU_DWork.UnitDelay4_DSTATE = rtb_UnitDelay5;

    /* Update for UnitDelay: '<S14>/Unit Delay5' */
    Vejlupek_CAR4_dsPIC33_ACU_DWork.UnitDelay5_DSTATE = rtb_UnitDelay6;

    /* Update for UnitDelay: '<S14>/Unit Delay6' */
    Vejlupek_CAR4_dsPIC33_ACU_DWork.UnitDelay6_DSTATE = rtb_UnitDelay7;

    /* Update for UnitDelay: '<S14>/Unit Delay7' */
    Vejlupek_CAR4_dsPIC33_ACU_DWork.UnitDelay7_DSTATE =
      Vejlupek_CAR4_dsPIC33_ACU44_M_B.RxUART1;

    /* Update for UnitDelay: '<S74>/Memory_4' */
    Vejlupek_CAR4_dsPIC33_ACU_DWork.Memory_4_DSTATE = rtb_MultiportSwitch;

    /* Update for UnitDelay: '<S73>/Memory_4' */
    Vejlupek_CAR4_dsPIC33_ACU_DWork.Memory_4_DSTATE_n = rtb_DataTypeConversion2;

    /* Update for UnitDelay: '<S75>/Memory_4' */
    Vejlupek_CAR4_dsPIC33_ACU_DWork.Memory_4_DSTATE_i = rtb_DataTypeConversion1;

    /* Update for UnitDelay: '<S76>/Memory_4' */
    Vejlupek_CAR4_dsPIC33_ACU_DWork.Memory_4_DSTATE_f = rtb_DataTypeConversion3;
  }
}

/* Model step function for TID1 */
void Vejlupek_CAR4_dsPIC33_ACU44_MainBoard_direct_step1(void) /* Sample time: [0.002s, 0.0s] */
{
  {
    uint16_T rtb_Switch;
    uint16_T rtb_BitwiseOperator1_m;
    uint16_T rtb_Switch_l;
    uint16_T rtb_Switch_o;
    uint16_T rtb_Switch_l3;
    uint16_T rtb_Switch_j;
    uint16_T rtb_BitwiseOperator1_mg;

    /* S-Function "dsPIC_SPI_IO_Interrupt" initialization Block: <S11>/SPI I//O Interrupt driven */
    {
      Vejlupek_CAR4_dsPIC33_ACU44_M_B.SPIIOInterruptdriven_o1 =
        SPI1Buffer_Receive[2];
      SPI1Buffer_Send[0] = 512U;
      Vejlupek_CAR4_dsPIC33_ACU44_M_B.SPIIOInterruptdriven_o2 =
        SPI1Buffer_Receive[3];
      SPI1Buffer_Send[1] = 768U;
      Vejlupek_CAR4_dsPIC33_ACU44_M_B.SPIIOInterruptdriven_o3 =
        SPI1Buffer_Receive[4];
      SPI1Buffer_Send[2] = 1024U;
      Vejlupek_CAR4_dsPIC33_ACU44_M_B.SPIIOInterruptdriven_o4 =
        SPI1Buffer_Receive[5];
      SPI1Buffer_Send[3] = 1280U;
      Vejlupek_CAR4_dsPIC33_ACU44_M_B.SPIIOInterruptdriven_o5 =
        SPI1Buffer_Receive[6];
      SPI1Buffer_Send[4] = 1536U;
      Vejlupek_CAR4_dsPIC33_ACU44_M_B.SPIIOInterruptdriven_o6 =
        SPI1Buffer_Receive[0];
      SPI1Buffer_Send[5] = 1792U;
      Vejlupek_CAR4_dsPIC33_ACU44_M_B.SPIIOInterruptdriven_o7 =
        SPI1Buffer_Receive[1];
      SPI1Buffer_Send[6] = 0U;

      {
        uint16_T tmp;
        tmp = SPI1BUF;
      }

      SPI1STAT = SPI1STAT & (0xffff - (1 << 6) ) ;

      /* enable Interrupt */
      if (_SPI1IE == 0) {
        SPI1InterruptIdx = 0;
        _SPI1IF = 1;
        _SPI1IE = 1;
      }
    }

    /* Switch: '<S50>/Switch' incorporates:
     *  Memory: '<S50>/Memory'
     *  RelationalOperator: '<S57>/Compare'
     */
    if (Vejlupek_CAR4_dsPIC33_ACU44_M_B.SPIIOInterruptdriven_o6 > 0U) {
      rtb_Switch = Vejlupek_CAR4_dsPIC33_ACU44_M_B.SPIIOInterruptdriven_o6;
    } else {
      rtb_Switch = Vejlupek_CAR4_dsPIC33_ACU_DWork.Memory_PreviousInput;
    }

    /* S-Function (sfix_bitop): '<S50>/Bitwise Operator1' incorporates:
     *  S-Function (sfix_bitop): '<S50>/Bitwise Operator5'
     */
    rtb_BitwiseOperator1_m = (rtb_Switch & 16383U) ^ 8192U;
    if (Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition1_semaphoreTaken != 0) {
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition1_Buffer0 =
        rtb_BitwiseOperator1_m;
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition1_ActiveBufIdx = false;
    } else {
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition1_Buffer1 =
        rtb_BitwiseOperator1_m;
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition1_ActiveBufIdx = true;
    }

    /* Switch: '<S51>/Switch' incorporates:
     *  Memory: '<S51>/Memory'
     *  RelationalOperator: '<S58>/Compare'
     */
    if (Vejlupek_CAR4_dsPIC33_ACU44_M_B.SPIIOInterruptdriven_o7 > 0U) {
      rtb_Switch_l = Vejlupek_CAR4_dsPIC33_ACU44_M_B.SPIIOInterruptdriven_o7;
    } else {
      rtb_Switch_l = Vejlupek_CAR4_dsPIC33_ACU_DWork.Memory_PreviousInput_m;
    }

    /* S-Function (sfix_bitop): '<S51>/Bitwise Operator1' incorporates:
     *  S-Function (sfix_bitop): '<S51>/Bitwise Operator5'
     */
    rtb_BitwiseOperator1_m = (rtb_Switch_l & 16383U) ^ 8192U;
    if (Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition2_semaphoreTaken != 0) {
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition2_Buffer0 =
        rtb_BitwiseOperator1_m;
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition2_ActiveBufIdx = false;
    } else {
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition2_Buffer1 =
        rtb_BitwiseOperator1_m;
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition2_ActiveBufIdx = true;
    }

    /* Switch: '<S49>/Switch' incorporates:
     *  Memory: '<S49>/Memory'
     *  RelationalOperator: '<S56>/Compare'
     */
    if (Vejlupek_CAR4_dsPIC33_ACU44_M_B.SPIIOInterruptdriven_o2 > 0U) {
      rtb_Switch_o = Vejlupek_CAR4_dsPIC33_ACU44_M_B.SPIIOInterruptdriven_o2;
    } else {
      rtb_Switch_o = Vejlupek_CAR4_dsPIC33_ACU_DWork.Memory_PreviousInput_i;
    }

    /* S-Function (sfix_bitop): '<S49>/Bitwise Operator1' incorporates:
     *  S-Function (sfix_bitop): '<S49>/Bitwise Operator5'
     */
    rtb_BitwiseOperator1_m = (rtb_Switch_o & 16383U) ^ 8192U;
    if (Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition3_semaphoreTaken != 0) {
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition3_Buffer0 =
        rtb_BitwiseOperator1_m;
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition3_ActiveBufIdx = false;
    } else {
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition3_Buffer1 =
        rtb_BitwiseOperator1_m;
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition3_ActiveBufIdx = true;
    }

    /* Switch: '<S52>/Switch' incorporates:
     *  Memory: '<S52>/Memory'
     *  RelationalOperator: '<S59>/Compare'
     */
    if (Vejlupek_CAR4_dsPIC33_ACU44_M_B.SPIIOInterruptdriven_o3 > 0U) {
      rtb_Switch_l3 = Vejlupek_CAR4_dsPIC33_ACU44_M_B.SPIIOInterruptdriven_o3;
    } else {
      rtb_Switch_l3 = Vejlupek_CAR4_dsPIC33_ACU_DWork.Memory_PreviousInput_d;
    }

    /* S-Function (sfix_bitop): '<S52>/Bitwise Operator1' incorporates:
     *  S-Function (sfix_bitop): '<S52>/Bitwise Operator5'
     */
    rtb_BitwiseOperator1_m = (rtb_Switch_l3 & 16383U) ^ 8192U;
    if (Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition4_semaphoreTaken != 0) {
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition4_Buffer0 =
        rtb_BitwiseOperator1_m;
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition4_ActiveBufIdx = false;
    } else {
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition4_Buffer1 =
        rtb_BitwiseOperator1_m;
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition4_ActiveBufIdx = true;
    }

    /* Switch: '<S47>/Switch' incorporates:
     *  Memory: '<S47>/Memory'
     *  RelationalOperator: '<S54>/Compare'
     */
    if (Vejlupek_CAR4_dsPIC33_ACU44_M_B.SPIIOInterruptdriven_o4 > 0U) {
      rtb_Switch_j = Vejlupek_CAR4_dsPIC33_ACU44_M_B.SPIIOInterruptdriven_o4;
    } else {
      rtb_Switch_j = Vejlupek_CAR4_dsPIC33_ACU_DWork.Memory_PreviousInput_p;
    }

    /* S-Function (sfix_bitop): '<S47>/Bitwise Operator1' incorporates:
     *  S-Function (sfix_bitop): '<S47>/Bitwise Operator5'
     */
    rtb_BitwiseOperator1_m = (rtb_Switch_j & 16383U) ^ 8192U;
    if (Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition5_semaphoreTaken != 0) {
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition5_Buffer0 =
        rtb_BitwiseOperator1_m;
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition5_ActiveBufIdx = false;
    } else {
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition5_Buffer1 =
        rtb_BitwiseOperator1_m;
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition5_ActiveBufIdx = true;
    }

    /* Switch: '<S48>/Switch' incorporates:
     *  Memory: '<S48>/Memory'
     *  RelationalOperator: '<S55>/Compare'
     */
    if (Vejlupek_CAR4_dsPIC33_ACU44_M_B.SPIIOInterruptdriven_o5 > 0U) {
      rtb_BitwiseOperator1_m =
        Vejlupek_CAR4_dsPIC33_ACU44_M_B.SPIIOInterruptdriven_o5;
    } else {
      rtb_BitwiseOperator1_m =
        Vejlupek_CAR4_dsPIC33_ACU_DWork.Memory_PreviousInput_a;
    }

    /* S-Function (sfix_bitop): '<S48>/Bitwise Operator1' incorporates:
     *  S-Function (sfix_bitop): '<S48>/Bitwise Operator5'
     */
    rtb_BitwiseOperator1_mg = (rtb_BitwiseOperator1_m & 16383U) ^ 8192U;
    if (Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition_semaphoreTaken != 0) {
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition_Buffer0 =
        rtb_BitwiseOperator1_mg;
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition_ActiveBufIdx = false;
    } else {
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition_Buffer1 =
        rtb_BitwiseOperator1_mg;
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition_ActiveBufIdx = true;
    }

    /* Update for Memory: '<S50>/Memory' */
    Vejlupek_CAR4_dsPIC33_ACU_DWork.Memory_PreviousInput = rtb_Switch;

    /* Update for Memory: '<S51>/Memory' */
    Vejlupek_CAR4_dsPIC33_ACU_DWork.Memory_PreviousInput_m = rtb_Switch_l;

    /* Update for Memory: '<S49>/Memory' */
    Vejlupek_CAR4_dsPIC33_ACU_DWork.Memory_PreviousInput_i = rtb_Switch_o;

    /* Update for Memory: '<S52>/Memory' */
    Vejlupek_CAR4_dsPIC33_ACU_DWork.Memory_PreviousInput_d = rtb_Switch_l3;

    /* Update for Memory: '<S47>/Memory' */
    Vejlupek_CAR4_dsPIC33_ACU_DWork.Memory_PreviousInput_p = rtb_Switch_j;

    /* Update for Memory: '<S48>/Memory' */
    Vejlupek_CAR4_dsPIC33_ACU_DWork.Memory_PreviousInput_a =
      rtb_BitwiseOperator1_m;
  }
}

/* Model step function for TID2 */
void Vejlupek_CAR4_dsPIC33_ACU44_MainBoard_direct_step2(void) /* Sample time: [0.005s, 0.0s] */
{
  {
    uint16_T rtb_RateTransition1;
    uint8_T rtb_FixPtSum1;
    uint8_T rtb_FixPtSwitch;
    if (Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition1_ActiveBufIdx == 0) {
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition1_semaphoreTaken = false;
      rtb_RateTransition1 =
        Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition1_Buffer0;
    } else {
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition1_semaphoreTaken = true;
      rtb_RateTransition1 =
        Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition1_Buffer1;
    }

    /* DataTypeConversion: '<S80>/Data Type Conversion21' incorporates:
     *  S-Function (sfix_bitop): '<S80>/Get Lower Byte #10'
     */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion21 = (uint8_T)
      (rtb_RateTransition1 & 255U);

    /* DataTypeConversion: '<S80>/Data Type Conversion22' incorporates:
     *  ArithShift: '<S94>/shift'
     */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion22 = (uint8_T)
      (rtb_RateTransition1 >> 8);
    if (Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition2_ActiveBufIdx == 0) {
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition2_semaphoreTaken = false;
      rtb_RateTransition1 =
        Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition2_Buffer0;
    } else {
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition2_semaphoreTaken = true;
      rtb_RateTransition1 =
        Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition2_Buffer1;
    }

    /* DataTypeConversion: '<S80>/Data Type Conversion23' incorporates:
     *  S-Function (sfix_bitop): '<S80>/Get Lower Byte #11'
     */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion23 = (uint8_T)
      (rtb_RateTransition1 & 255U);

    /* DataTypeConversion: '<S80>/Data Type Conversion24' incorporates:
     *  ArithShift: '<S95>/shift'
     */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion24 = (uint8_T)
      (rtb_RateTransition1 >> 8);
    if (Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition3_ActiveBufIdx == 0) {
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition3_semaphoreTaken = false;
      rtb_RateTransition1 =
        Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition3_Buffer0;
    } else {
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition3_semaphoreTaken = true;
      rtb_RateTransition1 =
        Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition3_Buffer1;
    }

    /* DataTypeConversion: '<S80>/Data Type Conversion25' incorporates:
     *  S-Function (sfix_bitop): '<S80>/Get Lower Byte #12'
     */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion25 = (uint8_T)
      (rtb_RateTransition1 & 255U);

    /* DataTypeConversion: '<S80>/Data Type Conversion26' incorporates:
     *  ArithShift: '<S96>/shift'
     */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion26 = (uint8_T)
      (rtb_RateTransition1 >> 8);
    if (Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition4_ActiveBufIdx == 0) {
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition4_semaphoreTaken = false;
      rtb_RateTransition1 =
        Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition4_Buffer0;
    } else {
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition4_semaphoreTaken = true;
      rtb_RateTransition1 =
        Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition4_Buffer1;
    }

    /* DataTypeConversion: '<S80>/Data Type Conversion27' incorporates:
     *  S-Function (sfix_bitop): '<S80>/Get Lower Byte #16'
     */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion27 = (uint8_T)
      (rtb_RateTransition1 & 255U);

    /* DataTypeConversion: '<S80>/Data Type Conversion28' incorporates:
     *  ArithShift: '<S98>/shift'
     */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion28 = (uint8_T)
      (rtb_RateTransition1 >> 8);
    if (Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition5_ActiveBufIdx == 0) {
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition5_semaphoreTaken = false;
      rtb_RateTransition1 =
        Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition5_Buffer0;
    } else {
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition5_semaphoreTaken = true;
      rtb_RateTransition1 =
        Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition5_Buffer1;
    }

    /* DataTypeConversion: '<S80>/Data Type Conversion29' incorporates:
     *  S-Function (sfix_bitop): '<S80>/Get Lower Byte #13'
     */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion29 = (uint8_T)
      (rtb_RateTransition1 & 255U);

    /* DataTypeConversion: '<S80>/Data Type Conversion30' incorporates:
     *  ArithShift: '<S97>/shift'
     */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion30 = (uint8_T)
      (rtb_RateTransition1 >> 8);

    /* UnitDelay: '<S93>/Output' */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.Output =
      Vejlupek_CAR4_dsPIC33_ACU_DWork.Output_DSTATE;

    /* RateTransition: '<Root>/Rate Transition8' */
    if (Vejlupek_CAR4_dsPIC33_ACU44__M->Timing.RateInteraction.TID2_3) {
      Vejlupek_CAR4_dsPIC33_ACU44_M_B.RateTransition8 =
        Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition8_Buffer0;
    }

    /* DataTypeConversion: '<S79>/Data Type Conversion10' incorporates:
     *  S-Function (sfix_bitop): '<S79>/Get Lower Byte #5'
     */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion10 = (uint8_T)
      (Vejlupek_CAR4_dsPIC33_ACU44_M_B.RateTransition8 & 255U);

    /* DataTypeConversion: '<S79>/Data Type Conversion12' incorporates:
     *  ArithShift: '<S88>/shift'
     */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion12 = (uint8_T)
      (Vejlupek_CAR4_dsPIC33_ACU44_M_B.RateTransition8 >> 8);

    /* RateTransition: '<Root>/Rate Transition9' */
    if (Vejlupek_CAR4_dsPIC33_ACU44__M->Timing.RateInteraction.TID2_3) {
      Vejlupek_CAR4_dsPIC33_ACU44_M_B.RateTransition9 =
        Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition9_Buffer0;
    }

    /* DataTypeConversion: '<S79>/Data Type Conversion13' incorporates:
     *  S-Function (sfix_bitop): '<S79>/Get Lower Byte #6'
     */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion13 = (uint8_T)
      (Vejlupek_CAR4_dsPIC33_ACU44_M_B.RateTransition9 & 255U);

    /* DataTypeConversion: '<S79>/Data Type Conversion14' incorporates:
     *  ArithShift: '<S89>/shift'
     */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion14 = (uint8_T)
      (Vejlupek_CAR4_dsPIC33_ACU44_M_B.RateTransition9 >> 8);

    /* DataTypeConversion: '<S79>/Data Type Conversion15' incorporates:
     *  DataStoreRead: '<Root>/QEI 3'
     *  S-Function (sfix_bitop): '<S79>/Get Lower Byte #7'
     */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion15 = (uint8_T)(ct_turnsE3
      & 255U);

    /* DataTypeConversion: '<S79>/Data Type Conversion16' incorporates:
     *  ArithShift: '<S90>/shift'
     *  DataStoreRead: '<Root>/QEI 3'
     */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion16 = (uint8_T)(ct_turnsE3 >>
      8);

    /* DataTypeConversion: '<S79>/Data Type Conversion17' incorporates:
     *  DataStoreRead: '<Root>/QEI 4'
     *  S-Function (sfix_bitop): '<S79>/Get Lower Byte #8'
     */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion17 = (uint8_T)(ct_turnsE4
      & 255U);

    /* DataTypeConversion: '<S79>/Data Type Conversion18' incorporates:
     *  ArithShift: '<S91>/shift'
     *  DataStoreRead: '<Root>/QEI 4'
     */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion18 = (uint8_T)(ct_turnsE4 >>
      8);
    if (Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition_ActiveBufIdx == 0) {
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition_semaphoreTaken = false;
      rtb_RateTransition1 =
        Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition_Buffer0;
    } else {
      Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition_semaphoreTaken = true;
      rtb_RateTransition1 =
        Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition_Buffer1;
    }

    /* DataTypeConversion: '<S79>/Data Type Conversion19' incorporates:
     *  S-Function (sfix_bitop): '<S79>/Get Lower Byte #9'
     */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion19 = (uint8_T)
      (rtb_RateTransition1 & 255U);

    /* DataTypeConversion: '<S79>/Data Type Conversion20' incorporates:
     *  ArithShift: '<S92>/shift'
     */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion20 = (uint8_T)
      (rtb_RateTransition1 >> 8);

    /* UnitDelay: '<S81>/Output' */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.Output_f =
      Vejlupek_CAR4_dsPIC33_ACU_DWork.Output_DSTATE_c;

    /* S-Function "ADC" Block: <S8>/current */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.current_o1 = ADCBuffChannelDMA[0];
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.current_o2 = ADCBuffChannelDMA[1];

    /* DataTypeConversion: '<S78>/Data Type Conversion2' incorporates:
     *  S-Function (sfix_bitop): '<S78>/Get Lower Byte #1'
     */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion2 = (uint8_T)
      (Vejlupek_CAR4_dsPIC33_ACU44_M_B.current_o2 & 255U);

    /* DataTypeConversion: '<S78>/Data Type Conversion3' incorporates:
     *  ArithShift: '<S82>/shift'
     */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion3_e = (uint8_T)
      (Vejlupek_CAR4_dsPIC33_ACU44_M_B.current_o2 >> 8);

    /* DataTypeConversion: '<S78>/Data Type Conversion4' incorporates:
     *  S-Function (sfix_bitop): '<S78>/Get Lower Byte #2'
     */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion4 = (uint8_T)
      (Vejlupek_CAR4_dsPIC33_ACU44_M_B.current_o1 & 255U);

    /* DataTypeConversion: '<S78>/Data Type Conversion5' incorporates:
     *  ArithShift: '<S83>/shift'
     */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion5 = (uint8_T)
      (Vejlupek_CAR4_dsPIC33_ACU44_M_B.current_o1 >> 8);

    /* DataTypeConversion: '<S78>/Data Type Conversion6' incorporates:
     *  DataStoreRead: '<Root>/Current M3'
     *  S-Function (sfix_bitop): '<S78>/Get Lower Byte #3'
     */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion6 = (uint8_T)(Current_M3 &
      255U);

    /* DataTypeConversion: '<S78>/Data Type Conversion7' incorporates:
     *  ArithShift: '<S84>/shift'
     *  DataStoreRead: '<Root>/Current M3'
     */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion7 = (uint8_T)(Current_M3 >>
      8);

    /* DataTypeConversion: '<S78>/Data Type Conversion8' incorporates:
     *  DataStoreRead: '<Root>/Current M4'
     *  S-Function (sfix_bitop): '<S78>/Get Lower Byte #4'
     */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion8 = (uint8_T)(Current_M4 &
      255U);

    /* DataTypeConversion: '<S78>/Data Type Conversion9' incorporates:
     *  ArithShift: '<S85>/shift'
     *  DataStoreRead: '<Root>/Current M4'
     */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion9 = (uint8_T)(Current_M4 >>
      8);

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

      TxTab_Uart1[TxPtrHaut_Uart1] = ((uint8_T)211U);
      TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (64-1);
      _U1TXIF = 1;
      _U1TXIE = 1;
      do {
        Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 64	*/
        Tmp = Tmp & (64-1);
      } while (Tmp == 0);

      TxTab_Uart1[TxPtrHaut_Uart1] = ((uint8_T)211U);
      TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (64-1);
      _U1TXIF = 1;
      _U1TXIE = 1;
      do {
        Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 64	*/
        Tmp = Tmp & (64-1);
      } while (Tmp == 0);

      TxTab_Uart1[TxPtrHaut_Uart1] = Vejlupek_CAR4_dsPIC33_ACU44_M_B.Output_f;
      TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (64-1);
      _U1TXIF = 1;
      _U1TXIE = 1;
      do {
        Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 64	*/
        Tmp = Tmp & (64-1);
      } while (Tmp == 0);

      TxTab_Uart1[TxPtrHaut_Uart1] =
        Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion2;
      TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (64-1);
      _U1TXIF = 1;
      _U1TXIE = 1;
      do {
        Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 64	*/
        Tmp = Tmp & (64-1);
      } while (Tmp == 0);

      TxTab_Uart1[TxPtrHaut_Uart1] =
        Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion3_e;
      TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (64-1);
      _U1TXIF = 1;
      _U1TXIE = 1;
      do {
        Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 64	*/
        Tmp = Tmp & (64-1);
      } while (Tmp == 0);

      TxTab_Uart1[TxPtrHaut_Uart1] =
        Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion4;
      TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (64-1);
      _U1TXIF = 1;
      _U1TXIE = 1;
      do {
        Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 64	*/
        Tmp = Tmp & (64-1);
      } while (Tmp == 0);

      TxTab_Uart1[TxPtrHaut_Uart1] =
        Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion5;
      TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (64-1);
      _U1TXIF = 1;
      _U1TXIE = 1;
      do {
        Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 64	*/
        Tmp = Tmp & (64-1);
      } while (Tmp == 0);

      TxTab_Uart1[TxPtrHaut_Uart1] =
        Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion6;
      TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (64-1);
      _U1TXIF = 1;
      _U1TXIE = 1;
      do {
        Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 64	*/
        Tmp = Tmp & (64-1);
      } while (Tmp == 0);

      TxTab_Uart1[TxPtrHaut_Uart1] =
        Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion7;
      TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (64-1);
      _U1TXIF = 1;
      _U1TXIE = 1;
      do {
        Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 64	*/
        Tmp = Tmp & (64-1);
      } while (Tmp == 0);

      TxTab_Uart1[TxPtrHaut_Uart1] =
        Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion8;
      TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (64-1);
      _U1TXIF = 1;
      _U1TXIE = 1;
      do {
        Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 64	*/
        Tmp = Tmp & (64-1);
      } while (Tmp == 0);

      TxTab_Uart1[TxPtrHaut_Uart1] =
        Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion9;
      TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (64-1);
      _U1TXIF = 1;
      _U1TXIE = 1;
    }

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

      TxTab_Uart1[TxPtrHaut_Uart1] =
        Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion10;
      TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (64-1);
      _U1TXIF = 1;
      _U1TXIE = 1;
      do {
        Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 64	*/
        Tmp = Tmp & (64-1);
      } while (Tmp == 0);

      TxTab_Uart1[TxPtrHaut_Uart1] =
        Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion12;
      TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (64-1);
      _U1TXIF = 1;
      _U1TXIE = 1;
      do {
        Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 64	*/
        Tmp = Tmp & (64-1);
      } while (Tmp == 0);

      TxTab_Uart1[TxPtrHaut_Uart1] =
        Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion13;
      TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (64-1);
      _U1TXIF = 1;
      _U1TXIE = 1;
      do {
        Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 64	*/
        Tmp = Tmp & (64-1);
      } while (Tmp == 0);

      TxTab_Uart1[TxPtrHaut_Uart1] =
        Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion14;
      TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (64-1);
      _U1TXIF = 1;
      _U1TXIE = 1;
      do {
        Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 64	*/
        Tmp = Tmp & (64-1);
      } while (Tmp == 0);

      TxTab_Uart1[TxPtrHaut_Uart1] =
        Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion15;
      TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (64-1);
      _U1TXIF = 1;
      _U1TXIE = 1;
      do {
        Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 64	*/
        Tmp = Tmp & (64-1);
      } while (Tmp == 0);

      TxTab_Uart1[TxPtrHaut_Uart1] =
        Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion16;
      TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (64-1);
      _U1TXIF = 1;
      _U1TXIE = 1;
      do {
        Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 64	*/
        Tmp = Tmp & (64-1);
      } while (Tmp == 0);

      TxTab_Uart1[TxPtrHaut_Uart1] =
        Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion17;
      TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (64-1);
      _U1TXIF = 1;
      _U1TXIE = 1;
      do {
        Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 64	*/
        Tmp = Tmp & (64-1);
      } while (Tmp == 0);

      TxTab_Uart1[TxPtrHaut_Uart1] =
        Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion18;
      TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (64-1);
      _U1TXIF = 1;
      _U1TXIE = 1;
      do {
        Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 64	*/
        Tmp = Tmp & (64-1);
      } while (Tmp == 0);

      TxTab_Uart1[TxPtrHaut_Uart1] =
        Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion19;
      TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (64-1);
      _U1TXIF = 1;
      _U1TXIE = 1;
      do {
        Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 64	*/
        Tmp = Tmp & (64-1);
      } while (Tmp == 0);

      TxTab_Uart1[TxPtrHaut_Uart1] =
        Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion20;
      TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (64-1);
      _U1TXIF = 1;
      _U1TXIE = 1;
    }

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

      TxTab_Uart1[TxPtrHaut_Uart1] =
        Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion21;
      TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (64-1);
      _U1TXIF = 1;
      _U1TXIE = 1;
      do {
        Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 64	*/
        Tmp = Tmp & (64-1);
      } while (Tmp == 0);

      TxTab_Uart1[TxPtrHaut_Uart1] =
        Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion22;
      TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (64-1);
      _U1TXIF = 1;
      _U1TXIE = 1;
      do {
        Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 64	*/
        Tmp = Tmp & (64-1);
      } while (Tmp == 0);

      TxTab_Uart1[TxPtrHaut_Uart1] =
        Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion23;
      TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (64-1);
      _U1TXIF = 1;
      _U1TXIE = 1;
      do {
        Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 64	*/
        Tmp = Tmp & (64-1);
      } while (Tmp == 0);

      TxTab_Uart1[TxPtrHaut_Uart1] =
        Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion24;
      TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (64-1);
      _U1TXIF = 1;
      _U1TXIE = 1;
      do {
        Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 64	*/
        Tmp = Tmp & (64-1);
      } while (Tmp == 0);

      TxTab_Uart1[TxPtrHaut_Uart1] =
        Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion25;
      TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (64-1);
      _U1TXIF = 1;
      _U1TXIE = 1;
      do {
        Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 64	*/
        Tmp = Tmp & (64-1);
      } while (Tmp == 0);

      TxTab_Uart1[TxPtrHaut_Uart1] =
        Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion26;
      TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (64-1);
      _U1TXIF = 1;
      _U1TXIE = 1;
      do {
        Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 64	*/
        Tmp = Tmp & (64-1);
      } while (Tmp == 0);

      TxTab_Uart1[TxPtrHaut_Uart1] =
        Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion27;
      TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (64-1);
      _U1TXIF = 1;
      _U1TXIE = 1;
      do {
        Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 64	*/
        Tmp = Tmp & (64-1);
      } while (Tmp == 0);

      TxTab_Uart1[TxPtrHaut_Uart1] =
        Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion28;
      TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (64-1);
      _U1TXIF = 1;
      _U1TXIE = 1;
      do {
        Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 64	*/
        Tmp = Tmp & (64-1);
      } while (Tmp == 0);

      TxTab_Uart1[TxPtrHaut_Uart1] =
        Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion29;
      TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (64-1);
      _U1TXIF = 1;
      _U1TXIE = 1;
      do {
        Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 64	*/
        Tmp = Tmp & (64-1);
      } while (Tmp == 0);

      TxTab_Uart1[TxPtrHaut_Uart1] =
        Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion30;
      TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (64-1);
      _U1TXIF = 1;
      _U1TXIE = 1;
      do {
        Tmp = ~(TxPtrHaut_Uart1 - TxPtrBas_Uart1);/* Tmp = 7 - (Haut - Bas) modulo 64	*/
        Tmp = Tmp & (64-1);
      } while (Tmp == 0);

      TxTab_Uart1[TxPtrHaut_Uart1] = Vejlupek_CAR4_dsPIC33_ACU44_M_B.Output;
      TxPtrHaut_Uart1 = (TxPtrHaut_Uart1 + 1) & (64-1);
      _U1TXIF = 1;
      _U1TXIE = 1;
    }

    /* Switch: '<S87>/FixPt Switch' incorporates:
     *  Constant: '<S86>/FixPt Constant'
     *  Sum: '<S86>/FixPt Sum1'
     */
    rtb_FixPtSwitch = (uint8_T)(uint16_T)
      (Vejlupek_CAR4_dsPIC33_ACU44_M_B.Output_f + 1);

    /* Sum: '<S99>/FixPt Sum1' incorporates:
     *  Constant: '<S99>/FixPt Constant'
     */
    rtb_FixPtSum1 = (uint8_T)(uint16_T)(Vejlupek_CAR4_dsPIC33_ACU44_M_B.Output +
      1);

    /* S-Function "dsPIC_C_function_Call" Block: <S10>/C Function Call [ecanFunctions.c] */
    ecan1_receive_matlab
      (&Vejlupek_CAR4_dsPIC33_ACU44_M_B.CFunctionCallecanFunctionsc[0]);

    /* Outputs for enable SubSystem: '<Root>/Enabled Subsystem' incorporates:
     *  Constant: '<S5>/Constant'
     *  DataTypeConversion: '<S38>/Extract Desired Bits'
     *  EnablePort: '<S4>/Enable'
     *  Logic: '<Root>/Logical Operator'
     *  RelationalOperator: '<S34>/Compare'
     *  RelationalOperator: '<S5>/Compare'
     */
    if (((Vejlupek_CAR4_dsPIC33_ACU44_M_B.CFunctionCallecanFunctionsc[0] &
          536870911UL) == 768UL) &&
        ((Vejlupek_CAR4_dsPIC33_ACU44_M_B.CFunctionCallecanFunctionsc[0] != 0UL)
         != 0)) {
      if (Vejlupek_CAR4_dsPIC33_ACU_DWork.EnabledSubsystem_MODE ==
          SUBSYS_DISABLED) {
        Vejlupek_CAR4_dsPIC33_ACU_DWork.EnabledSubsystem_MODE = SUBSYS_ENABLED;
      }
    } else {
      if (Vejlupek_CAR4_dsPIC33_ACU_DWork.EnabledSubsystem_MODE ==
          SUBSYS_ENABLED) {
        Vejlupek_CAR4_dsPIC33_ACU_DWork.EnabledSubsystem_MODE = SUBSYS_DISABLED;
      }
    }

    if (Vejlupek_CAR4_dsPIC33_ACU_DWork.EnabledSubsystem_MODE == SUBSYS_ENABLED)
    {
      /* DataStoreWrite: '<S4>/Data Store Write' incorporates:
       *  ArithShift: '<S23>/shift'
       *  DataTypeConversion: '<S19>/Data Type Conversion4'
       *  DataTypeConversion: '<S19>/Data Type Conversion5'
       *  DataTypeConversion: '<S39>/Extract Desired Bits'
       *  DataTypeConversion: '<S40>/Extract Desired Bits'
       *  S-Function (sfix_bitop): '<S19>/null lower 4 bites2'
       */
      ct_turnsE3 = (uint16_T)(uint8_T)
        (Vejlupek_CAR4_dsPIC33_ACU44_M_B.CFunctionCallecanFunctionsc[1] >> 8) <<
        8 | (uint16_T)(uint8_T)
        Vejlupek_CAR4_dsPIC33_ACU44_M_B.CFunctionCallecanFunctionsc[1];

      /* DataStoreWrite: '<S4>/Data Store Write1' incorporates:
       *  ArithShift: '<S24>/shift'
       *  DataTypeConversion: '<S20>/Data Type Conversion4'
       *  DataTypeConversion: '<S20>/Data Type Conversion5'
       *  DataTypeConversion: '<S41>/Extract Desired Bits'
       *  DataTypeConversion: '<S42>/Extract Desired Bits'
       *  S-Function (sfix_bitop): '<S20>/null lower 4 bites2'
       */
      ct_turnsE4 = (uint16_T)
        (Vejlupek_CAR4_dsPIC33_ACU44_M_B.CFunctionCallecanFunctionsc[1] >> 24) <<
        8 | (uint16_T)(uint8_T)
        (Vejlupek_CAR4_dsPIC33_ACU44_M_B.CFunctionCallecanFunctionsc[1] >> 16);

      /* DataStoreWrite: '<S4>/Data Store Write2' incorporates:
       *  ArithShift: '<S21>/shift'
       *  DataTypeConversion: '<S17>/Data Type Conversion4'
       *  DataTypeConversion: '<S17>/Data Type Conversion5'
       *  DataTypeConversion: '<S43>/Extract Desired Bits'
       *  DataTypeConversion: '<S44>/Extract Desired Bits'
       *  S-Function (sfix_bitop): '<S17>/null lower 4 bites2'
       */
      Current_M3 = (uint16_T)(uint8_T)
        (Vejlupek_CAR4_dsPIC33_ACU44_M_B.CFunctionCallecanFunctionsc[2] >> 8) <<
        8 | (uint16_T)(uint8_T)
        Vejlupek_CAR4_dsPIC33_ACU44_M_B.CFunctionCallecanFunctionsc[2];

      /* DataStoreWrite: '<S4>/Data Store Write3' incorporates:
       *  ArithShift: '<S22>/shift'
       *  DataTypeConversion: '<S18>/Data Type Conversion4'
       *  DataTypeConversion: '<S18>/Data Type Conversion5'
       *  DataTypeConversion: '<S45>/Extract Desired Bits'
       *  DataTypeConversion: '<S46>/Extract Desired Bits'
       *  S-Function (sfix_bitop): '<S18>/null lower 4 bites2'
       */
      Current_M4 = (uint16_T)
        (Vejlupek_CAR4_dsPIC33_ACU44_M_B.CFunctionCallecanFunctionsc[2] >> 24) <<
        8 | (uint16_T)(uint8_T)
        (Vejlupek_CAR4_dsPIC33_ACU44_M_B.CFunctionCallecanFunctionsc[2] >> 16);
    }

    /* end of Outputs for SubSystem: '<Root>/Enabled Subsystem' */

    /* Embedded MATLAB: '<S12>/myMux' incorporates:
     *  ArithShift: '<S31>/shift'
     *  ArithShift: '<S32>/shift'
     *  ArithShift: '<S33>/shift'
     *  ArithShift: '<S67>/shift'
     *  Constant: '<S12>/identifier'
     *  Constant: '<S9>/data length - set maualy'
     *  Constant: '<S9>/just a dummy'
     *  DataStoreRead: '<S9>/Buttons Ts = 5e-3'
     *  DataStoreRead: '<S9>/DISABLE Ts = 5e-3'
     *  DataStoreRead: '<S9>/Motor_Momentum Ts = 5e-3'
     *  DataStoreRead: '<S9>/Steering_Rear Ts = 5e-3'
     *  DataTypeConversion: '<S27>/Data Type Conversion2'
     *  DataTypeConversion: '<S27>/Data Type Conversion3'
     *  DataTypeConversion: '<S28>/Data Type Conversion2'
     *  DataTypeConversion: '<S28>/Data Type Conversion3'
     *  DataTypeConversion: '<S29>/Data Type Conversion2'
     *  DataTypeConversion: '<S29>/Data Type Conversion3'
     *  DataTypeConversion: '<S65>/Data Type Conversion'
     *  DataTypeConversion: '<S9>/Data Type Conversion'
     *  S-Function (sfix_bitop): '<S27>/Get Lower Byte'
     *  S-Function (sfix_bitop): '<S28>/Get Lower Byte'
     *  S-Function (sfix_bitop): '<S29>/Get Lower Byte'
     *  S-Function (sfix_bitop): '<S65>/Bitwise Operator'
     *  SignalConversion: '<S63>/TmpHiddenBufferAt SFunction Inport5'
     */
    /* Embedded MATLAB Function 'Send ECAN1 Message/myMux': '<S63>:1' */
    /* '<S63>:1:2' */
    /* '<S63>:1:3' */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.y[0] = 2048U;
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.y[1] = 1024U;
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.y[2] = 1024U;
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.y[3] = 0U;
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.y[4] = 0U;
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.y[5] = 0U;
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.y[6] = 0U;
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.y[7] = 0U;

    /* '<S63>:1:7' */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.y[4] = (uint16_T)(uint8_T)(Steering_Rear &
      255) << 8 | (uint16_T)(uint8_T)(Steering_Rear >> 8);

    /* '<S63>:1:12' */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.y[5] = (uint16_T)(uint8_T)(Motor_Momentum &
      255) << 8 | (uint16_T)(uint8_T)(Motor_Momentum >> 8);

    /* '<S63>:1:17' */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.y[6] = (uint16_T)(uint8_T)(Buttons & 255U) <<
      8 | (uint16_T)(uint8_T)(Buttons >> 8);

    /* '<S63>:1:22' */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.y[7] = (uint16_T)(DISABLE << 8 | 211);

    /* Outputs for enable SubSystem: '<S12>/C-function Call' incorporates:
     *  Constant: '<S30>/Constant'
     *  Constant: '<S9>/data length - set maualy'
     *  EnablePort: '<S60>/Enable'
     *  RelationalOperator: '<S30>/Compare'
     */
    if (Vejlupek_CAR4_dsPIC33_ACU_DWork.CfunctionCall_MODE == SUBSYS_DISABLED) {
      Vejlupek_CAR4_dsPIC33_ACU_DWork.CfunctionCall_MODE = SUBSYS_ENABLED;
    }

    if (Vejlupek_CAR4_dsPIC33_ACU_DWork.CfunctionCall_MODE == SUBSYS_ENABLED) {
      /* S-Function "dsPIC_C_function_Call" Block: <S60>/ECAN1 Transmit [ecanFunctions.c] */
      ecan1_transmit_matlab(Vejlupek_CAR4_dsPIC33_ACU44_M_B.y);
    }

    /* end of Outputs for SubSystem: '<S12>/C-function Call' */

    /* Update for UnitDelay: '<S93>/Output' */
    Vejlupek_CAR4_dsPIC33_ACU_DWork.Output_DSTATE = rtb_FixPtSum1;

    /* Update for UnitDelay: '<S81>/Output' */
    Vejlupek_CAR4_dsPIC33_ACU_DWork.Output_DSTATE_c = rtb_FixPtSwitch;
  }
}

/* Model step function for TID3 */
void Vejlupek_CAR4_dsPIC33_ACU44_MainBoard_direct_step3(void) /* Sample time: [0.01s, 0.0s] */
{
  /* local block i/o variables */
  uint16_T rtb_ButtonsTs1e2;
  boolean_T rtb_IntegertoBitConverter[12];

  {
    int16_T rtb_Abs;
    boolean_T rtb_DISABLETs1e2;
    real_T rtb_PulseGenerator4;

    /* DataStoreRead: '<S2>/Buttons Ts = 1e-2' */
    rtb_ButtonsTs1e2 = Buttons;

    /* DataStoreRead: '<S2>/Steering_Front Ts = 1e-2' */
    rtb_Abs = Steering_Front;

    /* Switch: '<S2>/Switch1' incorporates:
     *  Constant: '<S2>/Zero steering angle'
     *  S-Function (sfix_bitop): '<S2>/Bitwise Operator2'
     */
    if ((rtb_ButtonsTs1e2 & 32U) > 0U) {
      rtb_Abs = 0;
    }

    /* Sum: '<Root>/Sum1' incorporates:
     *  Constant: '<Root>/Servo 3'
     */
    rtb_ButtonsTs1e2 = 4012U + (uint16_T)rtb_Abs;

    /* Saturate: '<Root>/Saturation' */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.Saturation = rt_SATURATE(rtb_ButtonsTs1e2,
      2592U, 5162U);

    /* Sum: '<Root>/Sum' incorporates:
     *  Constant: '<Root>/Servo 1'
     */
    rtb_ButtonsTs1e2 = (uint16_T)rtb_Abs + 3813U;

    /* Saturate: '<Root>/Saturation1' */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.Saturation1 = rt_SATURATE(rtb_ButtonsTs1e2,
      2038U, 4765U);

    /* S-Function "dsPIC_PWM_OC_HW" Block: <Root>/Output Compare HW */
    {
      if (Vejlupek_CAR4_dsPIC33_ACU44_M_B.Saturation > 49999)/* Saturate */
        OC1RS = 49999;
      else
        OC1RS = Vejlupek_CAR4_dsPIC33_ACU44_M_B.Saturation;
      if (Vejlupek_CAR4_dsPIC33_ACU44_M_B.Saturation1 > 49999)/* Saturate */
        OC2RS = 49999;
      else
        OC2RS = Vejlupek_CAR4_dsPIC33_ACU44_M_B.Saturation1;
    }

    /* DataStoreRead: '<S1>/Steering_Rear Ts = 1e-2' */
    rtb_Abs = Steering_Front;

    /* DataStoreRead: '<S1>/Buttons Ts = 1e-2' */
    rtb_ButtonsTs1e2 = Buttons;

    /* Switch: '<S1>/Switch' incorporates:
     *  DataStoreRead: '<S1>/Motor_Momentum Ts = 1e-2'
     *  S-Function (sfix_bitop): '<S1>/Bitwise Operator1'
     */
    if (!((rtb_ButtonsTs1e2 & 16U) > 0U)) {
      rtb_Abs = Motor_Momentum;
    }

    /* Gain: '<Root>/Gain' */
    rtb_Abs <<= 1U;

    /* Saturate: '<Root>/Saturation2' */
    rtb_Abs = rt_SATURATE(rtb_Abs, -3400, 3400);

    /* RelationalOperator: '<S25>/Compare' */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.Compare = (rtb_Abs <= 0);

    /* S-Function "dsPIC_Digital_OutputWrite" Block: <S7>/DIR 1 */
    LATAbits.LATA10 = Vejlupek_CAR4_dsPIC33_ACU44_M_B.Compare;

    /* RelationalOperator: '<S26>/Compare' */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.Compare_k = (rtb_Abs <= 0);

    /* S-Function "dsPIC_Digital_OutputWrite" Block: <S7>/DIR 2 */
    LATBbits.LATB10 = Vejlupek_CAR4_dsPIC33_ACU44_M_B.Compare_k;

    /* DataTypeConversion: '<S7>/Data Type Conversion' incorporates:
     *  Abs: '<S7>/Abs'
     */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion = (uint16_T)rt_ABS
      (rtb_Abs);

    /* Abs: '<S7>/Abs1' */
    rtb_Abs = rt_ABS(rtb_Abs);

    /* DataTypeConversion: '<S7>/Data Type Conversion3' */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion3 = (uint16_T)rtb_Abs;

    /* S-Function "dsPIC_PWM_motor" Block: <S7>/PWM Motor Output */
    P1DC1 = Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion ;
    P1DC2 = Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion3 ;

    /* DataStoreRead: '<Root>/DISABLE Ts = 1e-2' */
    rtb_DISABLETs1e2 = DISABLE;

    /* DataStoreRead: '<Root>/Buttons Ts = 1e-2' */
    rtb_ButtonsTs1e2 = Buttons;

    /* Communications Blockset Integer-to-Bit Converter (scominttobit) - '<S13>/Integer to Bit Converter' */
    {
      int bitIdx = 0;
      int bitCount;
      long unsigned int u = (long unsigned int)rtb_ButtonsTs1e2;

      /* Convert every integer-valued input sample to 12 output bits
       * (output bit vector order: LSB-first)
       */
      for (bitCount = 0; bitCount < 12; bitCount++) {
        rtb_IntegertoBitConverter[bitIdx] = (boolean_T)(u & (long unsigned int)1);
        u >>= 1;
        bitIdx += 1;
      }
    }

    /* Logic: '<S13>/Logical Operator1' */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.LogicalOperator1 = (rtb_DISABLETs1e2 ||
      rtb_IntegertoBitConverter[0]);

    /* S-Function "dsPIC_Digital_OutputWrite" Block: <S7>/DISable 1 */
    LATAbits.LATA7 = Vejlupek_CAR4_dsPIC33_ACU44_M_B.LogicalOperator1;

    /* Logic: '<S13>/Logical Operator2' */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.LogicalOperator2 = (rtb_DISABLETs1e2 ||
      rtb_IntegertoBitConverter[1]);

    /* S-Function "dsPIC_Digital_OutputWrite" Block: <S7>/DISable 2 */
    LATCbits.LATC9 = Vejlupek_CAR4_dsPIC33_ACU44_M_B.LogicalOperator2;

    /* S-Function "dsPIC_C_function_Call" Block: <S8>/Angle => Count by 1 roll = 1000 QEI 500 mode x2 without index chx */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.AngleCountby1roll1000QEI500mode = GetQEI1();

    /* S-Function "dsPIC_C_function_Call" Block: <S8>/Angle => Count by 1 roll = 1000 QEI 500 mode x2 without index chx1 */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.AngleCountby1roll1000QEI500mo_f = GetQEI2();

    /* DiscretePulseGenerator: '<S6>/Pulse Generator4' */
    rtb_PulseGenerator4 = (Vejlupek_CAR4_dsPIC33_ACU_DWork.clockTickCounter <
      50L) && (Vejlupek_CAR4_dsPIC33_ACU_DWork.clockTickCounter >= 0L) ? 1.0 :
      0.0;
    if (Vejlupek_CAR4_dsPIC33_ACU_DWork.clockTickCounter >= 99L) {
      Vejlupek_CAR4_dsPIC33_ACU_DWork.clockTickCounter = 0L;
    } else {
      Vejlupek_CAR4_dsPIC33_ACU_DWork.clockTickCounter =
        Vejlupek_CAR4_dsPIC33_ACU_DWork.clockTickCounter + 1L;
    }

    /* DataTypeConversion: '<S6>/Data Type Conversion11' */
    Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion11 = (rtb_PulseGenerator4
      != 0.0);

    /* S-Function "dsPIC_Digital_OutputWrite" Block: <S6>/Op controlLED */
    LATAbits.LATA8 = Vejlupek_CAR4_dsPIC33_ACU44_M_B.DataTypeConversion11;
    Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition8_Buffer0 =
      Vejlupek_CAR4_dsPIC33_ACU44_M_B.AngleCountby1roll1000QEI500mode;
    Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition9_Buffer0 =
      Vejlupek_CAR4_dsPIC33_ACU44_M_B.AngleCountby1roll1000QEI500mo_f;
  }
}

void Vejlupek_CAR4_dsPIC33_ACU44_MainBoard_direct_step(int_T tid)
{
  switch (tid) {
   case 0 :
    Vejlupek_CAR4_dsPIC33_ACU44_MainBoard_direct_step0();
    break;

   case 1 :
    Vejlupek_CAR4_dsPIC33_ACU44_MainBoard_direct_step1();
    break;

   case 2 :
    Vejlupek_CAR4_dsPIC33_ACU44_MainBoard_direct_step2();
    break;

   case 3 :
    Vejlupek_CAR4_dsPIC33_ACU44_MainBoard_direct_step3();
    break;

   default :
    break;
  }
}

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

  /* Registration code */

  /* initialize real-time model */
  (void) memset((void *)Vejlupek_CAR4_dsPIC33_ACU44__M,0,
                sizeof(RT_MODEL_Vejlupek_CAR4_dsPIC33_));

  /* block I/O */
  (void) memset(((void *) &Vejlupek_CAR4_dsPIC33_ACU44_M_B),0,
                sizeof(BlockIO_Vejlupek_CAR4_dsPIC33_A));

  /* states (dwork) */
  (void) memset((void *)&Vejlupek_CAR4_dsPIC33_ACU_DWork, 0,
                sizeof(D_Work_Vejlupek_CAR4_dsPIC33_AC));

  /* exported global states */
  Motor_Momentum = 0;
  Steering_Front = 0;
  Steering_Rear = 0;
  Buttons = 0;
  Current_M3 = 0;
  Current_M4 = 0;
  ct_turnsE1 = 0;
  ct_turnsE2 = 0;
  ct_turnsE3 = 0;
  ct_turnsE4 = 0;
  DISABLE = FALSE;

  /* S-Function "dsPIC_MASTER" initialization Block: <Root>/Master */
  /* 0 testfcy 	*/
  /*  2.0E+007	*/
  /* CONFIG TIMER 1 for scheduling steps 	*/
  ConfigIntTimer1(T1_INT_PRIOR_0 & T1_INT_OFF);
  T1CON = 0x8000 ;                     /* T1_PS_1_1	*/
  PR1 = 19999;

  /* Configuration TIMER 3 */
  ConfigIntTimer3(T3_INT_PRIOR_0 & T3_INT_OFF);
  OpenTimer3(T3_ON & T3_GATE_OFF &
             T3_PS_1_8 & T3_SOURCE_INT , 49999);

  /* Configuration TRIS */
  TRISA = 64127;
  TRISB = 57711;
  TRISC = 64983;

  /* Configuration ADCHS */
  AD1PCFGL = (496U);

  /* S-Function "dsPIC_SPI_config" initialization Block: <Root>/SPI Configuration */
  {
    _SPI1IE = 0;                       /* Disable Interrupts */
    _SPI1EN = 0;                       /* Disable the module */
    _SPI1IF = 0;                       /* Disable the Interrupt flag */
    SPI1CON1 = 1582;                   /* Initalizes the spi module */
    SPI1CON2 = 0;                      /* do not use frame SPI mode  */
    SPI1STAT = 32768;                  /* Enable/Disable the spi module */
  }

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

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

  /* S-Function "dsPIC_PWM_out" initialization Block: <Root>/Output Compare HW */
  /* Initialise Output Capture */
  /*config output capture*/
  OC1RS = 0;
  OC1R = 0;
  OC1CON = (1 << 15) + (1 << 3) + 6;

  /*config output capture*/
  OC2RS = 0;
  OC2R = 0;
  OC2CON = (1 << 15) + (1 << 3) + 6;

  /* S-Function "dsPIC_PWM_motor" initialization Block: <S7>/PWM Motor Output */
  /* Initialise Output Capture */
  SetDCMCPWM1(1,0,0);
  SetDCMCPWM1(2,0,0);

#define config                         (PWM1_INT_DIS & PWM1_FLTA_DIS_INT & PWM1_INT_PR1 & PWM1_FLTA_INT_PR0)

  ConfigIntMCPWM1( config );

#define config1                        (PWM1_EN & 0xDFFF & PWM1_OP_SCALE1 & PWM1_IPCLK_SCALE1 & PWM1_MOD_FREE)
#define config2                        (0xffff & PWM1_PEN1L & PWM1_MOD1_IND & PWM1_PDIS1H & PWM1_PEN2L & PWM1_MOD2_IND & PWM1_PDIS2H & PWM1_PDIS3L & PWM1_PDIS3H)
#define config3                        (PWM1_SEVOPS1 & PWM1_OSYNC_TCY & PWM1_UEN)

  OpenMCPWM1(2000,0,config1,config2,config3);

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

  /* Use internal counter Trigger, continuous simulatenous sampling */
  AD1CON1 = 0x0004 | (7 << 5) | (1 << 10)| OutFormatBitsval;
  AD1CON2 = 0x0400 | (NoSamplesADC << 2) | VoltRef;
  AD1CSSL = (3 & 65535);
  AD1CON3 = (ADCSval | SAMCval) & 0xff7f;
  AD1CHS0 = 0x0000;
  IPC2 = (IPC2 | (6 << 12));           /*Interrupt Priority : 6*/
  _AD1IF = 0;
  DMACS0 = 0;
  DMA4CON = 0;
  DMA4REQ = 13;
  DMA4STA = __builtin_dmaoffset(ADCBuffChannelDMA);
  DMA4PAD = (volatile uint16_T) &ADC1BUF0;
  DMA4CNT = 2-1;
  _DMA4IF = 0;
  _DMA4IE = 0;
  AD1CON1bits.ADON= 1;
  DMA4CONbits.CHEN = 1;

  /* _AD1IE = 1; */
  /* 1 sample/output-Step ; Do not need interrupt so do not enable it. */
  /* Clear Off Any Interrupt Due To Configuration */
  /* Turn on ADC Module */
  AD1CON1bits.ADON= 1;

  /* Start for DiscretePulseGenerator: '<S6>/Pulse Generator4' */
  Vejlupek_CAR4_dsPIC33_ACU_DWork.clockTickCounter = 0L;

  /* S-Function "dsPIC_PIN_Mapping : Config" initialization Block: <Root>/PIN Mapping */
  /*unlock */
  OSCCON = 0x46;
  OSCCON = 0x57;
  OSCCONbits.IOLOCK = 0;
  RPINR14 = 4368;
  RPINR16 = 515;
  RPINR18 = 65298;
  RPINR20 = 65288;
  RPINR26 = 20;
  RPOR2 = 65283;
  RPOR3 = 2303;
  RPOR5 = 5119;
  RPOR6 = 65298;
  RPOR9 = 2047;
  RPOR10 = 4351;

  /*lock*/
  OSCCON = 0x46;
  OSCCON = 0x57;
  OSCCONbits.IOLOCK = 1;

  /* S-Function "dsPIC_C_function_Call" Block: <S3>/C Function Call [ecanFunctions.c] */
  ecan1_init(&Vejlupek_CAR4_dsPIC33_AC_ConstP.Constant_Value_j[0]);

  /* S-Function "dsPIC_C_function_Call" Block: <Root>/QEI Inicialization */
  QEIInit();
  Vejlupek_CAR4_ds_PrevZCSigState.MemoryWriteIfValiddatareceived_ = POS_ZCSIG;

  /* InitializeConditions for Memory: '<S50>/Memory' */
  Vejlupek_CAR4_dsPIC33_ACU_DWork.Memory_PreviousInput = 0U;

  /* InitializeConditions for RateTransition: '<Root>/Rate Transition1' */
  Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition1_Buffer0 = 0U;

  /* InitializeConditions for Memory: '<S51>/Memory' */
  Vejlupek_CAR4_dsPIC33_ACU_DWork.Memory_PreviousInput_m = 0U;

  /* InitializeConditions for RateTransition: '<Root>/Rate Transition2' */
  Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition2_Buffer0 = 0U;

  /* InitializeConditions for Memory: '<S49>/Memory' */
  Vejlupek_CAR4_dsPIC33_ACU_DWork.Memory_PreviousInput_i = 0U;

  /* InitializeConditions for RateTransition: '<Root>/Rate Transition3' */
  Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition3_Buffer0 = 0U;

  /* InitializeConditions for Memory: '<S52>/Memory' */
  Vejlupek_CAR4_dsPIC33_ACU_DWork.Memory_PreviousInput_d = 0U;

  /* InitializeConditions for RateTransition: '<Root>/Rate Transition4' */
  Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition4_Buffer0 = 0U;

  /* InitializeConditions for Memory: '<S47>/Memory' */
  Vejlupek_CAR4_dsPIC33_ACU_DWork.Memory_PreviousInput_p = 0U;

  /* InitializeConditions for RateTransition: '<Root>/Rate Transition5' */
  Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition5_Buffer0 = 0U;

  /* InitializeConditions for RateTransition: '<Root>/Rate Transition8' */
  Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition8_Buffer0 = 0U;

  /* InitializeConditions for RateTransition: '<Root>/Rate Transition9' */
  Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition9_Buffer0 = 0U;

  /* InitializeConditions for Memory: '<S48>/Memory' */
  Vejlupek_CAR4_dsPIC33_ACU_DWork.Memory_PreviousInput_a = 0U;

  /* InitializeConditions for RateTransition: '<Root>/Rate Transition' */
  Vejlupek_CAR4_dsPIC33_ACU_DWork.RateTransition_Buffer0 = 0U;
}

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