找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1917|回復: 4
打印 上一主題 下一主題
收起左側

為什么用FOR循環(huán)取余數(shù)碼管顯示就不正常呢

[復制鏈接]
跳轉到指定樓層
樓主
ID:505859 發(fā)表于 2019-4-6 17:54 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
#include<reg52.h>
sbit LA=P2^2;
sbit LB=P2^3;
sbit LC=P2^4;
unsigned char code smguan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
                                            0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned char Ledbuf[6]={0x00,0x00,0x00,0x00,0x00,0x00};
unsigned int counter=0;
unsigned char buf[6];
unsigned int stopwatch=65000;

void T1_Timer();
void main(void)
{
         
         
         
         unsigned char i=0;
         T1_Timer();
        while(1)
        {


                 if(counter==1000)
                   {
                                 counter=0;         
                                 stopwatch--;
                                  for(i=0;i<6;i++)
                                            {
                                                        buf[i]=stopwatch%10;
                                                         stopwatch /=10;                                                       
                                            }
                   }                                                       
        }
}

void T1_Timer()
{
          TMOD=0X01;
          TH0=0XFC;
          TL0=0X18;
          TR0=1;
          EA=1;
          ET0=1;


}

void Keyscan()
{
         static unsigned char i=0;
         P0=0X00;
         switch(i)
         {
                 case 0: LA=0;LB=0;LC=0;break;
                 case 1: LA=1;LB=0;LC=0;break;
                 case 2: LA=0;LB=1;LC=0;break;
                 case 3: LA=1;LB=1;LC=0;break;
                 case 4: LA=0;LB=0;LC=1;break;
                 case 5: LA=1;LB=0;LC=1;break;
                 case 6: LA=0;LB=1;LC=1;break;
                 case 7: LA=1;LB=1;LC=1;break;
                 default:break;

         }
           P0=smguan[buf[i]];
                 i++;
            if(i==8)i=0;
}

void interrupt_Timter1() interrupt 1
{

                  TH0=0XFC;
              TL0=0X18;
                  counter++;
                  Keyscan();
}

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

沙發(fā)
ID:164602 發(fā)表于 2019-4-6 22:09 | 只看該作者
是你的程序問題,與for無關
給你改好了,試試看
#include<reg52.h>
sbit LA=P2^2;
sbit LB=P2^3;
sbit LC=P2^4;
unsigned char code smguan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
                                            0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned char Ledbuf[6]={0x00,0x00,0x00,0x00,0x00,0x00};
unsigned int counter=0;
unsigned char buf[6];
unsigned int stopwatch=12345,ST;

void T1_Timer();
void main(void)
{
         
         
         
         unsigned char i=0;
         T1_Timer();
        while(1)
        {

                                 ST=stopwatch;
                 if(counter>=1000)
                   {
                                 counter=0;         
                                 stopwatch--;
                                  for(i=0;i<6;i++)
                                            {
                                                        buf[i]=ST%10;
                                                         ST /=10;                                                        
                                            }
                   }                                                        
        }
}

void T1_Timer()
{
          TMOD=0X01;
          TH0=0Xfc;
          TL0=0X18;
          TR0=1;
          EA=1;
          ET0=1;


}

void Keyscan()
{
         static unsigned char j=0;
         P0=0X00;
         switch(j)
         {
                 case 0: LA=0;LB=0;LC=0;break;
                 case 1: LA=1;LB=0;LC=0;break;
                 case 2: LA=0;LB=1;LC=0;break;
                 case 3: LA=1;LB=1;LC=0;break;
                 case 4: LA=0;LB=0;LC=1;break;
                 case 5: LA=1;LB=0;LC=1;break;
                 case 6: LA=0;LB=1;LC=1;break;
                 case 7: LA=1;LB=1;LC=1;break;
                 default:break;

         }
           P0=smguan[buf[j]];
                 j++;
            if(j==8)j=0;
}

void interrupt_Timter1() interrupt 1
{

                  TH0=0Xfc;
              TL0=0X18;
                  counter++;
                  Keyscan();
}
回復

使用道具 舉報

板凳
ID:213173 發(fā)表于 2019-4-6 22:43 | 只看該作者
你這樣處理只循環(huán)1次stopwatch就面目全非了,需要加一個臨時變量過渡才不會破壞stopwatch。
void main(void)
{
        unsigned char i=0;
        unsigned int count;
        T1_Timer();
        while(1)
        {
                if(counter==1000)
                {
                        counter=0;         
                        stopwatch--;
                        count=stopwatch;
                        for(i=0;i<6;i++)
                        {
                                buf[i]=count%10;
                                count/=10;
                        }
                }
        }
}
回復

使用道具 舉報

地板
ID:505859 發(fā)表于 2019-4-7 08:33 | 只看該作者
wulin 發(fā)表于 2019-4-6 22:43
你這樣處理只循環(huán)1次stopwatch就面目全非了,需要加一個臨時變量過渡才不會破壞stopwatch。
void main(voi ...

好的,謝謝
回復

使用道具 舉報

5#
ID:505859 發(fā)表于 2019-4-7 08:33 | 只看該作者
HC6800-ES-V2.0 發(fā)表于 2019-4-6 22:09
是你的程序問題,與for無關
給你改好了,試試看
#include

好了,謝謝!
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復 返回頂部 返回列表