本帖最后由 hsanper 于 2016-2-6 22:30 編輯
#define uchar unsigned char
#define uint unsigned int
#include<reg51.h>
#include<intrins.h>
sbit P07=P0^7;
sbit P10=P1^0;
sbit P11=P1^1;
sbit P12=P1^2;
sbit P13=P1^3;
bit l=0;//狀態標志位
bit c=1;//小數點標志
uint i;
int z=0;
bit k=0;
uchar r=1;
uint dis=0x7f;
uint time=0;
uchar ocode1[6]={10,10,0,0,0,0};
uchar ocode2[6]={0,0,10,10,0,0};
uchar ocode3[6]={0,0,0,0,10,10};
uchar tcode[8]={0,0,0,0,0,0,0,1};//放時間位
uchar code dcode[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
//共陽 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 暗
/*******************************************************************/
/* */
/* 延時子程序delay( ) */
/* */
/*******************************************************************/
void delay(int ms)
{
int i;
while(ms--)
{
for(i = 0; i< 250; i++)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
}
}
/*******************************************************************/
/* */
/* 數碼管掃描程序void display() */
/* */
/*******************************************************************/
void display()
{
P3=0x00;//!!!!!!非常重要,先關所有顯示
P0=dcode[tcode[time]];
time++;
if(time==8)
{
time=0;//各位顯示
P07=c;
}
if(dis==0xdf||dis==0xf7)
{P07=0;}//小數位
P3=dis;
dis=_iror_(dis,1);//掃描位
if(dis==0xfd)
dis=0x7f;//只掃描前6位
}
/*******************************************************************/
/* */
/* 主程序void main() */
/* */
/*******************************************************************/
void main()
{
TMOD=0x22;
TCON=0x00;
TH1=0x06;//(65536-250)/256; //定時250us
TL1=0x06;//(65536-250)%256;
ET1=1;
TH0=0x06;//(65536-250)/256; //定時250us
TL0=0x06;//(65536-250)%256;
ET0=1;
EA=1;
TR0=1;
TR1=1;
P1=0xff;
P3=0xfc; //開始6位為0
P0=dcode[0];
while(1)
{ }
}
/*******************************************************************/
/* */
/* 中斷程序timer0() */
/* */
/*******************************************************************/
timer0() interrupt 1 //using 2
{
i++;
if(i==15)
{
i=0;
display(); //數目管掃描顯示
}
}
/*******************************************************************/
/* */
/* 中斷程序timer1() */
/* */
/*******************************************************************/
timer1() interrupt 3 //using 1
{
unsigned int b; //static
b++;
if(b==4000)
{
b=0;
tcode[0]++;
if(tcode[0]==10)
{
tcode[1]++;
tcode[0]=0;//時間顯示的秒個位
if(tcode[1]==6)
{
tcode[2]++;
tcode[1]=0;//時間顯示的秒十位
if(tcode[2]==10)
{
tcode[3]++;
tcode[2]=0;//時間顯示的分個位
if(tcode[3]==6)
{
tcode[4]++;
tcode[3]=0;//時間顯示的分十位
if(tcode[4]==10)
{
tcode[5]++;
tcode[4]=0;//時間顯示的時個位
if(tcode[5]==2&&tcode[4]==4)
{
tcode[5]=0;
tcode[4]=0;
} //時十位已在前控制
}
}
}
}
}
}
}
|