⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timer1.c

📁 protues仿真keil程序 初学者的福音
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <stc90.h>
#include <hardware.h>
#include <intrins.h>
#include <eeprom.h>

unsigned char BeepTimes;
unsigned char KeyCode ;
//按键值
unsigned char KeyPressing1,KeyPressed1,KeyPressing2,KeyPressed2 ;
//按键持续按下的键盘码 按键点按的键盘码

unsigned char Key1PressingTime ,Key2PressingTime;
//按键持续按下的时间
bit IsKeyPressed ;
bit IsScanKeyTime ;
void keyScan(void);
void KeyProgram(void);


#define BLACK 0xff
//段码送BLACK 就是熄灭
code unsigned char Led7Code[]=
{
    // 0 -                    -                  -9   //- //-    // ~  //~    //_//_		
    0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xbf,0xbf, 0xbf, 0xbf,0xbf,0xbf   
};
//共阳  低电平点亮
code unsigned char LedBIt[4]=
{
    0x7F,0XBF,0xDF,0xEF 
}
;
//位码口数据
unsigned char BitNum ;
//当前点亮第几个数码管
unsigned char DisplayData[4]=
{
    0x00,0x00,0x00,0x00 
}
;
//4个数码管分别要显示的数据值
unsigned char IsFlash[4]=
{
    0x00,0x00,0x00,0x00 
}
;
//4个数码管是否要闪烁 00表示不闪烁 01表示要闪烁
unsigned char Time5ms ;
//用于闪烁的5ms
bit IsFlashTime ;
//当前是否为闪烁时间。
bit IsDisplay1BitTime ;
//当前是否到了1bit的显示时间 就是5ms的时间
bit IsTime1s;
//bit IsGo;//时间是否跑。
//unsigned char Time300ms ;

void Display(void);
unsigned char IsRun[6];
unsigned char TimeSave[6]=
{
    0,0,0,0,0,0 
};
unsigned char TimeDelay[6]=
{
    0,0,0,0,0,0 
};
//00-99表示秒为单位 100-199表示分为单位 范围为00-99
//6个延时控制引脚延时时间,0或者100则不需要延时或者已经到了延时时间 非0则需要延时控制。
unsigned char CurrLed;//当前显示第几路led开关的数据。
//unsigned char Time1000ms;
bit IsSetFirst;
#define SET1     1
#define SET2     2
#define SET3     3
#define SET4     4
#define SET5     5
#define SET6     6

#define WORK     0

unsigned char MachineState ;
//机器状态 	 倒计时状态		设置状态
///开机为正常倒计时状态,全部数据为0 ,
//按下任何一个颜色按键 进入设置状态,屏幕显示相关设置值或者该颜色端口的当前倒计时的时间。
//10s后没有继续按键,则退出到正常显示。
//设置结束 则退出到正常显示 , 循环显示各个端口的倒计时时间。
//后台进行时间检测 时间到则自动修改输出电平。

void  funUp(void);
void  funDown(void);
void  funLeft(void);
void  funRight(void);
void  funOk(void)  ;
unsigned char Time60s; 



main()
{
    unsigned char i ;
    
    PLed0=1 ;
    PLed1=1 ;
    PLed2=1 ;
    PLed3=1 ;
    PLed4=1 ;
    PLed5=1 ;
    //开机所有电路都是低电平。
    TMOD=0X01 ;
    
    TH0=0XeC ;
    //5ms定时中断 4个数码管 所以每个数码管20ms刷新一次。
    TL0=0X78 ;
    ET0=1 ;
    PT0=0 ;
    //定时器0作为显示用  低优先级。
    TR0=1 ;
    
    EA=1 ;


    
	delay(60000);
    for(i=0;i<6;i++)
    {
		TimeSave[i]=byte_read(0x4000+i);
        TimeDelay[i]=TimeSave[i];
        IsRun[i]=0 ;
    }
    MachineState=WORK ;
    //开机后处于正常工作状态。
    //但是时间不运行。
    
    BeepTimes=2;//上电短响1次。
    //开机显示4个0  直到按下任意按键。
    
	
	
	
	
	while(1)
    {
        if(IsDisplay1BitTime==1)
        {
            IsDisplay1BitTime=0 ;
            Display();
        }
        if((PKeyPort1!=0xff)||(PKeyPort2&0x0e)!=0x0e)
        break ;
    }
    
    
    while(1)
    {
        //到了显示时间	执行显示子程序
        if(IsDisplay1BitTime==1)
        {
            IsDisplay1BitTime=0 ;
            Display();
        }
        
        // 50ms时间到 则检测一次按键 带消抖
        if(IsScanKeyTime==1)
        {
            IsScanKeyTime=0 ;
            KeyProgram();
        }
        //每一路时间-1,并检查 是否到了
        if(MachineState==WORK)
        {
            //工作状态下 每秒钟执行一次这个代码 修改并检查每个时间是否到位。
            unsigned char i ;
            if((IsTime1s==1))
            {
                IsTime1s=0 ;
                //每分钟以分为单位的线路自减1
                if(++Time60s>=60)
                {
                    Time60s=0 ;
                    for(i=0;i<6;i++)
                    {
                        if(IsRun[i])
                        if((TimeDelay[i]>100))
                        TimeDelay[i]--;
                    }
                }
                
                //每秒钟以秒为单位的线路自减1
                for(i=0;i<6;i++)
                {
                    if(IsRun[i])
                    if((TimeDelay[i]>0)&&(TimeDelay[i]<100))
                    TimeDelay[i]--;
                }
                
				//倒计时部分代码
                if(IsRun[0]==1)
                {
                    if((TimeDelay[0]==0)||(TimeDelay[0]==100))
                    {
                        PLed0=1 ;
                        IsRun[0]=0 ;
						if(TimeSave[0]!=0)
							BeepTimes=2;
                    }
                    else 
                    PLed0=0 ;
                }
                else 
                PLed0=1 ;
                if(IsRun[1]==1)
                {
                    if((TimeDelay[1]==0)||(TimeDelay[1]==100))
                    {
                        PLed1=1 ;
                        IsRun[1]=0 ;
						if(TimeSave[1]!=0)
							BeepTimes=4;

                    }
                    else PLed1=0 ;
                }
                else 
                PLed1=1 ;
                if(IsRun[2]==1)
                {
                    if((TimeDelay[2]==0)||(TimeDelay[2]==100))
                    {
                        PLed2=1 ;
                        IsRun[2]=0 ;
						if(TimeSave[2]!=0)
							BeepTimes=6;
                    }
                    else PLed2=0 ;
                }
                else 
                PLed2=1 ;
                if(IsRun[3]==1)
                {
                    if((TimeDelay[3]==0)||(TimeDelay[3]==100))
                    {
                        PLed3=1 ;
                        IsRun[3]=0 ;
						if(TimeSave[3]!=0)
							BeepTimes=8;
                    }
                    else PLed3=0 ;
                }
                else 
                PLed3=1 ;
                if(IsRun[4]==1)
                {
                    if((TimeDelay[4]==0)||(TimeDelay[4]==100))
                    {
                        PLed4=1 ;
                        IsRun[4]=0 ;
						if(TimeSave[4]!=0)
							BeepTimes=10;
                    }
                    else PLed4=0 ;
                }
                else 
                PLed4=1 ;
                if(IsRun[5]==1)
                {
                    if((TimeDelay[5]==0)||(TimeDelay[5]==100))
                    {
                        PLed5=1 ;
                        IsRun[5]=0 ;
						if(TimeSave[5]!=0)
							BeepTimes=12;
                    }
                    else PLed5=0 ;
                }
                else 
                PLed5=1 ;
                
                if(IsRun[CurrLed])
                {
                    if(TimeDelay[CurrLed]<100)
                    IsFlash[1]=0 ;
                    else 
                    IsFlash[1]=1 ;
                    DisplayData[0]=CurrLed+1 ;
                    DisplayData[1]=10 ;
                    DisplayData[2]=TimeDelay[CurrLed]/10%10 ;
                    DisplayData[3]=TimeDelay[CurrLed]%10 ;
                }
                else 
                {
                    DisplayData[0]=0 ;
                    DisplayData[1]=0 ;
                    DisplayData[2]=0 ;
                    DisplayData[3]=0 ;
                    IsFlash[1]=0 ;
                }
                
            }
            
        }
        
        //设置状态
        else 
        {
            PLed0=1 ;
            PLed1=1 ;
            PLed2=1 ;
            PLed3=1 ;
            PLed4=1 ;
            PLed5=1 ;
            for(i=0;i<6;i++)
            {
                TimeDelay[i]=TimeSave[i];
                IsRun[i]=0 ;
            }
            
            DisplayData[0]=MachineState ;
            //显示当前设置哪一路的延时数据
            
            if(TimeSave[MachineState-1]<100)
            IsFlash[1]=0 ;
            else 
            IsFlash[1]=1 ;
            DisplayData[1]=10 ;
            
            //刚刚进入设置状态,把显示的时间设置为原来设置保存的时间
            if(IsSetFirst==1)
            {
                //设置闪烁位为十位
                IsSetFirst=0 ;
                DisplayData[2]=TimeSave[MachineState-1]/10%10 ;
                DisplayData[3]=TimeSave[MachineState-1]%10 ;
                IsFlash[2]=0x01 ;
            }
        }
    }
}


//30us执行结束
void FunTimer0(void)interrupt 1 
{
    TH0=0XeC ;
    TL0=0X78 ;  
    //5ms定时中断 4个数码管 所以每个数码管20ms刷新一次。
    //每到了5ms,就到了该显示1位数据的时间。
    IsDisplay1BitTime=1 ;
     if(Time5ms%50==0)
    	{
		IsFlashTime=!IsFlashTime ;
		if(BeepTimes>0)
		 {
    	  PBeep=BeepTimes%2  ;
		  BeepTimes--;
		 }

		}
    //250ms闪烁一次。
    //每50ms检测一次按键
    if(Time5ms%10==0)
    IsScanKeyTime=1 ;
    //每1000ms
    if(++Time5ms>=200)
    {
        Time5ms=0 ;
        IsTime1s=1 ;
    }
    
}


void Display(void)
{
    PDisplaySeg=BLACK ;
    //消隐
    PDisplayBit|=0xf0 ;
    //高4位清零
    PDisplayBit&=LedBIt[BitNum];
    //高4位 送位码
    if(IsFlashTime&&IsFlash[BitNum])
    PDisplaySeg=BLACK ;
    //黑屏用于闪烁
    else 
    PDisplaySeg=Led7Code[DisplayData[BitNum]];
    //送段码
    
    //每5ms会自动设置下次显示的数码管位置。
    if(++BitNum>=4)BitNum=0 ;
    //为下一个数码管送数据做准备。
}


void keyScan(void)
{
    unsigned char ReadData ;
    
    //P1口按键检测
    ReadData=PKeyPort1^0xff ;
    //
    KeyPressed1=ReadData&(ReadData^KeyPressing1);
    KeyPressing1=ReadData ;
    
    //P2口按键检测
    ReadData=PKeyPort2^0xff ;
    //0000 1110
    KeyPressed2=ReadData&(ReadData^KeyPressing2)&0x0e ;
    KeyPressing2=ReadData&0x0e ;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -