打印

项目中的USART与DMA 的使用

[复制链接]
256|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
项目使用了串口1发送了和接收数据, 接收数据用的是中断,众所周知,在相同效果下,程序进入中断的次数越少越好。 原来有接收一张数据,8个字节,那就要进八次中断,现在使用DAM 串口接收,接收8个字节只需要进一次中断。
      
        结论:串口发送不宜使用DMA,不方便, 串口接收应考虑使用DMA。  下面贴上DMA 程序函数。
      
void STM32_Shenzhou_COMInit(USART_InitTypeDef* USART_InitStruct)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);        /* Enable GPIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); /* Enable UART clock */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);         /* Connect PXx to USARTx_Tx*/

/* Connect PXx to USARTx_Rx*/
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);

/* Configure USART Tx as alternate function  */
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);

/* Configure USART Rx as alternate function  */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOA, &GPIO_InitStructure);

USART_Init(USART1, USART_InitStruct);        /* USART configuration */
USART_Cmd(USART1, ENABLE); /* Enable USART */
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
USART_ClearFlag(USART1,USART_FLAG_TC);
}

void Printf_Init(void)
{
USART_InitTypeDef USART_InitStructure;   
USART_InitStructure.USART_BaudRate = 115200;
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;
STM32_Shenzhou_COMInit(&USART_InitStructure);
}

使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

437

主题

437

帖子

0

粉丝