/**
****************************************************************************** * @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