Exemplo n.º 1
0
Arquivo: main.c Projeto: dazuo78/TBall
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
	float x,y,z;
    short sx,sy,sz;
    unsigned char regVal;
    unsigned char regVals[29]; //0x1D~0x39
	/* STM32F103xG HAL library initialization:
	   - Configure the Flash prefetch
	   - Systick timer is configured by default as source of time base, but user 
		 can eventually implement his proper time base source (a general purpose 
		 timer for example or other time source), keeping in mind that Time base 
		 duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
		 handled in milliseconds basis.
	   - Set NVIC Group Priority to 4
	   - Low Level Initialization
	*/
	HAL_Init();

	/* Configure the system clock to 72 MHz */
	SystemClock_Config();
    
    //uart
    BSP_COM_Init(COM1, &huart);

	/* Configure LED1 and LED3 */
	BSP_LED_Init(LED1);
	BSP_LED_Init(LED3);

	/* Configure Key push-button */
	BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO);

	/*##-1- Configure the I2C peripheral ######################################*/
	ADXL345_Init(ADXL345_I2C_COMM);
  
   
    // full resolution
    ADXL345_SetRangeResolution(ADXL345_RANGE_PM_2G, 1); 
    
    // enter measurement mode
    ADXL345_SetPowerMode(1);
    
    // self test mode
//    regVal = ADXL345_GetRegisterValue(ADXL345_DATA_FORMAT);
//    regVal |= ADXL345_SELF_TEST;
//    ADXL345_SetRegisterValue(ADXL345_DATA_FORMAT, regVal);
    // dump ADXL345 registers
    HAL_Delay(1);
    //ADXL345_GetMultiRegisterValue(ADXL345_THRESH_TAP, sizeof(regVals), regVals);

	/* Infinite loop */  
    printf("\n\r UART Printf Example: retarget the C library printf function to the UART\n\r");
	while (1)
	{
        
        ADXL345_GetGxyz(&x, &y, &z);
        printf("\n\rx: %.1f, y: %.1f, z: %.1f\n\r", x, y, z);
       // ADXL345_GetXyz(&sx, &sy, &sz);
       // printf("\n\rx: %d, y: %d, z: %d\n\r", sx, sy, sz);
        HAL_Delay(1000);
	}
}
Exemplo n.º 2
0
/***************************************************************************//**
 * @brief Main function.
 *
 * @return None.
*******************************************************************************/
void main(void)
{
    /* Set pin 7 and pin 8 (J11 connector) as input, to avoid the output pins
    of the Pmod ACL to be connected to an output pin of the microcontroller. */
    PM7 |= (1 << 6);    // P76 (PMOD-IRQA)
    PM7 |= (1 << 7);    // P77 (PMOD-IRQB)
    
    /* Initialize RDKRL78G14. */
    RDKRL78G14_Init();
    
    /* Enable interrupts. */
    __enable_interrupt();
        
    /* Initialize timer. */
    TIME_Init();
    
    /* Initialize the ST7579 Display. */
    ST7579_Init();
    
    /* Initialize ADXL345. */
    if(ADXL345_Init(ADXL345_SPI_COMM) == 0)
    {
        ADI_Component("ADXL345 OK");
    }
    else
    {
        ADI_Component("ADXL345 Error");
    }
    ADXL345_SetTapDetection(ADXL345_SINGLE_TAP |
                            ADXL345_DOUBLE_TAP, // Tap type.
                            ADXL345_TAP_Z_EN,   // Axis control.
                            0x10,               // Tap duration.
                            0x10,               // Tap latency.
                            0x40,               // Tap window. 
                            0x10,               // Tap threshold.
                            0x00);              // Interrupt Pin.
    ADXL345_SetFreeFallDetection(0x01,  // Free-fall detection enabled.
                                 0x05,  // Free-fall threshold.
                                 0x14,  // Time value for free-fall detection.
                                 0x00); // Interrupt Pin.
    
    /* Set the range and the resolution. */
    ADXL345_SetRangeResolution(ADXL345_RANGE_PM_4G, ADXL345_FULL_RES);
    ADXL345_SetPowerMode(0x1);          // Measure mode.
    while(1)
    {
        /* Read and display the output data of each axis. */ 
        ADXL345_GetGxyz(&x, &y, &z);
        ST7579_String(2, 0, "X data:      [g]", 0); 
        ST7579_FloatNumber(2, 42, x, 3, 0); 
        ST7579_String(3, 0, "Y data:      [g]", 0); 
        ST7579_FloatNumber(3, 42, y, 3, 0); 
        ST7579_String(4, 0, "Z data:      [g]", 0); 
        ST7579_FloatNumber(4, 42, z, 3, 0);
        intSource = ADXL345_GetRegisterValue(ADXL345_INT_SOURCE);
        if((intSource & ADXL345_SINGLE_TAP) != 0)
        {
            ST7579_String(5, 0, "Single Tap", 0);
        }
        if((intSource & ADXL345_DOUBLE_TAP) != 0)
        {
            ST7579_String(6, 0, "Double Tap", 0);
        }
        if((intSource & ADXL345_FREE_FALL) != 0)
        {
            ST7579_String(7, 0, "Free-Fall", 0);
        }
        TIME_DelayMs(100);
    }
}