C51时钟3个及模数转换程序.docx
- 文档编号:3152645
- 上传时间:2023-05-05
- 格式:DOCX
- 页数:23
- 大小:18.27KB
C51时钟3个及模数转换程序.docx
《C51时钟3个及模数转换程序.docx》由会员分享,可在线阅读,更多相关《C51时钟3个及模数转换程序.docx(23页珍藏版)》请在冰点文库上搜索。
C51时钟3个及模数转换程序
C51实时时钟程序(三个)
时钟一:
#include
#include
#defineucharunsignedchar
#defineuintunsignedint
ucharcodetable[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
ucharcount=0,secL,secH,minL,minH,hourL,hourH;
longintsum;
voidDelay(uintk)
{
while(k>0)k--;
}
voidinit_timer0()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
voidtimer0()interrupt1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==20)
{
count=0;
sum++;
}
if(count>20)
count=0;
}
voidcalculate()
{
hourH=sum/60/60%24/10;
hourL=sum/60/60%24%10;
minH=sum/60%60/10;
minL=sum/60%60%10;
secH=sum%60/10;
secL=sum%10;
if(sum==86400)
sum=0;
}
voiddisplay_led()
{
uchari,temp;
temp=0x7f;
for(i=0;i<8;i++)
{
P2=temp;
switch(i)
{
case0:
P0=table[hourH];break;
case1:
P0=table[hourL];break;
case2:
P0=0xbf;break;
case3:
P0=table[minH];break;
case4:
P0=table[minL];break;
case5:
P0=0xbf;break;
case6:
P0=table[secH];break;
case7:
P0=table[secL];break;
}
Delay(140);
P0=0xff;
temp=_cror_(temp,1);
}
}
voidmain()
{
init_timer0();
while
(1)
{
calculate();
display_led();
}
}
时钟二:
#include
#include
#defineucharunsignedchar
#defineuintunsignedint
ucharcodejian[]={0xee,0xde,0xbe,0x7e,0xed,0xdd,0xbd,0x7d,
0xeb,0xdb,0xbb,0x7b,0xe7,0xd7,0xb7,0x77};//键值
ucharcodetable[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//断码
ucharcount=0,secL,secH,minL,minH,hourL,hourH;
ucharkey,n,stop=0,cut=0;
sbitbeep=P3^0;
voiddelay(uintm)//延时
{
while(m>0)m--;
}
voidkeypan()//读键值
{
ucharL,H,i;
P1=0x0f;
L=P1&0x0f;
if(L!
=0x0f)
{
delay(100);
if(L!
=0x0f)
L=P1&0x0f;
}
P1=0xf0;
H=P1&0xf0;
if(H!
=0xf0)
{
delay(100);
if(H!
=0xf0)
H=P1&0xf0;
}
key=L+H;
for(i=0;i<16;i++)
{
if(key==jian[i])
n=i;
}
}
voidinit_timer0()//timer0中断
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
voidtimer0()interrupt1//中断服务
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count>20)
count=0;
}
voidcalculate()//计时计算
{
if(stop==0)
{
if(count==20)
{
count=0;
secL=secL+1;
if(secL==10)
{
secL=0;
secH=secH+1;
if(secH==6)
{
secH=0;
minL=minL+1;
if(minL==10)
{
minL=0;
minH=minH+1;
if(minH==6)
{
minH=0;
hourL=hourL+1;
if(hourL==10)
{
hourL=0;
hourH=hourH+1;
}
}
}
}
}
elseif(hourH==2)
{
if(hourL==4)
{
hourL=0;
hourH=0;
}
}
}
}
}
voidDe_play()//控制
{
if(n==0)
{
cut=0;
if(count==20&&cut==0)
{
count=0;
secL=secL+1;
if(secL==10)
{
secL=0;
secH=secH+1;
if(secH==6)
secH=0;
}
}
}
elseif(n==1)
{
cut=0;
if(count==20&&cut==0)
{
count=0;
secL=secL-1;
if(secL==-1)
{
secL=9;
secH=secH-1;
if(secH==-1)
secH=5;
}
}
}
elseif(n==2)
{
cut=0;
if(count==20&&cut==0)
{
count=0;
minL=minL+1;
if(minL==10)
{
minL=0;
minH=minH+1;
if(minH==6)
minH=0;
}
}
}
elseif(n==3)
{
cut=0;
if(count==20&&cut==0)
{
count=0;
minL=minL-1;
if(minL==-1)
{
minL=9;
minH=minH-1;
if(minH==-1)
minH=5;
}
}
}
elseif(n==4)
{
cut=0;
if(count==20&&cut==0)
{
count=0;
hourL=hourL+1;
if(hourL==10)
{
hourL=0;
hourH=hourH+1;
}
}
elseif(hourH==2&&hourL==4)
{
hourL=0;
hourH=0;
}
}
elseif(n==5)
{
cut=0;
if(count==20&&cut==0)
{
count=0;
hourL=hourL-1;
if(hourL==-1)
{
hourL=9;
hourH=hourH-1;
if(hourH==-1)
{
hourH=2;
hourL=3;
}
}
}
}
elseif(n==13){cut=1;beep=0;delay(1000);beep=1;n=16;}
elseif(n==14){stop=1;beep=0;delay(1000);beep=1;n=16;}
elseif(n==15){stop=0;beep=0;delay(10000);beep=1;n=16;}
}
voiddisplay_led()//显示
{
uchari,temp;
temp=0x7f;
for(i=0;i<8;i++)
{
P2=temp;
switch(i)
{
case0:
P0=table[hourH];break;
case1:
P0=table[hourL];break;
case2:
P0=0xbf;break;
case3:
P0=table[minH];break;
case4:
P0=table[minL];break;
case5:
P0=0xbf;break;
case6:
P0=table[secH];break;
case7:
P0=table[secL];break;
}
delay(140);
P0=0xff;
temp=_cror_(temp,1);
}
}
voidmain()//主函数
{
beep=1;
init_timer0();
while
(1)
{
display_led();
calculate();
keypan();
De_play();
}
}
时钟三:
#include
#include
#defineucharunsignedchar
#defineuintunsignedint
ucharcodejian[]={0xee,0xde,0xbe,0x7e,0xed,0xdd,0xbd,0x7d,
0xeb,0xdb,0xbb,0x7b,0xe7,0xd7,0xb7,0x77};//键值
ucharcodetable[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//断码
ucharcodeLED_data[]={0,1,2,3,4,5,6,7,8,9};
ucharcount=0,secL,secH,minL,minH,hourL,hourH;
ucharkey,n,stop=0,count1=0,cut=0;
sbitbeep=P3^0;
voiddelay(uintm)//延时
{
while(m>0)m--;
}
voidkeypan()//读键值
{
ucharL,H,i;
P1=0x0f;
L=P1&0x0f;
if(L!
=0x0f)
{
delay(100);
if(L!
=0x0f)
L=P1&0x0f;
}
P1=0xf0;
H=P1&0xf0;
if(H!
=0xf0)
{
delay(100);
if(H!
=0xf0)
H=P1&0xf0;
}
key=L+H;
for(i=0;i<16;i++)
{
if(key==jian[i])
n=i;
}
}
voidinit_timer0()//timer0中断
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
voidtimer0()interrupt1//中断服务
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count>20)
count=0;
}
voidcalculate()//计时计算
{
if(stop==0)
{
if(count==20)
{
count=0;
secL=secL+1;
if(secL==10)
{
secL=0;
secH=secH+1;
if(secH==6)
{
secH=0;
minL=minL+1;
if(minL==10)
{
minL=0;
minH=minH+1;
if(minH==6)
{
minH=0;
hourL=hourL+1;
if(hourL==10)
{
hourL=0;
hourH=hourH+1;
}
}
}
}
}
elseif(hourH==2)
{
if(hourL==4)
{
hourL=0;
hourH=0;
}
}
}
}
}
voidDe_play()//控制
{
if(n==12)
{
if(count==20)
{
count=0;
count1=count1+1;
if(count1==7)
count1=0;
beep=0;delay(5000);beep=1;
n=16;
}
}
elseif(n==13)
{
if(count==20)
{
count=0;
count1=count1-1;
if(count1==-1)
count1=7;
beep=0;delay(5000);beep=1;
n=16;
}
}
elseif(n>=0&&n<=9)
{
switch(count1)
{
case1:
hourH=LED_data[n];break;
case2:
hourL=LED_data[n];break;
case3:
minH=LED_data[n];break;
case4:
minL=LED_data[n];break;
case5:
secH=LED_data[n];break;
case6:
secL=LED_data[n];break;
}
}
elseif(n==14){stop=1;beep=0;delay(1000);beep=1;n=16;}
elseif(n==15){stop=0;beep=0;delay(10000);beep=1;n=16;}
}
voiddisplay_led()//显示
{
uchari,temp;
temp=0x7f;
for(i=0;i<8;i++)
{
P2=temp;
switch(i)
{
case0:
P0=table[hourH];break;
case1:
P0=table[hourL];break;
case2:
P0=0xbf;break;
case3:
P0=table[minH];break;
case4:
P0=table[minL];break;
case5:
P0=0xbf;break;
case6:
P0=table[secH];break;
case7:
P0=table[secL];break;
}
delay(140);
P0=0xff;
temp=_cror_(temp,1);
}
}
voidmain()//主函数
{
beep=1;
init_timer0();
while
(1)
{
display_led();
calculate();
keypan();
De_play();
}
}
C51模数转换程序(查询法)
#include
#defineuintunsignedint
#defineucharunsignedchar
sbitADDC=P3^1;
sbitADDB=P3^2;
sbitADDA=P3^3;
sbitSTART=P3^5;
sbitEOC=P3^6;
sbitOE=P3^7;
ucharcodetablez[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x58,0x00,0x10};
ucharcodetablex[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
ucharshub;
floatshud;
ucharshudz,shuds,shudb;
voiddelay(uintk)
{
while(k>0)k--;
}
voiddisplay()
{
P0=0xef;
P1=tablez[shudz];
delay(100);
P0=0xdf;
P1=tablex[shuds];
delay(100);
P0=0xbf;
P1=tablex[shudb];
delay(100);
}
voidADCset()
{
ADDC=0;
ADDB=0;
ADDA=0;
START=0;
//delay(10);
START=1;
//delay(10);
START=0;
if(START==1)
EOC=0;
else
EOC=1;
while(EOC==0)
{
OE=1;
shub=P2;
OE=0;
}
shud=shub*4.93/256;
shudz=(unsignedchar)shud;
shuds=((unsignedchar)(shud*10))%10;
shudb=((unsignedchar)(shud*100))%10;
}
voidmain()
{
while
(1)
{
ADCset();
display();
}
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C51 时钟 转换 程序