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

📄 timerx8_8c-source.html

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

⌨️ 快捷键说明

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