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

📄 timer128_8c-source.html

📁 国外牛人公开的AVR代码
💻 HTML
📖 第 1 页 / 共 3 页
字号:
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 <a name="l00236"></a><a class="code" href="group__timer.html#ga11">00236</a> <span class="keywordtype">void</span> <a class="code" href="group__timer.html#ga11">timer0ClearOverflowCount</a>(<span class="keywordtype">void</span>)00237 {00238     <span class="comment">// clear the timer overflow counter registers</span>00239     Timer0Reg0 = 0; <span class="comment">// initialize time registers</span>00240     Timer0Reg1 = 0; <span class="comment">// initialize time registers</span>00241 }00242 <a name="l00243"></a><a class="code" href="group__timer.html#ga12">00243</a> <span class="keywordtype">long</span> <a class="code" href="group__timer.html#ga12">timer0GetOverflowCount</a>(<span class="keywordtype">void</span>)00244 {00245     <span class="comment">// return the current timer overflow count</span>00246     <span class="comment">// (this is since the last timer0ClearOverflowCount() command was called)</span>00247     <span class="keywordflow">return</span> Timer0Reg0;00248 }00249 00250 <span class="keywordtype">void</span> timer2ClearOverflowCount(<span class="keywordtype">void</span>)00251 {00252     <span class="comment">// clear the timer overflow counter registers</span>00253     Timer2Reg0 = 0; <span class="comment">// initialize time registers</span>00254     Timer2Reg1 = 0; <span class="comment">// initialize time registers</span>00255 }00256 00257 <span class="keywordtype">long</span> timer2GetOverflowCount(<span class="keywordtype">void</span>)00258 {00259     <span class="comment">// return the current timer overflow count</span>00260     <span class="comment">// (this is since the last timer2ClearOverflowCount() command was called)</span>00261     <span class="keywordflow">return</span> Timer2Reg0;00262 }00263 00264 <a name="l00265"></a><a class="code" href="group__timerpwm.html#ga0">00265</a> <span class="keywordtype">void</span> <a class="code" href="group__timerpwm.html#ga0">timer1PWMInit</a>(u08 bitRes)00266 {00267     <span class="comment">// configures timer1 for use with PWM output</span>00268     <span class="comment">// on pins OC1A, OC1B, and OC1C</span>00269 00270     <span class="comment">// enable Timer1 as 8,9,10bit PWM</span>00271     <span class="keywordflow">if</span>(bitRes == 9)00272     {   <span class="comment">// 9bit mode</span>00273         sbi(TCCR1A,WGMA1);00274         cbi(TCCR1A,WGMA0);00275     }00276     <span class="keywordflow">else</span> <span class="keywordflow">if</span>( bitRes == 10 )00277     {   <span class="comment">// 10bit mode</span>00278         sbi(TCCR1A,WGMA1);00279         sbi(TCCR1A,WGMA0);00280     }00281     <span class="keywordflow">else</span>00282     {   <span class="comment">// default 8bit mode</span>00283         cbi(TCCR1A,WGMA1);00284         sbi(TCCR1A,WGMA0);00285     }00286 00287     <span class="comment">// set clear-timer-on-compare-match</span>00288     <span class="comment">//cbi(TCCR1B,CTC1);</span>00289     <span class="comment">// clear output compare value A</span>00290     outb(OCR1AH, 0);00291     outb(OCR1AL, 0);00292     <span class="comment">// clear output compare value B</span>00293     outb(OCR1BH, 0);00294     outb(OCR1BL, 0);00295     <span class="comment">// clear output compare value C</span>00296     outb(OCR1CH, 0);00297     outb(OCR1CL, 0);00298 }00299 <a name="l00300"></a><a class="code" href="group__timer128.html#ga22">00300</a> <span class="keywordtype">void</span> <a class="code" href="group__timerpwm.html#ga1">timer1PWMInitICR</a>(u16 topcount)00301 {00302     <span class="comment">// set PWM mode with ICR top-count</span>00303     cbi(TCCR1A,WGM10);00304     sbi(TCCR1A,WGM11);00305     sbi(TCCR1B,WGM12);00306     sbi(TCCR1B,WGM13);00307     00308     <span class="comment">// set top count value</span>00309     ICR1H = (u08)(topcount&gt;&gt;8);00310     ICR1L = (u08)topcount;00311     00312     <span class="comment">// clear output compare value A</span>00313     outb(OCR1AH, 0);00314     outb(OCR1AL, 0);00315     <span class="comment">// clear output compare value B</span>00316     outb(OCR1BH, 0);00317     outb(OCR1BL, 0);00318     <span class="comment">// clear output compare value C</span>00319     outb(OCR1CH, 0);00320     outb(OCR1CL, 0);00321 }00322 <a name="l00323"></a><a class="code" href="group__timerpwm.html#ga2">00323</a> <span class="keywordtype">void</span> <a class="code" href="group__timerpwm.html#ga2">timer1PWMOff</a>(<span class="keywordtype">void</span>)00324 {00325     <span class="comment">// turn off PWM on Timer1</span>00326     cbi(TCCR1A,WGMA1);00327     cbi(TCCR1A,WGMA0);00328     <span class="comment">// clear (disable) clear-timer-on-compare-match</span>00329     <span class="comment">//cbi(TCCR1B,CTC1);</span>00330     <span class="comment">// set PWM1A/B/C (OutputCompare action) to none</span>00331     <a class="code" href="group__timerpwm.html#ga5">timer1PWMAOff</a>();00332     <a class="code" href="group__timerpwm.html#ga6">timer1PWMBOff</a>();00333     <a class="code" href="group__timer128.html#ga29">timer1PWMCOff</a>();00334 }00335 <a name="l00336"></a><a class="code" href="group__timerpwm.html#ga3">00336</a> <span class="keywordtype">void</span> <a class="code" href="group__timerpwm.html#ga3">timer1PWMAOn</a>(<span class="keywordtype">void</span>)00337 {00338     <span class="comment">// turn on channel A (OC1A) PWM output</span>00339     <span class="comment">// set OC1A as non-inverted PWM</span>00340     sbi(TCCR1A,COMA1);00341     cbi(TCCR1A,COMA0);00342 }00343 <a name="l00344"></a><a class="code" href="group__timerpwm.html#ga4">00344</a> <span class="keywordtype">void</span> <a class="code" href="group__timerpwm.html#ga4">timer1PWMBOn</a>(<span class="keywordtype">void</span>)00345 {00346     <span class="comment">// turn on channel B (OC1B) PWM output</span>00347     <span class="comment">// set OC1B as non-inverted PWM</span>00348     sbi(TCCR1A,COMB1);00349     cbi(TCCR1A,COMB0);00350 }00351 <a name="l00352"></a><a class="code" href="group__timer128.html#ga26">00352</a> <span class="keywordtype">void</span> <a class="code" href="group__timer128.html#ga26">timer1PWMCOn</a>(<span class="keywordtype">void</span>)00353 {00354     <span class="comment">// turn on channel C (OC1C) PWM output</span>00355     <span class="comment">// set OC1C as non-inverted PWM</span>00356     sbi(TCCR1A,COMC1);00357     cbi(TCCR1A,COMC0);00358 }00359 <a name="l00360"></a><a class="code" href="group__timerpwm.html#ga5">00360</a> <span class="keywordtype">void</span> <a class="code" href="group__timerpwm.html#ga5">timer1PWMAOff</a>(<span class="keywordtype">void</span>)00361 {00362     <span class="comment">// turn off channel A (OC1A) PWM output</span>00363     <span class="comment">// set OC1A (OutputCompare action) to none</span>00364     cbi(TCCR1A,COMA1);00365     cbi(TCCR1A,COMA0);00366 }00367 <a name="l00368"></a><a class="code" href="group__timerpwm.html#ga6">00368</a> <span class="keywordtype">void</span> <a class="code" href="group__timerpwm.html#ga6">timer1PWMBOff</a>(<span class="keywordtype">void</span>)00369 {00370     <span class="comment">// turn off channel B (OC1B) PWM output</span>00371     <span class="comment">// set OC1B (OutputCompare action) to none</span>00372     cbi(TCCR1A,COMB1);00373     cbi(TCCR1A,COMB0);00374 }00375 <a name="l00376"></a><a class="code" href="group__timer128.html#ga29">00376</a> <span class="keywordtype">void</span> <a class="code" href="group__timer128.html#ga29">timer1PWMCOff</a>(<span class="keywordtype">void</span>)00377 {00378     <span class="comment">// turn off channel C (OC1C) PWM output</span>00379     <span class="comment">// set OC1C (OutputCompare action) to none</span>00380     cbi(TCCR1A,COMC1);00381     cbi(TCCR1A,COMC0);00382 }00383 <a name="l00384"></a><a class="code" href="group__timerpwm.html#ga7">00384</a> <span class="keywordtype">void</span> <a class="code" href="group__timerpwm.html#ga7">timer1PWMASet</a>(u16 pwmDuty)00385 {00386     <span class="comment">// set PWM (output compare) duty for channel A</span>00387     <span class="comment">// this PWM output is generated on OC1A pin</span>00388     <span class="comment">// NOTE:    pwmDuty should be in the range 0-255 for 8bit PWM</span>00389     <span class="comment">//          pwmDuty should be in the range 0-511 for 9bit PWM</span>00390     <span class="comment">//          pwmDuty should be in the range 0-1023 for 10bit PWM</span>00391     outb(OCR1AH, (pwmDuty&gt;&gt;8));     <span class="comment">// set the high 8bits of OCR1A</span>00392     outb(OCR1AL, (pwmDuty&amp;0x00FF)); <span class="comment">// set the low 8bits of OCR1A</span>00393 }00394 <a name="l00395"></a><a class="code" href="group__timerpwm.html#ga8">00395</a> <span class="keywordtype">void</span> <a class="code" href="group__timerpwm.html#ga8">timer1PWMBSet</a>(u16 pwmDuty)00396 {00397     <span class="comment">// set PWM (output compare) duty for channel B</span>00398     <span class="comment">// this PWM output is generated on OC1B pin</span>00399     <span class="comment">// NOTE:    pwmDuty should be in the range 0-255 for 8bit PWM</span>00400     <span class="comment">//          pwmDuty should be in the range 0-511 for 9bit PWM</span>00401     <span class="comment">//          pwmDuty should be in the range 0-1023 for 10bit PWM</span>00402     outb(OCR1BH, (pwmDuty&gt;&gt;8));     <span class="comment">// set the high 8bits of OCR1B</span>00403     outb(OCR1BL, (pwmDuty&amp;0x00FF)); <span class="comment">// set the low 8bits of OCR1B</span>00404 }00405 <a name="l00406"></a><a class="code" href="group__timer128.html#ga32">00406</a> <span class="keywordtype">void</span> <a class="code" href="group__timer128.html#ga32">timer1PWMCSet</a>(u16 pwmDuty)00407 {00408     <span class="comment">// set PWM (output compare) duty for channel C</span>00409     <span class="comment">// this PWM output is generated on OC1C pin</span>00410     <span class="comment">// NOTE:    pwmDuty should be in the range 0-255 for 8bit PWM</span>00411     <span class="comment">//          pwmDuty should be in the range 0-511 for 9bit PWM</span>00412     <span class="comment">//          pwmDuty should be in the range 0-1023 for 10bit PWM</span>00413     outb(OCR1CH, (pwmDuty&gt;&gt;8));     <span class="comment">// set the high 8bits of OCR1C</span>00414     outb(OCR1CL, (pwmDuty&amp;0x00FF)); <span class="comment">// set the low 8bits of OCR1C</span>00415 }00416 00417 <a name="l00418"></a><a class="code" href="group__timer128.html#ga33">00418</a> <span class="keywordtype">void</span> <a class="code" href="group__timer128.html#ga33">timer3PWMInit</a>(u08 bitRes)00419 {00420     <span class="comment">// configures timer1 for use with PWM output</span>00421     <span class="comment">// on pins OC3A, OC3B, and OC3C</span>00422 00423     <span class="comment">// enable Timer3 as 8,9,10bit PWM</span>00424     <span class="keywordflow">if</span>(bitRes == 9)00425     {   <span class="comment">// 9bit mode</span>00426         sbi(TCCR3A,WGMA1);00427         cbi(TCCR3A,WGMA0);00428     }00429     <span class="keywordflow">else</span> <span class="keywordflow">if</span>( bitRes == 10 )00430     {   <span class="comment">// 10bit mode</span>00431         sbi(TCCR3A,WGMA1);00432         sbi(TCCR3A,WGMA0);00433     }00434     <span class="keywordflow">else</span>00435     {   <span class="comment">// default 8bit mode</span>00436         cbi(TCCR3A,WGMA1);00437         sbi(TCCR3A,WGMA0);00438     }00439 00440     <span class="comment">// set clear-timer-on-compare-match</span>00441     <span class="comment">//cbi(TCCR3B,CTC1);</span>00442     <span class="comment">// clear output compare value A</span>00443     outb(OCR3AH, 0);00444     outb(OCR3AL, 0);00445     <span class="comment">// clear output compare value B</span>00446     outb(OCR3BH, 0);00447     outb(OCR3BL, 0);00448     <span class="comment">// clear output compare value B</span>00449     outb(OCR3CH, 0);00450     outb(OCR3CL, 0);00451 }00452 <a name="l00453"></a><a class="code" href="group__timer128.html#ga34">00453</a> <span class="keywordtype">void</span> <a class="code" href="group__timer128.html#ga34">timer3PWMInitICR</a>(u16 topcount)00454 {00455     <span class="comment">// set PWM mode with ICR top-count</span>00456     cbi(TCCR3A,WGM30);00457     sbi(TCCR3A,WGM31);00458     sbi(TCCR3B,WGM32);00459     sbi(TCCR3B,WGM33);00460     00461     <span class="comment">// set top count value</span>00462     ICR3H = (u08)(topcount&gt;&gt;8);00463     ICR3L = (u08)topcount;

⌨️ 快捷键说明

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