- /****************************************************************************
- * [url=home.php?mod=space&uid=288409]@file[/url] main.c
- * [url=home.php?mod=space&uid=895143]@version[/url] V1.0
- * $Date: 16/09/22 10:04a $
- * @brief
- * Display how to use DSP FFT function
- *
- * @note
- * Copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
- *
- ******************************************************************************/
- #include <stdio.h>
- #include "M451Series.h"
- #include "arm_math.h"
- #define TEST_LENGTH_SAMPLES 2048 //Must be fftsize*2
-
- /* -------------------------------------------------------------------
- * External Input and Output buffer Declarations for FFT Bin Example
- * ------------------------------------------------------------------- */
- extern float32_t testInput_f32_10khz[TEST_LENGTH_SAMPLES];
- static float32_t testOutput[TEST_LENGTH_SAMPLES/2];
- /* ------------------------------------------------------------------
- * Global variables for FFT Bin Example
- * ------------------------------------------------------------------- */
- uint32_t fftSize = 1024;
- uint32_t ifftFlag = 0;
- uint32_t doBitReverse = 1;
- /* Reference index at which max energy of bin ocuurs */
- uint32_t refIndex = 213, testIndex = 0;
- arm_cfft_radix4_instance_f32 S;
- float32_t maxValue,Mainfreq;
- int CalTime;
- void SYS_Init(void)
- {
- /*---------------------------------------------------------------------------------------------------------*/
- /* Init System Clock */
- /*---------------------------------------------------------------------------------------------------------*/
- /* Enable HIRC clock */
- CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);
- /* Waiting for HIRC clock ready */
- CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);
- /* Switch HCLK clock source to HIRC */
- CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));
- /* Enable HXT */
- CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);
- /* Waiting for clock ready */
- CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);
- /* Set core clock as PLL_CLOCK from PLL and SysTick source to HCLK/2*/
- CLK_SetCoreClock(72000000);
- CLK_SetSysTickClockSrc(CLK_CLKSEL0_STCLKSEL_HCLK_DIV2);
- /* Enable peripheral clock */
- CLK_EnableModuleClock(UART0_MODULE);
- CLK_EnableModuleClock(TMR0_MODULE);
- /* Peripheral clock source */
- CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_TMR0SEL_HXT, CLK_CLKDIV0_UART(1));
- CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0SEL_HXT, 0);
- }
- void UART_Init(void)
- {
- /* Set PD multi-function pins for UART0 RXD and TXD */
- SYS->GPD_MFPL = SYS_GPD_MFPL_PD0MFP_UART0_RXD | SYS_GPD_MFPL_PD1MFP_UART0_TXD;
- /* Reset UART module */
- SYS_ResetModule(UART0_RST);
- /* Configure UART0 and set UART0 Baudrate */
- UART_Open(UART0, 115200);
- }
- /*---------------------------------------------------------------------------------------------------------*/
- /* Main Function */
- /*---------------------------------------------------------------------------------------------------------*/
- int32_t main(void)
- {
- /* Unlock protected registers */
- SYS_UnlockReg();
- /* Init System, peripheral clock and multi-function I/O */
- SYS_Init();
- /* Lock protected registers */
- SYS_LockReg();
- /* Init UART for printf */
- UART_Init();
- /* Measure FFT calcultion time */
- TIMER_Open(TIMER0, TIMER_CONTINUOUS_MODE, 1);
- TIMER_Start(TIMER0);
-
- /* Initialize the CFFT/CIFFT module */
- arm_cfft_radix4_init_f32(&S, fftSize,ifftFlag, doBitReverse);
-
- /* Process the data through the CFFT/CIFFT module */
- arm_cfft_radix4_f32(&S, testInput_f32_10khz);
- TIMER_Close(TIMER0);
- /* Process the data through the Complex Magnitude Module for calculating the magnitude at each bin */
- arm_cmplx_mag_f32(testInput_f32_10khz, testOutput,fftSize/2);
- /* Calculates maxValue and returns corresponding BIN value */
- arm_max_f32(testOutput, fftSize/2, &maxValue, &testIndex);
- CalTime=TIMER_GetCounter(TIMER0);
- /* Mainfreq=testindex*sample rate/fftsize */
- Mainfreq=testIndex*5000/fftSize;
-
- printf("Main frequency is %f\n",Mainfreq);
- printf("time is %d \n",CalTime);
-
- while(1);
-
- }
|