搜狐作者
15年10个月前IP:未同步
284789
3楼
成品倒计时交通灯 是通过信号机给出P3-1低电平红灯亮 P3-2黄灯亮 P3-3绿灯亮并根据信号机控制的时间自动修正倒计时的时间 我就是不会怎么样根据信号机控制的时间自动修正倒计时的时间。这是我写的手动赋值的程序,怎么根据P3-1,P3-3低电平时间自动赋值Sec。著第二个定时器还没用上。

#include <reg52.h>

typedef   unsigned char  uchar;
typedef   unsigned int   uint;          
          
sbit  r = P3^1;        
sbit  y = P3^2;
sbit  g = P3^3;          
          
bit   control=0;




uchar  Count,Sec,n,numberr,numberg,s,t;

uchar code  LEDData[ ] = {0xdf,0x06,0xbb,0x2f,0x66,0x6d,0xfd,0x07,0xff,0xef,};
  
uchar data  display[4];

char code reserve [3] _at_ 0x3b;  //保留0x3b开始的3个字节

/**********************************************************

延时函数                                            

**********************************************************/
void delayms(uint ms)
{
   uchar t;
   while(ms--)
   {
     for(t = 0; t < 114; t++);
   }
}

/**********************************************************

独立键盘扫描子函数

*********************************************************/
void  keyscan()
{
   P3=0xff;                     //置P1端口为输入
  delayms(1);
                  
   if(P3 !=0xff)                //读P1口
   {
  
          
           if((r|control)==0)
        
          {            
              TR0=1;
              Sec= 18;  
            control=1;
                 if(Sec==0)
             {
            TR0=0;
             }
                
          

           if((g|control)==0)
          {
              TR0=1;          //启动运行
              Sec=14;      
               control=1; }
               if(Sec==0)
        
            {    TR0=0; }
            
             if((y|control)==0)
           {
            TR0=0;
               Sec=0;
                
           }





        }
        
     }
   }


/********************************************************

主函数

*********************************************************/
main()
{
   P1=0x00;
   P2=0x00;
   Count=0x00;
   Sec=0;
   n=0x00;
   numberr=0x00;
   numberg=0x00;
  
   TMOD=0x01;
   TH0=0x4c;           //50ms定时
   TL0=0x00;      
   ET0=1;
  
   T2CON=0x00;         //设置T2CON寄存器        
   TH2=0xfc;           //1ms定时
   TL2=0x66;
   ET1=1;               //启用Timer2中断
   EA=1;               //总中断允许
   TR2=0;
   TR0=0;
                //启动定时器2
  
   while(1)
   {
      keyscan();
     led();
   }
}

/*********************************************************

  Timer0中断函数

**********************************************************/
void Timer0() interrupt 1
{
   TH0=0x4c;                 //50ms定时
   TL0=0x00;
   ++Count;
   if(Count==20)
   {
     Count=0;
     --Sec;            
      
     if(Sec==0)
     {
       TR0=0;
       control=0;
      // P1=0X00;
      // P2=0X00;    
          
     }
   }
}

/*********************************************************

  Timer2中断函数

**********************************************************/
void  timer1() interrupt 5
{
  

                      
   TH2=0xfc;                  
   TL2=0x66;

        
  
}

/**********************************************************

    显示子函数

/**********************************************************/
led()
{
     if(Sec!=0)
      {
            s  = LEDData[Sec/10];    
           t  = LEDData[Sec%10];
          
        if(Sec/10==0)
            {
                s=  0x00;
            }
            
               P1 = s;
            P2 = t;                      
              
        }
  }
/*************************************************************
评论
文号 / 284789

实习会员
名片发私信
学术分 0
总主题 1 帖总回复 2 楼拥有证书:笔友
注册于 2011/01/27 19:19:53最后登录 2018/01/10 01:36:45
主体类型:个人
所属领域:无
认证方式:邮箱
IP归属地:0

个人简介

暂未填写
笔记
{{note.content}}
{{n.user.username}}
{{fromNow(n.toc)}} {{n.status === noteStatus.disabled ? "已屏蔽" : ""}} {{n.status === noteStatus.unknown ? "正在审核" : ""}} {{n.status === noteStatus.deleted ? '已删除' : ''}}
  • 编辑
  • 删除
  • {{n.status === 'disabled' ? "解除屏蔽" : "屏蔽" }}
我也是有底线的