本帖最后由 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;
} //时十位已在前控制
}
}
}
}
}
}
}
|