stm32f103多个串口配置dma程序纯代码版详解.doc
《stm32f103多个串口配置dma程序纯代码版详解.doc》由会员分享,可在线阅读,更多相关《stm32f103多个串口配置dma程序纯代码版详解.doc(18页珍藏版)》请在课桌文档上搜索。
1、软件:KeilMDK芯片:STM32F103/STM32F107串口数量:4备注:4个串口均配置为DMA读取/串口1初始化函数void uart1_init(u32 bound) /GPIO端口设置GPIO_InitTypeDef GPIO_InitStructure;USART_InitTypeDef USART_InitStructure;NVIC_InitTypeDef NVIC_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);/使能USART1,GPIOA时
2、钟USART_DeInit(USART1); /复位串口1/USART1_T* PA.9GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; /PA.9GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;/复用推挽输出GPIO_Init(GPIOA, &GPIO_InitStructure); /初始化PA9/USART1_R* PA.10GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;GP
3、IO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;/浮空输入GPIO_Init(GPIOA, &GPIO_InitStructure); /初始化PA10/Usart1 NVIC 配置NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ;/抢占优先级3NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;/子优先级3NVIC_InitStruc
4、ture.NVIC_IRQChannelCmd = ENABLE;/IRQ通道使能NVIC_Init(&NVIC_InitStructure);/根据指定的参数初始化VIC存放器/*Enable DMA Channel4 Interrupt */NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;NVIC_InitStruc
5、ture.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&NVIC_InitStructure);/*Enable DMA Channel5 Interrupt */NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel5_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;NVIC_InitStructure.NVIC_IRQChannelCmd =
6、 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_
7、InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;/无硬件数据流控制USART_InitStructure.USART_Mode = USART_Mode_R* | USART_Mode_T*;/收发模式USART_Init(USART1, &USART_InitStructure); /初始化串口/ USART_ITConfig(USART1, USART_IT_R*NE, ENABLE);/开启中断USART_ITConfig(USART1, USART_IT_IDLE , ENABLE);/开
8、启空闲中断USART_Cmd(USART1, ENABLE); /使能串口 /串口2初始化函数void uart2_init(u32 bound)GPIO_InitTypeDef GPIO_InitStructure;USART_InitTypeDef USART_InitStructure;NVIC_InitTypeDef NVIC_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);/使能GPIOA时钟RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);/
9、使能USART2时钟GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;/PA2GPIO_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;/PA3GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; /浮空输入GP
10、IO_Init(GPIOA, &GPIO_InitStructure); RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,ENABLE);/复位串口2RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,DISABLE);/停顿复位USART_InitStructure.USART_BaudRate = bound;/一般设置为9600;USART_InitStructure.USART_WordLength = USART_WordLength_8b;/8位数据长度USART_InitStructure.USART
11、_StopBits = USART_StopBits_1;/一个停顿位USART_InitStructure.USART_Parity = USART_Parity_No;/奇偶校验位USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;/无硬件数据流控制USART_InitStructure.USART_Mode = USART_Mode_R* | USART_Mode_T*;/收发模式USART_Init(USART2, &USART_InitStructure); ; /初始化串口NV
12、IC_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中指定的参数初始化外设NV
13、IC存放器/Enable DMA Channel7 Interrupt /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);/*Enable DMA Channel6
14、Interrupt */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_R*NE, ENABLE)
15、;/开启中断USART_ITConfig(USART2, USART_IT_IDLE , ENABLE);/开启空闲中断USART_Cmd(USART2, ENABLE); /使能串口 /串口3初始化函数void uart3_init(u32 bound) /GPIO端口设置GPIO_InitTypeDef GPIO_InitStructure;USART_InitTypeDef USART_InitStructure;NVIC_InitTypeDef NVIC_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE)
16、;/使能GPIOB时钟RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);/使能USART3时钟USART_DeInit(USART3); /复位串口3/USART1_T* PB.10GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; /PB.10GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;/复用推挽输出GPIO_Init(GPIOB, &GPIO_InitSt
17、ructure); /初始化PB.11/USART1_R* PB.11GPIO_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 初始化设置/*ifdef EN_USART3_R* /如果使能了接收USART_InitStructure.USART_Baud
18、Rate = 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;/无硬件数据流控制U
19、SART_InitStructure.USART_Mode = USART_Mode_R* | USART_Mode_T*;/收发模式USART_Init(USART3, &USART_InitStructure); /初始化串口/Usart1 NVIC 配置NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;/抢占优先级3NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;/子优先级3NVI
20、C_InitStructure.NVIC_IRQChannelCmd = ENABLE;/IRQ通道使能NVIC_Init(&NVIC_InitStructure);/根据指定的参数初始化VIC存放器/ /*Enable DMA Channel2 Interrupt */ /NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel2_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1
21、;NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&NVIC_InitStructure);/*Enable DMA Channel3 Interrupt */NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel3_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;NVIC_InitStructure.NVIC_
22、IRQChannelCmd = ENABLE;NVIC_Init(&NVIC_InitStructure);/ USART_ITConfig(USART3, USART_IT_R*NE, ENABLE);/开启中断USART_ITConfig(USART3, USART_IT_IDLE , ENABLE);/开启空闲中断USART_Cmd(USART3, ENABLE); /使能串口 /串口4初始化函数void uart4_init(u32 bound)GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStruct
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- stm32f103 串口 配置 dma 程序 代码 详解
链接地址:https://www.desk33.com/p-21519.html