chenguor 发表于 2012-11-21 15:48:38

请教:数码管与流水灯不同时工作?

#include<reg52.h>
#include"smguan.h"             //文件省略      
code uint8 shumaguan_1[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
code uint8 shumaguan_2[] = {0x40,0x79,0x24,0x30,0x19, 0x12,0x02,0x78,0x00,0x10,0xff};   //带小数点
void delay_ms(uint16 m) //用法延时100ms,就把ms_number赋值为50                                    
{   uint16 i;
      uint8 j;
      for(i=0;i<m;i++)
      {      for(j=0;j<200;j++);
                for(j=0;j<102;j++);
      }
}
/*****************数码管刷新函数***********************/
void refresh_led()               
{       static uint8 j = 0;
       P0=0xff;   
      switch(j)
      {   case 0:ADDR0 = 0;ADDR1 = 0;ADDR2 = 0;j++;P0 = shumaguan_1];break;
            case 1:ADDR0= 1;ADDR1 = 0;ADDR2 = 0;j=0;P0 = shumaguan_2];break;      
                default:break;
      }
}
/************************流水灯**********************/
void led_1()
{      uint8 j;                                                                  
      while(1)                                                               
      {      P3 = ~(0x01 << j++);                                                                     
                delay_ms(shu_zi);             //延时
                if(j == 8)                                                         
                {j = 0;}               
      }
}
/********************键盘*******************************/
void scan_board()
{   if(KeyIn1==0)                                 //按keyin1控制时间加0.1s显示数码管上
      {delay_ms(20);         
                if(KeyIn1==0)
                {if(n >= 50)   n = 0;
                     else                n++;
                        shuzi=n;
                        shu_zi=50*shuzi;                                       
                        while(!KeyIn1);         
               }
      }
      if(KeyIn2==0)                                        //按keyin2控制时间减1s显示数码管上
      {
                delay_ms(20);         
                if(KeyIn2==0);
                {
                        if(n<= 0)
                              n = 50;
                        else
                              n--;
                        shuzi=n;
                        shu_zi=50*shuzi;                              
                        while(!KeyIn2);
                }
      }               
      if(KeyIn3==0)
      {
                delay_ms(20);         
                if(KeyIn3==0);
                {
                        led_1();                              //按keyin3键流水灯亮
                        while(!KeyIn3);
                }
      }                        
}

void timer1_init()                  
{
。。。。。。。。。。。。。。。。。。。《省略》                        
}      
void main()                        
{      
      KeyOut1 =0;                                           //键盘初始化
      ENLED1 = 0;ADDR3 = 1;               
      timer1_init();
      while(1);                                    
}
void interrupt_timer1() interrupt 3               
{      TH1 = 0xFC;                                                
      TL1 = 0x67;
      counter++;                                                                                                
      if(5 ==counter)                                                                        
      {
                counter = 0;
                scan_board();
                a = shuzi%10;                        
                a = shuzi/10%10;
      }
                refresh_led();                                                         
}

程序功能是:通过按键来改变流水灯间隔的时间,要求间隔时间在0.1S到5S可调,并同时显示在数码管上(2个数码管)。
现在程序可以实现时间在0.1S到5S 可调,可按KeyIn3控制流水灯开始,但问题是:流水灯与数码管不同时显示(有时流水灯工作,只显示一个数码管不是同时显示2个数码管),我想了很久也没找出原因,麻烦各位帮我找找原因(或者改进程序)?非常感谢!!

lcw_swust 发表于 2012-11-21 16:17:05

因为流水灯函数里有个while(1)

chenguor 发表于 2012-11-21 19:05:57

lcw_swust 发表于 2012-11-21 16:17 static/image/common/back.gif
因为流水灯函数里有个while(1)

谢谢!!!!!!!

daicp 发表于 2012-11-21 19:14:20

要学会用定时中断

853728579 发表于 2012-11-21 21:15:54

建议你使用一个不可剥夺性的OS来控制,实行多任务操作。这样会比较方便一点。
页: [1]
查看完整版本: 请教:数码管与流水灯不同时工作?