STM32F103多个串口配置DMA程序纯代码版详解.docx

上传人:b****8 文档编号:30549701 上传时间:2023-08-16 格式:DOCX 页数:32 大小:21.28KB
下载 相关 举报
STM32F103多个串口配置DMA程序纯代码版详解.docx_第1页
第1页 / 共32页
STM32F103多个串口配置DMA程序纯代码版详解.docx_第2页
第2页 / 共32页
STM32F103多个串口配置DMA程序纯代码版详解.docx_第3页
第3页 / 共32页
STM32F103多个串口配置DMA程序纯代码版详解.docx_第4页
第4页 / 共32页
STM32F103多个串口配置DMA程序纯代码版详解.docx_第5页
第5页 / 共32页
点击查看更多>>
下载资源
资源描述

STM32F103多个串口配置DMA程序纯代码版详解.docx

《STM32F103多个串口配置DMA程序纯代码版详解.docx》由会员分享,可在线阅读,更多相关《STM32F103多个串口配置DMA程序纯代码版详解.docx(32页珍藏版)》请在冰豆网上搜索。

STM32F103多个串口配置DMA程序纯代码版详解.docx

STM32F103多个串口配置DMA程序纯代码版详解

软件:

KeilMDK

芯片:

STM32F103/STM32F107

串口数量:

4

备注:

4个串口均配置为DMA读取

//串口1初始化函数

voiduart1_init(u32bound)

{

//GPIO端口设置

GPIO_InitTypeDefGPIO_InitStructure;

USART_InitTypeDefUSART_InitStructure;

NVIC_InitTypeDefNVIC_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA,ENABLE);//使能USART1,GPIOA时钟

USART_DeInit(USART1);//复位串口1

//USART1_TXPA.9

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//PA.9

GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//复用推挽输出

GPIO_Init(GPIOA,&GPIO_InitStructure);//初始化PA9

//USART1_RXPA.10

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;//浮空输入

GPIO_Init(GPIOA,&GPIO_InitStructure);//初始化PA10

//Usart1NVIC配置

NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;//抢占优先级3

NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;//子优先级3

NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;//IRQ通道使能

NVIC_Init(&NVIC_InitStructure);//根据指定的参数初始化VIC寄存器

/*EnableDMAChannel4Interrupt*/

NVIC_InitStructure.NVIC_IRQChannel=DMA1_Channel4_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;

NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;

NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;

NVIC_Init(&NVIC_InitStructure);

 

/*EnableDMAChannel5Interrupt*/

NVIC_InitStructure.NVIC_IRQChannel=DMA1_Channel5_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;

NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;

NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;

NVIC_Init(&NVIC_InitStructure);

 

//USART参数初始化设置

USART_InitStructure.USART_BaudRate=bound;//一般设置为9600;

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;//收发模式

USART_Init(USART1,&USART_InitStructure);//初始化串口

//USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//开启中断

USART_ITConfig(USART1,USART_IT_IDLE,ENABLE);//开启空闲中断

USART_Cmd(USART1,ENABLE);//使能串口

}

//串口2初始化函数

voiduart2_init(u32bound)

{

GPIO_InitTypeDefGPIO_InitStructure;

USART_InitTypeDefUSART_InitStructure;

NVIC_InitTypeDefNVIC_InitStructure;

 

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//使能GPIOA时钟

RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//使能USART2时钟

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;//PA2

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//复用推挽

GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_Init(GPIOA,&GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;//PA3

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;//浮空输入

GPIO_Init(GPIOA,&GPIO_InitStructure);

RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,ENABLE);//复位串口2

RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,DISABLE);//停止复位

USART_InitStructure.USART_BaudRate=bound;//一般设置为9600;

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;//收发模式

USART_Init(USART2,&USART_InitStructure);;//初始化串口

NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn;//使能串口2中断

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;//先占优先级2级

NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;//从优先级2级

NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;//使能外部中断通道

NVIC_Init(&NVIC_InitStructure);//根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器

//////////////////////EnableDMAChannel7Interrupt//////////////////////////////

NVIC_InitStructure.NVIC_IRQChannel=DMA1_Channel7_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;

NVIC_InitStructure.NVIC_IRQChannelSubPriority=2;

NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;

NVIC_Init(&NVIC_InitStructure);

 

/*EnableDMAChannel6Interrupt*/

NVIC_InitStructure.NVIC_IRQChannel=DMA1_Channel6_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;

NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;

NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;

NVIC_Init(&NVIC_InitStructure);

//USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//开启中断

USART_ITConfig(USART2,USART_IT_IDLE,ENABLE);//开启空闲中断

USART_Cmd(USART2,ENABLE);//使能串口

}

//串口3初始化函数

voiduart3_init(u32bound)

{

//GPIO端口设置

GPIO_InitTypeDefGPIO_InitStructure;

USART_InitTypeDefUSART_InitStructure;

NVIC_InitTypeDefNVIC_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//使能GPIOB时钟

RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);//使能USART3时钟

USART_DeInit(USART3);//复位串口3

//USART1_TXPB.10

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//PB.10

GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//复用推挽输出

GPIO_Init(GPIOB,&GPIO_InitStructure);//初始化PB.11

//USART1_RXPB.11

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11;

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;//浮空输入

GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_Init(GPIOB,&GPIO_InitStructure);//初始化PB

//USART初始化设置

//#ifdefEN_USART3_RX//如果使能了接收

USART_InitStructure.USART_BaudRate=bound;//一般设置为9600;

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;//收发模式

USART_Init(USART3,&USART_InitStructure);//初始化串口

//Usart1NVIC配置

NVIC_InitStructure.NVIC_IRQChannel=USART3_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;//抢占优先级3

NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;//子优先级3

NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;//IRQ通道使能

NVIC_Init(&NVIC_InitStructure);//根据指定的参数初始化VIC寄存器

//////////////////////*EnableDMAChannel2Interrupt*//////////////////

NVIC_InitStructure.NVIC_IRQChannel=DMA1_Channel2_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;

NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;

NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;

NVIC_Init(&NVIC_InitStructure);

/*EnableDMAChannel3Interrupt*/

NVIC_InitStructure.NVIC_IRQChannel=DMA1_Channel3_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;

NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;

NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;

NVIC_Init(&NVIC_InitStructure);

 

//USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);//开启中断

USART_ITConfig(USART3,USART_IT_IDLE,ENABLE);//开启空闲中断

USART_Cmd(USART3,ENABLE);//使能串口

}

//串口4初始化函数

voiduart4_init(u32bound)

{

GPIO_InitTypeDefGPIO_InitStructure;

USART_InitTypeDefUSART_InitStructure;

NVIC_InitTypeDefNVIC_InitStructure;

DMA_InitTypeDefDMA_InitStructure;//定义DMA初始化结构体DMA_InitStructure

//NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);//选择NVIC优先级分组0

RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4,ENABLE);

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2,ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);

//打开串口外设的时钟

RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4,ENABLE);

//串口4所使用管脚输出输入定义

//定义UART4Tx(PC.10)脚为复用推挽输出

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//IO口的第2脚

GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//IO口速度

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//IO口复用推挽输出

GPIO_Init(GPIOC,&GPIO_InitStructure);//初始化串口1输出IO口

//定义UART4Rx(PC.11)为悬空输入

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11;//IO口的第3脚

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;//IO口悬空输入

GPIO_Init(GPIOC,&GPIO_InitStructure);//初始化串口1输入IO口

//串口4参数初始化定义部分,串口1参数为9600,8,1,N接收中断方式

USART_InitStructure.USART_BaudRate=bound;//设定传输速率

USART_InitStructure.USART_WordLength=USART_WordLength_8b;//设定传输数据位数

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;//使用接收和发送功能

USART_Init(UART4,&USART_InitStructure);//初始化串口4

USART_ITConfig(UART4,USART_IT_IDLE,ENABLE);//使能串口4接收中断

USART_Cmd(UART4,ENABLE);//使能串口4

USART_ClearFlag(UART4,USART_FLAG_TC);//清标志(后增加)

DMA_DeInit(DMA2_Channel3);//重置DMA2通道配置

DMA_InitStructure.DMA_PeripheralBaseAddr=0x40004C04;//外设地址

DMA_InitStructure.DMA_MemoryBaseAddr=(u32)USART4_RECEIVE_DATA;//内存地址

DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralSRC;//外设作为数据目的地

DMA_InitStructure.DMA_BufferSize=512;//DMA缓存大小:

BufferSize

DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable;//外设地址寄存器不递增

DMA_InitStructure.DMA_MemoryInc=DMA_MemoryInc_Enable;//内存地址寄存器递增

DMA_InitStructure.DMA_PeripheralDataSize=DMA_PeripheralDataSize_Byte;//外设数据宽度为8位

DMA_InitStructure.DMA_MemoryDataSize=DMA_PeripheralDataSize_Byte;//内存数据宽度为8位

DMA_InitStructure.DMA_Mode=DMA_Mode_Circular;//工作在正常缓存模式

DMA_InitStructure.DMA_Priority=DMA_Priority_VeryHigh;//设置DMA通道优先级为高

DMA_InitStructure.DMA_M2M=DMA_M2M_Disable;//禁止DMA通道设置为内存至内存传输

DMA_Init(DMA2_Channel3,&DMA_InitStructure);//

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 初中教育 > 初中作文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1