打印

KeilC 定时中断求救

[复制链接]
2841|15
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
wyhkerry|  楼主 | 2007-7-12 10:10 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
沙发
computer00| | 2007-7-12 10:25 | 只看该作者

你的定时器是不是需要软件清中断标志啊...

使用特权

评论回复
板凳
ayb_ice| | 2007-7-12 10:44 | 只看该作者

程序帖出来...

使用特权

评论回复
地板
wyhkerry|  楼主 | 2007-7-12 11:06 | 只看该作者

程序如下 希望大家帮忙哈 谢谢

我在medvin里模拟仿真的时候  就可以返回的啊

#include <REG52.H>
#include <MATH.H>
#include <intrins.h>

#define uchar unsigned char
#define uint unsigned int


void Init_T0( ); 
void Enable_int();
void Watchdog();
void Init_TGC();
void Init_Encoder();
void BeepOn();
void BeepOff();
void Send_Data(uchar bite);
void Rec_Data(void);
void LightLed(void);
void Delay40us();


uint DelayTime,DetectTime;
uint ActiveJob,ActiveJob0;
uint PortNo,Activeport;
uint PWMP;
uint BeepMode,BeepTime,BeepReq;
uint Gain1Data,Gain2Data;
uint IPMode,push;



uchar LED;
uchar TgcData[8];
uchar KeyData;
uchar Send_Buff;
sfr  P4=0xC0;
sfr  ADCH=0xC6;
sfr  T3=0xFF;
sfr  ADCON=0xC5;
sfr  PWM0=0xFC;
sfr  IEN0=0xA8;
sbit Gain1A=P3^0;
sbit Gain1B=P3^1;
sbit Gain2A=P2^6;
sbit Gain2B=P2^7;
sbit SwitchB=P3^7;
sbit CLK=P2^3;
sbit DAT=P2^4;
sbit EN0=P3^2;
sbit EN1=P3^3;
sbit EN2=P3^4;

main()
{
  bit Gain1A0,Gain2A0;
  uchar row,Table,scan,col;
  uchar code KeyCode[]=
                  { 0,35,8,9,25,10,11,23,
                   121,36,0,7,24,119,34,22,
                   0,37,123,6,26,120,118,21,
                   0,33,124,5,122,38,117,20,
                   0,32,0,4,0,39,50,19,
                   0,47,15,3,31,0,49,18,
                   133,0,115,125,75,0,0,17,
                   0,46,116,114,0,52,0,48,
                   0,81,80,0,0,51,0,16,
                   0,86,85,0,43,0,61,30,
                   0,107,113,110,112,101,102,2
                 };
  uchar code pccode[]=
                  {0,52,61,62,68,70,69,60,
                 9,51,0,54,67,10,43,53,
                 0,59,7,46,77,1,18,44,
                 0,35,124,37,120,66,11,45,    /* 124 print screen*/
                 0,27,0,38,0,75,50,36,
                 0,34,102,30,28,0,42,29,
                 133,0,12,126,75,0,0,20,   //     133menu   75comment  must change
                 0,26,3,4,0,58,0,33,
                 0,81,80,0,0,49,0,13,     //  81 SAVE  80 FREQ must change
                 0,86,85,0,90,0,41,88,     //  86  85   change
                 0,107,6,0,5,101,116,22   //  107  101   cahngge 
                };
  uchar code LEDcode[]=
                  {121,133,81,86,107,113,85,116,
                 115,124,123,125,114,112,75,119,
                 120,101,102,30,117,118,122                    
                };
   P2&=0xDF;
   P0=0xFF;
   Enable_int();
   Init_T0( );
//begin:
    for(;;)
    { 
        //ActiveJob=0;
    P4=0;
    P2=0x03;
    if(P1!=0xFF)                       //check press the key or not 
    {   
    // if(!ActiveJob)                     //check again ??
    //  {
            DelayTime=15;                   //  eliminate trembling
        while(DelayTime);
         // }    
          }
    //      P4=0;    
//        P2=0x03;
//        P1=0xBF;


if (P1!=0xFF)
       {    
        for(row=0;row<8;row++)
      {        
            P4=~(1<<row);   // check every row 
            P1=0xFF;
              scan=0x01;
            for(col=0;col<8;col++)
                {
                    if((P1&scan)==0)     //    scan every collume
                    {
                        ActiveJob=1;
                        Table=row+8*col;
                        goto lable;
                    }
                    scan=_irol_(scan,1);
                }
        }
    }

// if(!ActiveJob0)
    if((P2&0x07)!=0x07)
        {
           DelayTime=15;
           while(DelayTime);
        }
        

if((P2&0x07)!=0x07)
{   
    
    for(row=0;row<8;row++)
            {
               P4=~(1<<row);                // check every row 
                
            if((P2&0x01)==0)             //p2.0            
             {   ActiveJob=1;             
                Table=row+64;
                goto lable;  
              }
               if((P2&0x02)==0)            //p2.1            
             {   ActiveJob=1;             
                Table=row+72;
                goto lable;  
              }
              if((P2&0x04)==0)            //p2.2            
             {   ActiveJob=1;             
                Table=row+80;
                goto lable;  
              }
         }
}

}

lable:
     if(ActiveJob) 
        {
            ActiveJob=0;
            KeyData=pccode[Table];
            Send_Data(KeyData);    
            Delay40us();
            Delay40us();
            Delay40us();
            Delay40us();
            Delay40us();
        

//            for(LED=0;LED<24;LED++)
//             {
//              if(LEDcode[LED]==KeyData)            
//                    LightLed();    
//            }
        } 


//goto begin;
}
        
/******************************************* INTERRUPT **************************************/

void T0_Int() interrupt 1 using 1               /* timer0 overflow interrupt */
{  
    bit temp;
    TR0=0;                                         /* stop timer0 */
    TH0=0xFB; TL0=0xF6;                           /* reload T0 */
    TR0=1;                                        /* restart timer0 */
    push++;
    if(push>100)                                  //100ms check the switch
      {    if(SwitchB==0&&temp==1)
            IPMode++;
        else if(SwitchB==0)
                temp=1;      
        if(IPMode>3)
            IPMode=1;
        }                                      
    if(DelayTime>0)
         DelayTime--;
    if(BeepTime>0)
        BeepTime--;
        
}    


/******************************************* INITIALIZE *************************************/
  void Enable_int()
{
    TCON=0x00;
    IEN0=0x82;                            //EA=1  ET0=1  

}
  void Init_T0()                                /* timer0 initialization */
{                             
    TMOD=0x01;                                     /* mode 1, 16-bit timer */
    TH0=0xFC, TL0=0x66;                             /* set counter registers */
    TCON|=0x10;                                     /* TR0=1, start timer0 */
}

使用特权

评论回复
5
hotpower| | 2007-7-12 13:08 | 只看该作者

T2才要自己清

使用特权

评论回复
6
ayb_ice| | 2007-7-12 13:24 | 只看该作者

随便说说

        DelayTime=15;       //  eliminate trembling
        while(DelayTime);
这个地方造成死机了吧....

使用特权

评论回复
7
wyhkerry|  楼主 | 2007-7-12 14:14 | 只看该作者

6楼的正解

6楼的大虾能否告素我原因啊 
程序运行到这个地方就进中断了,当DelayTime=0时就继续向下执行,是不是程序有问题啊
还望多多指教 谢谢!

使用特权

评论回复
8
微风| | 2007-7-12 14:15 | 只看该作者

没有自减语句,delaytime怎么可能等于0

使用特权

评论回复
9
ayb_ice| | 2007-7-12 14:40 | 只看该作者

随便说说

        DelayTime=15;       //  eliminate trembling
        while(DelayTime)
            DelayTime--;

使用特权

评论回复
10
ptt1234| | 2007-7-12 14:47 | 只看该作者

此处不懂

if(P2!=0XFF)
{DelayTime=15;
while(DelayTime);//等到定时中断,有可能是你要15次中断程序后才跳出,因而你认为是不是一直在定时中断的原因
另:你中断程序用的是工作组1,而MAIN()用的是工作组0,会不会
DelayTime在主程序和中断程序处理不在一起呢?

使用特权

评论回复
11
wyhkerry|  楼主 | 2007-7-12 15:39 | 只看该作者

谢谢

谢谢 
我的中断函数里有自减啊  放那里不可以吗?

使用特权

评论回复
12
wyhkerry|  楼主 | 2007-7-12 15:44 | 只看该作者

10楼的兄弟 多谢

我定义的是1ms定时中断
{DelayTime=15;
while(DelayTime);
此处为了键盘的消抖,故通过进入中断15次的延迟时间来消抖的
而且我用的是单步跟踪 他执行完中断子函数后,就死在最后哪个花括号那里了啊
但是我medwin模拟仿真了,执行完中断子函数后,就会自动返回主函数那里
为什么在KEIL C中就不可以呢 比较费解  麻烦各位了哈 

使用特权

评论回复
13
ptt1234| | 2007-7-12 15:48 | 只看该作者

我搞错了


Delaytime和工作组没有关系.
但我运行你的程序没有错,我把DelaTime的值和定时值减小了,运行好象没有问题,能回到主函数中.看如下程序
#include <REG52.H>
#include <MATH.H>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int

void Init_T0( ); 
void Enable_int();
//void Watchdog();
//void Init_TGC();
//void Init_Encoder();
//void BeepOn();
//void BeepOff();
extern void Send_Data(uchar bite);
//void Rec_Data(void);
//void LightLed(void);
extern void Delay40us(void);

uint DelayTime,DetectTime;
uint ActiveJob,ActiveJob0;
/*uint PortNo,Activeport;
uint PWMP;*/
uint BeepMode,BeepTime,BeepReq;
uint Gain1Data,Gain2Data;
uint IPMode,push;
 
/*uchar LED;
uchar TgcData[8];*/
uchar KeyData;
//uchar Send_Buff;
sfr  P4=0xC0;
sfr  ADCH=0xC6;
sfr  T3=0xFF;
sfr  ADCON=0xC5;
sbit Gain1A=P3^0;
sbit Gain1B=P3^1;
sbit Gain2A=P2^6;
sbit Gain2B=P2^7;
sbit SwitchB=P3^7;
sbit CLK=P2^3;
sbit DAT=P2^4;
sbit EN0=P3^2;
sbit EN1=P3^3;
sbit EN2=P3^4;
main()
{
 /* bit Gain1A0,Gain2A0;*/
//  uchar row,Table,scan,col;
/*  uchar code KeyCode[]=
                  { 0,35,8,9,25,10,11,23,
                   121,36,0,7,24,119,34,22,
                   0,37,123,6,26,120,118,21,
                   0,33,124,5,122,38,117,20,
                   0,32,0,4,0,39,50,19,
                   0,47,15,3,31,0,49,18,
                   133,0,115,125,75,0,0,17,
                   0,46,116,114,0,52,0,48,
                   0,81,80,0,0,51,0,16,
                   0,86,85,0,43,0,61,30,
                   0,107,113,110,112,101,102,2
                 };*/
/*   uchar code pccode[]=
                  {0,52,61,62,68,70,69,60,
                 9,51,0,54,67,10,43,53,
                 0,59,7,46,77,1,18,44,
                 0,35,124,37,120,66,11,45,    // 124 print screen
                 0,27,0,38,0,75,50,36,
                 0,34,102,30,28,0,42,29,
                 133,0,12,126,75,0,0,20,   //     133menu   75comment  must change
                 0,26,3,4,0,58,0,33,
                 0,81,80,0,0,49,0,13,     //  81 SAVE  80 FREQ must change
                 0,86,85,0,90,0,41,88,     //  86  85   change
                 0,107,6,0,5,101,116,22   //  107  101   cahngge 
                }; 
  uchar code LEDcode[]=
                  {121,133,81,86,107,113,85,116,
                 115,124,123,125,114,112,75,119,
                 120,101,102,30,117,118,122                    
                }; */
   P1=0x00;
   P0=0x00;
   Enable_int();
   Init_T0( );
//begin:
    for(;;)
    { 
        //ActiveJob=0;
    
    P4=0;
    P2=0x03;
    if(P1!=0xFF)                       //check press the key or not 
    {   
    // if(!ActiveJob)                     //check again ??
    //  {
            DelayTime=2;                   //  eliminate trembling
        while(DelayTime);
         // }    
          }
    P1=0X07;
    P2=0X70;
    //      P4=0;    
//        P2=0x03;
//        P1=0xBF;

/*if (P1!=0xFF)
       {    
        for(row=0;row<8;row++)
      {        
            P4=~(1<<row);   // check every row 
            P1=0xFF;
              scan=0x01;
            for(col=0;col<8;col++)
                {
                    if((P1&scan)==0)     //    scan every collume
                    {
                        ActiveJob=1;
                        Table=row+8*col;
                        goto lable;
                    }
                    scan=_irol_(scan,1);
                }
        }
    }
// if(!ActiveJob0)
    if((P2&0x07)!=0x07)
        {
           DelayTime=15;
           while(DelayTime);
        }
        
if((P2&0x07)!=0x07)
{   
    
    for(row=0;row<8;row++)
            {
               P4=~(1<<row);                // check every row 
                
            if((P2&0x01)==0)             //p2.0            
             {   ActiveJob=1;             
                Table=row+64;
                goto lable;  
              }
               if((P2&0x02)==0)            //p2.1            
             {   ActiveJob=1;             
                Table=row+72;
                goto lable;  
              }
              if((P2&0x04)==0)            //p2.2            
             {   ActiveJob=1;             
                Table=row+80;
                goto lable;  
              }
         }
}
}
lable:
     if(ActiveJob) 
        {
            ActiveJob=0;
            KeyData=pccode[Table];
            Send_Data(KeyData);    
            Delay40us();
            Delay40us();
            Delay40us();
            Delay40us();
            Delay40us();
        
//            for(LED=0;LED<24;LED++)
//             {
//              if(LEDcode[LED]==KeyData)            
//                    LightLed();    
//            }*/
        } 

//goto begin;
}
        
/******************************************* INTERRUPT **************************************/
void T0_Int() interrupt 1 using 1               /* timer0 overflow interrupt */
{  
    bit temp;
    TR0=0;                                         /* stop timer0 */
    TH0=0xFF; TL0=0xEF;                           /* reload T0 */
    TR0=1;                                        /* restart timer0 */
    push++;
    if(push>100)                                  //100ms check the switch
      {    if(SwitchB==0&&temp==1)
            IPMode++;
        else if(SwitchB==0)
                temp=1;      
        if(IPMode>3)
            IPMode=1;
        }                                      
    if(DelayTime>0)
         DelayTime--;
    if(BeepTime>0)
        BeepTime--;
        
}    

/******************************************* INITIALIZE *************************************/
  void Enable_int()
{
    TCON=0x00;
    IE=0x82;                            //EA=1  ET0=1  
}
  void Init_T0()                                /* timer0 initialization */
{                             
    TMOD=0x01;                                     /* mode 1, 16-bit timer */
    TH0=0xFF, TL0=0xEF;                             /* set counter registers */
    TCON|=0x10;                                     /* TR0=1, start timer0 */
}

使用特权

评论回复
14
ptt1234| | 2007-7-12 15:49 | 只看该作者

我也是用KEIL C

能行

使用特权

评论回复
15
linkas| | 2007-7-12 15:57 | 只看该作者

用单个IC控制充电

www.linkas.com.cn

使用特权

评论回复
16
wyhkerry|  楼主 | 2007-7-13 10:45 | 只看该作者

谢谢

我再试试下  我的怎么不可以呢  郁闷
14楼的兄弟 你也是用单步执行的吗? 他可以返回去啊?

使用特权

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

本版积分规则

32

主题

92

帖子

1

粉丝