📄 定时计数器t0作定时应用.htm
字号:
<P class=style3 align=left><FONT face=宋体><a href='blog.asp?name=gooker'>首页(383)</a> <a href=blog.asp?subjectid=106&name=gooker>极酷软件(26)</a> <a href=blog.asp?subjectid=160&name=gooker>Proteus专栏(7)</a> <a href=blog.asp?subjectid=128&name=gooker>职业生涯(53)</a> <a href=blog.asp?subjectid=107&name=gooker>休闲娱乐(115)</a> <a href=blog.asp?subjectid=104&name=gooker>大史记(7)</a> <a href=blog.asp?subjectid=105&name=gooker>IT气象站(23)</a> <a href=blog.asp?subjectid=108&name=gooker>技术文章(148)</a> </FONT></P></TD></TR>
<TR bgColor=#ffffff>
<TD bgColor=#ffffff height=0>
<P align=left><SPAN class=style2><FONT face=宋体 size=2 target="_blank"><FONT color=#ff0000>[美文]</FONT> <FONT color=#ff9900>[</FONT><A href="more.asp?name=gooker&id=3839"><FONT color=#ff9900>感受读书的美妙诱惑</FONT></A><FONT color=#000000><FONT color=#ff9900>]</FONT> </FONT><FONT color=#ff6600>[</FONT><A href="more.asp?name=gooker&id=3842"><FONT color=#ff6600>超越完美</FONT></A><FONT color=#000000><FONT color=#ff6600>]</FONT> </FONT><FONT color=#ff3300>[</FONT><A href="more.asp?name=gooker&id=3776"><FONT color=#ff3300>给我一支烟</FONT></A><FONT color=#000000><FONT color=#ff3300>]</FONT> </FONT><FONT color=#ff6600>[</FONT><A href="more.asp?name=gooker&id=3600"><FONT color=#ff6600>有一种爱经不起等待</FONT></A><FONT color=#000000><FONT color=#ff6600>]</FONT> </FONT><FONT color=#ff9900>[</FONT><A href="more.asp?name=gooker&id=3778"><FONT color=#ff9900>够坚持,成功终归是你的</FONT></A><FONT color=#ff9900>]</FONT></FONT></SPAN><SPAN class=style2><FONT face=宋体 size=2><FONT color=#ff0033><FONT color=#000000><BR><FONT color=#ff0000>[推荐]</FONT> <FONT color=#ff0000><A href="more.asp?name=gooker&id=3240"><FONT color=#0099ff>[DX51传奇]</FONT></A> <FONT color=#0033ff>[</FONT><A href="more.asp?name=gooker&id=1251"><FONT color=#0033ff>单片机应用编程技巧</FONT></A><FONT color=#0033ff>]</FONT> </FONT><FONT color=#3300ff>[</FONT><A href="more.asp?name=gooker&id=1119"><FONT color=#3300ff>程序的书写规则</FONT></A><FONT color=#3300ff>]</FONT> <FONT color=#0033ff>[</FONT><A href="more.asp?name=gooker&id=1252"><FONT color=#0033ff>一位技術管理人20年的工作經歷和感悟</FONT></A></FONT></FONT><FONT color=#0033ff>]</FONT><BR><FONT color=#ff0000>[教程]</FONT> [<A href="more.asp?name=gooker&id=3168"><FONT color=#0033ff>虾米是怎样炼成的</FONT></A>] [<A href="more.asp?name=gooker&id=1526"><FONT color=#0000ff>如何快速掌握一款新的MCU</FONT></A>] [<A href="more.asp?name=gooker&id=1457"><FONT color=#0000ff>开发工程师人生之路</FONT></A>] [<A href="more.asp?name=gooker&id=4067"><FONT color=#0066ff>年轻工程师如何锻炼成高手的</FONT></A>]</SPAN></P></FONT></TD></TR>
<TR>
<TD id=""> 一个人的成就的大小,最关键的是你能否,时刻让你的心保持平静。如果你拥有一颗时时刻刻都能使你平静的心灵,那么在人生的漫漫旅途中你就不会再有迷茫,你会听到你内心的声音,会了解自己最需要的是什么!当一个人真正了解自己的人生需要的是什么时,一切都不会成为你成功的障碍,因为你真正的需要它,就象我们需要氧气一样!平静你的心吧,它会让你找到人生的方向,它会改变你的生活。</TD></TR></TBODY></TABLE><BR>
<SCRIPT type=text/javascript><!--
google_ad_client = "pub-1121460651525858";
google_ad_width = 468;
google_ad_height = 15;
google_ad_format = "468x15_0ads_al_s";
google_ad_channel ="";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "003366";
google_color_url = "003366";
google_color_text = "000000";
//--></SCRIPT>
<SCRIPT src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript>
</SCRIPT>
<BR>
<SCRIPT type=text/javascript><!--
google_ad_client = "pub-1121460651525858";
google_alternate_ad_url = "http://blog.21ic.org/uploadfile-/2005630185616187.gif";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text";
google_ad_channel ="";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_url = "008000";
google_color_text = "000000";
//--></SCRIPT>
<SCRIPT src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript>
</SCRIPT>
<META content=海涛的笔记,海涛的博客,blog,C程序,笔记,PIC单片机,中国电子网,休闲,学习,娱乐 name=key><BR>
<STYLE type=text/css>
<!--
.style4 {font-size: 12px}
body,td,th {
font-size: 12px;
}
.style6 {font-size: 12px; color: #999999; }
.style8 {
font-size: 12px;
color: #223355;
font-weight: bold;
}
-->
</STYLE>
<DIV align=right> </DIV>
<TABLE style="TABLE-LAYOUT: fixed; WORD-BREAK: break-all" cellSpacing=1 cellPadding=3 width="98%" bgColor=#cccccc border=0>
<TBODY>
<TR bgColor=#f8f8f8>
<TD vAlign=center bgColor=#e6e6e6><SPAN class=style8><IMG height=16 src="uploadfile-/200562118842686.gif" width=13 align=absMiddle> <a href=blog.asp?name=gooker&subjectid=108>[技术文章]<a href=more.asp?name=gooker&id=5485>定时计数器T0作定时应用技术(一)</a></SPAN></TD></TR>
<TR bgColor=#ffffff>
<TD height=0>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><SPAN class=style4>
<TABLE borderColor=#ffffff cellSpacing=0 width="90%" align=center border=1>
<TBODY>
<TR>
<TD>
<DIV align=center>
<P class=style24>AT89S51单片机试验及实践教程之定时计数器T0作定时应用技术(一)<BR><BR></P></DIV></TD></TR>
<TR>
<TD>
<P class=style28><STRONG>1. 实验任务 </STRONG></P>
<P class=style28>用 AT89S51 单片机的定时 / 计数器 T0 产生一秒的定时时间,作为秒计数时间,当一秒产生时,秒计数加 1 ,秒计数到 60 时,自动从 0 开始。硬件电路如下图所示 </P>
<P class=style28><STRONG>2. 电路原理图 </STRONG></P>
<P class=style28><a href=http://www.tengdamc.com/download/shiyanjc/images/151.jpg target=_blank><IMG height=396 src="http://www.tengdamc.com/download/shiyanjc/images/151.jpg" width=553 border='0' title='点击在新窗口查看原始图片' onload='java_script_:if(this.width>300)this.width=300'></a></P>
<P class=style28 align=center>图 4.15.1 </P>
<P class=style28><STRONG>3. 系统板上硬件连线 </STRONG></P>
<P class=style28>• 把“单片机系统”区域中的 P0.0/AD0 - P0.7/AD7 端口用 8 芯排线连接到“四路静态数码显示模块”区域中的任一个 a - h 端口上;要求: P0.0/AD0 对应着 a , P0.1/AD1 对应着 b , …… , P0.7/AD7 对应着 h 。 </P>
<P class=style28>• 把“单片机系统”区域中的 P2.0/A8 - P2.7/A15 端口用 8 芯排线连接到“四路静态数码显示模块”区域中的任一个 a - h 端口上;要求: P2.0/A8 对应着 a , P2.1/A9 对应着 b , …… , P2.7/A15 对应着 h 。 </P>
<P class=style28><STRONG>4. 程序设计内容 </STRONG></P>
<P class=style28>AT89S51 单片机的内部 16 位定时 / 计数器是一个可编程定时 / 计数器,它既可以工作在 13 位定时方式,也可以工作在 16 位定时方式和 8 位定时方式。只要通过设置特殊功能寄存器 TMOD ,即可完成。定时 / 计数器何时工作也是通过软件来设定 TCON 特殊功能寄存器来完成的。 </P>
<P class=style28>现在我们选择 16 位定时工作方式,对于 T0 来说,最大定时也只有 65536us ,即 65.536ms ,无法达到我们所需要的 1 秒的定时,因此,我们必须通过软件来处理这个问题,假设我们取 T0 的最大定时为 50ms ,即要定时 1 秒需要经过 20 次的 50ms 的定时。对于这 20 次我们就可以采用软件的方法来统计了。 </P>
<P class=style28>因此,我们设定 TMOD = 00000001B ,即 TMOD = 01H </P>
<P class=style28>下面我们要给 T0 定时 / 计数器的 TH0 , TL0 装入预置初值,通过下面的公式可以计算出 </P>
<P class=style28>TH0 =( 2 16 - 50000 ) / 256 </P>
<P class=style28>TL0 =( 2 16 - 50000 ) MOD 256 </P>
<P class=style28>当 T0 在工作的时候,我们如何得知 50ms 的定时时间已到,这回我们通过检测 TCON 特殊功能寄存器中的 TF0 标志位,如果 TF0 = 1 表示定时时间已到。 </P>
<P class=style28><STRONG>5. 程序框图 </STRONG></P>
<P class=style28><a href=http://www.tengdamc.com/download/shiyanjc/images/152.jpg target=_blank><IMG height=468 src="http://www.tengdamc.com/download/shiyanjc/images/152.jpg" width=353 border='0' title='点击在新窗口查看原始图片' onload='java_script_:if(this.width>300)this.width=300'></a></P>
<P class=style28 align=center>图 4.15.2 </P>
<P class=style28><STRONG>6. 汇编源程序(查询法) </STRONG></P>
<P class=style28>SECOND EQU 30H </P>
<P class=style28>TCOUNT EQU 31H </P>
<P class=style28>ORG 00H </P>
<P class=style28>START: MOV SECOND,#00H </P>
<P class=style28>MOV TCOUNT,#00H </P>
<P class=style28>MOV TMOD,#01H </P>
<P class=style28>MOV TH0,#(65536-50000) / 256 </P>
<P class=style28>MOV TL0,#(65536-50000) MOD 256 </P>
<P class=style28>SETB TR0 </P>
<P class=style28>DISP: MOV A,SECOND </P>
<P class=style28>MOV B,#10 </P>
<P class=style28>DIV AB </P>
<P class=style28>MOV DPTR,#TABLE </P>
<P class=style28>MOVC A,@A+DPTR </P>
<P class=style28>MOV P0,A </P>
<P class=style28>MOV A,B </P>
<P class=style28>MOVC A,@A+DPTR </P>
<P class=style28>MOV P2,A </P>
<P class=style28>WAIT: JNB TF0,WAIT </P>
<P class=style28>CLR TF0 </P>
<P class=style28>MOV TH0,#(65536-50000) / 256 </P>
<P class=style28>MOV TL0,#(65536-50000) MOD 256 </P>
<P class=style28>INC TCOUNT </P>
<P class=style28>MOV A,TCOUNT </P>
<P class=style28>CJNE A,#20,NEXT </P>
<P class=style28>MOV TCOUNT,#00H </P>
<P class=style28>INC SECOND </P>
<P class=style28>MOV A,SECOND </P>
<P class=style28>CJNE A,#60,NEX </P>
<P class=style28>MOV SECOND,#00H </P>
<P class=style28>NEX: LJMP DISP </P>
<P class=style28>NEXT: LJMP WAIT </P>
<P class=style28>TABLE: DB 3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH </P>
<P class=style28>END </P>
<P class=style28><STRONG>7. C语言源程序(查询法) </STRONG></P>
<P class=style28>#include <AT89X51.H> </P>
<P class=style28>unsigned char code dispcode[]={0x3f,0x06,0x5b,0x4f, </P>
<P class=style28>0x66,0x6d,0x7d,0x07, </P>
<P class=style28>0x7f,0x6f,0x77,0x7c, </P>
<P class=style28>0x39,0x5e,0x79,0x71,0x00}; </P>
<P class=style28>unsigned char second; </P>
<P class=style28>unsigned char tcount; </P>
<P class=style28>void main(void) </P>
<P class=style28>{ </P>
<P class=style28>TMOD=0x01; </P>
<P class=style28>TH0=(65536-50000)/256; </P>
<P class=style28>TL0=(65536-50000)%256; </P>
<P class=style28>TR0=1; </P>
<P class=style28>tcount=0; </P>
<P class=style28>second=0; </P>
<P class=style28>P0=dispcode[second/10]; </P>
<P class=style28>P2=dispcode[second%10]; </P>
<P class=style28>while(1) </P>
<P class=style28>{ </P>
<P class=style28>if(TF0==1) </P>
<P class=style28>{ </P>
<P class=style28>tcount++; </P>
<P class=style28>if(tcount==20) </P>
<P class=style28>{ </P>
<P class=style28>tcount=0; </P>
<P class=style28>second++; </P>
<P class=style28>if(second==60) </P>
<P class=style28>{ </P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -