⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.lst

📁 我找的ADXL345的代码
💻 LST
📖 第 1 页 / 共 5 页
字号:
ARM COMPILER V2.42,  main                                                                  11/08/08  15:14:09  PAGE 1   


ARM COMPILER V2.42, COMPILATION OF MODULE main
OBJECT MODULE PLACED IN main.OBJ
COMPILER INVOKED BY: C:\Keil\ARM\BIN\CA.exe main.c THUMB DEBUG TABS(4) 

stmt  level    source

    1          /*
    2          Project:            ADXL345 Demo 
    3          Functions:          Page rotation, shake detection and single / double tap detection
    4          Hardware platform:  ADuC7026EVB and ADXL345EVB
    5          Development tool:       Keil UV3          
    6          Designed by:        Nicolle Jia, CAST team, ADI
    7          Version:            V1.0
    8          Data:               2008.5.29
    9          */
   10          
   11          #include <ADuC7026.h>
   12          #include <I2C_Master.h>
   13          
   14          //Reigster of ADXL345                                       
   15          unsigned char   DataX1, DataX2, DataY1, DataY2, DataZ1, DataZ2; //High byte and Low byte of Data register 
             -of X, Y, Z
   16          unsigned int    DataX, DataY, DataZ;  //Word  of Data register of X, Y, Z
   17          unsigned char   DevID;      //Device ID
   18          unsigned char   Interrupt;
   19          
   20          //Status;    01-Left, 02-Right, 03-Up, 04-Down, 05-Top, 06-Bottom, 08-Forward, 10-Backward, 20-Single Tap, 
             -40-Double Tap
   21          unsigned char   RotateStatus, LastRotateStatus;
   22          unsigned char   TapStatus;
   23          unsigned char   ShakeStatus, LastShakeStatus, ShakeDetectStep;  
   24          
   25          //Counters
   26          unsigned int    RotateCounter;
   27          unsigned int    TapCounter; 
   28          unsigned int    ShakeDetectCounter, ShakeStopCounter;
   29          
   30          //Start flag
   31          unsigned char   IsStart;   
   32          
   33          
   34          //Uart put char, send Data directly
   35          void PutChar( char Data)
   36          {
   37   1          COMTX=Data; 
   38   1          while(!(0x020==(COMSTA0 & 0x020)))  {;}     
   39   1      }
   40          
   41          //Uart display char, send HEX of Data
   42          void DisplayChar( char Data)
   43          {
   44   1          unsigned char Temp;
   45   1      
   46   1          Temp=Data>>4;   //Send high byte
   47   1          if(Temp<0x0A)
   48   1          {
   49   2              COMTX=0x30+Temp;
   50   2          }
   51   1          else
   52   1          {
   53   2              COMTX='\A'-0x0A+Temp;   
   54   2          }
   55   1          while(!(0x020==(COMSTA0 & 0x020)))  {;} 
   56   1      
   57   1          Temp=Data&0x0F;     //Send low byte
ARM COMPILER V2.42,  main                                                                  11/08/08  15:14:09  PAGE 2   

   58   1          if(Temp<0x0A)
   59   1          {
   60   2              COMTX=0x30+Temp;
   61   2          }
   62   1          else
   63   1          {
   64   2              COMTX='\A'-0x0A+Temp;   
   65   2          }
   66   1          while(!(0x020==(COMSTA0 & 0x020)))  {;} 
   67   1      }
   68          
   69          //Uart interrupt service function, implement the communication with PC demo software
   70          void IRQ_Handler() __irq
   71          {
   72   1          unsigned char UartDataReceived; // Received Data
   73   1          unsigned char UartInterrupt;    // Interrupt status
   74   1          
   75   1          UartInterrupt =COMIID0 ;
   76   1          if(UartInterrupt==0x04)//Has Received a Data
   77   1          {
   78   2              UartDataReceived=COMRX; //Get received data
   79   2      
   80   2              if(UartDataReceived==0xAA)  //Test connection by sending out device ID  
   81   2              {
   82   3                  COMTX=DevID;    
   83   3              }
   84   2              if(UartDataReceived==0x55)  //Start command 
   85   2              {
   86   3                  IsStart=0x01;   
   87   3              }
   88   2              if(UartDataReceived==0xA5)  //Stop command  
   89   2              {
   90   3                  IsStart=0x00;   
   91   3              }
   92   2          }
   93   1          /*
   94   1          else if(UartInterrupt==0x02) // Has Send a Data
   95   1          {
   96   1              // Nothing to do
   97   1          }
   98   1          */
   99   1      
  100   1      }
  101          
  102          //ADuC7026 UART initialization
  103          void UART_Initiate()
  104          {
  105   1      
  106   1          // Setup tx & rx pins on P1.0 and P1.1
  107   1          GP1CON = 0x2211;                // I2C on P1.2 and P1.3   Setup tx & rx pins on P1.0 and P1.1 for UART
  108   1      
  109   1          //Initiate the UART Port to 115200bps
  110   1          POWKEY1 = 0x01;             //Start PLL setting,changeless
  111   1          POWCON=0x00;
  112   1          POWKEY2 = 0xF4;             //Finish PLL setting,changeless
  113   1               
  114   1          COMCON0 = 0x80;                 // Setting DLAB
  115   1          COMDIV0 = 0x0B;                 // Setting DIV0 and DIV1 to DL calculated
  116   1          COMDIV1 = 0x00;
  117   1          COMCON0 = 0x07;                 // Clearing DLAB
  118   1      
  119   1          // fractional divider
  120   1          COMDIV2 = 0x883E;               // M=1
  121   1                                          // N=01101010101  =853
  122   1                                          // M+N/2048  =1.4165
  123   1                                          //41.78MHz/(16*2*2^CD*DL*(M+N/2048))     //CD=0  DL=0B=11
ARM COMPILER V2.42,  main                                                                  11/08/08  15:14:09  PAGE 3   

  124   1                                          //115.2Kbps  M+N/2048 =1.0303   M=1, N=  62=0x3EH=000 0011 1110
  125   1                                          //comdiv2=0x883E
  126   1      
  127   1          //Enable UART interrupt
  128   1          COMIEN0=0x03;                   
  129   1          IRQEN = 0x4000; 
  130   1                                              
  131   1      }
  132          
  133          //ADuC7026 I2C1 initialization
  134          void I2C1_Initiate() 
  135          {
  136   1      
  137   1          //Initiate the I2C1 Port to 400kbps
  138   1          GP1CON = 0x2211;                // I2C on P1.2 and P1.3   Setup tx & rx pins on P1.0 and P1.1 for UART
  139   1          I2C1CFG = 0x82;                 // Master Enable & Enable Generation of Master Clock
  140   1          
  141   1          // I2C-Master setup
  142   1          I2C1DIV = 0x3232;               // 0x3232 = 400kHz
  143   1                                          // 0xCFCF = 100kHz  
  144   1      
  145   1          //Enable I2C1 Master Interupt
  146   1          FIQEN |= SM_MASTER1_BIT;    
  147   1      
  148   1      }
  149          
  150          //ADuc7026 initialization, UART and I2C1
  151          void ADuC7026_Initiate(void)
  152          {
  153   1      
  154   1          UART_Initiate();
  155   1          I2C1_Initiate();
  156   1      
  157   1      }
  158          
  159          //ADXL345 initialization, register configuration
  160          void ADXL345_Initiate()
  161          {
  162   1          I2C_WRITE_REGISTER(0x2D,0x08);  //Power CTL: Measure mode
  163   1          I2C_WRITE_REGISTER(0x2C,0x0C);  //Rate: 200Hz
  164   1          I2C_WRITE_REGISTER(0x31,0x01);  //Data Format: 8g right justified   128=1g
  165   1          //I2C_WRITE_REGISTER(0x2E,0xE0);    //Int En: Data Rdy, Single Tap, Doulbe Tap
  166   1              I2C_WRITE_REGISTER(0x2E,0xE4);  //Int En: Data Rdy, Single Tap, Doulbe Tap,Free fall
  167   1              I2C_WRITE_REGISTER(0x2A,0x01);  //Z Axis Tap
  168   1          I2C_WRITE_REGISTER(0x1D,0x20);  //Tap Threshold: 2G;
  169   1              I2C_WRITE_REGISTER(0x28,0x09);  //FreeFall Threshold: 300mg;
  170   1              I2C_WRITE_REGISTER(0x29,0x14);  //FreeFall Timing:100ms
  171   1          I2C_WRITE_REGISTER(0x21,0x50);  //Dur:50ms
  172   1          I2C_WRITE_REGISTER(0x22,0x20);  //Latent: 40ms
  173   1          I2C_WRITE_REGISTER(0x23,0xF0);  //Window: 300ms
  174   1          
  175   1      }
  176          
  177          //Delay
  178          void Delay(unsigned int Time1, unsigned int Timer2)
  179          {
  180   1          unsigned int i, j, k=0xFFFF;
  181   1          for(i=0;i<Time1;i++)
  182   1          {
  183   2              for(j=0; j<Timer2; j++)
  184   2                  while(k>0) k--;
  185   2          }
  186   1      }
  187          void  main(void)
  188          {                                
  189   1          ADuC7026_Initiate();    //ADuC7026 Initialization
ARM COMPILER V2.42,  main                                                                  11/08/08  15:14:09  PAGE 4   

  190   1          ADXL345_Initiate();     //ADXL345 Initialization
  191   1      
  192   1          //Variables initialization
  193   1          IsStart=0x00;
  194   1      
  195   1          //Variables for shake detection
  196   1          ShakeStatus=0x00;
  197   1          LastShakeStatus=0x00;
  198   1          ShakeDetectStep=0x00;   
  199   1          ShakeDetectCounter=0; 
  200   1          ShakeStopCounter=0;
  201   1      
  202   1          //Variables for Page Rotation
  203   1          RotateStatus=0x00;
  204   1          LastRotateStatus=0x00;
  205   1          RotateCounter=0;
  206   1      
  207   1          //Variables for Tap
  208   1          TapStatus=0x00;
  209   1          TapCounter=0;
  210   1          
  211   1      
  212   1      
  213   1      
  214   1          DevID=(unsigned char)I2C_READ_REGISTER(0x00);   //get device ID first
  215   1          I2C_READ_REGISTER(0x30);    //clear interrupt;
  216   1      
  217   1          while(1)  //Endless loop
  218   1          {
  219   2              
  220   2              if(IsStart==0x01)  // Start
  221   2              {
  222   3                  Interrupt=(unsigned char)I2C_READ_REGISTER(0x30);    // get interrupt status                                          
  223   3      
  224   3                  // Double Tap
  225   3                  //if((Interrupt&0x20)==0x20)    //Double Tap interrupt
  226   3                                if((Interrupt&0x04)==0x04)    //freefall interrupt
  227   3                  {
  228   4                      //if((RotateStatus==0x05)    && (LastRotateStatus==0x05) )  // double tap function is available only whe
             -n RotateStatus==Top
  229   4                      //{
  230   4                          //if(TapStatus==0x00)       // no tap interrupt asserted before
  231   4                          //{
  232   4                              TapStatus=0x40;     // double tap assert
  233   4                              TapCounter=0x00;    // clear time counter, 
  234   4                              PutChar(TapStatus); // send status to PC demo
  235   4                          //}
  236   4                          
  237   4                      //}
  238   4                          
  239   4                  }
  240   3                  else 
  241   3                  {
  242   4                      if(TapCounter>=75)      // wait for time counter overflow, then clear TapStatus,
  243   4                      {
  244   5                          TapStatus=0x00;     // clear TapStatus
  245   5                          TapCounter=0x00;    // clear time counter,
  246   5                          PutChar(TapStatus); // send status to PC demo
  247   5                      }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -