先上代码:
#define comm_com usart2#define comm_com_clk rcc_apb1periph_usart2#define comm_com_tx_gpio_clk rcc_ahb1periph_gpiod //uart tx#define comm_com_tx_pin gpio_pin_5#define comm_com_tx_gpio_port gpiod#define comm_com_tx_source gpio_pinsource5#define comm_com_tx_af gpio_af_usart2#define comm_com_rx_gpio_clk rcc_ahb1periph_gpiod //uart rx#define comm_com_rx_pin gpio_pin_6#define comm_com_rx_gpio_port gpiod#define comm_com_rx_source gpio_pinsource6#define comm_com_rx_af gpio_af_usart2#define comm_com_irqn usart2_irqn#define comm_com_priority 9 //优先级#define comm_com_baudrate 115200 //波特率#define comm_com_irqhandler usart2_irqhandler //中断函数接口(见stm32f4xx_it.c)usart配置:/************************************************函数名称 :usart_comm_configuration功 能 :通信串口配置参 数 :无返 回 值 :无作 者 :strongerhuang*************************************************/void usart_comm_configuration(void){ gpio_inittypedef gpio_initstructure; usart_inittypedef usart_initstructure; nvic_inittypedef nvic_initstructure; /* 时钟配置 */ rcc_ahb1periphclockcmd(comm_com_tx_gpio_clk | comm_com_rx_gpio_clk, enable); if((usart1 == comm_com) || (usart6 == comm_com)) rcc_apb2periphclockcmd(comm_com_clk, enable); else rcc_apb1periphclockcmd(comm_com_clk, enable); /* 复用配置 */ gpio_pinafconfig(comm_com_tx_gpio_port, comm_com_tx_source, comm_com_tx_af); gpio_pinafconfig(comm_com_rx_gpio_port, comm_com_rx_source, comm_com_rx_af); /* 引脚配置 */ gpio_initstructure.gpio_pin = comm_com_tx_pin; //usart tx gpio_initstructure.gpio_mode = gpio_mode_af; //复用模式 gpio_initstructure.gpio_speed = gpio_speed_50mhz; gpio_initstructure.gpio_otype = gpio_otype_pp; gpio_initstructure.gpio_pupd = gpio_pupd_up; gpio_init(comm_com_tx_gpio_port, &gpio_initstructure); gpio_initstructure.gpio_pin = comm_com_rx_pin; //usart rx gpio_init(comm_com_rx_gpio_port, &gpio_initstructure); /* nvic配置 */ nvic_initstructure.nvic_irqchannel = comm_com_irqn; nvic_initstructure.nvic_irqchannelpreemptionpriority = comm_com_priority; nvic_initstructure.nvic_irqchannelsubpriority = 0; nvic_initstructure.nvic_irqchannelcmd = enable; nvic_init(&nvic_initstructure); /* usart配置 */ usart_initstructure.usart_baudrate = comm_com_baudrate; //波特率 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(comm_com, &usart_initstructure); usart_clearflag(comm_com, usart_flag_rxne | usart_flag_tc); usart_itconfig(comm_com, usart_it_rxne, enable); //接收中断 usart_dmacmd(comm_com, usart_dmareq_tx, enable); //使能dma usart_cmd(comm_com, enable); //使能usart} dma配置中的宏定义:
#define comm_dr_address ((uint32_t)usart2 + 0x04)#define comm_dma dma1#define comm_dma_clk rcc_ahb1periph_dma1#define comm_tx_dma_channel dma_channel_4#define comm_tx_dma_stream dma1_stream6#define comm_tx_dma_flag_tcif dma_flag_tcif6#define comm_tx_dma_irqn dma1_stream6_irqn#define comm_tx_dma_priority 8 //优先级#define comm_tx_dma_irqhandler dma1_stream6_irqhandler //中断函数接口(见stm32f4xx_it.c)#define comm_tx_dma_it_tcif dma_it_tcif6 通讯串口的配置
/************************************************函数名称 :usart_comm_dma_configuration功 能 :通信串口的dma配置参 数 :无返 回 值 :无作 者 :strongerhuang*************************************************/void usart_comm_dma_configuration(void){ dma_inittypedef dma_initstructure; nvic_inittypedef nvic_initstructure; /* 使能时钟 */ rcc_ahb1periphclockcmd(comm_dma_clk, enable); /* nvic配置 */ nvic_initstructure.nvic_irqchannel = comm_tx_dma_irqn; nvic_initstructure.nvic_irqchannelpreemptionpriority = comm_tx_dma_priority; nvic_initstructure.nvic_irqchannelsubpriority = 0; nvic_initstructure.nvic_irqchannelcmd = enable; nvic_init(&nvic_initstructure); /* dma配置 */ dma_deinit(comm_tx_dma_stream); dma_initstructure.dma_channel = comm_tx_dma_channel; //dma通道 dma_initstructure.dma_peripheralbaseaddr = comm_dr_address; //外设地址 dma_initstructure.dma_memory0baseaddr = (uint32_t)0; //内存地址(待传入参数) dma_initstructure.dma_dir = dma_dir_memorytoperipheral; //传输方向 dma_initstructure.dma_buffersize = 0; //传输长度(待传入参数) 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_priority = dma_priority_medium; //优先级 dma_initstructure.dma_fifomode = dma_fifomode_disable; dma_initstructure.dma_fifothreshold = dma_fifothreshold_halffull; dma_initstructure.dma_memoryburst = dma_memoryburst_single; dma_initstructure.dma_peripheralburst = dma_peripheralburst_single; dma_init(comm_tx_dma_stream, &dma_initstructure); dma_clearflag(comm_tx_dma_stream, comm_tx_dma_flag_tcif); dma_itconfig(comm_tx_dma_stream, dma_it_tc, enable); //使能dma传输完成中断 dma_cmd(comm_tx_dma_stream, disable); //初始化禁止} j接下来实现数据发送与接收
/************************************************函数名称 :comm_sendbufbydma功 能 :通信串口通过dma发送数据参 数 :buf ------ 数据(地址) length --- 数据长度(字节)返 回 值 :无作 者 :strongerhuang*************************************************/void comm_sendbufbydma(uint8_t *buf, uint16_t length){ dma_cmd(comm_tx_dma_stream, disable); //关闭dma //内存地址 dma_memorytargetconfig(comm_tx_dma_stream, (uint32_t)buf, dma_memory_0); dma_setcurrdatacounter(comm_tx_dma_stream, length); //设置dma传输长度 dma_cmd(comm_tx_dma_stream, enable); //使能dma} dma是一种在嵌入式实时任务处理中常用的功能。
而uart发送数据包,使用dma方式能大量减轻cpu处理的时间,使其cpu资源不被大量浪费,尤其在uart收发大量数据包(如高频率收发指令)时具有明显优势。
物联网在农业实践中的七大实际应用和好处
西门子PLC基础指令知识详解!
禁售的挽救,苹果推出换购优惠
助力汽车自我思考
Linux文件“哲学”是否与Windows相同呢?
单片机DMA驱动串口过程
诺基亚最新消息:诺基亚6砸核桃效果怎么样?诺基亚8曝光,在路上!
智能空气检测仪安装注意事项
颜值秒杀苹果7,内置小米CPU,小米5C发布在即
手持式叶绿素检测仪的特点及技术指标
自恢复保险丝工作原理
VR技术发展历程_VR产业面临问题和发展前景
EtherCAT I/O 马达控制 机器人从站控制器新选择——AX58100
三星note8什么时候上市?三星note8最新消息:性能悍将配备骁龙845处理器,或许是iPhone8的唯一对手?
上海新阳表示ARF193nm光刻胶配套的光刻机预计12月底到货
英特尔第十代酷睿+Z490独占黑科技
不堆激光功率,如何提升机床加工效率?
FPGA与CPLD的区别
南京集成电路大学揭牌暨产业人才培养高端论坛活动在新区举行
扫描双面印刷的文档时,背面的文字会透射到正面,如何处理?