📄 accelerometer.c
字号:
RX_msg.pu8Buffer->u8Data[3] != PING_FREEFALL)
{
LED_TurnOffLed(LED4);
BUZZER_OFF();
}
break;
case XYZ_STATE:
break;
case FREEFALL_STATE:
if (RX_msg.pu8Buffer->u8Data[2] == PING_FREEFALL)
{
SoundsBuzzerOnce(LED4,200,0);
}
break;
case SHOCK_STATE:
switch (RX_msg.pu8Buffer->u8Data[2])
{
case X_AXIS:
SoundsBuzzerOnce(LED4,40,40);
break;
case Y_AXIS:
SoundsBuzzerTwice(LED4,40);
break;
case Z_AXIS:
SoundsBuzzerThrice(LED4,40);
break;
}
break;
case ANTITHEFT_STATE:
SoundsBuzzerOnce(LED4,40,0);
break;
}
}
}
/* Turn Radio Back on for next packet */
gi8PCRadioState = RECEIVER_ALWAYS_ON;
}
/*******************************************************************************
* This function calculate and set in RAM the new calibration values
* of the accelerometer demo application.
*******************************************************************************/
void calibration_app(void)
{
#if ACCEL_ENABLED==TRUE
uint16_t X=0, Y=0, Z=0;
uint8_t i;
LED_TurnOnAllLeds(); /* Turn ON all the LEDs*/
SoundsBuzzerOnce(LED1, 800, 0);
CalculateNewCalibrationValues();
SetNewCalibrationValues();
NVM_Erase(gNvmInternalInterface_c, NvmType, 0x00010000);
NVM_BlankCheck(gNvmInternalInterface_c, NvmType, 0x00010000, 7);
NVM_Write(gNvmInternalInterface_c, NvmType,(void *)(&sCalValues), 0x00010000, 7);
NVM_Read(gNvmInternalInterface_c, NvmType, (void *)(&sCalValues), 0x00010000, 7);
LED_TurnOffAllLeds(); /* Turn off all the LEDs*/
gi8AppStatus = XYZ_STATE;
#endif
}
/*******************************************************************************
* This is the interrupt function when SW4 has been pressed and catch the Event
*******************************************************************************/
void calibration_app_isr(void)
{
gu16Events |= SW4_EVENT;
}
/*******************************************************************************
* This function setup the BUZZER
*******************************************************************************/
void SetupBuzzer(void)
{
Gpio_SetPortFunction(gGpioPort0_c, gGpioNormalMode_c, 0x00000800);
Gpio_SetPortDir(gGpioPort0_c, 0x00000800, 0x00000800);
Timer_Setup(gTmr1_c, Tmr1_Isr, 1250);
}
/*******************************************************************************
* This function setup the Non-Volatile Memory
*******************************************************************************/
void NVMSetup(void)
{
#if ACCEL_ENABLED==TRUE
VRegCntl.vReg = g1P8VReg_c;
VRegCntl.cntl.vReg1P8VEn = TRUE;
CRM_VRegCntl(&VRegCntl);
DelayMs(2);
NVM_Detect(gNvmInternalInterface_c, &NvmType);
#endif
}
/*******************************************************************************
* This function setup the GPIOs connected to the adc module
*******************************************************************************/
void AdcGpioInit(void)
{
Gpio_SetPortFunction(gGpioPort1_c, gGpioAlternate1Mode_c, 0x000003FF);
Gpio_SetPortFunction(gGpioPort1_c, gGpioNormalMode_c, 0x00000C00);
}
/*******************************************************************************
* This function setup the ADC
*******************************************************************************/
void ADC_Setup(void)
{
#if ACCEL_ENABLED==TRUE
AdcConfig_t sAdc_Config;
AdcConvCtrl_t adcConvCtrl;
Adc_Init();
AdcGpioInit();
Adc_Reset();
(void)Adc_SetFifoCtrl(FIFO_DEEP);
Adc_DefaultConfig(sAdc_Config, 24000);
sAdc_Config.adcMode = gAdcManualControl_c;
(void)Adc_SetConfig(&sAdc_Config);
Adc_TurnOn();
adcConvCtrl.adcTmrOn = TRUE;
adcConvCtrl.adcSeqIrqEn = FALSE;
adcConvCtrl.adcChannels = 0;
adcConvCtrl.adcChannels |= (1 << gAdcChan2_c) | (1 << gAdcChan3_c) | (1 << gAdcChan4_c);
adcConvCtrl.adcChannels |= (1 << gAdcBatt_c);
adcConvCtrl.adcTmBtwSamples = 1999; // 2 ms between samples
adcConvCtrl.adcSeqMode = gAdcSeqOnConvTime_c; //sequence mode
adcConvCtrl.adcRefVoltage = gAdcBatteryRefVoltage_c;
(void)Adc_SetConvCtrl(gAdcPrimary_c, &adcConvCtrl);
#endif
}
/*******************************************************************************
* This function setup the TMR Module
*******************************************************************************/
void Timer_Setup(TmrNumber_t tNumber, TmrCallbackFunction_t pCallbackFunc, uint16_t comp_value)
{
TmrEnable(tNumber);
TmrSetMode(tNumber, gTmrNoOperation_c);
pConfig.tmrOutputMode = gTmrToggleOF_c;
pConfig.tmrCoInit = FALSE;
pConfig.tmrCntDir = FALSE;
pConfig.tmrCntLen = TRUE;
pConfig.tmrCntOnce = FALSE;
pConfig.tmrSecondaryCntSrc = gTmrSecondaryCnt0Input_c;
pConfig.tmrPrimaryCntSrc = gTmrPrimaryClkDiv4_c;
pStatusCtrl.uintValue = 0x4001;
SetCompLoad1Val(tNumber, 0x00); //clear comparator 1 load
SetCompLoad2Val(tNumber, 0x00); //clear comparator 2 load
SetLoadVal(tNumber, 0x00); //timer start from 0
SetComp1Val(tNumber, comp_value);
SetComp2Val(tNumber, 0x00); //clear comparator 2 value
TmrSetMode(tNumber, gTmrCntRiseEdgFallEdgPriSrc_c);
TmrSetConfig(tNumber, &pConfig);
TmrSetCallbackFunction(tNumber, gTmrCompEvent_c, pCallbackFunc);
TmrSetStatusControl(tNumber, &pStatusCtrl);
}
/*******************************************************************************
* This function setup the Accelerometer
*******************************************************************************/
void SetupAccelerometer(void)
{
#if ACCEL_ENABLED == TRUE
Gpio_SetPortFunction(gGpioPort0_c, gGpioNormalMode_c, 0x00000700);
Gpio_SetPortDir(gGpioPort0_c, 0x00000700, 0x00000700);
Gpio_SetPinData(ACCEL_GSLEEP, gGpioPinStateHigh_c);
Gpio_SetPinData(ACCEL_GSELECT1,gGpioPinStateLow_c);
Gpio_SetPinData(ACCEL_GSELECT2,gGpioPinStateLow_c);
#endif
}
/*******************************************************************************
* This function change the application state to set in the
* next accelerometer application.
*******************************************************************************/
void increase_App(void)
{
#if ACCEL_ENABLED==FALSE
gi8AppStatus = PC_RADIO_STATE;
RX_msg.u8BufSize = RX_SIZE;
MLMERXEnableRequest(&RX_msg, 0x0000F000);
#else
gi8AppStatus++;
if(gi8AppStatus == XYZ_STATE)
{
BUZZER_OFF();
#if OTAP_ENABLED == FALSE
MLMERXDisableRequest(&RX_msg);
#endif
}
if(gi8AppStatus == LAST_STATE)
{
gi8AppStatus = PC_RADIO_STATE;
RX_msg.u8BufSize = RX_SIZE;
MLMERXEnableRequest(&RX_msg, 0x0000F000);
}
#endif
SetLeds(gi8AppStatus, 60);
LED_TurnOffAllLeds();
}
/*******************************************************************************
* This is the interrupt function when SW1 has been pressed and catch the Event
*******************************************************************************/
void increase_AppState_isr(void)
{
gu16Events |= SW1_EVENT;
}
/*******************************************************************************
* This function is using for change the operating channel.
*******************************************************************************/
void increase_channel(void)
{
static uint8_t LED = LED3;
LED_TurnOffAllLeds();
if (gu8Channel >= CHANNEL26)
{
gu8Channel = CHANNEL11;
}
else
{
gu8Channel += 5;
}
(void)MLMESetChannelRequest((channel_num_t)gu8Channel);
SoundsBuzzerOnce(LED, 60, 0);
if(LED == LED1) LED = LED4;
else LED >>= 1;
}
/*******************************************************************************
* This is the interrupt function when SW2 has been pressed and catch the Event
*******************************************************************************/
void increase_channel_isr(void)
{
gu16Events |= SW2_EVENT;
}
/*******************************************************************************
* This function is using for turning on and turning off the LEDs when
* changing the accelerometer application.
*******************************************************************************/
void SetLeds(uint8_t gi8App, uint16_t gi16LedsDelay)
{
switch(gi8App)
{
case PC_RADIO_STATE:
SoundsBuzzerOnce(LED4, gi16LedsDelay, 0);
break;
case XYZ_STATE:
SoundsBuzzerOnce(LED3, gi16LedsDelay, 0);
break;
case FREEFALL_STATE:
LED_TurnOnLed(LED4);
SoundsBuzzerOnce(LED3, gi16LedsDelay, 0);
LED_TurnOffLed(LED4);
break;
case SHOCK_STATE:
SoundsBuzzerOnce(LED2, gi16LedsDelay, 0);
break;
case ANTITHEFT_STATE:
LED_TurnOnLed(LED4);
SoundsBuzzerOnce(LED2, gi16LedsDelay, 0);
LED_TurnOffLed(LED4);
break;
case BATTERYSAVE_STATE :
LED_TurnOnLed(LED2);
SoundsBuzzerOnce(LED3, gi16LedsDelay, 0);
LED_TurnOffLed(LED2);
break;
}
}
/*******************************************************************************
* This is the Timer0 interrupt. When the value of gu16timerEvent is equal
* to gu16msTimer a Timer Event is set.
*******************************************************************************/
void Tmr_Isr(TmrNumber_t tmrNumber)
{
gu16msTimer++;
if (gu16timerEvent[0] != 0)
{
if(gu16timerEvent[0] == gu16msTimer)
{
gu16Events |= TIMER_EVENT;
gu16timerEvent[0] = 0; /* Turn off the timer0 */
}
}
}
/*******************************************************************************
* This is the Timer1 interrupt. To sound the Buzzer.
*******************************************************************************/
void Tmr1_Isr(TmrNumber_t tmrNumber)
{
if(Buzzer)
{
if(toggle&0x01)
{
Gpio_SetPinData(gGpioPin11_c,gGpioPinStateHigh_c);
}
else
{
Gpio_SetPinData(gGpioPin11_c,gGpioPinStateLow_c);
}
toggle++;
}
else
{
toggle = 0;
}
}
/************************************************************************************
*************************************************************************************
* Private functions
*************************************************************************************
************************************************************************************/
/************************************************************************************
*************************************************************************************
* Private Debug stuff
*************************************************************************************
************************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -