|
  先給廣大的電子愛(ài)好者點(diǎn)個(gè)贊。一下我將開(kāi)源一個(gè)基于STC89C52單片機(jī)的雙保持演示繼電器的源代碼。
部分程序預(yù)覽(完整版請(qǐng)下載本帖附件)
- //可以設(shè)定觸發(fā)時(shí)間 也可以保存觸發(fā)時(shí)間 觸發(fā) 時(shí)間到 保持兩秒,隨后又進(jìn)入觸發(fā)時(shí)間,循環(huán)進(jìn)行,類(lèi)似pwm技術(shù)
- //擁有上述功能的時(shí)間繼電器
- #include"stc89c5xrc.h"
- #include"MacroAndConst.h"
- #include"key_block.h"
- #include"delay.H"
- #include"display.H"
- #include"timer1.h"
- #include"define_io.h"
- uint16 second_num0;
- uint16 second_num1;
- uint16 t0cnt=0;
- uint16 newsecond;
- uint16 second=0;
- sbit beep=P2^3;
- /*bool second_flag;
- bool minite_flag;
- bool hour_flag;*/
- //---------------------------------------------
- void main()
- {
- init_timer1();
- init_24c02();
- // second=read24c02(0)*255+read24c02(1);//讀取數(shù)據(jù)時(shí)要賦值給second
- second=newsecond; //不斷電的情況下,復(fù)位也可以不丟失存儲(chǔ)的定時(shí)時(shí)間?答案是不可以
- while(1)
- {
- display(second);
- if(plus_key==0)
- {
- display(second);
- if(plus_key==0)
- {
- second+=10;
- }
- }
- if(minus_key==0)
- {
- display(second);
- if(minus_key==0)
- { second--;
- }
- while(minus_key==0) //加時(shí)候快點(diǎn)沒(méi)關(guān)系, 主要是減的時(shí)候調(diào)整好時(shí)間就行了
- {display(second);}
- }
- if(excute_key==0)
- {
- display(second);
- if(excute_key==0)
- {
-
- TR1=1;
- }
- while(excute_key==0)
- {display(second);}
- }
- if(restore_key==0)
- {
- display(second);
-
- if(restore_key==0)
- {
- uint8 i=0;
- for(i=0;i<=1;i++)
- {
- beep=0;
- delay(100);
- beep=1;
- delay(100);
- }
- //second_num0=second/255; //用數(shù)據(jù)分離的思想將16的數(shù)據(jù)分別存儲(chǔ)到8位數(shù)據(jù)中
- //second_num1=second%255;
- //write24c02(0,0);
- //write24c02(1,0); //如果讀取出現(xiàn)亂碼,將這兩行改成 write24c02(0,0); write24c02(1,0);即可
- newsecond=second; //設(shè)置的倒計(jì)時(shí)時(shí)間存在newsecond這里
- }
- }
- }
- }
- //---------------------------------------------
- void timer1()interrupt 3 using 1
- {
- second--;
- if(second==0)
- {
- TR1=0; TR0=1;P10=0;//
- }
-
- TH1=(65536-9216)/256;
- TL1=(65536-9216)%256;
- }
- void timer0()interrupt 1 using 0 //時(shí)間到后,保持2s長(zhǎng)的時(shí)間
- {
- t0cnt++;
- if(t0cnt==40)
- {
- TR0=0;TR1=1;P10=1;t0cnt=0;
- second=newsecond; //觸發(fā)時(shí)間到后,second恢復(fù)到先前設(shè)置的時(shí)間
- //second=read24c02(0)*255+read24c02(1);// 兩次讀取,產(chǎn)生警告.
- }
-
- TH0=(65536-46080)/256;
- TL0=(65536-46080)%256;
- }
復(fù)制代碼
|
評(píng)分
-
查看全部評(píng)分
|