您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > STM32F103多个串口配置DMA程序(纯代码版详解)
..软件:KeilMDK芯片:STM32F103/STM32F107串口数量:4备注:4个串口均配置为DMA读取//串口1初始化函数voiduart1_init(u32bound){//GPIO端口设置GPIO_InitTypeDefGPIO_InitStructure;USART_InitTypeDefUSART_InitStructure;NVIC_InitTypeDefNVIC_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA,ENABLE);//使能USART1,GPIOA时钟USART_DeInit(USART1);//复位串口1//USART1_TXPA.9GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//PA.9GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//复用推挽输出GPIO_Init(GPIOA,&GPIO_InitStructure);//初始化PA9//USART1_RXPA.10GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;//浮空输入GPIO_Init(GPIOA,&GPIO_InitStructure);//初始化PA10//Usart1NVIC配置NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;//抢占优先级3NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;//子优先级3NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;//IRQ通道使能NVIC_Init(&NVIC_InitStructure);//根据指定的参数初始化VIC寄存器/*EnableDMAChannel4Interrupt*/NVIC_InitStructure.NVIC_IRQChannel=DMA1_Channel4_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;NVIC_Init(&NVIC_InitStructure);../*EnableDMAChannel5Interrupt*/NVIC_InitStructure.NVIC_IRQChannel=DMA1_Channel5_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;NVIC_Init(&NVIC_InitStructure);//USART参数初始化设置USART_InitStructure.USART_BaudRate=bound;//一般设置为9600;USART_InitStructure.USART_WordLength=USART_WordLength_8b;//字长为8位数据格式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_ITConfig(USART1,USART_IT_RXNE,ENABLE);//开启中断USART_ITConfig(USART1,USART_IT_IDLE,ENABLE);//开启空闲中断USART_Cmd(USART1,ENABLE);//使能串口}//串口2初始化函数voiduart2_init(u32bound){GPIO_InitTypeDefGPIO_InitStructure;USART_InitTypeDefUSART_InitStructure;NVIC_InitTypeDefNVIC_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//使能GPIOA时钟RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//使能USART2时钟GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;//PA2GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//复用推挽GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOA,&GPIO_InitStructure);GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;//PA3GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;//浮空输入..GPIO_Init(GPIOA,&GPIO_InitStructure);RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,ENABLE);//复位串口2RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,DISABLE);//停止复位USART_InitStructure.USART_BaudRate=bound;//一般设置为9600;USART_InitStructure.USART_WordLength=USART_WordLength_8b;//8位数据长度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(USART2,&USART_InitStructure);;//初始化串口NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn;//使能串口2中断NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;//先占优先级2级NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;//从优先级2级NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;//使能外部中断通道NVIC_Init(&NVIC_InitStructure);//根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器//////////////////////EnableDMAChannel7Interrupt//////////////////////////////NVIC_InitStructure.NVIC_IRQChannel=DMA1_Channel7_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;NVIC_InitStructure.NVIC_IRQChannelSubPriority=2;NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;NVIC_Init(&NVIC_InitStructure);/*EnableDMAChannel6Interrupt*/NVIC_InitStructure.NVIC_IRQChannel=DMA1_Channel6_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;NVIC_Init(&NVIC_InitStructure);//USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//开启中断USART_ITConfig(USART2,USART_IT_IDLE,ENABLE);//开启空闲中断USART_Cmd(USART2,ENABLE);//使能串口}//串口3初始化函数voiduart3_init(u32bound){..//GPIO端口设置GPIO_InitTypeDefGPIO_InitStructure;USART_InitTypeDefUSART_InitStructure;NVIC_InitTypeDefNVIC_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//使能GPIOB时钟RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);//使能USART3时钟USART_DeInit(USART3);//复位串口3//USART1_TXPB.10GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//PB.10GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//复用推挽输出GPIO_Init(GPIOB,&GPIO_InitStructure);//初始化PB.11//USART1_RXPB.11GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;//浮空输入GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOB,&GPIO_InitStructure);//初始化PB//USART初始化设置//#ifdefEN_USART3_RX//如果
本文标题:STM32F103多个串口配置DMA程序(纯代码版详解)
链接地址:https://www.777doc.com/doc-5717003 .html