|
计时器原程序- //A:0,B:7,C:2,D:4,E:5,F:1,G:6,H:3共阴
- #include<STC10F.h>
- sbit port_led1 =P3^0;
- sbit port_led2 =P3^1;
- sbit port_led3 =P3^2;
- sbit port_led4 =P3^3;
- sbit port_relay =P3^4;
- sbit port_led0 =P3^5;
- sbit port_key =P3^7;
- #define time_set0 5
- //-----------------------------------------------
- // 表格
- //-----------------------------------------------
- const unsigned char frg[16] = {
- 0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
- //-----------------------------------------------
- unsigned char windows;
- unsigned char num0;
- unsigned char num1;
- unsigned char num2;
- unsigned char num3;
- unsigned char num4;
- unsigned long data_time; //倒计时显示寄存器
- unsigned long data_set; //时间设定
- unsigned long data_check; //数据交换
- unsigned char time_ms0;
- unsigned char time_ms1;
- unsigned char data_wei;
- unsigned char soon_key;
- unsigned char at24_l;
- unsigned char at24_h;
- unsigned char address_h; //擦除高地址
- unsigned char address_l; //擦除低地址
- unsigned char wob_ok;
- unsigned char wob_wei;
- unsigned char wob_inc;
- unsigned char wob_dec;
- unsigned char time_fanhui; //设定完毕自动返回计时
- unsigned char data_alarm; //报警间隔计时
- unsigned char long_win; //蜂鸣器响的次序
- unsigned char data_buzzer; //蜂鸣器响的时间
- bit flag_alarm;
- bit flag_buzzer;
- bit flag_set; //设定闪烁的标志
- bit flag_ok;
- bit flag_wei;
- bit flag_inc;
- bit flag_inc_check;
- bit flag_dec;
- bit flag_dec_check;
- bit flag_soon; //长按的标志
- bit flag_close; //按键闭合的标志
- bit flag_write; //烧写的标志
- //-----------------------------------------------
- //定时中断0
- //-----------------------------------------------
- void Timer0(void) interrupt 1
- {
- TH0=0x3c;
- TL0=0xb0; //50ms
- //-----------------------------------------
- if(windows==0&&data_time==0)
- {
- time_fanhui++;
- if(time_fanhui>40)
- {
- // windows=0;
- // flag_write=1;
- time_fanhui=0;
-
- data_time=data_set;
- // time_ms0=0;
- port_relay=1;
- //flag_alarm=0;
- port_led0=1;
- // flag_set=0; //停止闪烁
- // data_wei=0; //再设定时仍然从个位开始
- }
- }
- //---------------------------------------
- if (flag_inc_check==0 && flag_dec_check==0)
- {
- flag_close=0;
- flag_soon=0;
- soon_key=0;
- }
- //---------------------------------------
- if(windows==0)
- {
- if(data_time==0)
- {
- port_relay=0;
- //flag_alarm=1;
- port_led0=0;
- }
- else
- {
- time_ms0++;
- if(time_ms0>=20)
- {
- time_ms0=0;
- data_time--;
- }
- }
- }
- //---------------------------------------
- if(windows==1)
- {
- if(flag_soon==0)
- {
- if(flag_close==1)
- {
- soon_key++;
- if(soon_key==30)
- {
- flag_soon=1;
- data_wei=0;
- }
- }
- }
- else
- {
- flag_inc=0;
- flag_dec=0;
- }
- }
- //-------------------------------------------------
- time_ms1++;
- if(time_ms1>=5)
- {
- time_ms1=0;
- if(windows==1)
- {
- flag_set=~flag_set;
- }
- }
- }
- //-----------------------------------------------
- //定时中断1
- //-----------------------------------------------
- void Timer1(void) interrupt 3
- {
- TH1=0xd8;
- TL1=0xf0; //10ms
- }
- //---------------------------------------------------------
- //1ms延时
- //---------------------------------------------------------
- void delay_1ms(unsigned int z)
- {
- unsigned int x,y;
- for(x=z;x>0;x--)
- for(y=75;y>0;y--);
- }
- //---------------------------------------------------------
- //ISP功能禁止
- void ISP_DISABLE(void)
- {
- ISP_CONTR=0;
- ISP_CMD =0;
- ISP_TRIG =0;
- }
-
- //字节读取
- unsigned char BYTE_READ(unsigned char isp_addh_buf,unsigned char isp_addl_buf)
- {
- ISP_CONTR=0x83;
- ISP_CMD =0x01;
- ISP_ADDRH=isp_addh_buf;
- ISP_ADDRL=isp_addl_buf;
- EA=0;
- ISP_TRIG=0x5a;
- ISP_TRIG=0xa5;
- EA=1;
- ISP_DISABLE();
- return ISP_DATA;
- }
- //字节编程
- void BYTE_PROGRAM(unsigned char isp_addh_buf,unsigned char isp_addl_buf,unsigned char isp_data_buf)
- {
- ISP_CONTR=0x83;
- ISP_CMD =0x02;
- ISP_ADDRH=isp_addh_buf;
- ISP_ADDRL=isp_addl_buf;
- ISP_DATA=isp_data_buf;
- EA=0;
- ISP_TRIG=0x5a;
- ISP_TRIG=0xa5;
- EA=1;
- ISP_DISABLE();
- }
- //扇区擦除
- void SECTOR_ERASE(void)
- {
- ISP_CONTR=0x83;
- ISP_CMD =0x03;
- //ISP_ADDRH=0x00;
- //ISP_ADDRL=0x00;
- ISP_ADDRH=address_h;
- ISP_ADDRL=address_l;
- EA=0;
- ISP_TRIG=0x5a;
- ISP_TRIG=0xa5;
- EA=1;
- ISP_DISABLE();
- }
- //---------------------------------------------------------
- //设定键
- //---------------------------------------------------------
- void key_ok(void)
- {
- P1=0xfe;
- delay_1ms(1);
- if(port_key==0&&flag_ok==0)
- {
- wob_ok++;
- if(wob_ok>10)
- {
- wob_ok=0;
- flag_ok=1;
- time_fanhui=0;
- windows++;
- if(windows>1)
- {
- windows=0;
- flag_write=1;
-
- if(data_time>0)
- {
- data_time=data_set;
- time_ms0=0;
- port_relay=1;
- //flag_alarm=0;
- port_led0=1;
- }
- flag_set=0; //停止闪烁
- data_wei=0; //再设定时仍然从个位开始
- }
- }
- }
- else
- {
- if(port_key==1)
- {
- wob_ok=0;
- flag_ok=0;
- }
- }
- }
- //---------------------------------------------------------
- //位移键
- //---------------------------------------------------------
- void key_wei(void)
- {
- P1=0xfb;
- delay_1ms(1);
- if(port_key==0&&flag_wei==0&&windows==1)
- {
- wob_wei++;
- if(wob_wei>10)
- {
- wob_wei=0;
- flag_wei=1;
- time_fanhui=0;
- data_wei++;
- if(data_wei>3)
- {
- data_wei=0;
- }
- }
- }
- else
- {
- if(port_key==1)
- {
- wob_wei=0;
- flag_wei=0;
- }
- }
- }
- //---------------------------------------------------------
- //加
- //---------------------------------------------------------
- void key_inc(void)
- {
- P1=0xbf;
- delay_1ms(1);
- if(port_key==0&&flag_inc==0)
- {
- wob_inc++;
- if(wob_inc>10)
- {
- wob_inc=0;
- flag_inc=1;
- flag_inc_check=1;
- time_fanhui=0;
- flag_close=1;
- if(windows==1)
- {
- if(data_wei==0)
- {
- if(data_set<9999)
- {
- data_set++;
- }
- }
- else if(data_wei==1)
- {
- if(data_set<9989)
- {
- data_set=data_set+10;
- }
- else
- {
- data_set=9999;
- }
- }
- else if(data_wei==2)
- {
- if(data_set<9899)
- {
- data_set=data_set+100;
- }
- else
- {
- data_set=9999;
- }
- }
- else if(data_wei==3)
- {
- if(data_set<8999)
- {
- data_set=data_set+1000;
- }
- else
- {
- data_set=9999;
- }
- }
- }
- }
- }
- else
- {
- if(port_key==1)
- {
- wob_inc=0;
- flag_inc=0;
- flag_inc_check=0;
- }
- }
- }
- //---------------------------------------------------------
- //减
- //---------------------------------------------------------
- void key_dec(void)
- {
- P1=0xef;
- delay_1ms(1);
- if(port_key==0&&flag_dec==0)
- {
- wob_dec++;
- if(wob_dec>10)
- {
- wob_dec=0;
- flag_dec=1;
- flag_dec_check=1;
- time_fanhui=0;
- flag_close=1;
- if(windows==1)
- {
- if(data_wei==0)
- {
- if(data_set>0)
- {
- data_set--;
- }
- }
- else if(data_wei==1)
- {
- if(data_set>=10)
- {
- data_set=data_set-10;
- }
- else
- {
- data_set=0;
- }
- }
- else if(data_wei==2)
- {
- if(data_set>=100)
- {
- data_set=data_set-100;
- }
- else
- {
- data_set=0;
- }
- }
- else if(data_wei==3)
- {
- if(data_set>=1000)
- {
- data_set=data_set-1000;
- }
- else
- {
- data_set=0;
- }
- }
- }
- }
- }
- else
- {
- if(port_key==1)
- {
- wob_dec=0;
- flag_dec=0;
- flag_dec_check=0;
- }
- }
- }
- //---------------------------------------------------------
- void data_write(void)
- {
- if(flag_write==1)
- {
- address_h=0x00;
- address_l=0x00;
- SECTOR_ERASE();
- at24_l=data_set;
- at24_h=data_set>>8;
- BYTE_PROGRAM(0x00,0x00,at24_h);
- BYTE_PROGRAM(0x00,0x01,at24_l);
-
- flag_write=0;
- }
- }
- //-----------------------------------------------
- // 显示程序
- //-----------------------------------------------
- void disp_loop(void)
- {
- if(windows==0)
- {
- data_check=data_time;
- }
- else if(windows==1)
- {
- data_check=data_set;
- }
- if(flag_set==0||data_wei!=3)
- {
- num0 = data_check/1000;
- P1 = frg[num0];
- port_led1=0;
- port_led2=1;
- port_led3=1;
- port_led4=1;
- delay_1ms(4);
- port_led1=1;
- port_led2=1;
- port_led3=1;
- port_led4=1;
- delay_1ms(1);
- }
- if(flag_set==0||data_wei!=2)
- {
- num1 = (data_check%1000)/100;
- P1 = frg[num1];
- port_led1=1;
- port_led2=0;
- port_led3=1;
- port_led4=1;
- delay_1ms(4);
- port_led1=1;
- port_led2=1;
- port_led3=1;
- port_led4=1;
- delay_1ms(1);
- }
- if(flag_set==0||data_wei!=1)
- {
- num2 = (data_check%100)/10;
- P1 = frg[num2];
- port_led1=1;
- port_led2=1;
- port_led3=0;
- port_led4=1;
- delay_1ms(4);
- port_led1=1;
- port_led2=1;
- port_led3=1;
- port_led4=1;
- delay_1ms(1);
- }
- if(flag_set==0||data_wei!=0)
- {
- num3 = data_check%10;
- P1 = frg[num3];
- port_led1=1;
- port_led2=1;
- port_led3=1;
- port_led4=0;
- delay_1ms(4);
- port_led1=1;
- port_led2=1;
- port_led3=1;
- port_led4=1;
- delay_1ms(1);
- }
- }
- //---------------------------------------------------------
- //初始化
- //---------------------------------------------------------
- void SysInit(void)
- {
- P1M0=0xff; //p1,0设定为开漏
- P1M1=0x00; //p1,1-p1,7设定为推挽输出
-
- P3M0=0x7f;
- P3M1=0x00; //推挽输出
-
- TMOD=0x11; //T0/T1=16BIT MODE
- TH0=0xF8;
- TL0=0x2F; //2ms
- TH1=0x3c;
- TL1=0xb0; //50ms
- ET0=1; //开T0中断
- ET1=1; //开T1中断
- EA=1; //开总中断
- TR0=1; //启动T0
- TR1=1; //开启T1
- port_relay=1;
- port_led0=1;
- }
- void main(void)
- {
- SysInit();
-
- at24_h=BYTE_READ(0x00,0x00);
- at24_l=BYTE_READ(0x00,0x01);
- data_set=at24_h;
- data_set=data_set<<8;
- data_set=data_set+at24_l;
- if(data_set>9999)
- {
- data_set=30;
- }
- data_time=data_set;
- while(1)
- {
- disp_loop();
- key_ok();
- key_wei();
- key_inc();
- key_dec();
- data_write();
- }
- }
复制代码
|
|