資源簡介
基于51單片機的電器定時開關C程序,程序通過外部中斷和計時器,實現了電器定時開關的功能,程序很簡單,可以用于初學者學習51單片機

代碼片段和文件信息
#include“reg51.h“
#define?uint?unsigned?int
#define?uchar?unsigned?char ??
uchar?flagshigettmm;
unsigned?char?num;
void?delay(uint?z);
void?init();
void?keyscan();
void?display(uchar?num);
unsigned?char?code?table[]={
0x3f0x060x5b0x4f
0x660x6d0x7d0x07
0x7f0x6f0x770x7c
0x390x5e0x790x71};
sbit?gew=P2^1;
sbit?shiw=P2^0;
sbit?key1=P3^2;
sbit?key2=P3^3;
sbit?key3=P3^4;
sbit?key4=P3^5;
void?main()
{
// num=55;
init();
while(1)
{
keyscan();
display(num);
}
}
void?init()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
num=55;
// shi=0;
// ge=0;
// shiw=0;
// gew=0;
}
void?delay(uint?z)
{
uint?xy;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void?keyscan()
{??
// num=55;
if(key1==0)
{
delay(10);
if(key1==0)//確認key1是否按下
{
shi++;
if(shi==10)
shi=0;
while(!key1);
num=shi*10+ge;
}
}
if(key2==0)
{
delay(10);
if(key2==0)//確認key2是否按下
{
shi--;
if(shi==0)
shi=9;
while(!key2);
num=shi*10+ge;
}
}
if(key3==0)
{
delay(10);
if(key3==0)//確認key3是否按下
{
ge++;
if(ge==10)
{
while(!key3);
ge=0;
num=(shi+1)*10;
}
else
{
while(!key3);
num=shi*10+ge;
}
TR0=1;
}
}
if(key4==0)
{
delay(10);
if(key4==0)//確認key4是否按下
{
ge--;
if(ge==0)
{
while(!key4);
ge=9;
num=(shi-1)*10+ge;
}
else
{
while(!key4);
num=(shi)*10+ge;
}
}
}
}
void?display(uchar?num)
{
// num=55;
?shi=num/10;
?ge=num%10;
?shiw=0;
?gew=1;//十位開
??P0=table[shi];
??delay(5);//顯示十位
?shiw=1;
?gew=0;//個位開
//?dula=1;
?P0=table[ge];
?//dula=0;
?delay(5);//顯示個位
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????6336??2010-11-03?19:52??電器定時開關tes\01
?????文件???????1049??2010-11-03?19:52??電器定時開關tes\01.hex
?????文件?????????55??2010-11-03?19:52??電器定時開關tes\01.lnp
?????文件??????10387??2010-11-03?19:52??電器定時開關tes\01.M51
?????文件???????1262??2010-11-03?22:13??電器定時開關tes\01.Opt
?????文件????????138??2010-11-04?23:35??電器定時開關tes\01.plg
?????文件???????2018??2010-11-03?22:13??電器定時開關tes\01.Uv2
?????文件??????????0??2010-11-03?18:41??電器定時開關tes\01_Uv2.Bak
?????文件???????5115??2002-04-16?14:32??電器定時開關tes\STARTUP.A51
?????文件??????11665??2010-11-03?19:52??電器定時開關tes\STARTUP.LST
?????文件????????749??2010-11-03?19:52??電器定時開關tes\STARTUP.OBJ
?????文件???????5730??2010-11-01?18:54??電器定時開關tes\tes
?????文件???????1773??2010-11-01?18:54??電器定時開關tes\tes.c
?????文件????????905??2010-11-01?18:54??電器定時開關tes\tes.hex
?????文件?????????54??2010-11-01?18:54??電器定時開關tes\tes.lnp
?????文件???????5785??2010-11-01?18:54??電器定時開關tes\tes.LST
?????文件???????9225??2010-11-01?18:54??電器定時開關tes\tes.M51
?????文件???????5913??2010-11-01?18:54??電器定時開關tes\tes.OBJ
?????文件???????9804??2010-11-01?18:54??電器定時開關tes\tes.plg
?????文件??????????0??2010-11-01?17:58??電器定時開關tes\tes.uv2
?????文件???????2128??2011-06-21?14:36??電器定時開關tes\Text2.c
?????文件???????6986??2010-11-03?19:52??電器定時開關tes\Text2.LST
?????文件???????6687??2010-11-03?19:52??電器定時開關tes\Text2.OBJ
?????目錄??????????0??2011-06-21?14:37??電器定時開關tes
-----------?---------??----------?-----??----
????????????????93764????????????????????24
評論
共有 條評論