智能车制作

标题: 使用定时器的输出比较产生定时中断代码 [打印本页]

作者: demon    时间: 2008-3-13 05:37
标题: 使用定时器的输出比较产生定时中断代码
<><FONT color=#2b6fd5 size=3></FONT>&nbsp;</P><P><FONT color=#2b6fd5 size=3>#include &lt;hidef.h&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* common defines and macros */<BR>#include &lt;mc9s12dg128.h&gt;&nbsp;&nbsp;&nbsp;&nbsp; /* derivative information */<BR>#pragma LINK_INFO DERIVATIVE "mc9s12dg128b"</FONT></P><P><FONT color=#2b6fd5 size=3></FONT>&nbsp;</P><P><FONT color=#2b6fd5 size=3>void main(void) {<BR>&nbsp; <BR>&nbsp; <BR>&nbsp;DDRB=0XFF;//PORTB设定为输出<BR>&nbsp;PORTB=0XFF;//PORTB初始化为全高<BR>&nbsp; </FONT></P><P><FONT color=#2b6fd5 size=3>&nbsp;TSCR1=0x90;//定时器使能且TFLG1能被自动清除</FONT></P><P><FONT color=#2b6fd5 size=3>&nbsp;</FONT><FONT color=#2b6fd5 size=3>TSCR2=0x07;//定时器预分频系数设定&nbsp; Prescale Factor=128<BR>&nbsp;TIE=0x01;//定时器通道0使能<BR>&nbsp;TIOS_IOS0=1;//定时器通道0设定为输出比较</FONT></P><P><FONT color=#2b6fd5 size=3>&nbsp; EnableInterrupts;//开中断</FONT></P><P><FONT color=#2b6fd5 size=3>&nbsp; for(;;) {} /* wait forever */<BR>&nbsp; /* please make sure that you never leave this function */<BR>}</FONT></P><P><FONT color=#2b6fd5 size=3></FONT>&nbsp;</P><P><BR><FONT color=#2b6fd5 size=3>/*<BR>*************************************************************************** <BR>*语法格式:interrupt void Timer(void)<BR>*实现功能ORTB小灯的闪烁<BR>*程序描述:<BR>**************************************************************************/<BR>void interrupt 8 Timer(void) //8为定时器通道0的中断标号<BR>{<BR>&nbsp;DisableInterrupts;&nbsp;&nbsp;//关中断 <BR>&nbsp;TC0=TCNT+19360;//F = Fosc / (TC*128) 中断频率设定<BR>&nbsp;PORTB++;//PORTB自增<BR>}&nbsp;&nbsp;<BR></FONT></P><P><FONT color=#2b6fd5 size=3>&nbsp;</P></FONT>
作者: wdcdd    时间: 2008-3-13 06:06
标题: Re:使用定时器的输出比较产生定时中断代码
<><FONT color=#2b6fd5 size=3>void interrupt 8 Timer0(void)</FONT></P><P><FONT color=#2b6fd5 size=3>我的程序里直接加这个编译不通过,为什么?在哪里还要设置什么吗?</FONT></P>
作者: demon    时间: 2008-3-13 06:09
标题: Re:使用定时器的输出比较产生定时中断代码
没有问题&nbsp; 不用加别的&nbsp; 可以通过
作者: sddof    时间: 2008-4-5 18:26
提示: 作者被禁止或删除 内容自动屏蔽
作者: 920lyj    时间: 2008-4-22 00:55
标题: Re:使用定时器的输出比较产生定时中断代码
谢谢楼主,帮我解决了这个几天都没弄好的问题!!!非常感谢!!!!<IMG src="http://www.intelligentcar.cn/images/Emotions/13.gif">
作者: liuchangyin    时间: 2008-5-23 02:12
标题: Re:使用定时器的输出比较产生定时中断代码
<>中断程序前缺少以下语句</P><P><FONT face=宋体 color=#e6421a size=7>#pragma CODE_SEG NON_BANKED</FONT></P>
作者: chenrunshe_007    时间: 2009-4-20 18:28
void interrupt 8 Timer(void) //8为定时器通道0的中断标号
{
DisableInterrupts;  //关中断
TC0=TCNT+19360;//F = Fosc / (TC*128) 中断频率设定
ORTB++;//PORTB自增
}  

1.“F = Fosc / (TC*128) 中断频率设定”中的Fosc是什么意思?2.中断频率是指每个一段时间它就中断,然后跳到中断函数那里执行是么?3.为什么是TC0=TCNT+19360,19360是怎么算出来的?
望高手解答




欢迎光临 智能车制作 (http://111.231.132.190/) Powered by Discuz! X3.2