STM32F407 USART+DMA共思路DMA通道,进行数据转发

2019-08-03 12:46

/**

****************************************************************************** * @file IO_Toggle/main.c

* @author MCD Application Team * @version V1.0.0

* @date 19-September-2011 * @brief Main program body

****************************************************************************** * @attention *

* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS

* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE

* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY

* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING

* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE

* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. *

*

© COPYRIGHT 2011 STMicroelectronics

****************************************************************************** */

/* Includes ------------------------------------------------------------------*/ #include \

//串口1连接电脑的USB-TTL小板子,串口2连接MAX485 /* Private typedef -----------------------------------------------------------*/ #define TX_MODE 0 #define RX_MODE 1

unsigned long boundrate[7]={600,2400,4800,9600,19200,38400,57600}; /* Private variables ---------------------------------------------------------*/ __IO unsigned char uart_rx_tx_buf[2];

USART_InitTypeDef USART_InitStruct; DMA_InitTypeDef DMA_InitStruct; GPIO_InitTypeDef GPIO_InitStruct; NVIC_InitTypeDef NVIC_InitStructure;

/* Private function prototypes -----------------------------------------------*/

/* Private functions ---------------------------------------------------------*/ /**

* @brief Main program * @param None * @retval None */ void uart_1_dma_init(void); void uart_2_dma_init(void); void Set485Mode(unsigned char flag); void Set_1_rate(unsigned char rate_num); void Set_2_rate(unsigned char rate_num); void _485PinConfig(void); int main(void) { uart_1_dma_init(); uart_2_dma_init(); Set485Mode(TX_MODE); while(1); }

void uart_1_dma_init(void) {

/* Enable uart1, DMA2 and GPIO clocks ****************************************/ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOB, ENABLE); //B6-TX, B7-RX; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); /***********************uart1 initial****************************************/ USART_InitStruct.USART_BaudRate=9600; USART_InitStruct.USART_HardwareFlowControl=USART_HardwareFlowControl_None; USART_InitStruct.USART_Mode=USART_Mode_Rx |USART_Mode_Tx; USART_InitStruct.USART_Parity=USART_Parity_No; USART_InitStruct.USART_StopBits=USART_StopBits_1; USART_InitStruct.USART_WordLength=USART_WordLength_8b; USART_Init(USART1,&USART_InitStruct); /***************GPIO**************************************************/ GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_USART1); GPIO_PinAFConfig(GPIOB,GPIO_PinSource7,GPIO_AF_USART1); GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF; GPIO_InitStruct.GPIO_Pin=GPIO_Pin_6 |GPIO_Pin_7; GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_NOPULL; GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOB,&GPIO_InitStruct);

/* DMA2 Stream0 channel0 configuration **************************************/ DMA_InitStruct.DMA_Channel = DMA_Channel_4;

DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)(&(USART1->DR));

DMA_InitStruct.DMA_Memory0BaseAddr = (uint32_t)uart_rx_tx_buf; DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralToMemory; DMA_InitStruct.DMA_BufferSize = 2;

DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;

DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStruct.DMA_Mode = DMA_Mode_Circular; DMA_InitStruct.DMA_Priority = DMA_Priority_High;

DMA_InitStruct.DMA_FIFOMode = DMA_FIFOMode_Disable;

DMA_InitStruct.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; DMA_InitStruct.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStruct.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA2_Stream2, &DMA_InitStruct); DMA_InitStruct.DMA_Channel = DMA_Channel_4;

DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)(&(USART1->DR)); DMA_InitStruct.DMA_Memory0BaseAddr = (uint32_t)uart_rx_tx_buf; DMA_InitStruct.DMA_DIR = DMA_DIR_MemoryToPeripheral; DMA_InitStruct.DMA_BufferSize = 2;

DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;

DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStruct.DMA_Mode = DMA_Mode_Circular; DMA_InitStruct.DMA_Priority = DMA_Priority_High;

DMA_InitStruct.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStruct.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; DMA_InitStruct.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStruct.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA2_Stream7, &DMA_InitStruct); /**********************DMA interrupt************************/ DMA_ITConfig(DMA2_Stream7,DMA_IT_TC,ENABLE); DMA_ITConfig(DMA2_Stream2,DMA_IT_TC,ENABLE); NVIC_InitStructure.NVIC_IRQChannel=DMA2_Stream7_IRQn; NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x00; NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x00; NVIC_Init(&NVIC_InitStructure); NVIC_InitStructure.NVIC_IRQChannel=DMA2_Stream2_IRQn;

NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x00; NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x01; NVIC_Init(&NVIC_InitStructure); /**********************start*********************************/

DMA_Cmd(DMA2_Stream7, DISABLE); //不使能DMA对于USART1的发送功能 DMA_Cmd(DMA2_Stream2, ENABLE); //使能DMA对于USART1的接收功能 USART_DMACmd(USART1,USART_DMAReq_Rx |USART_DMAReq_Tx,ENABLE); USART_Cmd(USART1,ENABLE); }

void uart_2_dma_init(void) {

/* Enable uart1, DMA2 and GPIO clocks ****************************************/ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1 | RCC_AHB1Periph_GPIOA, ENABLE); // A2-TX,A3-RX RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); /***********************uart1 initial****************************************/ USART_InitStruct.USART_BaudRate=9600; USART_InitStruct.USART_HardwareFlowControl=USART_HardwareFlowControl_None; USART_InitStruct.USART_Mode=USART_Mode_Rx |USART_Mode_Tx; USART_InitStruct.USART_Parity=USART_Parity_No; USART_InitStruct.USART_StopBits=USART_StopBits_1; USART_InitStruct.USART_WordLength=USART_WordLength_8b; USART_Init(USART2,&USART_InitStruct); /***************GPIO**************************************************/ GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF; GPIO_InitStruct.GPIO_Pin=GPIO_Pin_2 |GPIO_Pin_3; GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_NOPULL; GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitStruct); GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2); GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2);

/* DMA2 Stream0 channel0 configuration **************************************/ DMA_InitStruct.DMA_Channel = DMA_Channel_4;

DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)(&(USART2->DR)); DMA_InitStruct.DMA_Memory0BaseAddr = (uint32_t)uart_rx_tx_buf; DMA_InitStruct.DMA_DIR = DMA_DIR_MemoryToPeripheral; DMA_InitStruct.DMA_BufferSize = 2;

DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;

DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStruct.DMA_Mode = DMA_Mode_Circular;

DMA_InitStruct.DMA_Priority = DMA_Priority_High;

DMA_InitStruct.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStruct.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; DMA_InitStruct.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStruct.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA1_Stream6, &DMA_InitStruct); DMA_InitStruct.DMA_Channel = DMA_Channel_4;

DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)(&(USART2->DR)); DMA_InitStruct.DMA_Memory0BaseAddr = (uint32_t)uart_rx_tx_buf; DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralToMemory; DMA_InitStruct.DMA_BufferSize = 2;

DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;

DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStruct.DMA_Mode = DMA_Mode_Circular; DMA_InitStruct.DMA_Priority = DMA_Priority_High;

DMA_InitStruct.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStruct.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; DMA_InitStruct.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStruct.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA1_Stream5, &DMA_InitStruct); /**********************DMA interrupt************************/ DMA_ITConfig(DMA1_Stream5,DMA_IT_TC,ENABLE); DMA_ITConfig(DMA1_Stream6,DMA_IT_TC,ENABLE); NVIC_InitStructure.NVIC_IRQChannel=DMA1_Stream5_IRQn; NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01; NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x00; NVIC_Init(&NVIC_InitStructure); NVIC_InitStructure.NVIC_IRQChannel=DMA1_Stream6_IRQn; NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01; NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x01; NVIC_Init(&NVIC_InitStructure); /**********************start*********************************/

DMA_Cmd(DMA1_Stream5, ENABLE); //使能USART2的串口接收DMA DMA_Cmd(DMA1_Stream6, DISABLE); //不使能USART2的串口发送DMA


STM32F407 USART+DMA共思路DMA通道,进行数据转发.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2016智慧树大学生心理健康期末考试

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: