int16_t GetMedianNum(int16_t * bArray, int16_t iFilterLen)
{
uint16_t i,j;// Ñ»·±äÁ¿
int32_t bTemp;
// ÓÃðÅÝ·¨¶ÔÊý×é½øÐÐÅÅÐò
for (j = 0; j < iFilterLen - 1; j ++)
{
for (i = 0; i < iFilterLen - j - 1; i ++)
{
if (bArray[i] > bArray[i + 1])
{
// »¥»»
bTemp = bArray[i];
bArray[i] = bArray[i + 1];
bArray[i + 1] = bTemp;
}
}
}
// ¼ÆËãÖÐÖµ
if ((iFilterLen & 1) > 0)
{
// Êý×éÓÐÆæÊý¸öÔªËØ£¬·µ»ØÖмäÒ»¸öÔªËØ
bTemp = bArray[(iFilterLen + 1) / 2];
}
else
{
// Êý×éÓÐżÊý¸öÔªËØ£¬·µ»ØÖмäÁ½¸öÔªËØƽ¾ùÖµ
bTemp = (bArray[iFilterLen / 2] + bArray[iFilterLen / 2 + 1]) / 2;
}
return (int16_t)bTemp;
}
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] Main program
* @param None
* @retval None
*/
int main(void)
{
uint16_t i=0,j=0;
int16_t readingX[20],readingY[20],readingZ[20];
int16_t o_X0=0,o_Y0=0,o_Z0=0;
int16_t o_X1,o_Y1,o_Z1;
/* STM32F4xx HAL library initialization:
- Configure the Flash prefetch, instruction and Data caches
- Configure the Systick to generate an interrupt each 1 msec
- Set NVIC Group Priority to 4
- Global MSP (MCU Support Package) initialization
*/
HAL_Init();
/* Configure the system clock to 180 MHz */
SystemClock_Config();
/* Configure LED2 */
BSP_LED_Init(LED2);
MX_I2C1_Init();
/* Output SYSCLK on MCO2 pin(PC.09) */
HAL_RCC_MCOConfig(RCC_MCO2, RCC_MCO2SOURCE_SYSCLK, RCC_MCODIV_1);
LCD_Init();
GetFontsWidth(); //fonts width get
if(ADXL_getDevId() == 0xE5) LCD_WriteString(0,0,(uint8_t *)"ADXL345 is OK!",&Font32x32,BLACK,WHITE,USEBACKCOLOR);
else return 1;
//Go into standby mode to configure the device.
ADXL_setPowerControl(0x00);
//Full resolution, +/-16g, 4mg/LSB.
ADXL_setDataFormatControl(0x0B);
//3.2kHz data rate.
ADXL_setDataRate(ADXL345_3200HZ);
//Measurement mode.
ADXL_setPowerControl(0x08);
/*##-3- Toggle PA05 IO in an infinite loop #################################*/
while (1)
{
readingX[j]=ADXL_getAx()/3; //ËõСÕñ·ù
readingY[j]=ADXL_getAy()/3; //ËõСÕñ·ù
readingZ[j]=ADXL_getAz()/3; //ËõСÕñ·ù
j++;
if(j==20) j=0;
LCD_DrawLine(i+2,40,i+2,239,WHITE);
LCD_DrawLine(i+3,40,i+3,239,WHITE);
o_X1=GetMedianNum(readingX,20);
LCD_DrawLine(i,o_X0+139,i+1,o_X1+139,RED);
o_X0=o_X1;
o_Y1=GetMedianNum(readingY,20);
LCD_DrawLine(i,o_Y0+139,i+1,o_Y1+139,GREEN);
o_Y0=o_Y1;
o_Z1=GetMedianNum(readingZ,20);
LCD_DrawLine(i,o_Z0+139,i+1,o_Z1+139,BLUE);
o_Z0=o_Z1;
i++;
if(i==320)
{
i=0;
LCD_DrawLine(0,40,0,239,WHITE);
LCD_DrawLine(1,40,1,239,WHITE);
}
BSP_LED_Toggle(LED2);
HAL_Delay(50);
}
}
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] System Clock Configuration
* The system Clock is configured as follow :
* System Clock source = PLL (HSE)
* SYSCLK(Hz) = 180000000
* HCLK(Hz) = 180000000
* AHB Prescaler = 1
* APB1 Prescaler = 4
* APB2 Prescaler = 2
* HSE Frequency(Hz) = 8000000
* PLL_M = 8
* PLL_N = 360
* PLL_P = 2
* PLL_Q = 7
* PLL_R = 2
* VDD(V) = 3.3
* Main regulator output voltage = Scale1 mode
* Flash Latency(WS) = 5
* @param None
* @retval None
*/
static void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;
HAL_StatusTypeDef ret = HAL_OK;
/* Enable Power Control clock */
__HAL_RCC_PWR_CLK_ENABLE();
/* The voltage scaling allows optimizing the power consumption when the device is
clocked below the maximum system frequency, to update the voltage scaling value
regarding system frequency refer to product datasheet. */
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/* Enable HSE Oscillator and activate PLL with HSE as source */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 360;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
RCC_OscInitStruct.PLL.PLLR = 2;
ret = HAL_RCC_OscConfig(&RCC_OscInitStruct);
if(ret != HAL_OK)
{
while(1) { ; }
}
/* Activate the OverDrive to reach the 180 MHz Frequency */
ret = HAL_PWREx_EnableOverDrive();
if(ret != HAL_OK)
{
while(1) { ; }
}
/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
ret = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
if(ret != HAL_OK)
{
while(1) { ; }
}
}
采集的视频如下,由于LCD的坐标原点为左上角,所以显示的时候可能会有倒置的现象。