最近写了个LPC2387的定时器相关应用,把思路&代码写出来供参考。 需求:LPC2387实现0.5秒为周期的定时器
思路:以定时器2(Timer2)为例,利用定时器的计时功能即可,MR0寄存器设置0.5秒对应的计数,计数到后产生中断并充值MR0寄存器。
问题:解决0.5秒对应多少计数
定时器2对外设时钟(Peripheral Clock)进行计数,要实现0.5秒的定时周期,即MR0的计数值为外设时钟频率 (Fpclk) 的1/2。
外设时钟频率 = CPU频率/外设分频 系数
Fpclk = CCLK/PCLK_DIV
而CPU频率(CCLK)由晶振频率(Fin)和PLL配置以及CPU分频系数(CCLK_DIV)决定
CCLK = Fin * M *2 / (N* CCLK_DIV) *M和N是PLL寄存器的MSEL和NSEL值,参加Datasheet
由上可知 外设频率
Fpclk = Fin * M *2 / (N* CCLK_DIV* PCLK_DIV)
笔者LPC2387板子的相关参数如下:
晶振为14.7456MHZ, M=16, N=1, CCLK_DIV=8, PCLK_DIV=4, 可知Fpclk = 14745600 HZ,定时器的MR0应填入Fin/2 = 7372800
部分实现代码,仅供参考
- #include <LPC23XX.h>
- #include <time.h>
- #define PERIPHERAL_CLK (14745600)
- /***********************************************************
- * 初始化定时器Timer2
- ************************************************************/
- void init_timer2()
- {
- PCONP |= (0x1<<22); //确保功率控制打开Timer2!
- T2MR0 = <span style="font-family: Arial, Helvetica, sans-serif;">PERIPHERAL_CLK /2; //0.5秒</span>
- T2MCR = 3; /* Interrupt and Reset on MR0 */
- //设置中断向量组
- // Timer2: 对应的中断源位是bit26
- VICIntEnable |= (0x1<<26);
- VICVectAddr26 = (unsigned long)TIMER2_IRQHandler; //中断处理函数
- VICVectPriority26 = 3; //Timer2(26号中断源)优先级为3, 1~15,15优先级最低
- }
- <pre code_snippet_id="306286" snippet_file_name="blog_20140422_2_1063791" name="code"
class="cpp">/*********************************************************** - * 打开定时器Timer
- ************************************************************/</pre>void enable_timer( unsigned char timer_num )<br>
- {<br>
- if ( timer_num == 0 )<br>
- {<br>
- <span style="white-space:pre"></span> T0TCR = 1;<br>
- }<br>
- else
if ( timer_num == 1 )<br> - {<br>
- <span style="white-space:pre"></span> T1TCR = 1;<br>
- }<br>
- else
if ( timer_num == 2 )<br> - {<br>
- <span style="white-space:pre"></span> T2TCR = 1;<br>
- }<br>
- else
if ( timer_num == 3 )<br> - {<br>
- <span style="white-space:pre"></span> T3TCR = 1;<br>
- }<br>
- return;<br>
- }<br>
- <pre></pre>
- <p></p>
- <pre></pre>
- <pre code_snippet_id="306286" snippet_file_name="blog_20140422_3_4030959" name="code"
class="cpp"><pre code_snippet_id="306286" snippet_file_name="blog_20140422_3_4030959" name="code"
class="cpp">/*********************************************************** - * 关闭定时器Timer
- ************************************************************/</pre>void disable_timer( unsigned char timer_num )<br>
- {<br>
- if ( timer_num == 0 )<br>
- {<br>
- <span style="white-space:pre"></span> T0TCR = 0;<br>
- }<br>
- else
if ( timer_num == 1 )<br> - {<br>
- <span style="white-space:pre"></span> T1TCR = 0;<br>
- }<br>
- else
if ( timer_num == 2 )<br> - {<br>
- <span style="white-space:pre"></span> T2TCR = 0;<br>
- }<br>
- else
if ( timer_num == 3 )<br> - {<br>
- <span style="white-space:pre"></span> T3TCR = 0;<br>
- }<br>
- return;<br>
- }
- <pre></pre>
- <pre code_snippet_id="306286" snippet_file_name="blog_20140422_3_4030959" name="code"
class="cpp"><pre code_snippet_id="306286" snippet_file_name="blog_20140422_3_4030959"></pre><pre code_snippet_id="306286" snippet_file_name="blog_20140422_4_9505600" name="code"
class="cpp"><pre code_snippet_id="306286" snippet_file_name="blog_20140422_4_9505600" name="code"
class="cpp">/*********************************************************** - * 定时器2中断处理函数
- ************************************************************/</pre>void TIMER2_IRQHandler (void) __irq<br>
- { <br>
- T2IR = 1;<span style="white-space:pre"> </span>/* 写入1,清除Timer2的MR0中断 */<br>
- /* 周期性做的事 */<br>
- VICVectAddr = 0;<span style="white-space:pre"> </span>//别忘了这句,走出中断<br>
- return;<br>
- }<span style="font-family:Arial,Helvetica,sans-serif"> </span>
- <pre></pre>
- <p></p>
- <p> </p>
- <p></p>
- <pre></pre>
- <pre></pre>
- </pre></pre></pre>
|