请写出在软件 Keil uVision5 环境中使用 Hal 库进行开发,编 写 C 程序用于配置时钟、配置 GPIO 引脚以实现 I2S 串口功能,并调用滤波器解码库 libPDMFilter_Keil.lib 实现滤波处理将 PDM 信号转化为 PCM 信号,的代码
时间: 2024-02-29 20:52:38 浏览: 169
以下是一个基本的C语言程序,实现了在Keil uVision5环境中使用Hal库进行开发,配置时钟、配置GPIO引脚以实现I2S串口功能,并调用滤波器解码库libPDMFilter_Keil.lib实现滤波处理将PDM信号转化为PCM信号。
```c
#include "stm32f4xx_hal.h"
#include "libPDMFilter_Keil.h"
#define I2S_BUFFER_SIZE 512
#define PDM_BUFFER_SIZE 1024
extern void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2S_Init(void);
uint16_t I2S_Buffer[I2S_BUFFER_SIZE];
uint16_t PDM_Buffer[PDM_BUFFER_SIZE];
uint16_t PCM_Buffer[PDM_BUFFER_SIZE / 8];
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_I2S_Init();
while (1)
{
HAL_I2S_Receive(&hi2s2, (uint16_t *)I2S_Buffer, I2S_BUFFER_SIZE, 1000);
PDM_Filter_64_LSB((uint8_t *)I2S_Buffer, (uint16_t *)PDM_Buffer, AUDIO_SAMPLING_FREQUENCY);
PDM_Filter_64_LSB((uint8_t *)&I2S_Buffer[16], (uint16_t *)&PDM_Buffer[64], AUDIO_SAMPLING_FREQUENCY);
PDM_Filter_64_LSB((uint8_t *)&I2S_Buffer[32], (uint16_t *)&PDM_Buffer[128], AUDIO_SAMPLING_FREQUENCY);
PDM_Filter_64_LSB((uint8_t *)&I2S_Buffer[48], (uint16_t *)&PDM_Buffer[192], AUDIO_SAMPLING_FREQUENCY);
for (int i = 0; i < PDM_BUFFER_SIZE / 8; i++)
{
PCM_Buffer[i] = (uint16_t)PDM_Filter((uint8_t *)&PDM_Buffer[i * 8]);
}
HAL_I2S_Transmit(&hi2s2, (uint16_t *)PCM_Buffer, PDM_BUFFER_SIZE / 8, 1000);
}
}
static void MX_I2S_Init(void)
{
hi2s2.Instance = SPI2;
hi2s2.Init.Mode = I2S_MODE_MASTER_RX;
hi2s2.Init.Standard = I2S_STANDARD_MSB;
hi2s2.Init.DataFormat = I2S_DATAFORMAT_16B;
hi2s2.Init.AudioFreq = AUDIO_SAMPLING_FREQUENCY;
hi2s2.Init.CPOL = I2S_CPOL_LOW;
hi2s2.Init.ClockSource = I2S_CLOCK_PLL;
hi2s2.Init.FullDuplexMode = I2S_FULLDUPLEXMODE_DISABLE;
hi2s2.Init.MCLKOutput = I2S_MCLKOUTPUT_DISABLE;
if (HAL_I2S_Init(&hi2s2) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
/* I2S2 GPIO Configuration */
/* PB12 ------> I2S2_WS */
/* PB13 ------> I2S2_CK */
/* PB15 ------> I2S2_SD */
GPIO_InitStruct.Pin = GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {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.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 25;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
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_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
Error_Handler();
}
}
```
需要注意的是,在使用libPDMFilter_Keil库时,需要在程序中包含相应的头文件,并且在程序中调用相应的PDM_Filter_64_LSB函数进行滤波处理。此外,还需要定义I2S_BUFFER_SIZE和PDM_BUFFER_SIZE来确定I2S和PDM缓冲区的大小。
阅读全文