打印
[STM32]

求教一个do while 循环问题

[复制链接]
1079|5
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
wuchaosxl|  楼主 | 2017-5-4 16:02 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本人用按键改变显示的值!按一次加一次!一共显示四位,每一位有一个位移right键。累加up键加数。enter键保存现有值。
void shift4( void)
{            
    if ( (shift_right_flag==1) )                        //第一位闪烁
         {   
                     up_flag=e;
        do{          
                 switch(up_flag)
                        {
                            case 0:
                                        e=0;
                                break;
                                case 1:
                                        e=1;
                            break;
                                case 2:
                                        e=2;
                                break;
                                case 3:
                                        e=3;
                                break;
                                case 4:
                                        e=4;
                                break;
                                case 5:
                                        e=5;
                                break;
                                case 6:
                                        e=6;
                                break;
                                case 7:
                                        e=7;
                                break;
                                case 8:
                                        e=8;
                                break;
                                case 9:
                                        e=9;
                                break;
                                case 10:
                                          e=0;
                                break;
             }       
                            
                                e=up_flag;
                               
                disp_sanshuo();//闪烁显示
                }
       
          while ( (enter_flag==2) ) ;
       
         }
         
    else if ( (shift_right_flag==2) )                        //第二位闪烁
    {                    up_flag=f;
             
           do{       
                     switch(up_flag)
                        {
                            case 0: f=0; break;
                                case 1:        f=1; break;
                                case 2: f=2; break;
                                case 3: f=3; break;
                                case 4: f=4; break;
                                case 5: f=5; break;
                                case 6: f=6; break;
                                case 7: f=7; break;
                                case 8: f=8; break;
                                case 9: f=9; break;
                                case 10:f=0; break;
            }
                          
                          f=up_flag;
           disp_sanshuo();//闪烁显示
           }
        while ( (enter_flag==2));
                         
          }       
                  
     else if  ( (shift_right_flag==3) )                //第三位闪烁
         {               
                        up_flag=g;
                   do{
                     
                           switch(up_flag)
                        {
                            case 0: g=0; break;
                                case 1:        g=1; break;
                                case 2: g=2; break;
                                case 3: g=3; break;
                                case 4: g=4; break;
                                case 5: g=5; break;
                                case 6: g=6; break;
                                case 7: g=7; break;
                                case 8: g=8; break;
                                case 9: g=9; break;
                                case 10:g=0; break;
             }
                          
                                g=up_flag ;
                        disp_sanshuo();//闪烁显示
                        }
          while ( (enter_flag==2) )        ;
       
         }
                
     else if( (shift_right_flag==4) )                //第四位闪烁
         {          
                       up_flag=h;
                do
                {       
                switch(up_flag)
                        {
                            case 0: h=0; break;
                                case 1:        h=1; break;
                                case 2: h=2; break;
                                case 3: h=3; break;
                                case 4: h=4; break;
                                case 5: h=5; break;
                                case 6: h=6; break;
                                case 7: h=7; break;
                                case 8: h=8; break;
                                case 9: h=9; break;
                                case 10:h=0; break;
            }
                          
                           h=up_flag;
                        disp_sanshuo();//闪烁显示
         }
                while ( (enter_flag==2) ) ;
               
         }
                
       
       
}


void get_key(void)                           //按键含义
{         u8 i;
u32 key_value;
  if(hw_jsbz==1)//32位数据接收结束
  {               
     hw_jsbz=0;         //必须清0,否则默认按键一直按着
     key_value= hw_jsm;

  switch(key_value)
                {
                        case 0x00ffa25d:        set_key_flag=1;
                                    break;
                        case 0x00ff629d:   if((set_key_flag==1)&&(enter_flag<2)         )
                                           {
                                                            function_flag++;             //菜单键
                                                           }
                                                           if(function_flag>7)
                                                           {
                                                            function_flag=0;
                                                                }
                                                break;

                        case 0x00ffe21d:       
                                                                    up_flag++;                             //向上加
                                                                         i++;
                                                              if(up_flag>9)
                                                             {
                                                               up_flag=0;
                                                             }                               
                                                break;
                                        default:         break;}


void  dis_flash_liangchengshangxian()        //显示FLASH 0,1,2,3  量程上限
{         
      
           read_flash(PAR_BUF,4);
           e=PAR_BUF[0];        //当前值1
           f=PAR_BUF[1];        //当前值0
           g=PAR_BUF[2];
           h=PAR_BUF[3];
           disp_geshi() ;         
           do
            {
             shift4();
            }while(enter_flag==2);           
       
                PAR_BUF[0]=e;
                PAR_BUF[1]=f;
                PAR_BUF[2]=g;
                PAR_BUF[3]=h;
            write_flash(PAR_BUF,4)        ;                        //将修改好后的内容写入flash

   
}

就这三个子程序。如果我现在按shift4(),这个函数写,那么e值就是保持不变。在设断点观察up键的时候能看到up键的变化。但是程序每次都会进入up_flag=e;  所以每次在执行完shift4()后,up又变成1。这时按right移位键的时候,第二位还是0.


现在想不明白如果把shift4()函数改成这样
void shift4( void)
{            
    if ( (shift_right_flag==1) )                        //第一位闪烁
         {   
                    
        do{          
                 switch(up_flag)
                        {
                            case 0:
                                        e=0;
                                break;
                                case 1:
                                        e=1;
                            break;
                                case 2:
                                        e=2;
                                break;
                                case 3:
                                        e=3;
                                break;
                                case 4:
                                        e=4;
                                break;
                                case 5:
                                        e=5;
                                break;
                                case 6:
                                        e=6;
                                break;
                                case 7:
                                        e=7;
                                break;
                                case 8:
                                        e=8;
                                break;
                                case 9:
                                        e=9;
                                break;
                                case 10:
                                          e=0;
                                break;
             }       
                            up_flag=e;
                                e=up_flag;
                               
                disp_sanshuo();//闪烁显示
                }
       
          while ( (enter_flag==2) ) ;
       
         }
         
    else if ( (shift_right_flag==2) )                        //第二位闪烁
    {                    
             
           do{       
                     switch(up_flag)
                        {
                            case 0: f=0; break;
                                case 1:        f=1; break;
                                case 2: f=2; break;
                                case 3: f=3; break;
                                case 4: f=4; break;
                                case 5: f=5; break;
                                case 6: f=6; break;
                                case 7: f=7; break;
                                case 8: f=8; break;
                                case 9: f=9; break;
                                case 10:f=0; break;
            }
                          up_flag=f;
                          f=up_flag;
           disp_sanshuo();//闪烁显示
           }
        while ( (enter_flag==2));
                         
          }       
                  
     else if  ( (shift_right_flag==3) )                //第三位闪烁
         {               
                        
                   do{
                     
                           switch(up_flag)
                        {
                            case 0: g=0; break;
                                case 1:        g=1; break;
                                case 2: g=2; break;
                                case 3: g=3; break;
                                case 4: g=4; break;
                                case 5: g=5; break;
                                case 6: g=6; break;
                                case 7: g=7; break;
                                case 8: g=8; break;
                                case 9: g=9; break;
                                case 10:g=0; break;
             }
                                   up_flag=g;
                                g=up_flag ;
                        disp_sanshuo();//闪烁显示
                        }
          while ( (enter_flag==2) )        ;
       
         }
                
     else if( (shift_right_flag==4) )                //第四位闪烁
         {          
                     
                do
                {       
                switch(up_flag)
                        {
                            case 0: h=0; break;
                                case 1:        h=1; break;
                                case 2: h=2; break;
                                case 3: h=3; break;
                                case 4: h=4; break;
                                case 5: h=5; break;
                                case 6: h=6; break;
                                case 7: h=7; break;
                                case 8: h=8; break;
                                case 9: h=9; break;
                                case 10:h=0; break;
            }
                            up_flag=h;
                           h=up_flag;
                        disp_sanshuo();//闪烁显示
         }
                while ( (enter_flag==2) ) ;
               
         }
                
       
       
}
如果调用的话,e的值随着up键累加能够累加,但是每次进入这个函数,e的初值都是0,而不是我指针里面的数。而且每次移位up的值会直接付给后面的f,g,h。
  不知道我这个   do  while  循环哪里有错。   之前我用8位单片机的时候就是按照第一种shift4  写的  而且可以用。

相关帖子

沙发
wuchaosxl|  楼主 | 2017-5-4 16:04 | 只看该作者
有人回答吗!求解

使用特权

评论回复
板凳
wuchaosxl|  楼主 | 2017-5-4 16:05 | 只看该作者
还是没人来解救吗!

使用特权

评论回复
地板
@若水| | 2017-5-5 14:38 | 只看该作者
看这个程序太累人了,你直接上传个附件还好点

使用特权

评论回复
5
datouyuan| | 2017-5-5 16:14 | 只看该作者
本帖最后由 datouyuan 于 2017-5-5 16:21 编辑

看得太累了,建议楼主重新排版。

              switch(up_flag)
                        {
                            case 0:
                                        e=0;
                                break;
                                case 1:
                                        e=1;
                            break;
                                case 2:
                                        e=2;
                                break;
                                case 3:
                                        e=3;
                                break;
                                case 4:
                                        e=4;
                                break;
                                case 5:
                                        e=5;
                                break;
                                case 6:
                                        e=6;
                                break;
                                case 7:
                                        e=7;
                                break;
                                case 8:
                                        e=8;
                                break;
                                case 9:
                                        e=9;
                                break;
                                case 10:
                                          e=0;
                                break;
             }

不过你那代码写得也太差了,比如这段只要一句就能代替。
        if(up_flag<10) e=up_flag;
        else if(up_flag==10) e=0;

使用特权

评论回复
6
datouyuan| | 2017-5-5 16:30 | 只看该作者
另外,扫了半天也没找到变量up_flag、e的定义。

使用特权

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

本版积分规则

4

主题

56

帖子

1

粉丝