stm32f103多个串口配置dma程序纯代码版详解.doc

上传人:李司机 文档编号:1166036 上传时间:2022-07-12 格式:DOC 页数:18 大小:149.05KB
返回 下载 相关 举报
stm32f103多个串口配置dma程序纯代码版详解.doc_第1页
第1页 / 共18页
stm32f103多个串口配置dma程序纯代码版详解.doc_第2页
第2页 / 共18页
stm32f103多个串口配置dma程序纯代码版详解.doc_第3页
第3页 / 共18页
stm32f103多个串口配置dma程序纯代码版详解.doc_第4页
第4页 / 共18页
stm32f103多个串口配置dma程序纯代码版详解.doc_第5页
第5页 / 共18页
点击查看更多>>
资源描述

《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

23、ure;NVIC_InitTypeDef NVIC_InitStructure;DMA_InitTypeDef DMA_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_AP

24、B2Periph_GPIOC,ENABLE);/ 翻开串口外设的时钟RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);/串口4所使用管脚输出输入定义/定义UART4 T* (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_I

25、nit(GPIOC, &GPIO_InitStructure); /初始化串口1输出IO口/定义 UART4 R* (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.USAR

26、T_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;/不用

27、流量控制USART_InitStructure.USART_Mode = USART_Mode_R* | USART_Mode_T*; /使用接收和发送功能USART_Init(UART4, &USART_InitStructure); /初始化串口4USART_ITConfig(UART4, USART_IT_IDLE,ENABLE); /使能串口4接收中断USART_Cmd(UART4, ENABLE); /使能串口4USART_ClearFlag(UART4, USART_FLAG_TC); / 清标志(后增加)DMA_DeInit(DMA2_Channel3); /重置DMA 2通道配

28、置DMA_InitStructure.DMA_PeripheralBaseAddr = 0*40004C04; /外设地址 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_Per

29、ipheralInc_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_Ci

30、rcular; /工作在正常缓存模式 DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; /设置DMA通道优先级为高 DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; /制止DMA通道设置为内存至内存传输 DMA_Init(DMA2_Channel3, &DMA_InitStructure); /初始化 DMA_ITConfig(DMA2_Channel3, DMA_IT_TC, ENABLE);DMA_ITConfig(DMA2_Channel3, DMA_IT_TE, ENABLE);USA

31、RT_DMACmd(UART4, USART_DMAReq_R*, ENABLE);DMA_Cmd(DMA2_Channel3, ENABLE);/使能串口4中断NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&NVIC_I

32、nitStructure);NVIC_InitStructure.NVIC_IRQChannel = DMA2_Channel3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);USART_Cmd(UART4, ENABLE);/DMA的配置void DMA_

33、Configuration(void) DMA_InitTypeDef DMA_InitStructure; /* DMA clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);/DMA1/串口1 dma配置/ /* DMA1 Channel4 串口1 T*) Config */ DMA_DeInit(DMA1_Channel4); DMA_InitStructure.DMA_PeripheralBaseAddr = 0*40013804; DMA_InitStructure.DMA_MemoryBaseAddr =

34、 (uint32_t)USART1_SEND_DATA; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_BufferSize = 512; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDa

35、taSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; /DMA工作模式DMA_Mode_Circular DMA_Mode_Normal DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel4, &DMA_InitStruc

36、ture); DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE); DMA_ITConfig(DMA1_Channel4, DMA_IT_TE, ENABLE); /* Enable USART1 DMA T* request */ USART_DMACmd(USART1, USART_DMAReq_T*, ENABLE); DMA_Cmd(DMA1_Channel4, DISABLE); /使能 /* DMA1 Channel5 (串口1 R*) Config */ DMA_DeInit(DMA1_Channel5); DMA_InitStructu

37、re.DMA_PeripheralBaseAddr = 0*40013804; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)USART1_RECEIVE_DATA; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = 512; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc =

38、DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; /DMA工作模式DMA_Mode_Circular DMA_Mode_Normal DMA_InitStructure.DMA_Priority = DMA_Priority_High;

39、DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel5, &DMA_InitStructure); DMA_ITConfig(DMA1_Channel5, DMA_IT_TC, ENABLE); DMA_ITConfig(DMA1_Channel5, DMA_IT_TE, ENABLE); /* Enable USART1 DMA R* request */ USART_DMACmd(USART1, USART_DMAReq_R*, ENABLE); DMA_Cmd(DMA1_Channel5, ENABLE);

40、/使能/串口2 dma配置/ /* DMA1 Channel7 (串口2 T*) Config */ DMA_DeInit(DMA1_Channel7); DMA_InitStructure.DMA_PeripheralBaseAddr = 0*40004404; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)USART2_SEND_DATA; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_BufferSize = 512; DMA_InitS

41、tructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; /DMA工作

42、模式 DMA_Mode_Circular DMA_Mode_Normal DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel7, &DMA_InitStructure); DMA_ITConfig(DMA1_Channel7, DMA_IT_TC, ENABLE); DMA_ITConfig(DMA1_Channel7, DMA_IT_TE, ENABLE); /* Enable USART1 DMA T* r

43、equest */ USART_DMACmd(USART2, USART_DMAReq_T*, ENABLE); DMA_Cmd(DMA1_Channel7, DISABLE); /使能 /* DMA1 Channel6 (串口2 R*) Config */ DMA_DeInit(DMA1_Channel6); DMA_InitStructure.DMA_PeripheralBaseAddr = 0*40004404; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)USART2_RECEIVE_DATA; DMA_InitStructure.

44、DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = 512; 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_MemoryDataSize_Byte; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; /DM

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

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号