chenguor 发表于 2012-11-24 17:28:56

关于按键和定时器中断的问题求解决?

#include<reg52.h>
#include"smguan.h"

uint16 counter = 0;       
uint8n=0 ,temp ;
uint8 a;
code uint8 table_A[]={0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff,0xff};
code uint8 ledcode_1[] = {0xc0,0xf9,0xa4,0xb0,0x99,
                                              0x92,0x82,0xf8,0x80,0x90,0xff};
code uint8 ledcode_2[] = {0x40,0x79,0x24,0x30,0x19,
                                              0x12,0x02,0x78,0x00,0x10,0xff};          //带小数点
void delay(void)          
{
        uint16 i=2000;
        while (i--);

}

/*
*数码管的定义变量显示子函数
*/
void refresh_led()               
{       
        static uint8 j = 0;
    P0=0xff;   
        switch(j)
        {
                case 0:ADDR0 = 0;ADDR1 = 0;ADDR2 = 0;j++;P0 = ledcode_1];break;
                case 1:ADDR0 = 1;ADDR1 = 0;ADDR2 = 0;j++;P0 = ledcode_2];break;
                case 2:ADDR0 = 0;ADDR1 = 1;ADDR2 = 1;j=0;P0 = table_A];break;       
                default:break;
        }
}
/*
键盘扫描
*/
void key_board()
{
        if(!KeyIn1)               
        {
                delay();       
                if(!KeyIn1)
                {
                        if(n >= 50)
                                        n = 0;
                                else
                                        n++;
                        a = n%10;               
                        a = n/10%10;
                        refresh_led();
                        while(!KeyIn1);          
          }
        }

        if(!KeyIn2)
        {
                delay();
                if(!KeyIn2);
                {
                        if(n<= 0)
                                n = 50;
                        else
                                n--;
                        a = n%10;               
                        a = n/10%10;
                        refresh_led();
                        while(!KeyIn2);
                }
        }

        if(!KeyIn3)
        {
                delay();
                if(!KeyIn3);
                {       
                        temp=1;
                        while(!KeyIn3);
                }
        }          

}


/*
*定时器中断初始化
*/
void timer1_init()                  
{
        TMOD |= 0x10;                  
        TMOD &= 0xdf;                  
        TH1 = 0xFc;                           //1ms
        TL1 = 0x67;                          
        TR1 = 1;                          
        EA = 1;                                  
        ET1 = 1;                             
}       
/*
*主函数
*/
void main()                        
{       
        KeyOut1 =0;                                          
        ENLED1 = 0;ADDR3 = 1;       
        timer1_init();                                       
        while(1)
        {
          key_board();
        }          
}
/*
*中断服务函数
*/
void interrupt_timer1() interrupt 3               
{
        static uint32sec = 0;
        TH1 = 0xFC;                                                          
        TL1 = 0x67;               
        counter++;
                                                                                                                 
       if(counter == 5000)                
        {
                sec++;
                counter = 0;
                if(temp==1)
                {
                        a = sec%10;       
                }               
        }
        refresh_led();
}
请问:怎么写,按键程序中 n 值(红色部分)才能控制if(counter == 5000)        中的5000(蓝色部分)   即n值为定时器中断中循环的次数??                                       
               




页: [1]
查看完整版本: 关于按键和定时器中断的问题求解决?