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

📄 timer_8c-source.html

📁 avr应用测试程序
💻 HTML
📖 第 1 页 / 共 3 页
字号:
00155 00156 <span class="preprocessor">#ifdef TCNT2    // support timer2 only if it exists</span>00157 <span class="preprocessor"></span>u16 <a class="code" href="group__timer128.html#ga12">timer2GetPrescaler</a>(<span class="keywordtype">void</span>)00158 {00159     <span class="comment">//TODO: can we assume for all 3-timer AVR processors,</span>00160     <span class="comment">// that timer2 is the RTC timer?</span>00161 00162     <span class="comment">// get the current prescaler setting</span>00163     <span class="keywordflow">return</span> (pgm_read_word(TimerRTCPrescaleFactor+(inb(TCCR2) &amp; <a class="code" href="group__timer.html#ga23">TIMER_PRESCALE_MASK</a>)));00164 }00165 <span class="preprocessor">#endif</span>00166 <span class="preprocessor"></span><a name="l00167"></a><a class="code" href="group__timerx8.html#ga8">00167</a> <span class="keywordtype">void</span> <a class="code" href="group__timer.html#ga8">timerAttach</a>(u08 interruptNum, <span class="keywordtype">void</span> (*userFunc)(<span class="keywordtype">void</span>) )00168 {00169     <span class="comment">// make sure the interrupt number is within bounds</span>00170     <span class="keywordflow">if</span>(interruptNum &lt; TIMER_NUM_INTERRUPTS)00171     {00172         <span class="comment">// set the interrupt function to run</span>00173         <span class="comment">// the supplied user's function</span>00174         TimerIntFunc[interruptNum] = userFunc;00175     }00176 }00177 <a name="l00178"></a><a class="code" href="group__timerx8.html#ga9">00178</a> <span class="keywordtype">void</span> <a class="code" href="group__timer.html#ga9">timerDetach</a>(u08 interruptNum)00179 {00180     <span class="comment">// make sure the interrupt number is within bounds</span>00181     <span class="keywordflow">if</span>(interruptNum &lt; TIMER_NUM_INTERRUPTS)00182     {00183         <span class="comment">// set the interrupt function to run nothing</span>00184         TimerIntFunc[interruptNum] = 0;00185     }00186 }00187 <span class="comment">/*</span>00188 <span class="comment">u32 timerMsToTics(u16 ms)</span>00189 <span class="comment">{</span>00190 <span class="comment">    // calculate the prescaler division rate</span>00191 <span class="comment">    u16 prescaleDiv = 1&lt;&lt;(pgm_read_byte(TimerPrescaleFactor+inb(TCCR0)));</span>00192 <span class="comment">    // calculate the number of timer tics in x milliseconds</span>00193 <span class="comment">    return (ms*(F_CPU/(prescaleDiv*256)))/1000;</span>00194 <span class="comment">}</span>00195 <span class="comment"></span>00196 <span class="comment">u16 timerTicsToMs(u32 tics)</span>00197 <span class="comment">{</span>00198 <span class="comment">    // calculate the prescaler division rate</span>00199 <span class="comment">    u16 prescaleDiv = 1&lt;&lt;(pgm_read_byte(TimerPrescaleFactor+inb(TCCR0)));</span>00200 <span class="comment">    // calculate the number of milliseconds in x timer tics</span>00201 <span class="comment">    return (tics*1000*(prescaleDiv*256))/F_CPU;</span>00202 <span class="comment">}</span>00203 <span class="comment">*/</span><a name="l00204"></a><a class="code" href="group__timerx8.html#ga10">00204</a> <span class="keywordtype">void</span> <a class="code" href="group__timer.html#ga10">timerPause</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> pause_ms)00205 {00206     <span class="comment">// pauses for exactly &lt;pause_ms&gt; number of milliseconds</span>00207     u08 timerThres;00208     u32 ticRateHz;00209     u32 pause;00210 00211     <span class="comment">// capture current pause timer value</span>00212     timerThres = inb(TCNT0);00213     <span class="comment">// reset pause timer overflow count</span>00214     TimerPauseReg = 0;00215     <span class="comment">// calculate delay for [pause_ms] milliseconds</span>00216     <span class="comment">// prescaler division = 1&lt;&lt;(pgm_read_byte(TimerPrescaleFactor+inb(TCCR0)))</span>00217     ticRateHz = F_CPU/<a class="code" href="group__timer.html#ga5">timer0GetPrescaler</a>();00218     <span class="comment">// precision management</span>00219     <span class="comment">// prevent overflow and precision underflow</span>00220     <span class="comment">//  -could add more conditions to improve accuracy</span>00221     <span class="keywordflow">if</span>( ((ticRateHz &lt; 429497) &amp;&amp; (pause_ms &lt;= 10000)) )00222         pause = (pause_ms*ticRateHz)/1000;00223     <span class="keywordflow">else</span>00224         pause = pause_ms*(ticRateHz/1000);00225 00226     <span class="comment">// loop until time expires</span>00227     <span class="keywordflow">while</span>( ((TimerPauseReg&lt;&lt;8) | inb(TCNT0)) &lt; (pause+timerThres) )00228     {00229         <span class="keywordflow">if</span>( TimerPauseReg &lt; (pause&gt;&gt;8));00230         {00231             <span class="comment">// save power by idling the processor</span>00232             set_sleep_mode(SLEEP_MODE_IDLE);00233             sleep_mode();00234         }00235     }00236 00237     <span class="comment">/* old inaccurate code, for reference</span>00238 <span class="comment">    </span>00239 <span class="comment">    // calculate delay for [pause_ms] milliseconds</span>00240 <span class="comment">    u16 prescaleDiv = 1&lt;&lt;(pgm_read_byte(TimerPrescaleFactor+inb(TCCR0)));</span>00241 <span class="comment">    u32 pause = (pause_ms*(F_CPU/(prescaleDiv*256)))/1000;</span>00242 <span class="comment">    </span>00243 <span class="comment">    TimerPauseReg = 0;</span>00244 <span class="comment">    while(TimerPauseReg &lt; pause);</span>00245 <span class="comment"></span>00246 <span class="comment">    */</span>00247 }00248 <a name="l00249"></a><a class="code" href="group__timerx8.html#ga11">00249</a> <span class="keywordtype">void</span> <a class="code" href="group__timer.html#ga11">timer0ClearOverflowCount</a>(<span class="keywordtype">void</span>)00250 {00251     <span class="comment">// clear the timer overflow counter registers</span>00252     Timer0Reg0 = 0; <span class="comment">// initialize time registers</span>00253 }00254 <a name="l00255"></a><a class="code" href="group__timerx8.html#ga12">00255</a> <span class="keywordtype">long</span> <a class="code" href="group__timer.html#ga12">timer0GetOverflowCount</a>(<span class="keywordtype">void</span>)00256 {00257     <span class="comment">// return the current timer overflow count</span>00258     <span class="comment">// (this is since the last timer0ClearOverflowCount() command was called)</span>00259     <span class="keywordflow">return</span> Timer0Reg0;00260 }00261 00262 <span class="preprocessor">#ifdef TCNT2    // support timer2 only if it exists</span>00263 <span class="preprocessor"></span><span class="keywordtype">void</span> timer2ClearOverflowCount(<span class="keywordtype">void</span>)00264 {00265     <span class="comment">// clear the timer overflow counter registers</span>00266     Timer2Reg0 = 0; <span class="comment">// initialize time registers</span>00267 }00268 00269 <span class="keywordtype">long</span> timer2GetOverflowCount(<span class="keywordtype">void</span>)00270 {00271     <span class="comment">// return the current timer overflow count</span>00272     <span class="comment">// (this is since the last timer2ClearOverflowCount() command was called)</span>00273     <span class="keywordflow">return</span> Timer2Reg0;00274 }00275 <span class="preprocessor">#endif</span>00276 <span class="preprocessor"></span><a name="l00277"></a><a class="code" href="group__timerpwm.html#ga0">00277</a> <span class="keywordtype">void</span> <a class="code" href="group__timerpwm.html#ga0">timer1PWMInit</a>(u08 bitRes)00278 {00279     <span class="comment">// configures timer1 for use with PWM output</span>00280     <span class="comment">// on OC1A and OC1B pins</span>00281 00282     <span class="comment">// enable timer1 as 8,9,10bit PWM</span>00283     <span class="keywordflow">if</span>(bitRes == 9)00284     {   <span class="comment">// 9bit mode</span>00285         sbi(TCCR1A,PWM11);00286         cbi(TCCR1A,PWM10);00287     }00288     <span class="keywordflow">else</span> <span class="keywordflow">if</span>( bitRes == 10 )00289     {   <span class="comment">// 10bit mode</span>00290         sbi(TCCR1A,PWM11);00291         sbi(TCCR1A,PWM10);00292     }00293     <span class="keywordflow">else</span>00294     {   <span class="comment">// default 8bit mode</span>00295         cbi(TCCR1A,PWM11);00296         sbi(TCCR1A,PWM10);00297     }00298 00299     <span class="comment">// clear output compare value A</span>00300     outb(OCR1AH, 0);00301     outb(OCR1AL, 0);00302     <span class="comment">// clear output compare value B</span>00303     outb(OCR1BH, 0);00304     outb(OCR1BL, 0);00305 }00306 00307 <span class="preprocessor">#ifdef WGM10</span>00308 <span class="preprocessor"></span><span class="comment">// include support for arbitrary top-count PWM</span>00309 <span class="comment">// on new AVR processors that support it</span>00310 <span class="keywordtype">void</span> <a class="code" href="group__timerpwm.html#ga1">timer1PWMInitICR</a>(u16 topcount)00311 {00312     <span class="comment">// set PWM mode with ICR top-count</span>00313     cbi(TCCR1A,WGM10);00314     sbi(TCCR1A,WGM11);00315     sbi(TCCR1B,WGM12);

⌨️ 快捷键说明

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