打印
[STM8]

串口发送和接收的问题

[复制链接]
817|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
aaa1125456|  楼主 | 2015-5-11 17:45 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
void main()
{      
    sys_cpuInit();
   
    uint16_t loop_i = 0;
    uint8_t ii;
    uint8_t count=0;
    uint8_t count1=0;
    uint8_t count2=0;
    /* LED初始化 */
    led_init();
    //app_ledBothOn();
    //app_ledBothOff();
   
    /*串口初始化*/
    uart3_init(2400);
   
    /*GPIO初始化*/
    gpioInit();
   
    /*延时函数初始化*/
    delay_init(12);
      
    uint8_t data[]={'0','1','2','3','4','5','6','7','8','9'};
   
    /* 使能全局中断 */
    enableInterrupts();
   
   
   
    /***** 程序主循环体 *****/
    app_wait(50);
    while(1)
    {   
        app_wait(50);
        ENP_INIT_H();        
        ENP_INIT_L();
        
        for( ii=0;ii<4;ii++)//D7 闪光
        {
        led2_on();
        app_wait(1);
      
        led2_off();
        app_wait(3);      
        }
        uart3_send(data,10);//发送数据
        
        delay_ms(1);
        app_wait(170);
        
        
        
        /*3位数全部接受到*/
        if(receivedata[0] != 0x01 && receivedata[1] != 0x01 && receivedata[2] != 0x01 )
        {
            
            if(receivedata[0] == 'r' && receivedata[1] == 'i' && receivedata[2] == 'g')                                               
            {  
                //接收到1次正确应答
                rig++;
                count1 ++;
                for(uint8_t ii=0;ii<4;ii++)//D6  闪光
                {
                    GPIO_WriteBit(GPIOB,GPIO_Pin_1,RESET);
                    app_wait(1);
      
                    GPIO_WriteBit(GPIOB,GPIO_Pin_1,SET);
                    app_wait(3);      
                }
                for(loop_i=0;loop_i<3;loop_i++)//清除 存储
                {
                    receivedata[loop_i]=0x01;
                }
              
            }
            else
            {               
                if(receivedata[0] == 'e' && receivedata[1] == 'r' && receivedata[2] == 'o')
                {
                    ero++;
                    count2++;
                }
                for(loop_i=0;loop_i<3;loop_i++)
                {
                    receivedata[loop_i]=0x01;
                }
               
            }
         
        }
                       
        count++;
        if(count == 3)//3次ACK 全正确
        {
            sss++;
            if(count1 ==3)
            {
                xxx++;
                GPIO_WriteBit(GPIOC,GPIO_Pin_4,RESET);
            }
            else                  
            {
                GPIO_WriteBit(GPIOB,GPIO_Pin_2,RESET);
            }
            
            
            count = 0;
            count1 = 0;
            break;
           
        }
        
    }
   
    UART3_RX_DISABLE();
   
    while(1);
   
   
}
INTERRUPT_HANDLER(USART3_RX_TIM5_CC_IRQHandler, 22)
{
    /* In order to detect unexpected events during development,
       it is recommended to set a breakpoint on the following instruction.
    */
  
    if (USART_GetFlagStatus(USART3, USART_FLAG_RXNE) != RESET)//注意!
    {
        
        receivedata[datanum]=USART3->DR;
        if (receivedata[datanum] == 57)
        {
            datanum = 9;
        }
        datanum++;
        
        led2_on();
        uint8_t aa=40;
        while(aa--);
        led2_off();
                       
        if (datanum>9)
        {
            datanum=0;            
        }      
        
    }   
}

这是发送数据 接收应答


void main()
{  
    uint8_t loop_i=0;
        
    result_t a_a=uart2_send(a,58);
    if(a_a==FAILED)
    {
        ab=1;
    }
  
    /*配置HSE 使能看门狗*/
    sys_cpuInit();
   
    /* LED初始化 */
   
    led_init();
    GPIO_Init(GPIOC, GPIO_Pin_4, GPIO_Mode_Out_PP_High_Fast); /*led D5初始化*/
    GPIO_WriteBit( GPIOC, GPIO_Pin_4, SET);
    GPIO_Init(GPIOB, GPIO_Pin_2, GPIO_Mode_Out_PP_High_Fast); /*led D4初始化*/
    GPIO_WriteBit( GPIOB, GPIO_Pin_2, SET);
      
    uint8_t data1[]={'r','i','g'}; /*接收正确数据 回答码*/
    uint8_t data2[]={'e','r','o'}; /*接收错误数据 回答码*/
    /*串口初始化*/
    uart3_init(2400);

    /* 使能全局中断 */
    enableInterrupts();     
   
   

    /***** 程序主循环体 *****/
    while(1)
    {   
            if ((receivedata[0] != 0x00)
                 && (receivedata[1] != 0x00)
                 && (receivedata[2] != 0x00)
                 && (receivedata[3] != 0x00)
                 && (receivedata[4] != 0x00)
                 && (receivedata[5] != 0x00)
                 && (receivedata[6] != 0x00)
                 && (receivedata[7] != 0x00)
                 && (receivedata[8] != 0x00)
                 && (receivedata[9] != 0x00)) /*判定 10次数据全部放入接收区*/
                {
        
                  
                    if ((receivedata[0] == 48)
                         && (receivedata[1] == 49)
                         && (receivedata[2] == 50)
                         && (receivedata[3] == 51)
                         && (receivedata[4] == 52)
                         && (receivedata[5] == 53)
                         && (receivedata[6] == 54)
                         && (receivedata[7] == 55)
                         && (receivedata[8] == 56)
                         && (receivedata[9] == 57)) /*接收区的数据为正确数据*/
                        {      
                           
                            for(loop_i=0;loop_i<10;loop_i++) /*清空接收区数据*/
                            {
                                receivedata[loop_i]=0x00;
                            }
                            for( loop_i=0;loop_i<4;loop_i++) /*接收正确数据 D6 闪烁*/
                            {
                                led3_on();
                                app_wait(1);
      
                                led3_off();
                                app_wait(3);      
                            }
                           
                            uart3_send(data1,3); /*接收到正确数据 发送数据"rig"*/
                            datanum=0;
                           
                                               
                        }
                    
                    else /*接收的数据为错误数据*/
                    {                       
                        for(loop_i=0;loop_i<10;loop_i++) /*清空数据*/
                        {
                          receivedata[loop_i]=0x00;
                        }
                        for( loop_i=0;loop_i<4;loop_i++) /*接收错误数据 D5 闪烁*/
                        {
                            GPIO_WriteBit(GPIOC, GPIO_Pin_4,RESET);
                            app_wait(1);
      
                            GPIO_WriteBit(GPIOC, GPIO_Pin_4,SET);
                            app_wait(3);      
                        }
                        uart3_send(data2,3); /*接收到错误数据 发送数据"ero"*/
                        datanum=0;
                        
                    }
                 
                }
      }
         
   
}

INTERRUPT_HANDLER(USART3_RX_TIM5_CC_IRQHandler, 22)
{
        yyy++;
    /* In order to detect unexpected events during development,
       it is recommended to set a breakpoint on the following instruction.
    */
  
    if (USART_GetFlagStatus(USART3, USART_FLAG_RXNE) != RESET)//注意!发送方接收应答
    {
        receivedata[datanum]=USART3->DR;//接收‘r’or ‘e’
        receive[datanum1]=receivedata[datanum];
        datanum1 ++;
        
        datanum++;
   
        if(datanum == 3)
        {
            datanum = 0;
        }
      
    }
   
}

这个是接收数据  发送应答

遇到问题是 有时候发送方 接收应答 回漏掉一次  也就是说只接收到 2次应答共 6个字节
根据yyy变量显示  应答3次回复  收到2次  有一次根本就没进中断  一般是最后一次丢掉  也有
中间丢掉的时候
沙发
aaa1125456|  楼主 | 2015-5-11 17:49 | 只看该作者
是 发送方  根本就没接收到 3次中的 一次 应答   没进中断  

使用特权

评论回复
板凳
aaa1125456|  楼主 | 2015-5-11 17:50 | 只看该作者
成功 的次数 也很多  

使用特权

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

本版积分规则

7

主题

44

帖子

0

粉丝