欢迎来到课桌文档! | 帮助中心 课桌文档-建筑工程资料库
课桌文档
全部分类
  • 党建之窗>
  • 感悟体会>
  • 百家争鸣>
  • 教育整顿>
  • 文笔提升>
  • 热门分类>
  • 计划总结>
  • 致辞演讲>
  • 在线阅读>
  • ImageVerifierCode 换一换
    首页 课桌文档 > 资源分类 > DOC文档下载  

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

    • 资源ID:21519       资源大小:149.05KB        全文页数:18页
    • 资源格式: DOC        下载积分:10金币
    快捷下载 游客一键下载
    会员登录下载
    三方登录下载: 微信开放平台登录 QQ登录  
    下载资源需要10金币
    邮箱/手机:
    温馨提示:
    用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP免费专享
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

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

    软件: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时钟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;GPIO_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_InitStructure.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_InitStructure.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 = 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_R* | USART_Mode_T*;/收发模式USART_Init(USART1, &USART_InitStructure); /初始化串口/ USART_ITConfig(USART1, USART_IT_R*NE, ENABLE);/开启中断USART_ITConfig(USART1, USART_IT_IDLE , ENABLE);/开启空闲中断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);/使能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; /浮空输入GPIO_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_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); ; /初始化串口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存放器/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 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);/开启中断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);/使能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_InitStructure); /初始化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_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_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;/子优先级3NVIC_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;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_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_InitStructure;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_APB2Periph_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_Init(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.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_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通道配置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_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); /初始化 DMA_ITConfig(DMA2_Channel3, DMA_IT_TC, ENABLE);DMA_ITConfig(DMA2_Channel3, DMA_IT_TE, ENABLE);USART_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_InitStructure);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_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 = (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_PeripheralDataSize_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_InitStructure); 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_InitStructure.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 = 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; 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); /使能/串口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_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_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_Channel7, &DMA_InitStructure); DMA_ITConfig(DMA1_Channel7, DMA_IT_TC, ENABLE); DMA_ITConfig(DMA1_Channel7, DMA_IT_TE, ENABLE); /* Enable USART1 DMA T* request */ 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.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

    注意事项

    本文(stm32f103多个串口配置dma程序纯代码版详解.doc)为本站会员(夺命阿水)主动上传,课桌文档仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知课桌文档(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    备案号:宁ICP备20000045号-1

    经营许可证:宁B2-20210002

    宁公网安备 64010402000986号

    课桌文档
    收起
    展开