野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9781|回复: 1

使用串口4一段时间主板会自动复位

[复制链接]
发表于 2017-6-22 10:48:46 | 显示全部楼层 |阅读模式
本帖最后由 yuan286 于 2017-6-22 10:53 编辑

/************************************************************************/
/*  描述  :USART4 GPIO 配置 */
/************************************************************************/
void USART4_Config(void)
{
                GPIO_InitTypeDef GPIO_InitStructure;
                USART_InitTypeDef USART_InitStructure;
                NVIC_InitTypeDef NVIC_InitStructure;
                /* config USART4 clock */
                RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
          RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);  //使能串口4时钟
                /* USART4 GPIO config */
                /* Configure USART4 Tx (PC.10) as alternate function push-pull */
                GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_10;
                GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_PP;
                GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
                GPIO_Init(GPIOC, &GPIO_InitStructure);  
                /* Configure USART4 Rx (PC.11) as input floating */
                GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
                GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
                GPIO_Init(GPIOC, &GPIO_InitStructure);
                /* USART4 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(UART4, &USART_InitStructure);//配置串口参数函数
                                       
                /* Configure the NVIC Preemption Priority Bits */
                NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
                /* Enable the USARTy Interrupt */
                NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
                NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =2;
                NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
                NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
                NVIC_Init(&NVIC_InitStructure);
               
                /* 使能串口4接收中断 */
                 /*USART4 receive data register is not empty */
    USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);  
    /* Enable the USART3 */
    USART_Cmd(UART4, ENABLE);                                 
}
/************************************************************************/
/*  USART2中断缓存 */
/************************************************************************/
#define UART4_BUFF_SIZE 512
uint8_t uart4_buff[UART4_BUFF_SIZE];
uint16_t uart4_p = 0;

void bsp_UART4_IRQHandler(void)
{
                if(USART_GetITStatus(UART4, USART_IT_RXNE) != RESET)
    {
                                if(uart4_p >= UART4_BUFF_SIZE) uart4_p = uart4_p - UART4_BUFF_SIZE;
                          uart4_buff[uart4_p] = USART_ReceiveData(UART4);
                                uart4_p = uart4_p + 1;
                }            
                        
}
//数据处理
void  USART4_datadeal(void)
{
          uint8_t crc_ok=0;
               
        if(((uart4_buff[0]+uart4_buff[1]+uart4_buff[2])&0X7F)==uart4_buff[3])crc_ok=0;   //crc16校验 成功
        else                                                                              crc_ok=1;   //CRC 校验失败
                        
          if(crc_ok==0)
                         {
              if(uart4_buff[0]==0xa5){PM2_5_Num2=(uint16_t)((uart4_buff[1]*128+uart4_buff[2])*0.4);}
                         }
               
                        
}
/************************************************************************/
/*  USART4中断 */
/************************************************************************/
void UART4_IRQHandler(void)
{
                bsp_UART4_IRQHandler();
          
         
}
#define Feed_Dog_TimeoutComp 200 //5000ms
#define USART1_AcceptData_Success_TimeoutComp 10  //10ms
#define USART2_AcceptData_Success_TimeoutComp 10  //10ms
#define USART3_AcceptData_Success_TimeoutComp 10  //10ms
#define USART4_AcceptData_Success_TimeoutComp 10  //10ms

uint16_t Feed_Dog_Time_Count = 0;
uint16_t USART1_AcceptData_Success_Time_Count = 0;
uint16_t USART2_AcceptData_Success_Time_Count = 0;
uint16_t USART3_AcceptData_Success_Time_Count = 0;
uint16_t USART4_AcceptData_Success_Time_Count = 0;


uint16_t usart1_last_buffer_length = 0;
uint16_t usart2_last_buffer_length = 0;
uint16_t usart3_last_buffer_length = 0;
uint16_t usart4_last_buffer_length = 0;
void TIM2_IRQHandler(void)
{
        uint16_t buffer_length = 0;
        if(TIM_GetITStatus(TIM2 ,TIM_IT_Update)!=RESET)
        {        
                //清除TIMx的中断待处理位:TIM 中断源
                TIM_ClearITPendingBit(TIM2 , TIM_FLAG_Update);
                       //USART4 接收数据完成
                                //喂狗
                    Feed_Dog_Time_Count++;
                    if(Feed_Dog_Time_Count > Feed_Dog_TimeoutComp)  //产生超时
                     {
                                                Feed_Dog_Time_Count = 0;
                                                Feed_Dog_Flag = 1;
                      }        
                                USART4_AcceptData_Success_Time_Count++;
                                buffer_length = get_uart4_rebuff_length();
                                if(buffer_length != usart4_last_buffer_length)  //未完成传输
                                {
                                                USART4_AcceptData_Success_Time_Count = 0;
                                                usart4_last_buffer_length = buffer_length;
                                }
                                else if(USART4_AcceptData_Success_Time_Count > USART4_AcceptData_Success_TimeoutComp)  //产生超时
                                {
                                                USART4_AcceptData_Success_Time_Count = 0;
                                                if(buffer_length > 0 && usart4_flag_change_enable == 1) usart4_acceptData_success_flag = 1;
                                                else usart4_acceptData_success_flag = 0;
                                }
              }

}
int main(void)
{        
        
        system_init();
        clean_uart1_rebuff();//清空了接收缓冲区数据
        clean_uart2_rebuff();//清空了接收缓冲区数据
        clean_uart3_rebuff();//清空了接收缓冲区数据
        clean_uart4_rebuff();//清空了接收缓冲区数据
        usart1_flag_change_enable = 1;//USART1 标识变更使能 0: 禁止修改标识 1: 允许修改标识
        usart2_flag_change_enable = 1;//USART2 标识变更使能 0: 禁止修改标识 1: 允许修改标识
        usart3_flag_change_enable = 1;//USART2 标识变更使能 0: 禁止修改标识 1: 允许修改标识
        usart4_flag_change_enable = 1;//USART2 标识变更使能 0: 禁止修改标识 1: 允许修改标识

        while(1)
        {
               
                                                work_main();
                                                 //USART1 数据接收完成
                                                if(usart1_acceptData_success_flag == 1)        
                                                {  
                                                                usart1_flag_change_enable = 0;//USART2 标识变更使能 0: 禁止修改标识 1: 允许修改标识
                                                                usart1_acceptData_success_flag = 0;
                                                          USART1_datadeal();            //接收数据处理
                                                                clean_uart1_rebuff();         //清空了接收缓冲区数据
                                                                usart1_flag_change_enable = 1;//USART2 标识变更使能 0: 禁止修改标识 1: 允许修改标识        
                                                }
                      //USART2 数据接收完成
                                                if(usart2_acceptData_success_flag == 1)        
                                                {  
                                                                usart2_flag_change_enable = 0;//USART2 标识变更使能 0: 禁止修改标识 1: 允许修改标识
                                                                usart2_acceptData_success_flag = 0;
                                                          USART2_datadeal();            //接收数据处理
                                                                clean_uart2_rebuff();         //清空了接收缓冲区数据
                                                                usart2_flag_change_enable = 1;//USART2 标识变更使能 0: 禁止修改标识 1: 允许修改标识        
                                                }
                                 //USART3 数据接收完成
                                                if(usart3_acceptData_success_flag == 1)        
                                                {  
                                                                usart3_flag_change_enable = 0;//USART2 标识变更使能 0: 禁止修改标识 1: 允许修改标识
                                                                usart3_acceptData_success_flag = 0;
                                                          USART3_datadeal();            //接收数据处理
                                                                clean_uart3_rebuff();         //清空了接收缓冲区数据
                                                                usart3_flag_change_enable = 1;//USART2 标识变更使能 0: 禁止修改标识 1: 允许修改标识        
                                                }
                                        //USART4 数据接收完成
                                                if(usart4_acceptData_success_flag == 1)        
                                                {  
                                                                usart4_flag_change_enable = 0;//USART2 标识变更使能 0: 禁止修改标识 1: 允许修改标识
                                                                usart4_acceptData_success_flag = 0;
                                                          USART4_datadeal();           //接收数据处理
                                                                clean_uart4_rebuff();        //清空了接收缓冲区数据
                                                                usart4_flag_change_enable = 1;//USART2 标识变更使能 0: 禁止修改标识 1: 允许修改标识        
                                                }
                                                if(Feed_Dog_Flag == 1) //喂狗        
                                            {
                                          Feed_Dog_Flag = 0;
                                          IWDG_ReloadCounter();        //重装载IWDG计数器 喂狗
                                                                 }

  }
}

这个是部分程序   程序运行一段时间后就单片机好像就自动复位了,接上串口4上的传感器就会出现这样的问题,不接就跟正常,也测试过不是传感器的问题,请哪位大神指导下,小弟在这谢谢了!


回复

使用道具 举报

发表于 2017-6-22 11:47:36 | 显示全部楼层
程序太长,把帮顶了。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-4-28 03:53 , Processed in 0.029553 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表