void UART1Tx_DMA_Config(void)
{
__HAL_RCC_DMA2_CLK_ENABLE();//DMA2ê±Öóê1Äü
__HAL_LINKDMA(&USART1_Handler,hdmatx,UART1TxDMA_Handler); //½«DMAóëUSART1áaÏμÆeà′(·¢ËíDMA)
//Tx DMAÅäÖÃ
UART1TxDMA_Handler.Instance=DMA2_Stream7; //êy¾Yá÷Ñ¡Ôñ
UART1TxDMA_Handler.Init.Channel=DMA_CHANNEL_4; //í¨μàÑ¡Ôñ
UART1TxDMA_Handler.Init.Direction=DMA_MEMORY_TO_PERIPH; //′æ′¢Æ÷μ½íaéè
UART1TxDMA_Handler.Init.PeriphInc=DMA_PINC_DISABLE; //íaéè·ÇÔöá¿Ä£ê½
UART1TxDMA_Handler.Init.MemInc=DMA_MINC_ENABLE; //′æ′¢Æ÷Ôöá¿Ä£ê½
UART1TxDMA_Handler.Init.PeriphDataAlignment=DMA_PDATAALIGN_BYTE; //íaéèêy¾Y3¤¶è:8λ
UART1TxDMA_Handler.Init.MemDataAlignment=DMA_MDATAALIGN_BYTE; //′æ′¢Æ÷êy¾Y3¤¶è:8λ
UART1TxDMA_Handler.Init.Mode=DMA_NORMAL; //DMA_CIRCULAR;//íaéèá÷¿ØÄ£ê½
UART1TxDMA_Handler.Init.Priority=DMA_PRIORITY_MEDIUM; //ÖDμèóÅÏ輶
UART1TxDMA_Handler.Init.FIFOMode=DMA_FIFOMODE_DISABLE;
UART1TxDMA_Handler.Init.FIFOThreshold=DMA_FIFO_THRESHOLD_FULL;
UART1TxDMA_Handler.Init.MemBurst=DMA_MBURST_SINGLE; //′æ′¢Æ÷í»·¢μ¥′Î′«êä
UART1TxDMA_Handler.Init.PeriphBurst=DMA_PBURST_SINGLE; //íaéèí»·¢μ¥′Î′«êä
HAL_DMA_DeInit(&UART1TxDMA_Handler);
HAL_DMA_Init(&UART1TxDMA_Handler);
/* ê1Äü·¢Ëí′«êäíê3éÖD¶Ï */
__HAL_DMA_ENABLE_IT(&UART1TxDMA_Handler, DMA_IT_TC);
/* ê1ÄüDMA Stream ÖD¶Ïí¨μà */
HAL_NVIC_EnableIRQ(DMA2_Stream7_IRQn); //ê1ÄüDMAÖD¶Ïí¨μà
HAL_NVIC_SetPriority(DMA2_Stream7_IRQn,2,1); //ÇàÕ¼óÅÏ輶0£¬×óóÅÏè¼0
__HAL_DMA_ENABLE(&UART1TxDMA_Handler);
}
void USART1_Init(u32 bound)
{
GPIO_InitTypeDef GPIO_Initure;
//UART 3õê¼»ˉéèÖÃ
USART1_Handler.Instance=USART1; //USART1
USART1_Handler.Init.BaudRate=bound; //2¨ìØÂê
USART1_Handler.Init.WordLength=UART_WORDLENGTH_8B; //×Ö3¤Îa8λêy¾Y¸ñê½
USART1_Handler.Init.StopBits=UART_STOPBITS_1; //ò»¸öí£Ö1λ
USART1_Handler.Init.Parity=UART_PARITY_NONE; //ÎTÆæżD£Ñéλ
USART1_Handler.Init.HwFlowCtl=UART_HWCONTROL_NONE; //ÎTó2¼tá÷¿Ø
USART1_Handler.Init.Mode=UART_MODE_TX_RX; //êÕ·¢Ä£ê½
__HAL_RCC_GPIOA_CLK_ENABLE(); //ê1ÄüGPIOAê±Öó
__HAL_RCC_USART1_CLK_ENABLE(); //ê1ÄüUSART1ê±Öó
GPIO_Initure.Pin=GPIO_PIN_9; //PA9
GPIO_Initure.Mode=GPIO_MODE_AF_PP; //¸′óÃíÆíìêä3ö
GPIO_Initure.Pull=GPIO_PULLUP; //éÏà-
GPIO_Initure.Speed=GPIO_SPEED_HIGH; //¸ßËù
GPIO_Initure.Alternate=GPIO_AF7_USART1; //¸′óÃÎaUSART1
HAL_GPIO_Init(GPIOA,&GPIO_Initure); //3õê¼»ˉPA9
GPIO_Initure.Pin=GPIO_PIN_10; //PA10
GPIO_Initure.Mode=GPIO_MODE_AF_PP; //¸′óÃíÆíìêä3ö
GPIO_Initure.Pull=GPIO_PULLUP; //éÏà-
GPIO_Initure.Speed=GPIO_SPEED_HIGH; //¸ßËù
GPIO_Initure.Alternate=GPIO_AF7_USART1; //¸′óÃÎaUSART1
HAL_GPIO_Init(GPIOA,&GPIO_Initure); //3õê¼»ˉPA10
HAL_UART_Init(&USART1_Handler); //HAL_UART_Init()»áê1ÄüUART1
__HAL_UART_CLEAR_IT(&USART6_Handler,UART_CLEAR_TCF);
#if EN_USART1_RX
__HAL_UART_ENABLE_IT(&USART1_Handler,UART_IT_RXNE); //¿aÆô½óêÕÖD¶Ï
HAL_NVIC_EnableIRQ(USART1_IRQn); //ê1ÄüUSART1ÖD¶Ïí¨μà
HAL_NVIC_SetPriority(USART1_IRQn,0,0); //ÇàÕ¼óÅÏ輶3£¬×óóÅÏ輶3
#endif
}
//′®¿ú1ÖD¶Ï·tÎñ3ìDò
void USART1_IRQHandler(void)
{
u8 res;
int i;
HAL_StatusTypeDef tmp;
// if (__HAL_UART_GET_FLAG(&USART1_Handler, USART_FLAG_ORE) != RESET)
if((__HAL_UART_GET_IT(&USART1_Handler,UART_IT_RXNE)!=RESET)) //½óêÕÖD¶Ï
{
tmp = HAL_UART_Receive(&USART1_Handler,&res,1,1000);
if(USART1_RX_CNT<USART1_MAX_RECV_LEN)
{
USART1_RX_BUF[USART1_RX_CNT]=res; //¼Ç¼½óêÕμ½μÄÖμ
USART1_RX_CNT++; //½óêÕêy¾YÔö¼ó1
}
else
{
USART1_RX_CNT = 0;
for(i=0;i<USART1_MAX_RECV_LEN;i++)
{
USART1_RX_BUF[USART1_RX_CNT]=0;
}
}
}
}
一周热门 更多>