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

📄 timerx8_8c-source.html

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

⌨️ 快捷键说明

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