⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timer128_8c-source.html

📁 ATMEL的AVR单片机库文件
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"><title>Procyon AVRlib: timer128.c Source File</title><link href="dox.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.3.6 --><div class="qindex"><a class="qindex" href="main.html">Main&nbsp;Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data&nbsp;Structures</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="functions.html">Data&nbsp;Fields</a> | <a class="qindex" href="globals.html">Globals</a></div><h1>timer128.c</h1><a href="timer128_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001 <span class="comment">/*! \file timer128.c \brief System Timer function library for Mega128. */</span>00002 <span class="comment">//*****************************************************************************</span>00003 <span class="comment">//</span>00004 <span class="comment">// File Name    : 'timer128.c'</span>00005 <span class="comment">// Title        : System Timer function library for Mega128</span>00006 <span class="comment">// Author       : Pascal Stang - Copyright (C) 2000-2003</span>00007 <span class="comment">// Created      : 11/22/2000</span>00008 <span class="comment">// Revised      : 02/24/2003</span>00009 <span class="comment">// Version      : 1.2</span>00010 <span class="comment">// Target MCU   : Atmel AVR Series</span>00011 <span class="comment">// Editor Tabs  : 4</span>00012 <span class="comment">//</span>00013 <span class="comment">// This code is distributed under the GNU Public License</span>00014 <span class="comment">//      which can be found at http://www.gnu.org/licenses/gpl.txt</span>00015 <span class="comment">//</span>00016 <span class="comment">//*****************************************************************************</span>00017 00018 <span class="preprocessor">#ifndef WIN32</span>00019 <span class="preprocessor"></span><span class="preprocessor">    #include &lt;avr/io.h&gt;</span>00020 <span class="preprocessor">    #include &lt;avr/signal.h&gt;</span>00021 <span class="preprocessor">    #include &lt;avr/interrupt.h&gt;</span>00022 <span class="preprocessor">    #include &lt;avr/pgmspace.h&gt;</span>00023 <span class="preprocessor">    #include &lt;avr/sleep.h&gt;</span>00024 <span class="preprocessor">#endif</span>00025 <span class="preprocessor"></span>00026 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span>00027 <span class="preprocessor">#include "<a class="code" href="timer128_8h.html">timer128.h</a>"</span>00028 00029 <span class="comment">// Program ROM constants</span>00030 <span class="comment">// the prescale division values stored in order of timer control register index</span>00031 <span class="comment">// STOP, CLK, CLK/8, CLK/64, CLK/256, CLK/1024</span>00032 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> __attribute__ ((progmem)) TimerPrescaleFactor[] = {0,1,8,64,256,1024};00033 <span class="comment">// the prescale division values stored in order of timer control register index</span>00034 <span class="comment">// STOP, CLK, CLK/8, CLK/32, CLK/64, CLK/128, CLK/256, CLK/1024</span>00035 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> __attribute__ ((progmem)) TimerRTCPrescaleFactor[] = {0,1,8,32,64,128,256,1024};00036 00037 <span class="comment">// Global variables</span>00038 <span class="comment">// time registers</span>00039 <span class="keyword">volatile</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> TimerPauseReg;00040 <span class="keyword">volatile</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> Timer0Reg0;00041 <span class="keyword">volatile</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> Timer0Reg1;00042 <span class="keyword">volatile</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> Timer2Reg0;00043 <span class="keyword">volatile</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> Timer2Reg1;00044 00045 <span class="keyword">typedef</span> void (*voidFuncPtr)(<span class="keywordtype">void</span>);00046 <span class="keyword">volatile</span> <span class="keyword">static</span> voidFuncPtr TimerIntFunc[TIMER_NUM_INTERRUPTS];00047 00048 <span class="comment">// delay for a minimum of &lt;us&gt; microseconds </span>00049 <span class="comment">// the time resolution is dependent on the time the loop takes </span>00050 <span class="comment">// e.g. with 4Mhz and 5 cycles per loop, the resolution is 1.25 us </span>00051 <span class="keywordtype">void</span> delay(<span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> us) 00052 {00053     <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> delay_loops;00054     <span class="keyword">register</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> i;00055 00056     delay_loops = (us+3)/5*CYCLES_PER_US; <span class="comment">// +3 for rounding up (dirty) </span>00057 00058     <span class="comment">// one loop takes 5 cpu cycles </span>00059     <span class="keywordflow">for</span> (i=0; i &lt; delay_loops; i++) {};00060 } 00061 <a name="l00062"></a><a class="code" href="timer128_8c.html#a9">00062</a> <span class="keywordtype">void</span> <a class="code" href="timer_8c.html#a7">timerInit</a>(<span class="keywordtype">void</span>)00063 {00064     u08 intNum;00065     <span class="comment">// detach all user functions from interrupts</span>00066     <span class="keywordflow">for</span>(intNum=0; intNum&lt;TIMER_NUM_INTERRUPTS; intNum++)00067         <a class="code" href="timer128_8h.html#a53">timerDetach</a>(intNum);00068 00069     <span class="comment">// initialize all timers</span>00070     <a class="code" href="timer_8c.html#a8">timer0Init</a>();00071     <a class="code" href="timer_8c.html#a9">timer1Init</a>();00072     timer2Init();00073     timer3Init();00074     <span class="comment">// enable interrupts</span>00075     sei();00076 }00077 <a name="l00078"></a><a class="code" href="timer128_8c.html#a10">00078</a> <span class="keywordtype">void</span> <a class="code" href="timer_8c.html#a8">timer0Init</a>()00079 {00080     <span class="comment">// initialize timer 0</span>00081     <a class="code" href="timer128_8h.html#a44">timer0SetPrescaler</a>( <a class="code" href="timer_8h.html#a20">TIMER0PRESCALE</a> );   <span class="comment">// set prescaler</span>00082     outp(0, TCNT0);                         <span class="comment">// reset TCNT0</span>00083     sbi(TIMSK, TOIE0);                      <span class="comment">// enable TCNT0 overflow interrupt</span>00084 00085     <a class="code" href="timer_8c.html#a17">timer0ClearOverflowCount</a>();             <span class="comment">// initialize time registers</span>00086 }00087 <a name="l00088"></a><a class="code" href="timer128_8c.html#a11">00088</a> <span class="keywordtype">void</span> <a class="code" href="timer_8c.html#a9">timer1Init</a>(<span class="keywordtype">void</span>)00089 {00090     <span class="comment">// initialize timer 1</span>00091     <a class="code" href="timer128_8h.html#a45">timer1SetPrescaler</a>( <a class="code" href="timer_8h.html#a21">TIMER1PRESCALE</a> );   <span class="comment">// set prescaler</span>00092     outp(0, TCNT1H);                        <span class="comment">// reset TCNT1</span>00093     outp(0, TCNT1L);00094     sbi(TIMSK, TOIE1);                      <span class="comment">// enable TCNT1 overflow</span>00095 }00096 00097 <span class="keywordtype">void</span> timer2Init(<span class="keywordtype">void</span>)00098 {00099     <span class="comment">// initialize timer 2</span>00100     <a class="code" href="timer128_8h.html#a46">timer2SetPrescaler</a>( TIMER2PRESCALE );   <span class="comment">// set prescaler</span>00101     outp(0, TCNT2);                         <span class="comment">// reset TCNT2</span>00102     sbi(TIMSK, TOIE2);                      <span class="comment">// enable TCNT2 overflow</span>00103 00104     timer2ClearOverflowCount();             <span class="comment">// initialize time registers</span>00105 }00106 00107 <span class="keywordtype">void</span> timer3Init(<span class="keywordtype">void</span>)00108 {00109     <span class="comment">// initialize timer 3</span>00110     <a class="code" href="timer128_8h.html#a47">timer3SetPrescaler</a>( TIMER3PRESCALE );   <span class="comment">// set prescaler</span>00111     outp(0, TCNT3H);                        <span class="comment">// reset TCNT3</span>00112     outp(0, TCNT3L);00113     sbi(ETIMSK, TOIE3);                     <span class="comment">// enable TCNT3 overflow</span>00114 }00115 <a name="l00116"></a><a class="code" href="timer128_8c.html#a14">00116</a> <span class="keywordtype">void</span> <a class="code" href="timer128_8h.html#a44">timer0SetPrescaler</a>(u08 prescale)00117 {00118     <span class="comment">// set prescaler on timer 0</span>00119     outp( (inp(TCCR0) &amp; ~<a class="code" href="timer_8h.html#a10">TIMER_PRESCALE_MASK</a>) | prescale, TCCR0);00120 }00121 <a name="l00122"></a><a class="code" href="timer128_8c.html#a15">00122</a> <span class="keywordtype">void</span> <a class="code" href="timer128_8h.html#a45">timer1SetPrescaler</a>(u08 prescale)00123 {00124     <span class="comment">// set prescaler on timer 1</span>00125     outp( (inp(TCCR1B) &amp; ~<a class="code" href="timer_8h.html#a10">TIMER_PRESCALE_MASK</a>) | prescale, TCCR1B);00126 }00127 <a name="l00128"></a><a class="code" href="timer128_8h.html#a46">00128</a> <span class="keywordtype">void</span> <a class="code" href="timer128_8h.html#a46">timer2SetPrescaler</a>(u08 prescale)00129 {00130     <span class="comment">// set prescaler on timer 2</span>00131     outp( (inp(TCCR2) &amp; ~<a class="code" href="timer_8h.html#a10">TIMER_PRESCALE_MASK</a>) | prescale, TCCR2);00132 }00133 <a name="l00134"></a><a class="code" href="timer128_8h.html#a47">00134</a> <span class="keywordtype">void</span> <a class="code" href="timer128_8h.html#a47">timer3SetPrescaler</a>(u08 prescale)00135 {00136     <span class="comment">// set prescaler on timer 2</span>00137     outp( (inp(TCCR3B) &amp; ~<a class="code" href="timer_8h.html#a10">TIMER_PRESCALE_MASK</a>) | prescale, TCCR3B);00138 }00139 <a name="l00140"></a><a class="code" href="timer128_8c.html#a18">00140</a> u16 <a class="code" href="timer_8c.html#a12">timer0GetPrescaler</a>(<span class="keywordtype">void</span>)00141 {00142     <span class="comment">// get the current prescaler setting</span>00143     <span class="keywordflow">return</span> (pgm_read_word(TimerPrescaleFactor+(inp(TCCR0) &amp; <a class="code" href="timer_8h.html#a10">TIMER_PRESCALE_MASK</a>)));00144 }00145 <a name="l00146"></a><a class="code" href="timer128_8c.html#a19">00146</a> u16 <a class="code" href="timer_8c.html#a13">timer1GetPrescaler</a>(<span class="keywordtype">void</span>)00147 {00148     <span class="comment">// get the current prescaler setting</span>00149     <span class="keywordflow">return</span> (pgm_read_word(TimerPrescaleFactor+(inp(TCCR1B) &amp; <a class="code" href="timer_8h.html#a10">TIMER_PRESCALE_MASK</a>)));00150 }00151 <a name="l00152"></a><a class="code" href="timer128_8h.html#a50">00152</a> u16 <a class="code" href="timer128_8c.html#a20">timer2GetPrescaler</a>(<span class="keywordtype">void</span>)00153 {00154     <span class="comment">// get the current prescaler setting</span>00155     <span class="keywordflow">return</span> (pgm_read_word(TimerPrescaleFactor+(inp(TCCR2) &amp; <a class="code" href="timer_8h.html#a10">TIMER_PRESCALE_MASK</a>)));00156 }00157 <a name="l00158"></a><a class="code" href="timer128_8h.html#a51">00158</a> u16 <a class="code" href="timer128_8c.html#a21">timer3GetPrescaler</a>(<span class="keywordtype">void</span>)00159 {00160     <span class="comment">// get the current prescaler setting</span>00161     <span class="keywordflow">return</span> (pgm_read_word(TimerPrescaleFactor+(inp(TCCR3B) &amp; <a class="code" href="timer_8h.html#a10">TIMER_PRESCALE_MASK</a>)));00162 }00163 <a name="l00164"></a><a class="code" href="timer128_8c.html#a22">00164</a> <span class="keywordtype">void</span> <a class="code" href="timer128_8h.html#a52">timerAttach</a>(u08 interruptNum, <span class="keywordtype">void</span> (*userFunc)(<span class="keywordtype">void</span>) )00165 {00166     <span class="comment">// make sure the interrupt number is within bounds</span>00167     <span class="keywordflow">if</span>(interruptNum &lt; TIMER_NUM_INTERRUPTS)00168     {00169         <span class="comment">// set the interrupt function to run</span>00170         <span class="comment">// the supplied user's function</span>00171         TimerIntFunc[interruptNum] = userFunc;00172     }00173 }00174 <a name="l00175"></a><a class="code" href="timer128_8c.html#a23">00175</a> <span class="keywordtype">void</span> <a class="code" href="timer128_8h.html#a53">timerDetach</a>(u08 interruptNum)00176 {00177     <span class="comment">// make sure the interrupt number is within bounds</span>00178     <span class="keywordflow">if</span>(interruptNum &lt; TIMER_NUM_INTERRUPTS)00179     {00180         <span class="comment">// set the interrupt function to run nothing</span>00181         TimerIntFunc[interruptNum] = 0;00182     }00183 }00184 <a name="l00185"></a><a class="code" href="timer128_8c.html#a24">00185</a> <span class="keywordtype">void</span> <a class="code" href="timer128_8h.html#a54">timerPause</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> pause_ms)00186 {00187     <span class="comment">// pauses for exactly &lt;pause_ms&gt; number of milliseconds</span>00188     u08 timerThres;00189     u32 ticRateHz;00190     u32 pause;00191 00192     <span class="comment">// capture current pause timer value</span>00193     timerThres = inb(TCNT2);00194     <span class="comment">// reset pause timer overflow count</span>00195     TimerPauseReg = 0;00196     <span class="comment">// calculate delay for [pause_ms] milliseconds</span>00197     <span class="comment">// prescaler division = 1&lt;&lt;(PRG_RDB(TimerPrescaleFactor+inp(TCCR2)))</span>00198     ticRateHz = F_CPU/<a class="code" href="timer128_8c.html#a20">timer2GetPrescaler</a>();00199     <span class="comment">// precision management</span>00200     <span class="comment">// prevent overflow and precision underflow</span>00201     <span class="comment">//  -could add more conditions to improve accuracy</span>00202     <span class="keywordflow">if</span>( ((ticRateHz &lt; 429497) &amp;&amp; (pause_ms &lt;= 10000)) )00203         pause = (pause_ms*ticRateHz)/1000;00204     <span class="keywordflow">else</span>00205         pause = pause_ms*(ticRateHz/1000);00206     00207     <span class="comment">// loop until time expires</span>00208     <span class="keywordflow">while</span>( ((TimerPauseReg&lt;&lt;8) | inb(TCNT2)) &lt; (pause+timerThres) )00209     {00210         <span class="keywordflow">if</span>( TimerPauseReg &lt; (pause&gt;&gt;8));00211         {00212             <span class="comment">// save power by idling the processor</span>00213             set_sleep_mode(SLEEP_MODE_IDLE);00214             sleep_mode();00215         }00216     }00217 }00218 <a name="l00219"></a><a class="code" href="timer128_8c.html#a25">00219</a> <span class="keywordtype">void</span> <a class="code" href="timer_8c.html#a17">timer0ClearOverflowCount</a>(<span class="keywordtype">void</span>)00220 {00221     <span class="comment">// clear the timer overflow counter registers</span>00222     Timer0Reg0 = 0; <span class="comment">// initialize time registers</span>00223     Timer0Reg1 = 0; <span class="comment">// initialize time registers</span>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -