请问:中断函数不起作用,求助?

2020-01-30 14:02发布

本帖最后由 chenguor 于 2012-11-18 11:02 编辑

#include <reg52.h>
#include"anjian.h"
void delay()                               
{
        uint16 n = 1000;
        while(n--);
}
/*功能函数*/
led_1()
{   。。。。。。。。。}
led_2()
{。。。。。。。。。。}
led_3()
{。。。。。。。。。。}
       
/*
键盘
*/
void key_board()
{
        if(!KeyIn1)                 
        {
                delay();         
                if(!KeyIn1)  
                {
                        led_1();
                        while(!KeyIn1);          
            }
        }
        if(!KeyIn2)
        {
                delay();
                if(!KeyIn2);
                {
                        led_2();
                        while(!KeyIn2);
                }
        }
        if(!KeyIn3)
        {
                delay();
                if(!KeyIn3);
                {
                        led_3();
                        while(!KeyIn3);
                }
        }
}
void int_init()                    
{
        TMOD |= 0x10;                  
        TMOD &= 0xdf;                  
        TH1 = 0xFC;                            
        TL1 = 0x67;
        TR1 = 1;                            
        EA = 1;                                    
        ET1 = 1;                          
}
/*主函数*/
main()
{
        P0 = 0XFF;               
        KeyOut1 =0;        //按键初始化
        int_init();
        while(1);
}
void interrupt_timer1() interrupt 3                 
{
      TH1 = 0xFC;                                                          
      TL1 = 0x67;
      key_board();                                                                  
}
这个程序的我想要的功能是:按KeyIn1键,显示功能函数led_1()的效果;按KeyIn2键,显示功能函数led_2()的效果;按KeyIn3键,显示功能函数led_3()的效果;但是按KeyIn1键,显示功能函数led_1()的效果,如果按KeyIn2键,显示功能函数led_1()的效果,就是没进入中断?请问各位这是什么原因?谢谢
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
18条回答
jswd0810
1楼-- · 2020-01-30 17:19
你把这儿改一下
void delay()                                
{
       static uint16 n = 1000;
        while(n--);
}
不过不建议这种办法
iquer
2楼-- · 2020-01-30 19:53
本帖最后由 iquer 于 2012-11-18 12:27 编辑

建议将key_board(); 这个函数放到主函数中,不过这样定时器就可以去掉了

/*主函数*/
main()
{
        P0 = 0XFF;               
        KeyOut1 =0;        //按键初始化
        int_init();
        while(1)
        {
             key_board();
         }
}
//定时器在这就没什么用了
void interrupt_timer1() interrupt 3                 
{
      TH1 = 0xFC;                                                         
      TL1 = 0x67;
}
lstcspring
3楼-- · 2020-01-31 01:10
 精彩回答 2  元偷偷看……
信天游
4楼-- · 2020-01-31 03:09
建议用标志位,在中断里将标志位置成不同的数,在主程序中判断标志位即可
chenguor
5楼-- · 2020-01-31 08:14
jswd0810 发表于 2012-11-18 12:15
你把这儿改一下
void delay()                                
{

好的  谢谢
chenguor
6楼-- · 2020-01-31 09:11
iquer 发表于 2012-11-18 12:24
建议将key_board(); 这个函数放到主函数中,不过这样定时器就可以去掉了

/*主函数*/

好的  谢谢

一周热门 更多>