|
單片機(jī)C語言實(shí)例-368-繼電器
#include<reg51.h>
sbit Key_UP=P3^2;
sbit Key_DOWN=P3^3;
unsigned char KeyV,TempKeyV;
sbit P25=P2^5;//數(shù)碼管第5位
sbit P26=P2^6;//數(shù)碼管第6位
sbit P27=P2^7;//數(shù)碼管第7位
sbit JDQ=P3^7;//繼電器
unsigned int js;
unsigned int ds;//倒計(jì)時(shí)初始時(shí)間
unsigned char jsflag;
static unsigned int i;
unsigned char code LEDDis[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xFF,0xBF};
void Delay(unsigned int ii) //1MS
{
unsigned int jj;
for(;ii>0;ii--)
for(jj=0;jj<125;jj++)
{;}
}
void main()
{
EA = 1; //允許CPU中斷
ET0 = 1; //定時(shí)器0中斷打開
ET1=1; //定時(shí)器1中斷打開
TMOD = 0x11; //設(shè)定時(shí)器0 1為模式1,16位模式
TH0=0xB8;
TL0=0x00; //設(shè)定時(shí)值為(20ms)
TH1=0xdc; //設(shè)定時(shí)值為(10ms)
TL1=0x00;
TR0 = 1;
TR1=1;
ds=999;
KeyV = 0;
TempKeyV = 0;
jsflag=0;
while(1);
}
//定時(shí)器0中斷外理中鍵掃描和顯示
void Key_Time0(void) interrupt 1 using 2
{
TH0=0xB8;
TL0=0x00; //設(shè)定時(shí)值為20000us(20ms)
if (!Key_UP)
KeyV = 1;
if (!Key_DOWN)
KeyV = 2;
if (KeyV!= 0) //有鍵按下
{
Delay(10); //延時(shí)防抖 按下10ms再測
if (!Key_UP)
TempKeyV = 1;
if (!Key_DOWN)
TempKeyV = 2;
if (KeyV == TempKeyV) //兩次值相等為確定接下了鍵
{
if (KeyV == 1){
jsflag=0;
if(ds==0){
ds=600;
}
js=js+1;
JDQ=0; //繼電器吸合
if(js==50){
js=0;
ds=ds-1;
}
}
if(KeyV==2){
jsflag=1;
}
}
}
if(jsflag==1){
JDQ=1;
}
if(ds==0){
JDQ=1;
jsflag=1;
KeyV=0;
}
}
void disp_Time1(void) interrupt 3 using 3
{ TH1=0xdc;
TL1=0x00;
P0=LEDDis[ds/100];
P25=0;
Delay(2);
P25=1;
P0=LEDDis[(ds%100)/10];
P26=0;
Delay(2);
P26=1;
P0=LEDDis[ds%10];
P27=0;
Delay(2);
P27=1;
}
更多詳情參考附件文檔
+08:00C163聯(lián)盟網(wǎng)2399.png (31.86 KB, 下載次數(shù): 3)
下載附件
保存到相冊
2022-4-6 10:54 上傳
|
|