#include "main.h"
#include "arm_math.h"
#define N 256 // FFT长度
#define Fs 10000 // 采样率
#define ADC_BUF_LEN 512 // ADC缓存长度
#define OLED_WIDTH 128 // OLED宽度
#define OLED_HEIGHT 64 // OLED高度
uint16_t ADC_Buffer[ADC_BUF_LEN]; // ADC采样缓存
uint16_t FFT_Input[N]; // FFT输入缓存
float32_t FFT_Output[N]; // FFT输出缓存
float32_t FFT_Mag[N/2]; // FFT幅值缓存
uint8_t OLED_Buffer[OLED_WIDTH*OLED_HEIGHT/8]; // OLED显示缓存
extern SPI_HandleTypeDef hspi1; // OLED所用SPI口
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_ADC1_Init(void);
static void MX_SPI1_Init(void);
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) { // ADC完成一次转换的回调函数
uint32_t i, j;
for(i=0, j=0; i<N; i+=2, j++) { // 交错采样,同时处理两个ADC采样值
FFT_Input[j] = ADC_Buffer[i];
}
arm_rfft_fast_instance_f32 S; // 初始化FFT结构体
arm_rfft_fast_init_f32(&S, N);
arm_rfft_fast_f32(&S, FFT_Input, FFT_Output, 0); // 执行FFT
arm_cmplx_mag_f32(FFT_Output, FFT_Mag, N/2); // 计算幅值
for(i=0; i<OLED_WIDTH; i++) { // 将频谱转换为显示格式
uint8_t row_data = 0;
for(j=i*OLED_HEIGHT/OLED_WIDTH; j<(i+1)*OLED_HEIGHT/OLED_WIDTH; j++) {
if(FFT_Mag[j] > 20000) { // 限制幅值范围
FFT_Mag[j] = 20000;
}
uint8_t bit = (FFT_Mag[j] * OLED_HEIGHT / 20000.0) * 255 / 8; // 将幅值映射到OLED高度
row_data |= (bit << (j % 8)); // 存储到OLED缓存中
}
OLED_Buffer[i*OLED_HEIGHT/8] = row_data;
}
HAL_SPI_Transmit(&hspi1, OLED_Buffer, OLED_WIDTH*OLED_HEIGHT/8, 10); // 将OLED缓存发送到OLED显示屏
}
int main(void) {
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_DMA_Init();
MX_ADC1_Init();
MX_SPI1_Init();
HAL_ADC_Start_DMA(&hadc1, (uint32_t*)ADC_Buffer, ADC_BUF_LEN); // 启动ADC采样
while (1) {
}
}
void SystemClock_Config(void) {
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC|RCC_PERIPHCLK_SPI1;
PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
PeriphClkInit.Spi1ClockSelection = RCC_SPI1CLKSOURCE_PCLK2;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
Error_Handler();
}
}
static void MX_ADC1_Init(void) {
ADC_ChannelConfTypeDef sConfig = {0};
/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
*/
hadc1.Instance = ADC1;
hadc1.Init.ScanConvMode = ENABLE;
hadc1.Init.ContinuousConvMode = ENABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 1;
if (HAL_ADC_Init(&hadc1) != HAL_OK) {
Error_Handler();
}
/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_13CYCLES_5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) {
Error_Handler();
}
}
static void MX_DMA_Init(void) {
/* DMA controller clock enable */
__HAL_RCC_DMA1_CLK_ENABLE();
/* DMA interrupt init */
/* DMA1_Channel1_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
static void MX_SPI1_Init(void) {
/* SPI1 parameter configuration*/
hspi1.Instance = SPI1;
hspi1.Init.Mode= SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_1LINE;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 10;
if (HAL_SPI_Init(&hspi1) != HAL_OK) {
Error_Handler();
}
}
void Error_Handler(void) {
/* USER CODE BEGIN Error_Handler_Debug /
/ User can add his own implementation to report the HAL error return state /
while(1) {
}
/ USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t file, uint32_t line) {
/ USER CODE BEGIN 6 /
/ User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) /
/ USER CODE END 6 */
}
#endif
|