新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 單片機(jī)學(xué)習(xí)——獨(dú)立按鍵P1^0

單片機(jī)學(xué)習(xí)——獨(dú)立按鍵P1^0

作者: 時(shí)間:2016-11-23 來(lái)源:網(wǎng)絡(luò) 收藏
題目:數(shù)碼管前三位顯示一個(gè)跑表,從000到999之間以0.01秒的速度運(yùn)行,當(dāng)按下一個(gè)獨(dú)立按鍵時(shí),跑表停止,松開手后跑表繼續(xù)運(yùn)行。(采用定時(shí)器中斷方式)

解決方案:(C語(yǔ)言編程)

本文引用地址:http://m.butianyuan.cn/article/201611/320183.htm

#include
#include
#define uint unsigned int
#define uchar unsigned char
uchar bai,shi,ge,num;
uint shu;
sbit s=P1^0;
sbit le=P3^6;
void delay(uint a);
void dis();
void display(uchar ba,uchar sh,uchar g);
uchar code tabledu[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,
0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0x89,0x86,0xc7,0xc7,0xc0,0xff};
uchar code tablewe[]={0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
void init()
{
shu=0;
TMOD=0x11;
TH0=0xd8;
TL0=0xf0;
EA=1;
ET0=1;
TR0=1;
}
void main()
{
init();
while(1){
if(s==0){
delay(5);
if(s==0){
TR0=0;
dis();
}
}
else
{
TR0=1;
dis();
}
}
}
void time0() interrupt 1
{
TH0=0xd8;
TL0=0xf0;
num++;
shu++;
if(shu==999)
shu=0;
bai=shu/100;
shi=shu%100/10;
ge=shu%10;
}
void display(uchar ba,uchar sh,uchar g)
{
le=1;
P2=tablewe[1];
le=0;
le=1;
P0=tabledu[ba];
le=0;
delay(5);
le=1;
P2=tablewe[2];
le=0;
le=1;
P0=tabledu[sh];
le=0;
delay(5);
le=1;
P2=tablewe[3];
le=0;
le=1;
P0=tabledu[g];
le=0;
delay(5);
}
void dis()
{
if(num%1==0)
{
display(bai,shi,ge);
}
if(num%10==0)
{
display(bai,shi,ge);
}
if(num==10)
{
num=0;
display(bai,shi,ge);
}
}

void delay(uint a) {
uint i,j;
for(j=a;j>0;j--)
for(i=250;i>0;i--);
}



關(guān)鍵詞: 單片機(jī)獨(dú)立按

評(píng)論


技術(shù)專區(qū)

關(guān)閉