打印

关于RS232串口通信想请教下

[复制链接]
284|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
白板就是这个|  楼主 | 2019-11-17 14:10 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
之前用PIC单片机232通信思想就是上位机发送一组数据,下位机接收这组数据放入缓冲区,然后分析接收到的数据进行其他的操作处理,现在刚学STM32也想这样来做。做个简单的协议,上位机发送协议来点亮LED,可是程序不是按照我的协议来的,无论上位机发什么数据LED灯都可以被点亮,求大神指导下。代码如下、/**
******************************************************************************
  * @brief  USART1 GPIO 配置,工作模式配置。9600 8-N-1
  * @param  无
  * @retval 无
******************************************************************************
  */
void USART1_Config(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;
        
        /* config USART1 clock */
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
        
        /* USART1 GPIO config */
        /* Configure USART1 Tx (PA.09) as alternate function push-pull */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);   
        /* Configure USART1 Rx (PA.10) as input floating */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
        
        /* USART1 mode config */
        USART_InitStructure.USART_BaudRate = 9600;
        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);
        
        /* 使能串口1接收中断 */
        USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
        
        
        USART_Cmd(USART1, ENABLE);
        USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
  //USART_ITConfig(USART1, USART_IT_TC, ENABLE);  
        USART_ClearFlag(USART1,USART_FLAG_TC);
}

/**
******************************************************************************
  * @brief  配置USART1接收中断
  * @param  无
  * @retval 无
******************************************************************************
  */

void NVIC_Configuration(void)
{
        NVIC_InitTypeDef NVIC_InitStructure;
        /* Configure the NVIC Preemption Priority Bits */  
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
        
        /* Enable the USARTy Interrupt */
        NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;         
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStructure);
}


/**
  * @brief  串口1处理函数
  * @param  None
  * @retval None
  */
void USART1_IRQHandler(void)
{
        
        if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
        {
                USART_ClearITPendingBit(USART1, USART_IT_RXNE);  //清除中断标志
                recv_buff[recv_pt] = USART_ReceiveData(USART1);
                recv_pt++;
                recv_pt&=0xff;//最多255个字节
               if((recv_buff[0]=0x05)&&(recv_buff[5]=0x03))recv_ok=1;
        
                //溢出-如果发生溢出需要先读SR,再读DR寄存器则可清除不断入中断的问题
if(USART_GetFlagStatus(USART1,USART_FLAG_ORE)==SET)
   {
     USART_ClearFlag(USART1,USART_FLAG_ORE); //读SR其实就是清除标志
     USART_ReceiveData(USART1); //读DR
   }
               
        }
               
}


//接收上位机数据点亮LED
void comm_debug(void)
{
         
         if(recv_ok==1){LED1(ON);recv_ok=0;recv_pt=0;}
}

int main(void)
{        
        /* LED 端口初始化 */
        LED_GPIO_Config();
/*按键初始化*/        
  Key_GPIO_Config();
        /* 配置SysTick 为10us中断一次 */
        SysTick_Init();
        /* USART1 配置模式为 115200 8-N-1,中断接收 */
        USART1_Config();
        /*中断源配置*/
        NVIC_Configuration();
        
//USART_SendData(USART1, USART_ReceiveData(USART1));

        while(1)
        {
               
                comm_debug();
               
               
  }
}

使用特权

评论回复

相关帖子

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

本版积分规则

377

主题

377

帖子

0

粉丝