本帖最后由 快鲨1 于 2019-1-19 23:23 编辑
#include<reg52.h>
void InitTimer0(void);
unsigned int code LedChar[]={
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e
};
void main()
{
unsigned int sec = 0;
InitTimer0();
while(1)
{
if(sec<=16)
{
P0=LedChar[sec];
sec++;
}
else
{
sec=0;
}
}
}
void InitTimer0(void)
{
TMOD = 0x01;
TH0 = 0xb8;
TL0 = 0x00;
TR0 = 1;
还是不行啊,一点变化都没有,还是一直停留在8这个数字上,就像图片上的一样。怎么办,还是说还是按参考答案的套路写算了?但是我不甘心啊,我想自己写一个出来。//编程实现数码管静态显示秒表的倒计时参考答案!
#include <reg52.h>
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
//ÓÃÊý×éÀ´´æ´¢ÊýÂë¹ÜµÄÕæÖµ±í£¬Êý×齫ÔÚÏÂÒ»ÕÂÏêϸ½éÉÜ
unsigned char code LedChar[] = {
0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
};
void main()
{
unsigned char cnt = 0; //¼Ç¼T0ÖжϴÎÊý
unsigned char sec = 0x0F; //¼Ç¼¾¹ýµÄÃëÊý£¬µ¹¼Æʱ³õÖµ0x0F(15)
ENLED = 0; //ʹÄÜU3£¬Ñ¡ÔñÊýÂë¹ÜDS1
ADDR3 = 1;
ADDR2 = 0;
ADDR1 = 0;
ADDR0 = 0;
TMOD = 0x01; //ÉèÖÃT0Ϊģʽ1
TH0 = 0xB8; //ΪT0¸³³õÖµ0xB800
TL0 = 0x00;
TR0 = 1; //Æô¶¯T0
while (1)
{
if (TF0 == 1) //ÅжÏT0ÊÇ·ñÒç³ö
{
TF0 = 0; //T0Òç³öºó£¬ÇåÁãÖжϱêÖ¾
TH0 = 0xB8; //²¢ÖØи³³õÖµ
TL0 = 0x00;
cnt++; //¼ÆÊýÖµ×Ô¼Ó1
if (cnt >= 50) //ÅжÏT0Òç³öÊÇ·ñ´ïµ½50´Î
{
cnt = 0; //´ïµ½50´Îºó¼ÆÊýÖµÇåÁã
P0 = LedChar[sec]; //µ±Ç°ÃëÊý¶ÔÓ¦µÄÕæÖµ±íÖеÄÖµË͵½P0¿Ú
if (sec > 0) //µ±ÃëÊý´óÓÚ0ʱ£¬¼õ1
{
sec--;
}
else //µ±ÃëÊýµÈÓÚ0ʱ£¬ÖØи³³õÖµ0x0F
{
sec = 0x0F;
}
}
}
}
}
|