stm32f103ze通过i2s和dma发送数据的代码
时间: 2023-08-21 11:02:43 浏览: 136
下面是stm32f103ze通过i2s和dma发送数据的代码:
```c
#include "stm32f10x.h"
#define I2S_DR_ADDRESS 0x4000380C
#define BufferSize 32
uint32_t TxBuffer[BufferSize] = {
0x12345678,0x23456789,0x34567890,0x45678901,
0x56789012,0x67890123,0x78901234,0x89012345,
0x90123456,0x01234567,0x12345678,0x23456789,
0x34567890,0x45678901,0x56789012,0x67890123,
0x78901234,0x89012345,0x90123456,0x01234567,
0x12345678,0x23456789,0x34567890,0x45678901,
0x56789012,0x67890123,0x78901234,0x89012345,
0x90123456,0x01234567
};
DMA_InitTypeDef DMA_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef I2S_InitStructure;
void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void DMA_Configuration(void)
{
DMA_DeInit(DMA1_Channel5);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)I2S_DR_ADDRESS;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)TxBuffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = BufferSize;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel5, &DMA_InitStructure);
}
void I2S_Configuration(void)
{
SPI_I2S_DeInit(SPI2);
I2S_InitStructure.I2S_Mode = I2S_Mode_MasterTx;
I2S_InitStructure.I2S_Standard = I2S_Standard_Phillips;
I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_16b;
I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Enable;
I2S_InitStructure.I2S_AudioFreq = I2S_AudioFreq_44k;
I2S_InitStructure.I2S_CPOL = I2S_CPOL_Low;
SPI_I2S_Init(SPI2, &I2S_InitStructure);
SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx, ENABLE);
I2S_Cmd(SPI2, ENABLE);
}
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
DMA_Configuration();
I2S_Configuration();
while (1);
}
```
这段代码主要包含四个函数,分别用于配置时钟、GPIO、DMA和I2S。其中,通过DMA发送数据是实现高效数据传输的关键。这段代码中的TxBuffer数组用于存储要发送的数据,使用DMA方式将其发送到I2S的数据寄存器中,然后通过I2S接口将数据传输出去。
阅读全文