野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9874|回复: 1

DMA内存发往串口实验

[复制链接]
发表于 2020-1-17 16:59:18 | 显示全部楼层 |阅读模式
/*
DMA内存发往串口实验
*/

#include"stm32f10x.h"
#define SENDBUFF_SIZE 5000 //内存块大小

int main(void)
{
        volatile unsigned int x;//延时计数
        uint16_t i;//填充计数
        uint8_t SendBuff[SENDBUFF_SIZE];//用数组开内存数据区

        GPIO_InitTypeDef GPIO_InitStructure;//端口结构体
        USART_InitTypeDef USART_InitStructure;//串口结构体
        DMA_InitTypeDef DMA_InitStructure;//DMA结构体

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_USART1,ENABLE);//开AB端口串口时钟
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);//开DMA1时钟(注意:是AHB)
        //GPIOA9串口输出
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//复位推挽输出
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9 ;
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_Init(GPIOA,&GPIO_InitStructure);
        //GPIOA10串口输入
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;//浮空输入
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
        GPIO_Init(GPIOA,&GPIO_InitStructure);
        //B口0脚led输出
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//推挽输出
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0 ;
        GPIO_Init(GPIOB,&GPIO_InitStructure);

        //串口设置
        USART_InitStructure.USART_BaudRate=115200;//波特率
        USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;//硬件流
        USART_InitStructure.USART_Mode= USART_Mode_Rx | USART_Mode_Tx;//模式
        USART_InitStructure.USART_Parity=USART_Parity_No;//非奇偶校验
        USART_InitStructure.USART_StopBits=USART_StopBits_1;//1位停止位
        USART_InitStructure.USART_WordLength=USART_WordLength_8b;//8位数据位
        USART_Init(USART1,&USART_InitStructure);//串口初始化
        USART_Cmd(USART1,ENABLE);//串口使能

        //DMA设置
        DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_BASE+0x04;//串口地址
        DMA_InitStructure.DMA_MemoryBaseAddr = (u32)SendBuff;//内存数据地址
        DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;//内存发到外设
        DMA_InitStructure.DMA_BufferSize = SENDBUFF_SIZE;//传输数据数量
        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_InitStructure.DMA_Mode = DMA_Mode_Circular;//循环发送
        DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;//中等优先级
        DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//禁止内存到内存传送
        DMA_Init(DMA1_Channel4, &DMA_InitStructure);//配置DMA通道
        DMA_Cmd (DMA1_Channel4,ENABLE);//打开DMA通道

        for(i=0;i<SENDBUFF_SIZE;i++) //内存数据生成
          {
            SendBuff[i]         = 'A';//内存数据填充
        }

        USART_DMACmd( USART1, USART_DMAReq_Tx, ENABLE);//请求发送

        while(1)
        {
            if (GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0)) //检测led电平
            GPIO_ResetBits(GPIOB,GPIO_Pin_0);//开灯
        else GPIO_SetBits(GPIOB,GPIO_Pin_0);//关灯
        for(x=0;x<0xfffff;x++);//延时
  }
}
回复

使用道具 举报

发表于 2020-1-18 20:52:07 | 显示全部楼层
很好很好,,
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-3-29 09:43 , Processed in 0.028044 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表