///////////////////////////////////////////////////////////////////////////////
//                                                                            /
// IAR ANSI C/C++ Compiler V6.30.1.53127/W32 for ARM    08/Mar/2012  18:18:24 /
// Copyright 1999-2011 IAR Systems AB.                                        /
//                                                                            /
//    Cpu mode     =  thumb                                                   /
//    Endian       =  little                                                  /
//    Source file  =  D:\Profiles\b01252\My Documents\Freescale\P0\software\s /
//                    ample code cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_ /
//                    sc\src\projects\TWRK20D50M_Demo\angle_calculation\angle /
//                    _cal.c                                                  /
//    Command line =  "D:\Profiles\b01252\My Documents\Freescale\P0\software\ /
//                    sample code cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz /
//                    _sc\src\projects\TWRK20D50M_Demo\angle_calculation\angl /
//                    e_cal.c" -D IAR -D TOWER -D TWR_K20D50M -lCN            /
//                    "D:\Profiles\b01252\My Documents\Freescale\P0\software\ /
//                    sample code cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz /
//                    _sc\build\iar\TWRK20D50M_Demo\FLASH_128KB_PFLASH\List\" /
//                     -lB "D:\Profiles\b01252\My                             /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\FLASH_128KB_PFLASH\List\" -o          /
//                    "D:\Profiles\b01252\My Documents\Freescale\P0\software\ /
//                    sample code cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz /
//                    _sc\build\iar\TWRK20D50M_Demo\FLASH_128KB_PFLASH\Obj\"  /
//                    --no_cse --no_unroll --no_inline --no_code_motion       /
//                    --no_tbaa --no_clustering --no_scheduling --debug       /
//                    --endian=little --cpu=Cortex-M4 -e --fpu=None           /
//                    --dlib_config "C:\Program Files\IAR Systems\Embedded    /
//                    Workbench 6.0_5\arm\INC\c\DLib_Config_Normal.h" -I      /
//                    "D:\Profiles\b01252\My Documents\Freescale\P0\software\ /
//                    sample code cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz /
//                    _sc\build\iar\TWRK20D50M_Demo\..\..\..\src\common\" -I  /
//                    "D:\Profiles\b01252\My Documents\Freescale\P0\software\ /
//                    sample code cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz /
//                    _sc\build\iar\TWRK20D50M_Demo\..\..\..\src\cpu\" -I     /
//                    "D:\Profiles\b01252\My Documents\Freescale\P0\software\ /
//                    sample code cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz /
//                    _sc\build\iar\TWRK20D50M_Demo\..\..\..\src\cpu\headers\ /
//                    " -I "D:\Profiles\b01252\My                             /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\drivers\uart\" -I        /
//                    "D:\Profiles\b01252\My Documents\Freescale\P0\software\ /
//                    sample code cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz /
//                    _sc\build\iar\TWRK20D50M_Demo\..\..\..\src\drivers\mcg\ /
//                    " -I "D:\Profiles\b01252\My                             /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\drivers\wdog\" -I        /
//                    "D:\Profiles\b01252\My Documents\Freescale\P0\software\ /
//                    sample code cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz /
//                    _sc\build\iar\TWRK20D50M_Demo\..\..\..\src\drivers\rcm\ /
//                    " -I "D:\Profiles\b01252\My                             /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\drivers\lptmr\" -I       /
//                    "D:\Profiles\b01252\My Documents\Freescale\P0\software\ /
//                    sample code cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz /
//                    _sc\build\iar\TWRK20D50M_Demo\..\..\..\src\platforms\"  /
//                    -I "D:\Profiles\b01252\My Documents\Freescale\P0\softwa /
//                    re\sample code cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50 /
//                    MHz_sc\build\iar\TWRK20D50M_Demo\..\" -I                /
//                    "D:\Profiles\b01252\My Documents\Freescale\P0\software\ /
//                    sample code cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz /
//                    _sc\build\iar\TWRK20D50M_Demo\..\..\..\src\projects\TWR /
//                    K20D50M_Demo\" -I "D:\Profiles\b01252\My                /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\projects\TWRK20D50M_Demo /
//                    \dma\" -I "D:\Profiles\b01252\My                        /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\projects\TWRK20D50M_Demo /
//                    \adc16\" -I "D:\Profiles\b01252\My                      /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\projects\TWRK20D50M_Demo /
//                    \i2c\" -I "D:\Profiles\b01252\My                        /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\projects\TWRK20D50M_Demo /
//                    \tsi\" -I "D:\Profiles\b01252\My                        /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\projects\TWRK20D50M_Demo /
//                    \adc_test\" -I "D:\Profiles\b01252\My                   /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\projects\TWRK20D50M_Demo /
//                    \angle_calculation\" -I "D:\Profiles\b01252\My          /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\projects\TWRK20D50M_Demo /
//                    \buzzer\" -I "D:\Profiles\b01252\My                     /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\projects\TWRK20D50M_Demo /
//                    \fft_test\" -I "D:\Profiles\b01252\My                   /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\projects\TWRK20D50M_Demo /
//                    \irda_test\" -I "D:\Profiles\b01252\My                  /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\projects\TWRK20D50M_Demo /
//                    \cmt_test\" -I "D:\Profiles\b01252\My                   /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\projects\TWRK20D50M_Demo /
//                    \protocol\" -I "D:\Profiles\b01252\My                   /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\projects\TWRK20D50M_Demo /
//                    \task_mgr\" -I "D:\Profiles\b01252\My                   /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\projects\TWRK20D50M_Demo /
//                    \TWRK20_specific\" -I "D:\Profiles\b01252\My            /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\projects\TWRK20D50M_Demo /
//                    \rtc\" -I "D:\Profiles\b01252\My                        /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\CMSIS\DSP_Lib\Lib\interm /
//                    ediateFiles\" -I "D:\Profiles\b01252\My                 /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\CMSIS\DSP_Lib\Include\"  /
//                    -I "D:\Profiles\b01252\My Documents\Freescale\P0\softwa /
//                    re\sample code cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50 /
//                    MHz_sc\build\iar\TWRK20D50M_Demo\..\..\..\src\CMSIS\CM4 /
//                    \DeviceSupport\ARM\ARMCM4\" -I "D:\Profiles\b01252\My   /
//                    Documents\Freescale\P0\software\sample code             /
//                    cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\build\ia /
//                    r\TWRK20D50M_Demo\..\..\..\src\CMSIS\CM4\CoreSupport\"  /
//                    -I "D:\Profiles\b01252\My Documents\Freescale\P0\softwa /
//                    re\sample code cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50 /
//                    MHz_sc\build\iar\TWRK20D50M_Demo\..\..\..\src\CMSIS\CM4 /
//                    \CoreSupportLiker Options-Library\" -I                  /
//                    "D:\Profiles\b01252\My Documents\Freescale\P0\software\ /
//                    sample code cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz /
//                    _sc\build\iar\TWRK20D50M_Demo\..\..\..\src\CMSIS\DSP_Li /
//                    b\Lib\intermediateFiles\arm_cortexM4_math.lib\" -I      /
//                    "D:\Profiles\b01252\My Documents\Freescale\P0\software\ /
//                    sample code cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz /
//                    _sc\build\iar\TWRK20D50M_Demo\..\..\..\build\" -Ol      /
//                    --use_c++_inline -I "C:\Program Files\IAR               /
//                    Systems\Embedded Workbench 6.0_5\arm\CMSIS\Include\"    /
//                    -D ARM_MATH_CM4                                         /
//    List file    =  D:\Profiles\b01252\My Documents\Freescale\P0\software\s /
//                    ample code cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_ /
//                    sc\build\iar\TWRK20D50M_Demo\FLASH_128KB_PFLASH\List\an /
//                    gle_cal.s                                               /
//                                                                            /
//                                                                            /
///////////////////////////////////////////////////////////////////////////////

        NAME angle_cal

        #define SHT_PROGBITS 0x1

        EXTERN asin
        EXTERN median
        EXTERN resultx
        EXTERN resulty
        EXTERN resultz
        EXTERN sqrt_16
        EXTERN ti_fall

        PUBLIC X_acc
        PUBLIC Y_acc
        PUBLIC Z_acc
        PUBLIC angle_calculation
        PUBLIC arr_medianas
        PUBLIC cat
        PUBLIC detect_fall_detection
        PUBLIC fall_input
        PUBLIC offset
        PUBLIC xy_angle
        PUBLIC xy_mag
        PUBLIC xz_angle
        PUBLIC xz_mag
        PUBLIC yz_angle
        PUBLIC yz_mag

// D:\Profiles\b01252\My Documents\Freescale\P0\software\sample code cesar\TWR-K20D50M_LABS_AND_CE\kinetis_50MHz_sc\src\projects\TWRK20D50M_Demo\angle_calculation\angle_cal.c
//    1 /* 
//    2 
//    3 accelerometer apps 
//    4 Process information from accelerometer
//    5 and calculates tilt angle
//    6 
//    7 
//    8 */
//    9 #include "common.h"
//   10 #include "sqrt16.h"
//   11 #include "median.h"
//   12 #include "asin.h"
//   13 #include "buzzer.h"
//   14 extern signed short resultx, resulty, resultz;
//   15 
//   16 /*Acceleration  RAM */

        SECTION `.bss`:DATA:REORDER:NOROOT(2)
//   17 signed int X_acc;
X_acc:
        DS8 4

        SECTION `.bss`:DATA:REORDER:NOROOT(2)
//   18 signed int Y_acc;
Y_acc:
        DS8 4

        SECTION `.bss`:DATA:REORDER:NOROOT(2)
//   19 signed int Z_acc;
Z_acc:
        DS8 4
//   20 

        SECTION `.bss`:DATA:REORDER:NOROOT(2)
//   21 unsigned int xy_mag;
xy_mag:
        DS8 4

        SECTION `.bss`:DATA:REORDER:NOROOT(2)
//   22 unsigned int xz_mag;
xz_mag:
        DS8 4

        SECTION `.bss`:DATA:REORDER:NOROOT(2)
//   23 unsigned int yz_mag;
yz_mag:
        DS8 4
//   24 

        SECTION `.bss`:DATA:REORDER:NOROOT(2)
//   25 signed  int xy_angle;
xy_angle:
        DS8 4

        SECTION `.bss`:DATA:REORDER:NOROOT(2)
//   26 signed  int xz_angle;
xz_angle:
        DS8 4

        SECTION `.bss`:DATA:REORDER:NOROOT(2)
//   27 signed  int yz_angle;
yz_angle:
        DS8 4
//   28 
//   29 

        SECTION `.bss`:DATA:REORDER:NOROOT(2)
//   30 struct tipo_mediana arr_medianas[3];
arr_medianas:
        DS8 108
//   31 //unsigned char ADC_buffer[3];

        SECTION `.bss`:DATA:REORDER:NOROOT(2)
//   32 unsigned int cat;
cat:
        DS8 4

        SECTION `.bss`:DATA:REORDER:NOROOT(2)
//   33 unsigned int offset;
offset:
        DS8 4

        SECTION `.bss`:DATA:REORDER:NOROOT(0)
//   34 unsigned char fall_input;
fall_input:
        DS8 1
//   35 
//   36 
//   37 
//   38 
//   39 /*
//   40  The angle caluclation 
//   41  
//   42    mag = sqrt(X^2 + Y^2)
//   43    
//   44    sin(alpha) = CO/hip;
//   45    
//   46    alpha = asin(CO/hip);
//   47    
//   48    where CO  cateto opuesto
//   49    hip = hipotenusa= magnitud
//   50    
//   51    
//   52    the other way can be
//   53    
//   54    
//   55    // to check what is better
//   56    atan = y/x
//   57    
//   58    where x/y can take values from -inf to + inf
//   59    
//   60      and forī
//   61      
//   62      
//   63    Note: Direct calculos based on  angle = asin(x/g) 
//   64    is possible but is only valid when plane XY is the in the same line of G
//   65    
//   66    no accept small tilt of the board.
//   67     
//   68 
//   69 */
//   70 

        SECTION `.text`:CODE:NOROOT(1)
        THUMB
//   71 void angle_calculation(void)
//   72  {
angle_calculation:
        PUSH     {R4-R6,LR}
//   73    signed int nv, x2, y2, z2;
//   74 
//   75  /*  if (accel_count != 0) return;
//   76    accel_count = 60; //60 msec
//   77    */
//   78    
//   79    nv = (signed char)(resultx);
        LDR.N    R0,??DataTable1
        LDRSB    R0,[R0, #+0]
//   80    X_acc = median(nv, &arr_medianas[0]);
        LDR.N    R1,??DataTable1_1
        BL       median
        LDR.N    R1,??DataTable1_2
        STR      R0,[R1, #+0]
//   81    
//   82    
//   83    nv = (signed char)(resulty);
        LDR.N    R0,??DataTable1_3
        LDRSB    R0,[R0, #+0]
//   84    Y_acc = median(nv, &arr_medianas[1]);
        LDR.N    R1,??DataTable1_4
        BL       median
        LDR.N    R1,??DataTable1_5
        STR      R0,[R1, #+0]
//   85    
//   86    nv = (signed char)(resultz);  
        LDR.N    R0,??DataTable1_6
        LDRSB    R0,[R0, #+0]
//   87    Z_acc = median(nv, &arr_medianas[2]);
        LDR.N    R1,??DataTable1_7
        BL       median
        LDR.N    R1,??DataTable1_8
        STR      R0,[R1, #+0]
//   88 
//   89    x2        = X_acc*X_acc;
        LDR.N    R0,??DataTable1_2
        LDR      R0,[R0, #+0]
        LDR.N    R1,??DataTable1_2
        LDR      R1,[R1, #+0]
        MUL      R4,R1,R0
//   90    y2        = Y_acc*Y_acc;
        LDR.N    R0,??DataTable1_5
        LDR      R0,[R0, #+0]
        LDR.N    R1,??DataTable1_5
        LDR      R1,[R1, #+0]
        MUL      R5,R1,R0
//   91 
//   92    xy_mag   = sqrt_16(x2 + y2);
        ADDS     R0,R5,R4
        BL       sqrt_16
        LDR.N    R1,??DataTable1_9
        STR      R0,[R1, #+0]
//   93   
//   94    if (Y_acc<0) cat = -Y_acc; else cat = Y_acc;
        LDR.N    R0,??DataTable1_5
        LDR      R0,[R0, #+0]
        CMP      R0,#+0
        BPL.N    ??angle_calculation_0
        LDR.N    R0,??DataTable1_10
        LDR.N    R1,??DataTable1_5
        LDR      R1,[R1, #+0]
        RSBS     R1,R1,#+0
        STR      R1,[R0, #+0]
        B.N      ??angle_calculation_1
??angle_calculation_0:
        LDR.N    R0,??DataTable1_10
        LDR.N    R1,??DataTable1_5
        LDR      R1,[R1, #+0]
        STR      R1,[R0, #+0]
//   95    
//   96    offset = (unsigned int)(cat<<7)/(unsigned int)xy_mag;
??angle_calculation_1:
        LDR.N    R0,??DataTable1_10
        LDR      R0,[R0, #+0]
        LSLS     R0,R0,#+7
        LDR.N    R1,??DataTable1_9
        LDR      R1,[R1, #+0]
        UDIV     R0,R0,R1
        LDR.N    R1,??DataTable1_11
        STR      R0,[R1, #+0]
//   97    if (offset>127) offset = 127;    
        LDR.N    R0,??DataTable1_11
        LDR      R0,[R0, #+0]
        CMP      R0,#+128
        BCC.N    ??angle_calculation_2
        LDR.N    R0,??DataTable1_11
        MOVS     R1,#+127
        STR      R1,[R0, #+0]
//   98    xy_angle = asin[offset];
??angle_calculation_2:
        LDR.N    R0,??DataTable1_11
        LDR      R0,[R0, #+0]
        LDR.N    R1,??DataTable1_12
        LDR      R0,[R1, R0, LSL #+2]
        LDR.N    R1,??DataTable1_13
        STR      R0,[R1, #+0]
//   99    
//  100    if (Y_acc>0)  xy_angle = -xy_angle;
        LDR.N    R0,??DataTable1_5
        LDR      R0,[R0, #+0]
        CMP      R0,#+1
        BLT.N    ??angle_calculation_3
        LDR.N    R0,??DataTable1_13
        LDR.N    R1,??DataTable1_13
        LDR      R1,[R1, #+0]
        RSBS     R1,R1,#+0
        STR      R1,[R0, #+0]
//  101    
//  102  
//  103    /////////   
//  104    
//  105    z2        = Z_acc*Z_acc;   
??angle_calculation_3:
        LDR.N    R0,??DataTable1_8
        LDR      R0,[R0, #+0]
        LDR.N    R1,??DataTable1_8
        LDR      R1,[R1, #+0]
        MUL      R6,R1,R0
//  106    xz_mag    = sqrt_16(x2 + z2);
        ADDS     R0,R6,R4
        BL       sqrt_16
        LDR.N    R1,??DataTable1_14
        STR      R0,[R1, #+0]
//  107    if (X_acc<0) cat = -X_acc; else cat = X_acc;
        LDR.N    R0,??DataTable1_2
        LDR      R0,[R0, #+0]
        CMP      R0,#+0
        BPL.N    ??angle_calculation_4
        LDR.N    R0,??DataTable1_10
        LDR.N    R1,??DataTable1_2
        LDR      R1,[R1, #+0]
        RSBS     R1,R1,#+0
        STR      R1,[R0, #+0]
        B.N      ??angle_calculation_5
??angle_calculation_4:
        LDR.N    R0,??DataTable1_10
        LDR.N    R1,??DataTable1_2
        LDR      R1,[R1, #+0]
        STR      R1,[R0, #+0]
//  108    offset = (unsigned int)(cat<<7)/(unsigned int)xz_mag;
??angle_calculation_5:
        LDR.N    R0,??DataTable1_10
        LDR      R0,[R0, #+0]
        LSLS     R0,R0,#+7
        LDR.N    R1,??DataTable1_14
        LDR      R1,[R1, #+0]
        UDIV     R0,R0,R1
        LDR.N    R1,??DataTable1_11
        STR      R0,[R1, #+0]
//  109    if (offset>127) offset = 127;    
        LDR.N    R0,??DataTable1_11
        LDR      R0,[R0, #+0]
        CMP      R0,#+128
        BCC.N    ??angle_calculation_6
        LDR.N    R0,??DataTable1_11
        MOVS     R1,#+127
        STR      R1,[R0, #+0]
//  110    xz_angle = asin[offset];
??angle_calculation_6:
        LDR.N    R0,??DataTable1_11
        LDR      R0,[R0, #+0]
        LDR.N    R1,??DataTable1_12
        LDR      R0,[R1, R0, LSL #+2]
        LDR.N    R1,??DataTable1_15
        STR      R0,[R1, #+0]
//  111    
//  112    if (X_acc>0)  xz_angle = -xz_angle;
        LDR.N    R0,??DataTable1_2
        LDR      R0,[R0, #+0]
        CMP      R0,#+1
        BLT.N    ??angle_calculation_7
        LDR.N    R0,??DataTable1_15
        LDR.N    R1,??DataTable1_15
        LDR      R1,[R1, #+0]
        RSBS     R1,R1,#+0
        STR      R1,[R0, #+0]
//  113    
//  114 
//  115    yz_mag    = sqrt_16(y2 + z2);
??angle_calculation_7:
        ADDS     R0,R6,R5
        BL       sqrt_16
        LDR.N    R1,??DataTable1_16
        STR      R0,[R1, #+0]
//  116    if (Y_acc<0) cat = -Y_acc; else cat = Y_acc;
        LDR.N    R0,??DataTable1_5
        LDR      R0,[R0, #+0]
        CMP      R0,#+0
        BPL.N    ??angle_calculation_8
        LDR.N    R0,??DataTable1_10
        LDR.N    R1,??DataTable1_5
        LDR      R1,[R1, #+0]
        RSBS     R1,R1,#+0
        STR      R1,[R0, #+0]
        B.N      ??angle_calculation_9
??angle_calculation_8:
        LDR.N    R0,??DataTable1_10
        LDR.N    R1,??DataTable1_5
        LDR      R1,[R1, #+0]
        STR      R1,[R0, #+0]
//  117    offset = (unsigned int)(cat<<7)/(unsigned int)yz_mag;
??angle_calculation_9:
        LDR.N    R0,??DataTable1_10
        LDR      R0,[R0, #+0]
        LSLS     R0,R0,#+7
        LDR.N    R1,??DataTable1_16
        LDR      R1,[R1, #+0]
        UDIV     R0,R0,R1
        LDR.N    R1,??DataTable1_11
        STR      R0,[R1, #+0]
//  118    if (offset>127) offset = 127;    
        LDR.N    R0,??DataTable1_11
        LDR      R0,[R0, #+0]
        CMP      R0,#+128
        BCC.N    ??angle_calculation_10
        LDR.N    R0,??DataTable1_11
        MOVS     R1,#+127
        STR      R1,[R0, #+0]
//  119    yz_angle = asin[offset];
??angle_calculation_10:
        LDR.N    R0,??DataTable1_11
        LDR      R0,[R0, #+0]
        LDR.N    R1,??DataTable1_12
        LDR      R0,[R1, R0, LSL #+2]
        LDR.N    R1,??DataTable1_17
        STR      R0,[R1, #+0]
//  120    if (Y_acc>0)  yz_angle = -yz_angle;
        LDR.N    R0,??DataTable1_5
        LDR      R0,[R0, #+0]
        CMP      R0,#+1
        BLT.N    ??angle_calculation_11
        LDR.N    R0,??DataTable1_17
        LDR.N    R1,??DataTable1_17
        LDR      R1,[R1, #+0]
        RSBS     R1,R1,#+0
        STR      R1,[R0, #+0]
//  121     
//  122  }
??angle_calculation_11:
        POP      {R4-R6,PC}       ;; return
//  123 
//  124 
//  125    /*Fall detection*/
//  126 #define FALL_LIMIT  45//50

        SECTION `.text`:CODE:NOROOT(1)
        THUMB
//  127 void detect_fall_detection(void)
//  128 {
//  129    if (xy_mag<FALL_LIMIT && xz_mag < FALL_LIMIT && yz_mag < FALL_LIMIT  && !ti_fall) 
detect_fall_detection:
        LDR.N    R0,??DataTable1_9
        LDR      R0,[R0, #+0]
        CMP      R0,#+45
        BCS.N    ??detect_fall_detection_0
        LDR.N    R0,??DataTable1_14
        LDR      R0,[R0, #+0]
        CMP      R0,#+45
        BCS.N    ??detect_fall_detection_0
        LDR.N    R0,??DataTable1_16
        LDR      R0,[R0, #+0]
        CMP      R0,#+45
        BCS.N    ??detect_fall_detection_0
        LDR.N    R0,??DataTable1_18
        LDRH     R0,[R0, #+0]
        CMP      R0,#+0
        BNE.N    ??detect_fall_detection_0
//  130     fall_input=1;
        LDR.N    R0,??DataTable1_19
        MOVS     R1,#+1
        STRB     R1,[R0, #+0]
        B.N      ??detect_fall_detection_1
//  131    else 
//  132    {
//  133      fall_input = 0;
??detect_fall_detection_0:
        LDR.N    R0,??DataTable1_19
        MOVS     R1,#+0
        STRB     R1,[R0, #+0]
//  134      ti_fall = 10;
        LDR.N    R0,??DataTable1_18
        MOVS     R1,#+10
        STRH     R1,[R0, #+0]
//  135     }
//  136    
//  137 }
??detect_fall_detection_1:
        BX       LR               ;; return

        SECTION `.text`:CODE:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
??DataTable1:
        DC32     resultx

        SECTION `.text`:CODE:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
??DataTable1_1:
        DC32     arr_medianas

        SECTION `.text`:CODE:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
??DataTable1_2:
        DC32     X_acc

        SECTION `.text`:CODE:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
??DataTable1_3:
        DC32     resulty

        SECTION `.text`:CODE:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
??DataTable1_4:
        DC32     arr_medianas+0x24

        SECTION `.text`:CODE:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
??DataTable1_5:
        DC32     Y_acc

        SECTION `.text`:CODE:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
??DataTable1_6:
        DC32     resultz

        SECTION `.text`:CODE:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
??DataTable1_7:
        DC32     arr_medianas+0x48

        SECTION `.text`:CODE:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
??DataTable1_8:
        DC32     Z_acc

        SECTION `.text`:CODE:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
??DataTable1_9:
        DC32     xy_mag

        SECTION `.text`:CODE:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
??DataTable1_10:
        DC32     cat

        SECTION `.text`:CODE:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
??DataTable1_11:
        DC32     offset

        SECTION `.text`:CODE:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
??DataTable1_12:
        DC32     asin

        SECTION `.text`:CODE:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
??DataTable1_13:
        DC32     xy_angle

        SECTION `.text`:CODE:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
??DataTable1_14:
        DC32     xz_mag

        SECTION `.text`:CODE:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
??DataTable1_15:
        DC32     xz_angle

        SECTION `.text`:CODE:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
??DataTable1_16:
        DC32     yz_mag

        SECTION `.text`:CODE:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
??DataTable1_17:
        DC32     yz_angle

        SECTION `.text`:CODE:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
??DataTable1_18:
        DC32     ti_fall

        SECTION `.text`:CODE:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
??DataTable1_19:
        DC32     fall_input

        SECTION `.iar_vfe_header`:DATA:REORDER:NOALLOC:NOROOT(2)
        SECTION_TYPE SHT_PROGBITS, 0
        DATA
        DC32 0

        SECTION __DLIB_PERTHREAD:DATA:REORDER:NOROOT(0)
        SECTION_TYPE SHT_PROGBITS, 0

        SECTION __DLIB_PERTHREAD_init:DATA:REORDER:NOROOT(0)
        SECTION_TYPE SHT_PROGBITS, 0

        END
// 
// 153 bytes in section .bss
// 528 bytes in section .text
// 
// 528 bytes of CODE memory
// 153 bytes of DATA memory
//
//Errors: none
//Warnings: none
