|
发表于 2013-1-13 23:16:36
|
显示全部楼层
| 来自江苏
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar c0,c1,temp,shiwan,wan,qian,bai,shi,ge;
uint shu;
sbit dula=P2^6; //共阴数码管 段选
sbit wela=P2^7; //共阴数码管 位选
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07, //共阴数码管 编码
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void init(); //函数声名(初始化子函数)
void display(uchar shiwan,uchar wan,uchar qian,uchar bai,uchar shi,uchar ge);
//函数声名(数码管显示子函数)
void delay(uint z); //函数声名(延时zms子函数)
void main()
{
init();
while(1)
{
display(7,6,5,bai,shi,ge);
}
}
void init()
{
TMOD=0x11;
TH0=(65536-46080)/256;
TL0=(65536-46080)%256;
TH1=(65536-46080)/256;
TL1=(65536-46080)%256;
EA=1;
ET0=1;
ET1=1;
TR0=1;
TR1=1;
temp=0xfe; //流水灯控制赋初值
P1=temp;
dula=0; //数码管控制赋初值
wela=0;
shu=432;
}
void timer0() interrupt 1 //定时器0中断(流水灯控制)
{
TH0=(65536-46080)/256;
TL0=(65536-46080)%256;
c0++;
if(c0==10)
{
c0=0;
temp=_crol_(temp,1);
P1=temp;
}
}
void timer1() interrupt 3 //定时器1中断(数码管控制)
{
TH1=(65536-46080)/256;
TL1=(65536-46080)%256;
c1++;
if(c1==2)
{
c1=0;
shu--;
if(shu==398)
{
TR1=0; //数码管停
TR0=0; //流水灯停
while(1)
{
P1=0xff;
delay(500);
P1=0x00;
delay(500);
}
}
bai=shu/100;
shi=shu%100/10;
ge=shu%10;
}
}
void display(uchar shiwan,uchar wan,uchar qian,uchar bai,uchar shi,uchar ge)
{ //显示子函数(数码管显示)
dula=1;
P0=table[shiwan];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delay(5);
dula=1;
P0=table[wan];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delay(5);
dula=1;
P0=table[qian];
dula=0;
P0=0xff;
wela=1;
P0=0xfb;
wela=0;
delay(5);
dula=1;
P0=table[bai];
dula=0;
P0=0xff;
wela=1;
P0=0xf7;
wela=0;
delay(5);
dula=1;
P0=table[shi];
dula=0;
P0=0xff;
wela=1;
P0=0xef;
wela=0;
delay(5);
dula=1;
P0=table[ge];
dula=0;
P0=0xff;
wela=1;
P0=0xdf;
wela=0;
delay(5);
}
void delay(uint z) //延时zms子函数
{
uint x;
for(z;z>0;z--)
for(x=113;x>0;x--);
}
|
|