void USART1_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_0);//ÅäÖÃPA9³ÉµÚ¶þ¹¦ÄÜÒý½Å TX
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_0);//ÅäÖÃPA10³ÉµÚ¶þ¹¦ÄÜÒý½Å RX
//USART1_IO_CONFIG
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
RCC_USARTCLKConfig(RCC_USART1CLK_PCLK);//( RCC_USART1CLK_PCLK );
USART_DeInit(USART1);
USART_InitStructure.USART_BaudRate = 115200;//19200;
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(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
USART_ClearFlag(USART1,USART_FLAG_TXE);
/*
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0x03;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure); */
}
/*******************************************************************************
* º¯ÊýÃû³Æ : DMA_Configuration
* ÃèÊö : ´®¿ÚµÄDMA·¢ËÍ
ͨѶ¿Ú2ΪRS485¶Ë¿Ú¡£
* ÊäÈë : None
* Êä³ö : None
* ·µ»Ø : None
*******************************************************************************/
void USART1_DMA(void)
{
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
DMA_DeInit(DMA1_Channel2); //Page146
DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_DR_Base; //Page33
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)UART_SEND;
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_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel2, &DMA_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel2_3_IRQn; //ǶÌ×ͨµÀΪDMA1_Stream6_IRQn
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ͨµÀÖжÏʹÄÜ
NVIC_Init(&NVIC_InitStructure);
USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);
DMA_Cmd(DMA1_Channel2, ENABLE);
//DMA_ITConfig(DMA1_Channel2, DMA_IT_TC, ENABLE);
} |