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

📄 accelerometer.c

📁 加速度信息采集程序.主要是通过界面显示物体移动的信息采集。
💻 C
📖 第 1 页 / 共 3 页
字号:
/****************************************************************************
accelerometer.c

Title:     ACCERLEROMETER.c

This application is used in conjunction with the 13192SARD SDK.
Out of reset if any pushbutton is pressed the device becomes a transmitter.
Otherwise the radio receives only.

TX: The transmitter is constantly monitoring the XYZ axis of the
accelerometers.  This data is formatted and sent over the
air at the hardcoded channel in the format:

x{Data}y{data}z{data}

When the data is sent LED4 is toggled.

RX: When the RX radio receives the x{Data}y{data}z{data} packet the packet is
forwarded to the SCI port on the 13192SARD in the same format.
The UART is set to 38400 8-N-1 with no flow control.  When a xyz packet
is received the LED3 is toggled.

On the 13192SARD make sure that jumpers are placed in 1-2, 3-4, and 11-12.
This connects the XYZ voltages to ADC channel 0,1,7.

$Author: b06900 $
$Date: 2008/03/05 22:48:47 $
$Name:  $
****************************************************************************/


#include <hidef.h> /* for EnableInterrupts macro */
#include "pub_def.h"
#include "APP_SMAC_API.h"
#include "freescale_radio_hardware.h"
#include "flash.h"  /* should be included only in one file */
#include "SCI.h"
#include "kbi.h"
#include "accelerometer.h"
#include "ledFunctions.h"
#include "eventDefs.h"

#if SMAC_FEATURE_OTAP == TRUE
#include "APP_OTAP_API.h"
#endif // SMAC_FEATURE_OTAP == TRUE

#if SMAC_FEATURE_SECURITY == TRUE
#include "APP_security_API.h"
#endif // SMAC_FEATURE_SECURITY == TRUE
#if (EMBEDDED_BOOTLOADER == TRUE)  
#include "bootloader user api.h"
#endif
/////////////////////////////
/* note: Buad Rate = 38400 */
/////////////////////////////

/* Enums, types */


/* Global Variables */
extern UINT8 gu8RTxMode;
extern INT8 gi8AppStatus;
extern UINT16 gu16msTimer;
extern UINT8 gu8Channel;
UINT8 gu8TxAccelState = 'x';
UINT8 u8LastX, u8LastY, u8LastZ;
extern UINT16 gu16timerEvent[2];
extern UINT16 gu16Events;
extern INT8 gi8PCRadioState;

extern UINT8 gau8TxDataBuffer[16];
extern UINT8 gau8RxDataBuffer[16];
extern tTxPacket gsTxPacket;
extern tRxPacket gsRxPacket;
extern tAccelCal sCalValues;
const UINT8 CalArray[6] @ 0xC202= {0x8D, 0xCD, 0x8D, 0xCD, 0x7D, 0xBE};     //Initial default calibration values
const tAccelCal * sCalDefaultValues = (tAccelCal *)CalArray;

void main(void)
{

////////////////////
/* Init routines    */
////////////////////

    MCUInit();
    RadioInit();
    AppInit();
    SCIInit();
    KBIInit();
    KBI_ENABLEINTERRUPTS;
    (void)MLMESetMC13192ClockRate(0);  /* Set initial Clk speed */
    
    /******************************************************************
  To adjust output power call the MLME_MC13192_PA_output_adjust() with:
  
  MAX_POWER     (+3 to +5dBm)
  NOMINAL_POWER (0 dBm)
  MIN_POWER     ~(-16dBm)
  
  or somewhere custom ? (0-15, 11 (NOMINAL_POWER) being Default power)

    ******************************************************************/
    (void)MLMEMC13192PAOutputAdjust(MAX_POWER);    //Set MAX power setting
  /* MLMEMC13192PAOutputAdjust(MIN_POWER);    //Set MIN power setting */
  /* MLMEMC13192PAOutputAdjust(NOMINAL_POWER);    //Set Nominal power setting */

    UseExternalClock();    /* switch clock sources from mcu to MC1319x CLKO*/
    
#if(EMBEDDED_BOOTLOADER == TRUE)
    boot_init();  /* Initialize the bootloader. */
#endif 

#if(EMBEDDED_BOOTLOADER == TRUE)    
    boot_call();  /* Check for user request to run bootloader. */
                  /* App will not return, if Bootloader is requested. */
#endif
    
    EnableInterrupts; 
    gi8AppStatus = INITIAL_STATE;    /* Initial Mode */
    if (MLMESetChannelRequest(gu8Channel) == SUCCESS) { /* Select channel here (0-15) */
                gi8AppStatus = PC_RADIO_STATE;
                gi8PCRadioState	 = RECEIVER_ALWAYS_ON;
    }
    
    /* Any Button can be pressed to be put into TX mode if none 
     * are pressed then it is in RX mode 
     */
  
  
#if ACCEL_ENABLED==TRUE
   ACCEL_INIT_MACRO
   ACCEL_G_4p0        /////////////////
   ACCEL_G_ON
#endif
  
  /**************************************************************
  *    TX_accel initialization 
  **************************************************************/
   


  for(;;){

        	  switch (gi8AppStatus) {
        	    
        	   case PC_RADIO_STATE:
        	       #if BUZZER_ENABLED==TRUE
        	       BUZZER = BUZZER_OFF;
                 #endif
                 PC_Radio_App();
                 break;
                 
        	   case XYZ_STATE:
        	      /* Ping, but be sure the buzzer is off. */
        	      pingPacket(200, PING_BATTERYSAVE_BUZZER_OFF);
        	      TxAccel();        	  
        	      break;
        	      
        	   case FREEFALL_STATE:
        	      freefall_app();
        	      if (gu16Events & PING_FREEFALL) {
        	         appEventPacket(PING_FREEFALL, 5);   
        	      } else {
        	         pingPacket(50, PING_NO_OPTIONS);
        	      }
        	      break;
        	      
        	   case SHOCK_STATE:
        	      pingPacket(200, PING_NO_OPTIONS);
        	      shockDetect_app();
        	      break;
        	      

        	   case ANTITHEFT_STATE:
        	      antiTheft_app();
        	      pingPacket(200, PING_NO_OPTIONS);
        	      break;

        	   case BATTERYSAVE_STATE:
        	      if ((gu16Events & TIMER_EVENT0) != 0) {
        	         pingPacket(100, PING_BATTERYSAVE_BUZZER_OFF);   
        	      } else {
        	         pingPacket(100, PING_BATTERYSAVE_BUZZER_ON);
        	      }
        	      batterySave_app();
        	      break;


        	   //case CALIBRATION_STATE:
               // setLed(0, 0);  /* Turn off all the LEDs*/       	      
               // setLed(15, 0);  /* Turn ON all the LEDs*/
               // BUZZER = BUZZER_ON;
               // delay(200);
        	     // BUZZER = BUZZER_OFF;
        	     // calibration_app();
               // setLed(0, 0);  /* Turn off all the LEDs*/       	      
        	     // gi8AppStatus = XYZ_STATE;
        	     // EnableInterrupts;
        	     // break;
        	  }        	  
       
       
        /* Check for debounce timer (TIMER1) */
      
        if ((gu16Events & TIMER_EVENT1) != 0) {
            gu16Events &= ~TIMER_EVENT1; /* Clear the event */
            gu16timerEvent[1] = 0; /* Turn off the timer */
        }
       
       
        /* See if a Channel Request has been made */	      
        if ((gu16Events & KBI3_EVENT) != 0) {
               #if BUZZER_ENABLED==TRUE
               BUZZER = BUZZER_ON;
               delay(10);
               BUZZER = BUZZER_OFF;
               #endif
               gu16Events &= ~KBI3_EVENT; /* Clear the event */
        	      //// (gu8Channel == 15) {
        	     ////    gu8Channel = 0;  
        	      ///} else {
        	      ///   gu8Channel += 5;
        	     /// }
        	      
        	      
        	      /* Actually Switch Channels now that the ack is received */
        	      (void)MLMESetChannelRequest(gu8Channel);
        	      
        	      /* Now notify user that channel has changed */
        	      setLedsMode(LED_BINARY_MODE, (UINT8)(gu8Channel/5), 100, LED_NO_FLAGS);
               setLed(0, 0);  /* Turn off all the LEDs*/       	      
        	      KBI1SC |= 0x02; /* Enable Interrupts */
        }

        /* Calibration Procedure */	      
        #if ACCEL_ENABLED==TRUE

        if ((gu16Events & KBI5_EVENT) != 0) {
               #if BUZZER_ENABLED==TRUE
               BUZZER = BUZZER_ON;
               delay(10);
               BUZZER = BUZZER_OFF;
               #endif
               gu16Events &= ~KBI5_EVENT; /* Clear the event */
        	      
        	      setLed(0, 0);  /* Turn off all the LEDs*/       	      
                setLed(15, 0);  /* Turn ON all the LEDs*/
                #if BUZZER_ENABLED==TRUE
                BUZZER = BUZZER_ON;
                delay(200);
        	      BUZZER = BUZZER_OFF;
        	      #endif
        	      calibration_app();
                setLed(0, 0);  /* Turn off all the LEDs*/       	      
        	      gi8AppStatus = XYZ_STATE;
        	      EnableInterrupts;

        	     
        	     //KBI1SC |= 0x02; /* Enable Interrupts */
        }
        #endif
        /* See if a new application is requested */
        if ((gu16Events & KBI2_EVENT) != 0) {
            #if BUZZER_ENABLED==TRUE
            BUZZER = BUZZER_ON;
            delay(10);
            BUZZER = BUZZER_OFF;
            #endif
            gu16Events &= ~KBI2_EVENT;
            gi8AppStatus++;
            if(gi8AppStatus == XYZ_STATE){
              #if BUZZER_ENABLED==TRUE
              BUZZER = BUZZER_OFF;
              #endif
              (void)MLMERXDisableRequest();
            }
            
            /* if we are on the last app set back to the XYZ_STATE (1st state) */
            if (gi8AppStatus == LAST_STATE) {
               gi8AppStatus = PC_RADIO_STATE;
            }
            
            /* If there are no accelerometers do not allow board to go to ACCEL apps */
            #if ACCEL_ENABLED==FALSE
               gi8AppStatus = PC_RADIO_STATE;  
            #endif

            /* Blink the LEDs to indicate that app is running */           
            setLedsMode(LED_HEX_MODE, gi8AppStatus - PC_RADIO_STATE + 1, 50, LED_NO_FLAGS);          
            setLed(0, 0);  /* Turn off all the LEDs*/
            delay(50);
        }
     }

}


/**************************************************************
*    Function:     freefall_app()
*    Parameters: none
*    Return: none
*
*     This application detects a freefall condition.
*     Drop the remote accel board and it will send the condition to the 
*     PC_Radio connected to the PC and sound the Buzzer and Blink
*     All LEDS for 2 seconds.
***************************************************************/
void freefall_app(void) {
      doAccelMeasurement(Z_AXIS);
          
          if ((gau8TxDataBuffer[1] <= sCalValues.NzOff + 5) && (gau8TxDataBuffer[1] >= sCalValues.NzOff - 5)) {
				 gu16Events |= PING_FREEFALL; /* Free Falling tell PC Radio */
             
            #if BUZZER_ENABLED
             BUZZER = BUZZER_ON;
            #endif
             delay(50); /* delay 1/2 second */
            
          } else {
             gu16Events &= ~PING_FREEFALL; /* Clear the Freefall event */
             #if BUZZER_ENABLED
             BUZZER = BUZZER_OFF;
             #endif
          }
  
}
/**************************************************************
*    Function:     batterySave_app()
*    Parameters: none
*    Return: none
*
*     If the remote accel board is still for more than 5 seconds
*     the PC_radio board will BUZZER will quiet and blink the accel
*     LEDs.
***************************************************************/
void batterySave_app(void) {

   switch (gu8TxAccelState) {
      case 'x':
      case 'X':
          doAccelMeasurement(X_AXIS);
          
          /* Moving Case - update the Last moved variable */
          if (compareADCReading(u8LastX, gau8TxDataBuffer[1], APP_BATTERYSAVER_THRESHOLD) == TRUE) {
             u8LastX = gau8TxDataBuffer[1];
             LED1 ^= 1;  /* Toggle LED1 */

⌨️ 快捷键说明

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