#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit we = P2^7;
sbit du = P2^6; //定义锁存器 端位选段
sbit LED1 = P1^0;
uchar counter,
time_counter ;
uchar code leddata[]={
0x3F,//0
0x06,//1
0x5B,//2
0x4F,//3
0x4F, //3
0x66, //4
0x6D,//5
0x7D,//6
0x07,//7
0x7F,//8
0x6F,//9
0x00,//ximie
0x00//zidingyi
};
void delay(uint z)
{
uint x,y;
for(x = z;x > 0;x--)
for(y = 114;y > 0; y--);
}
void display(uchar i)
{
uchar shi,ge ;
shi = i/10; //求模
ge = i%10; //求余
we = 1;
P0 = 0xbf;//点亮第 1
we = 0;
du = 1;
P0 = leddata[shi];
du = 0;
delay(1);
we = 1;
P0 = 0x7f;//点亮第二8
we = 0;
du = 1;
P0 = leddata[ge];
du = 0;
delay(1);
}
void main()
{
TMOD = 0x10;//定时器1 工作模式1 16位模式定时器
TH1 = 0x4b;
TL1 = 0xfe;//定时50ms
TR1 = 1; //启动定时器
while(1)
{
if(TF1 == 1)
{
TF1 = 0; //手动青0
TH1 = 0x4b;
TL1 = 0xfe;//定时50ms ,因溢出后寄存器里是0,重新赋初值
counter++;
}
if(counter == 20)
{
counter = 0;
LED1 = ~LED1;
time_counter ++;
} // (即从4bfe加到ffff)花50ms溢出一次,TF1这时自动置1,当检测到TF==1后,counter计次数
if(time_counter == 60)
time_counter = 0 ;
display(time_counter);
} /* 计数的
当定时器溢出20次
就是一秒钟的时间到了 */
}
一周热门 更多>