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

📄 timer_8c-source.html

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

⌨️ 快捷键说明

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