单片机 【计数器】/定时器

 

 

16位寄存器=1111111111111111(2进制)=十进制(65535)

 

=======================================================总结==============================

 

工作模式1:

               

 

                    

                                      

                                    

 

 

 ==============================================================程序,20秒计时器,点亮一个灯

#include<reg52.h>
sbit du=P2^6;//¶ÎÑ¡¶Ë¿Ú
sbit we=P2^7;//λѡ¶Ë¿Ú
void delay(unsigned int x)
{
while(x--)
{
unsigned char i;
for(i=0;i<120;i++);
}

}    
unsigned char leddata[]={ 

0x3F, //"0"
0x06, //"1"
0x5B, //"2"
0x4F, //"3"
0x66, //"4"
0x6D, //"5"
0x7D, //"6"
0x07, //"7"
0x7F, //"8"
0x6F, //"9"
0x77, //"A"
0x7C, //"B"
0x39, //"C"
0x5E, //"D"
0x79, //"E"
0x71, //"F"
0x76, //"H"
0x38, //"L"
0x37, //"n"
0x3E, //"u"
0x73, //"P"
0x5C, //"o"
0x40, //"-"
0x00, //??
0x00 //???

};
void display(unsigned char i)
{ 
unsigned char shi,ge;
shi=i/10;
ge=i%10;
we=1; //´ò¿ªÎ»Ñ¡
P0=0xfe;
we=0; //¹Ø±Õλѡ

du=1;//´ò¿ª¶ÎÑ¡
P0=leddata[shi];
du=0;
delay(5);

we=1;
P0=0xfd;
we=0;

du=1;
P0=leddata[ge];
du=0;
delay(5);
}

void main()
{
unsigned char a;//50msÅжÏ
unsigned char b;
TR1=1;//Æô¶¯¶¨Ê±Æ÷
TMOD = 0x10;//²»ÄÜλѰַ,ËùÒÔÒªÓÃ16½øÖÆ£¬¹¤×÷ģʽ1
TH1 = 0x4b;
TL1=0xfc;//0x4bfc ¶¨Ê±50ms

while(1)
{
if(TF1 == 1)
{
TH1=0x4b;
TL1=0xfc;
TF1=0;
a++;//50ºÁÃë¼ÆÊý¼ÓÒ»
}
if(a==20)//ÅжÏÊÇ·ñ1s
{
a=0;//ÇåÁã±ãÓÚÏ´Î
b++;
}
if(b == 20)//20s¼ÆÊ±Æ
{
TR1=0;//ʱ¼äµ½
P1=0xfe;
}

display(b);
}
}

 

posted @ 2017-12-31 18:23  Justice-V  阅读(483)  评论(0)    收藏  举报