Exemplo n.º 1
0
/*********************************************************\
* Initialize MMA865xQ
\*********************************************************/
void MMA865x_Init (void)
{
  byte n;
  /*
  **  Reset sensor, and wait for reboot to complete
  */
  IIC_RegWrite(SlaveAddressIIC, CTRL_REG2, RST_MASK);
  do {
    n = IIC_RegRead(SlaveAddressIIC, CTRL_REG2);
  } while (n & RST_MASK);
  /*
  **  Configure sensor for:
  **    - Sleep Mode Poll Rate of 50Hz (20ms)
  **    - System Output Data Rate of 200Hz (5ms)
  **    - Full Scale of +/-8g
  */
  IIC_RegWrite(SlaveAddressIIC, CTRL_REG1, ASLP_RATE_20MS + DATA_RATE_5MS);
  /*
  **  Configure sensor data for:
  **    - Full Scale of +/-8g
  **
  **  XYZ Data Event Flag Enable
  */
  IIC_RegWrite(SlaveAddressIIC, XYZ_DATA_CFG_REG, FULL_SCALE_2G);
  full_scale = FULL_SCALE_2G;
}
Exemplo n.º 2
0
/*********************************************************\
* Put MMA865xQ into Active Mode
\*********************************************************/
void MMA865x_Active (void)
{
  /*
  ** Set the Active bit in System Control 1 Register.
  */
  IIC_RegWrite(SlaveAddressIIC, CTRL_REG1, (IIC_RegRead(SlaveAddressIIC, CTRL_REG1) | ACTIVE_MASK));
}
Exemplo n.º 3
0
/*********************************************************\
* Put MMA865xQ into Standby Mode
\*********************************************************/
byte MMA865x_Standby (void)
{
  byte n;
  /*
  **  Read current value of System Control 1 Register.
  **  Put sensor into Standby Mode by clearing the Active bit.
  **  Return with previous value of System Control 1 Register.
  */
  n = IIC_RegRead(SlaveAddressIIC, CTRL_REG1);
  IIC_RegWrite(SlaveAddressIIC, CTRL_REG1, n & (~ACTIVE_MASK));
  return (n & ACTIVE_MASK);
}
Exemplo n.º 4
0
/*********************************************************\
* Put MPL3115A2 into Standby Mode
\*********************************************************/
byte MPL3115A2_Standby (void)
{
  byte n;  
  /*
  **  Read current value of System Control 1 Register.
  **  Put sensor into Standby Mode.
  **  Return with previous value of System Control 1 Register.
  */
  n = IIC_RegRead(SlaveAddressIIC, CTRL_REG1);
  IIC_RegWrite(SlaveAddressIIC, CTRL_REG1, n & STANDBY_SBYB_0);

  return(n);
}          
Exemplo n.º 5
0
/*********************************************************\
* Initialize MPL3115A2
\*********************************************************/
void MPL3115A2_Init_Bar (void)
{
  
  /*
  **  Configure sensor for:
  **    - 128 Oversampling
  **    - Barometer Mode
  **    - Set Interrupts to Active Low/Open Drain
  **    - Set Data Event Flags for Pressure/Altitude and Temperature
  **    - Set Generate Data Event Flag
  */
  IIC_RegWrite(SlaveAddressIIC, CTRL_REG1, (OS2_MASK | OS1_MASK | OS0_MASK));
  IIC_RegWrite(SlaveAddressIIC, CTRL_REG2, CLEAR_CTRLREG2);    
  IIC_RegWrite(SlaveAddressIIC, CTRL_REG3, (PP_OD1_MASK | PP_OD2_MASK));  
  IIC_RegWrite(SlaveAddressIIC, CTRL_REG4, INT_EN_CLEAR);
  IIC_RegWrite(SlaveAddressIIC, CTRL_REG5, INT_CFG_CLEAR);    
  IIC_RegWrite(SlaveAddressIIC, PT_DATA_CFG_REG, (DREM_MASK | PDEFE_MASK | TDEFE_MASK));    
 
}
Exemplo n.º 6
0
/***********************************************************
* Function:       // 函数名称
* Description:    // 函数功能、性能等的描述
* Input:          // 1.输入参数1,说明,包括每个参数的作用、取值说明及参数间关系
* Input:          // 2.输入参数2,说明,包括每个参数的作用、取值说明及参数间关系
* Output:         // 1.输出参数1,说明
* Return:         // 函数返回值的说明
* Others:         // 其它说明
***********************************************************/
uint8_t mma8451_config( uint16_t param1, uint16_t param2 )
{
    unsigned char res = 0;
    param_mma8451_word1 = param1;
    param_mma8451_word2 = param2;

    //standby
    if( IIC_RegWrite( MMA845X_ADDR, CTRL_REG1, ( CTRL_REG1_value & ~ACTIVE_MASK ) ) )
    {
        goto lbl_mma8451_config_err;
    }


    /*
       res = IIC_RegWrite( MMA845X_ADDR, CTRL_REG1, CTRL_REG1_value);
       if( res )
       {
       goto lbl_mma8451_config_err;
       }
     */
    if( IIC_RegWrite( MMA845X_ADDR, XYZ_DATA_CFG_REG, FULL_SCALE_4G ) )
    {
        goto lbl_mma8451_config_err;
    }


    /*Step 2: Set the data rate to 50 Hz (for example, but can choose any sample rate).
       CTRL_REG1_Data = IIC_RegRead(0x2A); //Note: Can combine this step with above
       CTRL_REG1_Data& = 0xC7; //Clear the sample rate bits
       CTRL_REG1_Data | = 0x20; //Set the sample rate bits to 50 Hz
       IC_RegWrite(0x2A, CTRL_REG1_Data); //Write updated value into the register.
     */


    /*
       res = IIC_RegRead( MMA845X_ADDR, CTRL_REG1, &value );
       if( res )
       {
       goto lbl_mma8451_config_err;
       }
       value	&= 0xC7;
       value	|= 0x20;
       res		= IIC_RegWrite( MMA845X_ADDR, CTRL_REG1, value );
       if( res )
       {
       goto lbl_mma8451_config_err;
       }
     */


    /*
       Step 3: Set the PL_EN bit in Register 0x11 PL_CFG. This will enable the orientation detection.
       PLCFG_Data = IIC_RegRead (0x11);
       PLCFG_Data | = 0x40;
       IIC_RegWrite(0x11, PLCFG_Data);
     */

    if( IIC_RegWrite( MMA845X_ADDR, PL_CFG_REG, 0xc0 ) )
    {
        goto lbl_mma8451_config_err;
    }


    /*Step 4: Set the Back/Front Angle trip points in register 0x13 following the table in the data sheet.
       NOTE: This register is readable in all versions of MMA845xQ but it is only modifiable in the MMA8451Q.
       PL_BF_ZCOMP_Data = IIC_RegRead(0x13);
       PL_BF_ZCOMP_Data& = 0x3F; //Clear bit 7 and 6
       Select one of the following to set the B/F angle value:
       PL_BF_ZCOMP_Data | = 0x00; //This does nothing additional and keeps bits [7:6] = 00
       PL_BF_ZCOMP_Data | = 0x40; //Sets bits[7:6] = 01
       PL_BF_ZCOMP_Data | = 0x80; //Sets bits[7:6] = 02
       PL_BF_ZCOMP_Data | = 0xC0; //Sets bits[7:6] = 03
       IIC_RegWrite(0x13, PL_BF_ZCOMP_Data); //Write in the updated Back/Front Angle

       Step 5: Set the Z-Lockout angle trip point in register 0x13 following the table in the data sheet.
       //NOTE: This register is readable in all versions of MMA845xQ but it is only modifiable in the MMA8451Q.
       PL_BF_ZCOMP_Data = IIC_RegRead(0x1C); //Read out contents of the register (can be read by all
       versions of MMA845xQ)
       The remaining parts of this step only apply to MMA8451Q
       PL_BF_ZCOMP_Data& = 0xF8; //Clear the last three bits of the register
       Select one of the following to set the Z-lockout value
       PL_BF_ZCOMP_Data | = 0x00; //This does nothing additional but the Z-lockout selection will remain at
       14°
       PL_BF_ZCOMP_Data | = 0x01; //Set the Z-lockout angle to 18°
       PL_BF_ZCOMP_Data | = 0x02; //Set the Z-lockout angle to 21°
       PL_BF_ZCOMP_Data | = 0x03; //Set the Z-lockout angle to 25°
       PL_BF_ZCOMP_Data | = 0x04; //Set the Z-lockout angle to 29°
       PL_BF_ZCOMP_Data | = 0x05; //Set the Z-lockout angle to 33°
       PL_BF_ZCOMP_Data | = 0x06; //Set the Z-lockout angle to 37°
       PL_BF_ZCOMP_Data | = 0x07; //Set the Z-lockout angle to 42°
       IIC_RegWrite(0x13, PL_BF_ZCOMP_Data); //Write in the updated Z-lockout angle
     */

    if( IIC_RegWrite( MMA845X_ADDR, PL_BF_ZCOMP_REG, PL_BF_ZCOMP_REG_value ) )
    {
        goto lbl_mma8451_config_err;
    }


    /*
       Step 6: Set the Trip Threshold Angle
       NOTE: This register is readable in all versions of MMA845xQ but it is only modifiable in the MMA8451Q.
       Select the angle desired in the table, and, Enter in the values given in the table for the corresponding angle.
       Refer to Figure 7 for the reference frame of the trip angles.
       P_L_THS_Data = IIC_RegRead(0x14); (can be read by all versions of MMA845xQ)
       The remaining parts of this step only apply to MMA8451Q
       P_L_THS_Data& = 0x07; //Clear the Threshold values
       Choose one of the following options
       P_L_THS_Data | = (0x07)<<3; //Set Threshold to 15°
       P_L_THS_Data | = (0x09)<<3; //Set Threshold to 20°
       P_L_THS_Data | = (0x0C)<<3; //Set Threshold to 30°
       P_L_THS_Data | = (0x0D)<<3; //Set Threshold to 35°
       P_L_THS_Data | = (0x0F)<<3; //Set Threshold to 40°
       P_L_THS_Data | = (0x10)<<3; //Set Threshold to 45°
       P_L_THS_Data | = (0x13)<<3; //Set Threshold to 55°
       P_L_THS_Data | = (0x14)<<3; //Set Threshold to 60°
       P_L_THS_Data | = (0x17)<<3; //Set Threshold to 70°
       P_L_THS_Data | = (0x19)<<3; //Set Threshold to 75°
       IIC_RegWrite(0x14, P_L_THS_Data);

       Step 7: Set the Hysteresis Angle
       NOTE: This register is readable in all versions of MMA845xQ but it is only modifiable in the MMA8451Q.
       Select the hysteresis value based on the desired final trip points (threshold + hysteresis)
       Enter in the values given in the table for that corresponding angle.
       Note: Care must be taken. Review the final resulting angles. Make sure there isn’t a resulting trip value
       greater than 90 or less than 0.
       The following are the options for setting the hysteresis.
       P_L_THS_Data = IIC_RegRead(0x14);
       NOTE: The remaining parts of this step only apply to the MMA8451Q.
       P_L_THS_Data& = 0xF8; //Clear the Hysteresis values
       P_L_THS_Data | = 0x01; //Set Hysteresis to ±4°
       P_L_THS_Data | = 0x02; //Set Threshold to ±7°
       P_L_THS_Data | = 0x03; //Set Threshold to ±11°
       P_L_THS_Data | = 0x04; //Set Threshold to ±14°
       P_L_THS_Data | = 0x05; //Set Threshold to ±17°
       P_L_THS_Data | = 0x06; //Set Threshold to ±21°
       P_L_THS_Data | = 0x07; //Set Threshold to ±24°
       IIC_RegWrite(0x14, P_L_THS_Data);
     */

    if( IIC_RegWrite( MMA845X_ADDR, PL_P_L_THS_REG, PL_P_L_THS_REG_vlaue ) )
    {
        goto lbl_mma8451_config_err;
    }


    /*
       Step 8: Register 0x2D, Control Register 4 configures all embedded features for interrupt
       detection.
       To set this device up to run an interrupt service routine:
       Program the Orientation Detection bit in Control Register 4.
       Set bit 4 to enable the orientation detection “INT_EN_LNDPRT”.
       CTRL_REG4_Data = IIC_RegRead(0x2D); //Read out the contents of the register
       CTRL_REG4_Data | = 0x10; //Set bit 4
       IIC_RegWrite(0x2D, CTRL_REG4_Data); //Set the bit and write into CTRL_REG4
     */

    if( IIC_RegWrite( MMA845X_ADDR, CTRL_REG4, 0x10 ) )
    {
        goto lbl_mma8451_config_err;
    }


    /*
       Step 9: Register 0x2E is Control Register 5 which gives the option of routing the interrupt to
       either INT1 or INT2
       Depending on which interrupt pin is enabled and configured to the processor:
       Set bit 4 “INT_CFG_LNDPRT” to configure INT1, or,
       Leave the bit clear to configure INT2.
       CTRL_REG5_Data = IIC_RegRead(0x2E);
       In the next two lines choose to clear bit 4 to route to INT2 or set bit 4 to route to INT1
       CTRL_REG5_Data& = 0xEF; //Clear bit 4 to choose the interrupt to route to INT2
       CTRL_REG5_Data | = 0x10; //Set bit 4 to choose the interrupt to route to INT1
       IIC_RegWrite(0x2E, CTRL_REG5_Data); //Write in the interrupt routing selection
     */
    //value=IIC_RegRead(MMA845X_ADDR,CTRL_REG5);
    ////value&=0xEF;
    //value |= 0x10;  // INT1
    //IIC_RegWrite(MMA845X_ADDR,CTRL_REG5,value);


    /*
       Step 10: Set the debounce counter in register 0x12
       This value will scale depending on the application-specific required ODR.
       If the device is set to go to sleep, reset the debounce counter before the device goes to sleep. This setting
       helps avoid long delays since the debounce will always scale with the current sample rate. The debounce
       can be set between 50 ms - 100 ms to avoid long delays.
       IIC_RegWrite(0x12, 0x05); //This sets the debounce counter to 100 ms at 50 Hz
     */

    res = IIC_RegWrite( MMA845X_ADDR, PL_COUNT_REG, 0x02 );
    if( res )
    {
        goto lbl_mma8451_config_err;
    }


    /*
       Step 11: Put the device in Active Mode
       CTRL_REG1_Data = IIC_RegRead(0x2A); //Read out the contents of the register
       CTRL_REG1_Data | = 0x01; //Change the value in the register to Active Mode.
       IIC_RegWrite(0x2A, CTRL_REG1_Data); //Write in the updated value to put the device in Active Mode
     */

    //	value=IIC_RegRead(MMA845X_ADDR,CTRL_REG1);
    //	value |= 0x01;
    //	IIC_RegWrite(MMA845X_ADDR,CTRL_REG1,value);
    //active

    /*TAP设置*/
    if( IIC_RegWrite( MMA845X_ADDR, PULSE_CFG_REG, 0x55 ) )
    {
        goto lbl_mma8451_config_err;
    }

    //加速度门限 0.1g 1-79	扩展为0.0625  1-127  扩大1.6倍
    /*
    	res = ( ( param_mma8451_word2 & 0xff00 ) >> 8 ) * 1.6;

    	if( IIC_RegWrite( MMA845X_ADDR, PULSE_THSX_REG, res ) )
    	{
    		goto lbl_mma8451_config_err;
    	}
    	if( IIC_RegWrite( MMA845X_ADDR, PULSE_THSY_REG, res ) )
    	{
    		goto lbl_mma8451_config_err;
    	}
    	if( IIC_RegWrite( MMA845X_ADDR, PULSE_THSZ_REG, res ) )
    	{
    		goto lbl_mma8451_config_err;
    	}

    	res = param_mma8451_word2 & 0xff;
    	if( IIC_RegWrite( MMA845X_ADDR, PULSE_TMLT_REG, res ) )
    	{
    		goto lbl_mma8451_config_err;
    	}
    */

    res = ( param_mma8451_word2 & 0xff) * 1.6;

    if( IIC_RegWrite( MMA845X_ADDR, PULSE_THSX_REG, res ) )
    {
        goto lbl_mma8451_config_err;
    }
    if( IIC_RegWrite( MMA845X_ADDR, PULSE_THSY_REG, res ) )
    {
        goto lbl_mma8451_config_err;
    }
    if( IIC_RegWrite( MMA845X_ADDR, PULSE_THSZ_REG, res ) )
    {
        goto lbl_mma8451_config_err;
    }

    res = (param_mma8451_word2 & 0xff00) >> 8;
    if( IIC_RegWrite( MMA845X_ADDR, PULSE_TMLT_REG, res ) )
    {
        goto lbl_mma8451_config_err;
    }



    if( IIC_RegWrite( MMA845X_ADDR, CTRL_REG1, ( CTRL_REG1_value | ACTIVE_MASK ) ) )
    {
        goto lbl_mma8451_config_err;
    }

    if( IIC_RegWrite( MMA845X_ADDR, CTRL_REG4, 0x10 ) )
    {
        goto lbl_mma8451_config_err;
    }


    return ERR_NONE;

lbl_mma8451_config_err:
    return res;
}
Exemplo n.º 7
0
/*********************************************************\
* Put MPL3115A2 into Active Mode
\*********************************************************/
void MPL3115A2_Active (void)
{
  IIC_RegWrite(SlaveAddressIIC, CTRL_REG1, (IIC_RegRead(SlaveAddressIIC, CTRL_REG1) | ACTIVE_MASK));
}