File: C:\Users\SLUG\Documents\MATLAB\SLUG_Autopilot\Stable\controlMCUSlugsMKII_dspic\sources\rt_zcfcn.c

    1   /*
    2    * File: rt_zcfcn.c
    3    *
    4    * Real-Time Workshop code generated for Simulink model controlMCUSlugsMKII.
    5    *
    6    * Model version                        : 1.714
    7    * Real-Time Workshop file version      : 7.5  (R2010a)  25-Jan-2010
    8    * Real-Time Workshop file generated on : Tue Nov 23 13:12:51 2010
    9    * TLC version                          : 7.5 (Jan 19 2010)
   10    * C source code generated on           : Tue Nov 23 13:12:52 2010
   11    *--------------------------------------------------------------
   12    *   Embedded Coder for Microchip dsPIC family.                 |
   13    *   Generate .c and .h files from your Matlab/simulink model   |
   14    *   and compile the diagram to .hex and .coff file that can be |
   15    *   downloaded directly into the microcontroller               |
   16    *                                                              |
   17    * Licence Accorded to Blake Mills                              |
   18    *                     University of Colorado-Boulder           |
   19    *                                                              |
   20    *   Written by Lubin KERHUEL -  http://www.kerhuel.eu          |
   21    *  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
   22    *   Version 3.3a                              22-Nov-2010      |
   23    *   For Matlab 7.10            R2010a                          |
   24    *--------------------------------------------------------------
   25    */
   26   
   27   #include "rt_zcfcn.h"
   28   
   29   /* Detect zero crossings events. */
   30   ZCEventType rt_ZCFcn(ZCDirection zcDir, ZCSigState* prevZc, real_T currValue)
   31   {
   32     slZcEventType zcsDir;
   33     slZcEventType tempEv;
   34     ZCEventType zcEvent = NO_ZCEVENT;    /* assume */
   35   
   36     /* zcEvent matrix */
   37     static const slZcEventType eventMatrix[4][4] = {
   38       /*          ZER              POS              NEG              UNK */
   39       { SL_ZCS_EVENT_NUL, SL_ZCS_EVENT_Z2P, SL_ZCS_EVENT_Z2N, SL_ZCS_EVENT_NUL },/* ZER */
   40   
   41       { SL_ZCS_EVENT_P2Z, SL_ZCS_EVENT_NUL, SL_ZCS_EVENT_P2N, SL_ZCS_EVENT_NUL },/* POS */
   42   
   43       { SL_ZCS_EVENT_N2Z, SL_ZCS_EVENT_N2P, SL_ZCS_EVENT_NUL, SL_ZCS_EVENT_NUL },/* NEG */
   44   
   45       { SL_ZCS_EVENT_NUL, SL_ZCS_EVENT_NUL, SL_ZCS_EVENT_NUL, SL_ZCS_EVENT_NUL }/* UNK */
   46     };
   47   
   48     /* get prevZcEvent and prevZcSign from prevZc */
   49     slZcEventType prevEv = (slZcEventType)(((uint8_T)(*prevZc)) >> 2);
   50     slZcSignalSignType prevSign = (slZcSignalSignType)(((uint8_T)(*prevZc)) & 0x03);
   51   
   52     /* get current zcSignal sign from current zcSignal value */
   53     slZcSignalSignType currSign = (slZcSignalSignType)((currValue) > 0.0 ?
   54       SL_ZCS_SIGN_POS :
   55       ((currValue) < 0.0 ? SL_ZCS_SIGN_NEG : SL_ZCS_SIGN_ZERO));
   56   
   57     /* get current zcEvent based on prev and current zcSignal value */
   58     slZcEventType currEv = eventMatrix[prevSign][currSign];
   59   
   60     /* get slZcEventType from ZCDirection */
   61     switch (zcDir) {
   62      case ANY_ZERO_CROSSING:
   63       zcsDir = SL_ZCS_EVENT_ALL;
   64       break;
   65   
   66      case FALLING_ZERO_CROSSING:
   67       zcsDir = SL_ZCS_EVENT_ALL_DN;
   68       break;
   69   
   70      case RISING_ZERO_CROSSING:
   71       zcsDir = SL_ZCS_EVENT_ALL_UP;
   72       break;
   73   
   74      default:
   75       zcsDir = SL_ZCS_EVENT_NUL;
   76       break;
   77     }
   78   
   79     /*had event, check if double zc happend remove double detection. */
   80     if (slZcHadEvent(currEv, zcsDir)) {
   81       currEv = (slZcEventType)(slZcUnAliasEvents(prevEv, currEv));
   82     } else {
   83       currEv = SL_ZCS_EVENT_NUL;
   84     }
   85   
   86     /* Update prevZc */
   87     tempEv = currEv << 2;                /* shift left by 2 bits */
   88     *prevZc = (ZCSigState)((currSign) | (tempEv));
   89     if (currEv & SL_ZCS_EVENT_ALL_DN) {
   90       zcEvent = FALLING_ZCEVENT;
   91     } else if (currEv & SL_ZCS_EVENT_ALL_UP) {
   92       zcEvent = RISING_ZCEVENT;
   93     } else {
   94       zcEvent = NO_ZCEVENT;
   95     }
   96   
   97     return zcEvent;
   98   }                                      /* rt_ZCFcn */
   99   
  100   /* File trailer for Real-Time Workshop generated code.
  101    *
  102    * [EOF]
  103    */
  104