以下是我自己写的,感觉串口配置出问题了,串口1发送没问题;但是串口二的接收字符串后马上串口1发送数组不行。有没有大佬帮忙修改一下
- #include "usart.h"
- #include "led.h"
- #include "SysTick.h"
- #include "string.h"
- #include "DFPlayer.h"
- /*******************************************************************************
- * 函 数 名 : USART1_Init
- * 函数功能 : USART1初始化函数
- * 输 入 : bound:波特率
- * 输 出 : 无
- *******************************************************************************/
- void USART1_Init(u32 bound)
- {
- //GPIO端口设置
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
-
- /* 配置GPIO的模式和IO口 */
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//TX //串口输出PA9
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //复用推挽输出
- GPIO_Init(GPIOA,&GPIO_InitStructure); /* 初始化串口输入IO */
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//RX //串口输入PA10
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //模拟输入
- GPIO_Init(GPIOA,&GPIO_InitStructure); /* 初始化GPIO */
-
- //USART1 初始化设置
- USART_InitStructure.USART_BaudRate = bound;//波特率设置
- 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); //初始化串口1
-
- USART_Cmd(USART1, ENABLE); //使能串口1
-
- USART_ClearFlag(USART1, USART_FLAG_TC);
-
- USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启相关中断
-
- while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
- //Usart1 NVIC 配置
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//串口1中断通道
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//抢占优先级3
- NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //子优先级3
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
- NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、
- }
- /*******************************************************************************
- * 函 数 名 : USART1_Send_Data
- * 函数功能 : USART1发送函数
- * 输 入 : buf
- * 输 入 : len
- * 输 出 : 无
- *******************************************************************************/
- void USART1_Send_Data(u8 *buf,uint16_t len)
- {
- u8 t=0;
- for(t=0;t<len;t++) //循环发送数据
- {
- USART_SendData(USART1,buf[t] );
- while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
- }
- }
- /*******************************************************************************
- * 函 数 名 : uart2_init
- * 函数功能 : IO端口及串口2,时钟初始化函数 A2,A3
- * 输 入 : 无
- * 输 出 : 无
- *******************************************************************************/
- void USART2_Init(u32 bt)
- {
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure; //声明一个结构体变量,用来初始化GPIO
- //使能串口的RCC时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE); //使能UART3所在GPIOB的时钟
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
- //串口使用的GPIO口配置
- // Configure USART2 Rx (PB.11) as input floating
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- // Configure USART2 Tx (PB.10) as alternate function push-pull
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- //配置串口
- USART_InitStructure.USART_BaudRate = bt;
- 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;
- // Configure USART
- USART_Init(USART2, &USART_InitStructure);//配置串口
- // Enable USART1 Receive interrupts 使能串口接收中断
- USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
- //串口发送中断在发送数据时开启
- //USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
- // Enable the USART3
- USART_Cmd(USART2, ENABLE);//使能串口
- //串口中断配置
- //Configure the NVIC Preemption Priority Bits
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
- // Enable the USART Interrupt
- NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- char USART_ReceiveString[50]; //接收PC端发送过来的字符
- int Receive_Flag = 0; //接收消息标志位
- int Receive_sum = 0; //数组下标
- void USART2_IRQHandler(void) //中断
- {
- if(USART_GetITStatus(USART2,USART_IT_RXNE) == 1) //USART_FLAG_RXNE判断数据,== 1则有数据
- {
- if(Receive_sum > 49) //数组能存放50个字节的数据
- {
- USART_ReceiveString[49] = '\0'; //数据字节超过50位时,将最后一位设置为\0
- Receive_Flag = 1; //接收标志位置1,停止接收数据
- Receive_sum = 0; //数组下标置0
- }
-
- if(Receive_Flag == 0) //接收标志位等于0,开始接收数据
- {
- USART_ReceiveString[Receive_sum] = USART_ReceiveData(USART2); //通过USART2串口接收字符
- Receive_sum++; //数组下标++
- }
-
- if(Receive_sum >= 2) //数组下标大于2
- {
- if(USART_ReceiveString[Receive_sum-2] == '\r' && USART_ReceiveString[Receive_sum-1] == '\n' )
- {
- USART_ReceiveString[Receive_sum-1] = '\0';
- USART_ReceiveString[Receive_sum-2] = '\0';
- Receive_Flag = 1; //接收标志位置1,停止接收数据
- Receive_sum = 0; //数组下标置0
- if(strcmp(USART_ReceiveString,"batteriesHarmful") == 0)
- {
- led1=!led1;
- }
- if(strcmp(USART_ReceiveString,"cansRecyclable") == 0)
- {
- Uart_DFPlayer(0x03 , 0x02) ;
- }
- if(strcmp(USART_ReceiveString,"cartonRecyclable") == 0)
- {
- Uart_DFPlayer(0x03 , 0x03) ;
- }
- if(strcmp(USART_ReceiveString,"cigaretteDry") == 0)
- {
- Uart_DFPlayer(0x03 , 0x04) ;
- }
- if(strcmp(USART_ReceiveString,"tissueDry") == 0)
- {
- Uart_DFPlayer(0x03 , 0x05) ;
- }
-
- Receive_Flag = 0;
- }
- }
- USART_ClearITPendingBit(USART2,USART_IT_RXNE); //接收后先清空标志位
- }
-
- }
复制代码
|