自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

STM32使用DMA發(fā)送串口數(shù)據(jù)

商務(wù)辦公
我們使用USART1串口外設(shè),從數(shù)據(jù)手冊(cè)中可以查到,USART1的發(fā)送和接收都是支持DMA的,使用的是DMA2.

[[400593]]

01概述

上一篇文章《STM32使用DMA接收串口數(shù)據(jù)》講解了如何使用DMA接收數(shù)據(jù),使用DMA外設(shè)和串口外設(shè),使用的中斷是串口空閑中斷。本篇文章主要講解使用DMA發(fā)送數(shù)據(jù),不會(huì)講解基礎(chǔ)的串口和DMA知識(shí),直接上代碼,如果有同學(xué)對(duì)DMA和串口都不熟悉,建議看一下上篇文章《STM32使用DMA接收串口數(shù)據(jù)》。

使用DMA發(fā)送數(shù)據(jù),首先我們要確認(rèn)使用的串口有沒(méi)有DMA。

我們使用USART1串口外設(shè),從數(shù)據(jù)手冊(cè)中可以查到,USART1的發(fā)送和接收都是支持DMA的,使用的是DMA2.

接下來(lái)就是擼代碼的時(shí)刻了

02代碼

DMA串口發(fā)送的代碼是在上一篇文章DMA串口接收的基礎(chǔ)上修改的。

  1. void UART_Init(void) 
  2.   USART_InitTypeDef USART_InitStructure; 
  3.   GPIO_InitTypeDef GPIO_InitStructure; 
  4.   NVIC_InitTypeDef NVIC_InitStructure; 
  5.  
  6.   /* Enable GPIO clock */ 
  7.   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); 
  8.   /* Enable UART1 clock */ 
  9.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); 
  10.   /* Connect PXx to USARTx_Tx*/ 
  11.   GPIO_PinAFConfig(GPIOA, 9, GPIO_AF_USART1); 
  12.  
  13.   /* Connect PXx to USARTx_Rx*/ 
  14.   GPIO_PinAFConfig(GPIOA, 10, GPIO_AF_USART1); 
  15.  
  16.   /* Configure USART Tx as alternate function  */ 
  17.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 
  18.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; 
  19.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 
  20.  
  21.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; 
  22.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
  23.   GPIO_Init(GPIOA, &GPIO_InitStructure); 
  24.  
  25.   /* Configure USART Rx as alternate function  */ 
  26.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 
  27.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; 
  28.   GPIO_Init(GPIOA, &GPIO_InitStructure); 
  29.  
  30.   USART_InitStructure.USART_BaudRate = 115200; 
  31.   USART_InitStructure.USART_WordLength = USART_WordLength_8b; 
  32.   USART_InitStructure.USART_StopBits = USART_StopBits_1; 
  33.   USART_InitStructure.USART_Parity = USART_Parity_No; 
  34.   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; 
  35.   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; 
  36.  
  37.   /* USART configuration */ 
  38.   USART_Init(USART1, &USART_InitStructure); 
  39.  
  40.   USART_ITConfig(USART1, USART_IT_IDLE, ENABLE); 
  41.  
  42.   /* Enable the USARTx Interrupt */ 
  43.   NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; 
  44.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =0; 
  45.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; 
  46.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
  47.   NVIC_Init(&NVIC_InitStructure); 
  48.  
  49.   /*使能串口DMA接收*/ 
  50.   USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE); 
  51.   /*使能串口DMA發(fā)送*/ 
  52.   USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE); 
  53.  
  54.   /* Enable USART */ 
  55.   USART_Cmd(USART1, ENABLE); 

在這里除了常規(guī)的串口配置,我們需要配置串口的DMA發(fā)送,和串口DMA接收一樣的API函數(shù),參數(shù)修改為USART_DMAReq_Tx即可。

串口DMA發(fā)送配置

  1. void Uart_Send_DMA_Config(void) 
  2.   DMA_InitTypeDef  DMA_InitStructure; 
  3.  
  4.   /* Enable DMA clock */ 
  5.   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); 
  6.  
  7.   /* Reset DMA Stream registers (for debug purpose) */ 
  8.   DMA_DeInit(DMA2_Stream7); 
  9.  
  10.   /* Check if the DMA Stream is disabled before enabling it. 
  11.      Note that this step is useful when the same Stream is used multiple times: 
  12.      enabled, then disabled then re-enabled... In this case, the DMA Stream disable 
  13.      will be effective only at the end of the ongoing data transfer and it will  
  14.      not be possible to re-configure it before making sure that the Enable bit  
  15.      has been cleared by hardware. If the Stream is used only once, this step might  
  16.      be bypassed. */ 
  17.   while (DMA_GetCmdStatus(DMA2_Stream7) != DISABLE) 
  18.   { 
  19.   } 
  20.  
  21.   /* Configure DMA Stream */ 
  22.   DMA_InitStructure.DMA_Channel = DMA_Channel_4;  //DMA請(qǐng)求發(fā)出通道 
  23.   DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART1->DR;//配置外設(shè)地址 
  24.   DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)UART_Buffer;//配置存儲(chǔ)器地址 
  25.   DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;//傳輸方向配置 
  26.   DMA_InitStructure.DMA_BufferSize = (uint32_t)UART_RX_LEN;//傳輸大小 
  27.   DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外設(shè)地址不變 
  28.   DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//memory地址自增 
  29.   DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//外設(shè)地址數(shù)據(jù)單位 
  30.   DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;//memory地址數(shù)據(jù)單位 
  31.   DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//DMA模式:正常模式 
  32.   DMA_InitStructure.DMA_Priority = DMA_Priority_High;//優(yōu)先級(jí):高 
  33.   DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;//FIFO 模式不使能.           
  34.   DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;// FIFO 閾值選擇 
  35.   DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;//存儲(chǔ)器突發(fā)模式選擇,可選單次模式、 4 節(jié)拍的增量突發(fā)模式、 8 節(jié)拍的增量突發(fā)模式或 16 節(jié)拍的增量突發(fā)模式。 
  36.   DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;//外設(shè)突發(fā)模式選擇,可選單次模式、 4 節(jié)拍的增量突發(fā)模式、 8 節(jié)拍的增量突發(fā)模式或 16 節(jié)拍的增量突發(fā)模式。 
  37.   DMA_Init(DMA2_Stream7, &DMA_InitStructure);  
  38.  
  39.   /* DMA Stream enable */ 
  40. //  DMA_Cmd(DMA2_Stream7, ENABLE); 

這里也是常規(guī)的DMA配置流程,不明白的同學(xué)請(qǐng)看文章《STM32DMA詳解》,這里值得注意的是,配置完成并沒(méi)有使能DMA2_Stream7,使能了就會(huì)立即將UART_Buffer的數(shù)據(jù)發(fā)送出去。

其他代碼處理

  1. void USART1_IRQHandler(void) 
  2.   uint8_t temp
  3.   if(USART_GetFlagStatus(USART1, USART_FLAG_IDLE) == SET
  4.   { 
  5.     DealWith_UartData(); 
  6. //    USART_ClearFlag(USART1, USART_FLAG_IDLE); 
  7.     temp = USART1->SR;   
  8.     temp = USART1->DR; //清USART_IT_IDLE標(biāo)志   
  9.   } 
  10.  
  11. void DealWith_UartData() 
  12.   DMA_Cmd(DMA2_Stream2, DISABLE); 
  13.   UART_Receive_flg = 1; 
  14.   UART_Receive_len = UART_RX_LEN - DMA_GetCurrDataCounter(DMA2_Stream2); 
  15.   UART_Buffer[UART_Receive_len] = 0; 
  16.   DMA_SetCurrDataCounter(DMA2_Stream2,UART_RX_LEN);  
  17.   DMA_ClearFlag(DMA2_Stream2, DMA_FLAG_TCIF2); 
  18.   DMA_Cmd(DMA2_Stream2, ENABLE); 
  19.  
  20. int main(void) 
  21.   UART_Receive_flg = 0; 
  22.  
  23.   Uart_Reveice_DMA_Config(); 
  24.   Uart_Send_DMA_Config(); 
  25.   UART_Init(); 
  26.  
  27.   while (1) 
  28.   { 
  29.     if(UART_Receive_flg) 
  30.     { 
  31.       UART_Receive_flg = 0; 
  32.       Uart_Send_DMA_Start(); 
  33.     } 
  34.   } 

上面3個(gè)函數(shù),簡(jiǎn)單邏輯就是,當(dāng)串口使用DMA接收了一定量的數(shù)據(jù),就會(huì)通過(guò)串口DMA發(fā)送出去,串口DMA發(fā)送的代碼如下:

  1. void Uart_Send_DMA_Start(void) 
  2.   DMA_SetCurrDataCounter(DMA2_Stream7,UART_Receive_len);  
  3.   DMA_ClearFlag(DMA2_Stream7, DMA_FLAG_TCIF7); 
  4.   /* DMA Stream enable */ 
  5.   DMA_Cmd(DMA2_Stream7, ENABLE); 

03后記

 

這一篇很簡(jiǎn)單,就是DMA使用的一個(gè)延伸,上面說(shuō)了這么多,也貼了很多代碼,不可能將所有代碼全部貼出來(lái),作為軟件工程師,還是在IDE里看代碼方便,如果感興趣的話,可以到下面github鏈接下載代碼,Keil和IAR的工程文件都有。

 

責(zé)任編輯:武曉燕 來(lái)源: 知曉編程
相關(guān)推薦

2021-05-06 08:54:39

串口DMASTM32

2021-02-03 13:04:24

STM32網(wǎng)絡(luò)控制器

2021-06-26 07:50:20

STM32串口開(kāi)發(fā)環(huán)形緩沖區(qū)

2021-08-31 08:01:40

STM32DSP指令

2022-07-25 14:31:55

LiteOS開(kāi)發(fā)工具

2021-08-26 06:29:47

STM32DWT數(shù)據(jù)觀察點(diǎn)觸發(fā)

2021-01-20 09:51:25

STM32網(wǎng)絡(luò)接口

2021-02-05 08:41:44

STM32網(wǎng)絡(luò)中斷

2022-10-10 08:24:15

編程數(shù)據(jù)邏輯語(yǔ)句

2021-12-22 06:56:07

STM32C語(yǔ)言內(nèi)存

2022-01-07 08:24:13

STM32枚舉結(jié)構(gòu)體

2021-03-06 21:21:11

STM32單片機(jī)追蹤庫(kù)

2021-04-22 08:39:23

哈佛結(jié)構(gòu)馮洛伊曼結(jié)構(gòu)ARM架構(gòu)

2021-11-15 09:53:16

STM32PSPMSP

2020-12-03 06:32:21

STM32單片機(jī)通信

2021-02-01 06:39:42

模塊封裝庫(kù)

2018-10-15 10:15:30

STM32Linux經(jīng)驗(yàn)

2020-01-10 22:51:30

硬件軟件人生第一份工作

2021-02-26 08:46:46

PHY寄存器網(wǎng)絡(luò)

2009-10-29 15:30:06

VB.NET串口通訊
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)