| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 642 人关注过本帖
标题:at89c51单片机中断程序的调用
只看楼主 加入收藏
我的世界123
Rank: 1
等 级:新手上路
帖 子:43
专家分:1
注 册:2013-10-14
结帖率:81.82%
收藏
已结贴  问题点数:10 回复次数:2 
at89c51单片机中断程序的调用
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
//#define _DEVICEFLAG_
sbit key0=P3^2;
sbit key1=P3^3;
sbit key2=P3^4;
sbit key3=P0^3;
sbit b1=P0^4;
sbit k1=P2^4;
sbit k2=P2^3;
sbit k3=P2^2;
sbit k4=P2^1;
sbit k5=P2^0;
void delay_ms (uint z)
{
 uint x,y;
 for (x=z;x>0;x--)
  for (y=110;y>0;y--);
}
void init()
{
 TMOD=0x01;
 TH0=0xfc;
 TL0=0x66;
 ET0=1;
 TR0=1;
 EA=1;
 TMOD|=0x01;    //定时器T0,工作方式1
   TH0=0xC3;
   TL0=0xC7;     //这两个寄存器存的是计数器的计数开始的值,这两个值累加至溢出后正好是50ms
   ET0=1;        //使T0中断可以溢出
   EA=1;         //开启总中断
   TF0=0;        //溢出位清零
   TR0=1;        //开启T0
}
uchar ControlFlag=0;
void timer0(void)interrupt 1
{   
     static uchar i,count=0;
   
    TH0=0xfc;
    TL0=0x66;
    count++;
    if(key0 == 0)
    {
#ifndef _DEVICEFLAG_               
        
    for(i=0;i<2;i++)
    {
        delay_ms(100);
    }
    if(key0 == 0)     //按键2
    {
        ControlFlag = 2;
    }
    else if(key0 == 1)
    {
        ControlFlag = 1;
    }         
#else
    if(key0 == 1)
    {
        ControlFlag = 1;
    }
#endif
}
}
//外部中断2服务程序     复位开关2
void timer1(void)interrupt 2
{   
    static uchar i,count=0;
        
    TH0=0xfc;
    TL0=0x66;
    count++;
    if(key1 == 1)
    {
    for(i=0;i<2;i++)
    {
        delay_ms(100);
    }
    if(key1 == 0)
    {
        ControlFlag = 6;
    }
    else if (key1 == 1)
    {
        ControlFlag = 5;   
    }
    }
}
//外部中断3服务程序    复位开关1
void it_timer3(void)interrupt 3
{
    static uchar i,count=0;
//    TMOD&=0xF0;
//    TH0=0x00;
//    TL0=0x00;
//    ET0=1;
//    EA=1;
//    TR0=1;
  TH0=0xC3;
   TL0=0xC7;
   count++;
    if(key2 == 1)
    {
    for(i=0;i<2;i++)
    {
        delay_ms(100);
    }
    if(key2 == 0)
    {
        ControlFlag = 4;
    }
    else if(key2 == 1)
    {
        ControlFlag = 3;
    }
  }
}
main()
{
    k1=0;
    k2=0;
    k3=0;
    k4=0;
    k5=0;
    key3=0;
    init();   
 while(1)
 {
#ifndef _DEVICEFLAG_                           
    if((b1==1)||(key3 == 1))
    {
        k5 = 1;
    }
    else if ((b1 == 0)&&(key3 == 0))
    {
        k5 = 0;
    }
#endif
/*************************************************************
*************************************************************/
#ifndef _DEVICEFLAG_
      switch(ControlFlag)     
      {   
          case 1:                              //机器总开关长按
                      if(key3 == 0)                  //服务器+5V为0时
                    {
                           if(b1 == 0)              //K1断开时
                        {
                            k1 = 1;
                            k4 = 1;
                            delay_ms(1000);
                            k4 = 0;
                        }
                       else if (b1== 1)                      //K1闭合时
                       {
                               k1 = 0;
                       }
                    }
                    else if(key3 ==1)                //服务器+5V为1时
                    {
                       if(b1 == 0)                          //K1断开时
                       {
                               k4 =1;
                             delay_ms(1000);
                            delay_ms(1000);
                            delay_ms(1000);
                            delay_ms(1000);
                            k4 = 0;

                       }
                       else if(b1 ==1)                         //K1闭合时   
                       {
                               k4 = 1;
                            delay_ms(1000);
                            delay_ms(1000);
                            delay_ms(1000);
                            k1= 0;
                            delay_ms(1000);
                            k4 = 0;   
                       }
                    }
                    ControlFlag = 0;                                       
                    break;
            case 2:                               //机器总开关短按
                    if(key3 == 0)                  //服务器+5V为0时
                    {
                        if(b1 ==0)              //K1为断开时
                        {
                            k1 = 1;
                            k4 = 1;
                            delay_ms(1000);
                            k4 = 0;
                        }
                       else if (b1 == 1)                     //K1为闭合时
                       {
                               k4 = 1;
                            delay_ms(1000);
                            k4 = 0;
                       }
                    }
                    else if(key3 ==1)               //服务器+5V为1时
                    {
                       if(b1 == 0)                         //K1断开时
                       {
                               k1 = 1;                  

                       }
                       else if(b1 ==1)                       //K1闭合时                 
                       {
                            ;   
                       }
                    }
                    ControlFlag = 0;
                    break;
            case 3:                                //复位开关1长按
                    if(b1 == 1)                             //K1闭合时
                    {
                        k2 = 1;
                        delay_ms(1000);
                        delay_ms(1000);
                        delay_ms(1000);
                        k2 = 0;            
                    }
                    ControlFlag = 0;
                    break;
            case 4:                                 //复位开关1短按
                    if(b1 == 1)                              //K1闭合时
                    {
                        k1 = 0;
                        delay_ms(1000);
                        k1 = 1;
                    }
                    ControlFlag = 0;
                    break;
            case 5:                                        //复位开关2长按
                    if(key3== 1)
                    {
                        k3= 1;
                        delay_ms(1000);
                        k3 = 0;
                    }
                    ControlFlag = 0;                             
                    break;
            case 6:                                                //复位开关2短按
                    if(key3 == 1)
                    {
                        k3 = 1;
                        delay_ms(1000);
                        k3 = 0;
                    }
                    ControlFlag = 0;
                    break;
#endif
            default:
                    break;

        }        
        delay_ms(10);
    }     
}

为什么我的中断只有第一个被调用了,求教
搜索更多相关主题的帖子: 单片机 include 计数器 寄存器 定时器 
2013-11-13 13:40
pink_duo
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:209
专家分:1054
注 册:2013-11-5
收藏
得分:5 
我用的是其他芯片,对51不了解,对底层驱动也是一知半解,能否说的详细点,三个都是定时器中断,你按键后触发进入的中断函数是哪个?

埋头做牛,抬头做人,低头做狗
2013-11-13 15:21
剑侠时代2013
Rank: 2
来 自:北京东燕郊经济开发
等 级:论坛游民
帖 子:6
专家分:10
注 册:2013-9-23
收藏
得分:5 
可以把问题说的详细一些么?
2013-11-13 17:06
快速回复:at89c51单片机中断程序的调用
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021516 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved