#define USART_REC_LEN 16 //定义最大接收字节数 16
extern u8 USART2_RX_BUF[USART_REC_LEN];
u8 USART2_RX_BUF[USART_REC_LEN];
void uart2_init(u32 bound)
{
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
//NVIC_InitTypeDef NVIC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2|RCC_APB2Periph_GPIOA, ENABLE); //使能USART2,GPIOA时钟
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
//USART2_TX GPIOA.2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA.2
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.2
//USART2_RX GPIOA.3初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//PA3
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.3
//Usart2 NVIC 配置
// NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//抢占优先级3
// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //子优先级3
// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
// NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;//串口波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式文章来源:https://www.toymoban.com/news/detail-820643.html
USART_Init(USART2, &USART_InitStructure); //初始化串口2
// USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启串口接受中断
/* DMA1 channel1 configuration */
DMA_DeInit(DMA1_Channel6);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(USART2->DR);//外设站点起始的地址
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)USART2_RX_BUF;//存储器的地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//传输方向 从外设站点搬到存储器站点
DMA_InitStructure.DMA_BufferSize = 16;//数据量(字节数小于65535)
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外设地址不需要自增
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//地址需要自增
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//宽度 字节
DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular ; //单次 循环
DMA_InitStructure.DMA_Priority = DMA_Priority_High;//优先级设置高级
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//使能 存储器到存储器的搬运
DMA_Init(DMA1_Channel6, &DMA_InitStructure);
/* Enable DMA1 Channel6 Transfer Complete interrupt */
// DMA_ITConfig(DMA1_Channel5, DMA_IT_TC, ENABLE);//传输结束的的中断
DMA_Cmd(DMA1_Channel6,ENABLE);
USART_DMACmd(USART2,USART_DMAReq_Rx,ENABLE);
USART_Cmd(USART2, ENABLE); //使能串口2
}文章来源地址https://www.toymoban.com/news/detail-820643.html
到了这里,关于stm32f103c8r6 串口2数据DMA的接收的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!