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

📄 accelerometer.c

📁 加速度信息采集程序.主要是通过界面显示物体移动的信息采集。
💻 C
📖 第 1 页 / 共 3 页
字号:
             gu16timerEvent[0] = gu16msTimer + 500;
             gu16Events &= ~TIMER_EVENT0; /* Clear the event */
          }
          gu8TxAccelState = 'y';
      	 break;
      	 
      case 'y':
      case 'Y':
          doAccelMeasurement(Y_AXIS);
          if (compareADCReading(u8LastY, gau8TxDataBuffer[1], APP_BATTERYSAVER_THRESHOLD) == TRUE) {
             LED1 ^= 1;  /* Toggle LED1 */
             u8LastY = gau8TxDataBuffer[1];
             gu16timerEvent[0] = gu16msTimer + 500;
             gu16Events &= ~TIMER_EVENT0; /* Clear the event */          
          }
          gau8TxDataBuffer[0] = 0x7A; /* Set Z */
          gu8TxAccelState = 'z';
          break;
          
      case 'z':
      case 'Z':
      	 doAccelMeasurement(Z_AXIS);
          if (compareADCReading(u8LastZ, gau8TxDataBuffer[1], APP_BATTERYSAVER_THRESHOLD) == TRUE) {
             LED1 ^= 1;  /* Toggle LED1 */
             u8LastZ = gau8TxDataBuffer[1];
             gu16timerEvent[0] = gu16msTimer + 500;
             gu16Events &= ~TIMER_EVENT0; /* Clear the event */
          }
          gu8TxAccelState = 'x';
          break;
      default:
          break;
   }/* End Switch */
   
   
}

/**************************************************************
*    Function:     antiTheft_app()
*    Parameters: none
*    Return: none
*
*    This application detects a tilt condition and sounds the
*     buzzer while the tilt condition exists.
***************************************************************/

void antiTheft_app(void) {

  UINT16 u16thresh;
   
          doAccelMeasurement(X_AXIS);
          
          /* Check X-Axis for 16% grade */
          u16thresh = (sCalValues.NxMax - sCalValues.NxOff);
          u16thresh *= 16;
          u16thresh /= 100;
          if (compareADCReading(128, gau8TxDataBuffer[1], (UINT8) u16thresh) == TRUE) {
             u8LastX = gau8TxDataBuffer[1];             
             #if BUZZER_ENABLED==TRUE
             BUZZER = BUZZER_ON;
             #endif    
             appEventPacket(X_AXIS, 3);
             return;
          } 
         
          doAccelMeasurement(Y_AXIS);
          u16thresh = (sCalValues.NyMax - sCalValues.NyOff);
          u16thresh <<= 4;
          u16thresh /= 100;           
          /* Moving Case - update the Last moved variable */
          if (compareADCReading(128, gau8TxDataBuffer[1], (UINT8) u16thresh) == TRUE) {
             u8LastY = gau8TxDataBuffer[1];             
             #if BUZZER_ENABLED==TRUE
             BUZZER = BUZZER_ON;
             #endif    
             appEventPacket(Y_AXIS, 3);             
             return;
          } 

          /* Both X and y are within threshold.  Turn Off Buzzer */          
          #if BUZZER_ENABLED==TRUE
          BUZZER = BUZZER_OFF;
          #endif
          
}

/**************************************************************
*    Function:     shockDetect_app()
*    Parameters: none
*    Return: none
*
*     This application chirps the buzzer:
*     - once for x movement.
*		- twice for y movement.
*     - thrice for z movement.
***************************************************************/

void shockDetect_app(void) {
   switch (gu8TxAccelState) {
      case 'x':
      case 'X':
          doAccelMeasurement(X_AXIS);
          
          /* Moving Case - update the Last moved variable */
          if (compareADCReading(u8LastX, gau8TxDataBuffer[1], APP_SHOCKDETECT_THRESHOLD) == TRUE) {
             u8LastX = gau8TxDataBuffer[1];
          #if BUZZER_ENABLED==TRUE
             BUZZER = BUZZER_ON;
             #endif 
             LED1 = LED_ON;
             delay(10);
             #if BUZZER_ENABLED==TRUE
             BUZZER = BUZZER_OFF;
             #endif 
             LED1 = LED_OFF;
             delay(10);             
             appEventPacket(X_AXIS, 3);             
          }
          gu8TxAccelState = 'y';
      	 break;
      	 
      case 'y':
      case 'Y':
          doAccelMeasurement(Y_AXIS);
          if (compareADCReading(u8LastY, gau8TxDataBuffer[1], APP_SHOCKDETECT_THRESHOLD) == TRUE) {
             u8LastY = gau8TxDataBuffer[1];
          #if BUZZER_ENABLED==TRUE
             BUZZER = BUZZER_ON;
             #endif 
             LED1 = LED_ON;
             delay(10);
             #if BUZZER_ENABLED==TRUE
             BUZZER = BUZZER_OFF;
             #endif 
             LED1 = LED_OFF;
             delay(10);
             #if BUZZER_ENABLED==TRUE    
             BUZZER = BUZZER_ON;
             #endif 
             LED1 = LED_ON;
             delay(10);
             #if BUZZER_ENABLED==TRUE
             BUZZER = BUZZER_OFF;
             #endif 
             LED1 = LED_OFF;
             delay(10);              
             appEventPacket(Y_AXIS, 3);          
          }
          gau8TxDataBuffer[0] = 0x7A; /* Set Z */
          gu8TxAccelState = 'z';
          break;
          
      case 'z':
      case 'Z':
      	 doAccelMeasurement(Z_AXIS);
          if (compareADCReading(u8LastZ, gau8TxDataBuffer[1], APP_SHOCKDETECT_THRESHOLD) == TRUE) {
             u8LastZ = gau8TxDataBuffer[1];
             #if BUZZER_ENABLED==TRUE
             BUZZER = BUZZER_ON;
             #endif 
             LED1 = LED_ON;
             delay(10);
             #if BUZZER_ENABLED==TRUE
             BUZZER = BUZZER_OFF;
             #endif 
             LED1 = LED_OFF;
             delay(10);    
             #if BUZZER_ENABLED==TRUE
             BUZZER = BUZZER_ON;
             #endif 
             LED1 = LED_ON;
             delay(10);
             #if BUZZER_ENABLED==TRUE
             BUZZER = BUZZER_OFF;
             #endif 
             LED1 = LED_OFF;
             delay(10); 
             #if BUZZER_ENABLED==TRUE
             BUZZER = BUZZER_ON;
             #endif 
             LED1 = LED_ON;
             delay(10);
             #if BUZZER_ENABLED==TRUE
             BUZZER = BUZZER_OFF;
             #endif 
             LED1 = LED_OFF;                          
             appEventPacket(Z_AXIS, 3);  
          }
          gu8TxAccelState = 'x';
          break;
      default:
          break;
   }/* End Switch */      
}

/**************************************************************
*    Function:     appPacket(UINT8)
*    Parameters: UINT8 rate
*    Return: none
*
*     This sends an App Packet to the PC_Radio to tell the
*     PC Radio that an event has occured.
***************************************************************/

void appEventPacket(UINT8 options, UINT8 retries) {

   UINT8 i;
   UINT8 u8status;
                      
                  gau8TxDataBuffer[0] = gi8AppStatus;
                  gau8TxDataBuffer[1] = options;
                  gsTxPacket.u8DataLength = 2;
                  
                  for (i=0; i<retries; i++) {
                     u8status = MCPSDataRequest(&gsTxPacket); /* transmit data */
                  }
}

/**************************************************************
*    Function:     pingPacket(UINT8)
*    Parameters: UINT8 rate
*    Return: none
*
*     This sends a ping packet to 
***************************************************************/

void pingPacket(UINT16 rate, UINT8 options) {

   static UINT16 gu8pingRate;
   UINT8 status;
   
   gu8pingRate = rate;
   
          	   /* Every 2 seconds send a packet - keep alive */  
               if ((gu16msTimer % gu8pingRate) == 0) {
                  LED2 = LED_ON;
                  gau8TxDataBuffer[0] = 'P';
                  gau8TxDataBuffer[1] = gi8AppStatus;
                  gau8TxDataBuffer[2] = options;
                  gau8TxDataBuffer[3] = (UINT8)(gu16msTimer >> 8);
                  gau8TxDataBuffer[4] = (UINT8) gu16msTimer;
                  gau8TxDataBuffer[5] = sCalValues.NxOff;
                  gau8TxDataBuffer[6] = sCalValues.NxMax;
                  gau8TxDataBuffer[7] = sCalValues.NyOff;
                  gau8TxDataBuffer[8] = sCalValues.NyMax;
                  gau8TxDataBuffer[9] = sCalValues.NzOff;
                  gau8TxDataBuffer[10] = sCalValues.NzMax;
                  gsTxPacket.u8DataLength = 11;
                  status = MCPSDataRequest(&gsTxPacket); /* transmit data */
                  
                  
                  /* Blink LED2 to show activity */
                  LED2 = LED_ON;
                  delay(5);
                  LED2 = LED_OFF;
                  LED1 = LED_OFF; /* Also Turn off LED1 for power saving */
               }
               
               switch (options) {
                  case PING_ACK:
                     /* Turn on Receiver for 2 * 10ms */
                     if (status == SUCCESS) {
                        (void)MLMERXEnableRequest(&gsRxPacket, 0);
                        delay(10);
                        (void)MLMERXDisableRequest();   
                     }                     
                     break;
               }

   
}

/**************************************************************
*    Function:     TX_accel
*    Parameters: none
*    Return:        none
*
*    This is the main thread of execution when ANY button is pressed
*    on reset.
**************************************************************/

void TxAccel(void)
{
 
   gsTxPacket.u8DataLength = 2;
   switch (gu8TxAccelState) {
      case 'x':
      case 'X':
          doAccelMeasurement(X_AXIS);
          gau8TxDataBuffer[0] = 0x78; /* Set X */
          if (compareADCReading(u8LastX, gau8TxDataBuffer[1], APP_DEFAULT_THRESHOLD) == TRUE) {
             (void)MCPSDataRequest(&gsTxPacket); /* transmit data */
             u8LastX = gau8TxDataBuffer[1];
             LED1 ^= 1;  /* Toggle LED1 */             
          }
          SCITransmitArray(gau8TxDataBuffer,2);///////////
          gu8TxAccelState = 'y'; 
         
      	 break;
      	 
      case 'y':
      case 'Y':
          doAccelMeasurement(Y_AXIS);
          gau8TxDataBuffer[0] = 0x79; /* Set Y */
          if (compareADCReading(u8LastY, gau8TxDataBuffer[1], APP_DEFAULT_THRESHOLD) == TRUE) {
             (void)MCPSDataRequest(&gsTxPacket); /* transmit data */
             LED1 ^= 1;  /* Toggle LED1 */
             u8LastY = gau8TxDataBuffer[1];          
          }
          SCITransmitArray(gau8TxDataBuffer,2);
          gu8TxAccelState = 'z';
           
          break;
          
      case 'z':
      case 'Z':
      	 doAccelMeasurement(Z_AXIS);
      	 gau8TxDataBuffer[0] = 0x7A; /* Set Z */
          if (compareADCReading(u8LastZ, gau8TxDataBuffer[1], APP_DEFAULT_THRESHOLD) == TRUE) {
             (void)MCPSDataRequest(&gsTxPacket); /* transmit data */
             LED1 ^= 1;  /* Toggle LED1 */
             u8LastZ = gau8TxDataBuffer[1];
          }
          SCITransmitArray(gau8TxDataBuffer,2); 
          gu8TxAccelState = 'x';
          ///SCITransmitArray(gau8TxDataBuffer,6);  ////////////
                  break;                         //////////
          break;
      default:
          /* set the variable to 'x' */
          gau8TxDataBuffer[0] = 0x78;
          break;
   }/* End Switch */
}/* end TX_accel */




void AppInit(void) 
{

  LED1DIR = 1;    /* Setup LED ports as outputs*/
  LED2DIR = 1;    /* Setup LED ports as outputs*/
  LED3DIR = 1;    /* Setup LED ports as outputs*/
  LED4DIR = 1;    /* Setup LED ports as outputs*/
  
  #if BUZZER_ENABLED==TRUE

⌨️ 快捷键说明

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