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

📄 timerx8_8c-source.html

📁 单片机的软件
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"><title>Procyon AVRlib: timerx8.c Source File</title><link href="dox.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.4.2 --><div class="qindex"><a class="qindex" href="main.html">Main&nbsp;Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data&nbsp;Structures</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="functions.html">Data&nbsp;Fields</a> | <a class="qindex" href="globals.html">Globals</a></div><h1>timerx8.c</h1><a href="timerx8_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*! \file timerx8.c \brief Timer function library for ATmegaXX8 Processors. */</span>00002 <span class="comment">//*****************************************************************************</span>00003 <span class="comment">//</span>00004 <span class="comment">// File Name    : 'timerx8.c'</span>00005 <span class="comment">// Title        : Timer function library for ATmegaXX8 Processors</span>00006 <span class="comment">// Author       : Pascal Stang - Copyright (C) 2000-2005</span>00007 <span class="comment">// Created      : 11/22/2000</span>00008 <span class="comment">// Revised      : 06/15/2005</span>00009 <span class="comment">// Version      : 1.0</span>00010 <span class="comment">// Target MCU   : Atmel AVR Series</span>00011 <span class="comment">// Editor Tabs  : 4</span>00012 <span class="comment">//</span>00013 <span class="comment">// This code is distributed under the GNU Public License</span>00014 <span class="comment">//      which can be found at http://www.gnu.org/licenses/gpl.txt</span>00015 <span class="comment">//</span>00016 <span class="comment">//*****************************************************************************</span>00017 00018 <span class="preprocessor">#include &lt;avr/io.h&gt;</span>00019 <span class="preprocessor">#include &lt;avr/signal.h&gt;</span>00020 <span class="preprocessor">#include &lt;avr/interrupt.h&gt;</span>00021 <span class="preprocessor">#include &lt;avr/pgmspace.h&gt;</span>00022 <span class="preprocessor">#include &lt;avr/sleep.h&gt;</span>00023 00024 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span>00025 <span class="preprocessor">#include "<a class="code" href="timerx8_8h.html">timerx8.h</a>"</span>00026 00027 <span class="comment">// Program ROM constants</span>00028 <span class="comment">// the prescale division values stored in order of timer control register index</span>00029 <span class="comment">// STOP, CLK, CLK/8, CLK/64, CLK/256, CLK/1024</span>00030 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> __attribute__ ((progmem)) TimerPrescaleFactor[] = {0,1,8,64,256,1024};00031 <span class="comment">// the prescale division values stored in order of timer control register index</span>00032 <span class="comment">// STOP, CLK, CLK/8, CLK/32, CLK/64, CLK/128, CLK/256, CLK/1024</span>00033 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> __attribute__ ((progmem)) TimerRTCPrescaleFactor[] = {0,1,8,32,64,128,256,1024};00034 00035 <span class="comment">// Global variables</span>00036 <span class="comment">// time registers</span>00037 <span class="keyword">volatile</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> TimerPauseReg;00038 <span class="keyword">volatile</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> Timer0Reg0;00039 <span class="keyword">volatile</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> Timer2Reg0;00040 00041 <span class="keyword">typedef</span> void (*voidFuncPtr)(void);00042 <span class="keyword">volatile</span> <span class="keyword">static</span> voidFuncPtr TimerIntFunc[TIMER_NUM_INTERRUPTS];00043 00044 <span class="comment">// delay for a minimum of &lt;us&gt; microseconds </span>00045 <span class="comment">// the time resolution is dependent on the time the loop takes </span>00046 <span class="comment">// e.g. with 4Mhz and 5 cycles per loop, the resolution is 1.25 us </span>00047 <span class="keywordtype">void</span> delay_us(<span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> time_us) 00048 {00049     <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> delay_loops;00050     <span class="keyword">register</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> i;00051 00052     delay_loops = (time_us+3)/5*CYCLES_PER_US; <span class="comment">// +3 for rounding up (dirty) </span>00053 00054     <span class="comment">// one loop takes 5 cpu cycles </span>00055     <span class="keywordflow">for</span> (i=0; i &lt; delay_loops; i++) {};00056 }00057 <span class="comment">/*</span>00058 <span class="comment">void delay_ms(unsigned char time_ms)</span>00059 <span class="comment">{</span>00060 <span class="comment">    unsigned short delay_count = F_CPU / 4000;</span>00061 <span class="comment"></span>00062 <span class="comment">    unsigned short cnt;</span>00063 <span class="comment">    asm volatile ("\n"</span>00064 <span class="comment">                  "L_dl1%=:\n\t"</span>00065 <span class="comment">                  "mov %A0, %A2\n\t"</span>00066 <span class="comment">                  "mov %B0, %B2\n"</span>00067 <span class="comment">                  "L_dl2%=:\n\t"</span>00068 <span class="comment">                  "sbiw %A0, 1\n\t"</span>00069 <span class="comment">                  "brne L_dl2%=\n\t"</span>00070 <span class="comment">                  "dec %1\n\t" "brne L_dl1%=\n\t":"=&amp;w" (cnt)</span>00071 <span class="comment">                  :"r"(time_ms), "r"((unsigned short) (delay_count))</span>00072 <span class="comment">    );</span>00073 <span class="comment">}</span>00074 <span class="comment">*/</span><a name="l00075"></a><a class="code" href="group__timer.html#ga1">00075</a> <span class="keywordtype">void</span> <a class="code" href="group__timer.html#ga1">timerInit</a>(<span class="keywordtype">void</span>)00076 {00077     u08 intNum;00078     <span class="comment">// detach all user functions from interrupts</span>00079     <span class="keywordflow">for</span>(intNum=0; intNum&lt;TIMER_NUM_INTERRUPTS; intNum++)00080         <a class="code" href="group__timer.html#ga9">timerDetach</a>(intNum);00081 00082     <span class="comment">// initialize all timers</span>00083     <a class="code" href="group__timer.html#ga2">timer0Init</a>();00084     <a class="code" href="group__timer.html#ga3">timer1Init</a>();00085 <span class="preprocessor">    #ifdef TCNT2    // support timer2 only if it exists</span>00086 <span class="preprocessor"></span>    timer2Init();00087 <span class="preprocessor">    #endif</span>00088 <span class="preprocessor"></span>    <span class="comment">// enable interrupts</span>00089     sei();00090 }00091 <a name="l00092"></a><a class="code" href="group__timer.html#ga2">00092</a> <span class="keywordtype">void</span> <a class="code" href="group__timer.html#ga2">timer0Init</a>()00093 {00094     <span class="comment">// initialize timer 0</span>00095     <a class="code" href="group__timer.html#ga4">timer0SetPrescaler</a>( <a class="code" href="group__timer.html#ga33">TIMER0PRESCALE</a> );   <span class="comment">// set prescaler</span>00096     TCNT0 = 0;                              <span class="comment">// reset TCNT0</span>00097     sbi(TIMSK0, TOIE0);                     <span class="comment">// enable TCNT0 overflow interrupt</span>00098 00099     <a class="code" href="group__timer.html#ga11">timer0ClearOverflowCount</a>();             <span class="comment">// initialize time registers</span>00100 }00101 <a name="l00102"></a><a class="code" href="group__timer.html#ga3">00102</a> <span class="keywordtype">void</span> <a class="code" href="group__timer.html#ga3">timer1Init</a>(<span class="keywordtype">void</span>)00103 {00104     <span class="comment">// initialize timer 1</span>00105     <a class="code" href="group__timer.html#ga6">timer1SetPrescaler</a>( <a class="code" href="group__timer.html#ga34">TIMER1PRESCALE</a> );   <span class="comment">// set prescaler</span>00106     TCNT1 = 0;                              <span class="comment">// reset TCNT1</span>00107     sbi(TIMSK1, TOIE1);                     <span class="comment">// enable TCNT1 overflow</span>00108 }00109 00110 <span class="preprocessor">#ifdef TCNT2    // support timer2 only if it exists</span>00111 <span class="preprocessor"></span><span class="keywordtype">void</span> timer2Init(<span class="keywordtype">void</span>)00112 {00113     <span class="comment">// initialize timer 2</span>00114     <a class="code" href="group__timer128.html#ga8">timer2SetPrescaler</a>( <a class="code" href="group__timer.html#ga35">TIMER2PRESCALE</a> );   <span class="comment">// set prescaler</span>00115     TCNT2 = 0;                              <span class="comment">// reset TCNT2</span>00116     sbi(TIMSK2, TOIE2);                     <span class="comment">// enable TCNT2 overflow</span>00117 00118     timer2ClearOverflowCount();             <span class="comment">// initialize time registers</span>00119 }00120 <span class="preprocessor">#endif</span>00121 <span class="preprocessor"></span><a name="l00122"></a><a class="code" href="group__timer.html#ga4">00122</a> <span class="keywordtype">void</span> <a class="code" href="group__timer.html#ga4">timer0SetPrescaler</a>(u08 prescale)00123 {00124     <span class="comment">// set prescaler on timer 0</span>00125     TCCR0B = ((TCCR0B &amp; ~<a class="code" href="group__timer.html#ga23">TIMER_PRESCALE_MASK</a>) | prescale);00126 }00127 <a name="l00128"></a><a class="code" href="group__timer.html#ga6">00128</a> <span class="keywordtype">void</span> <a class="code" href="group__timer.html#ga6">timer1SetPrescaler</a>(u08 prescale)00129 {00130     <span class="comment">// set prescaler on timer 1</span>00131     TCCR1B = ((TCCR1B &amp; ~<a class="code" href="group__timer.html#ga23">TIMER_PRESCALE_MASK</a>) | prescale);00132 }00133 00134 <span class="preprocessor">#ifdef TCNT2    // support timer2 only if it exists</span>00135 <span class="preprocessor"></span><span class="keywordtype">void</span> <a class="code" href="group__timer128.html#ga8">timer2SetPrescaler</a>(u08 prescale)00136 {00137     <span class="comment">// set prescaler on timer 2</span>00138     TCCR2B = ((TCCR2B &amp; ~<a class="code" href="group__timer.html#ga23">TIMER_PRESCALE_MASK</a>) | prescale);00139 }00140 <span class="preprocessor">#endif</span>00141 <span class="preprocessor"></span><a name="l00142"></a><a class="code" href="group__timer.html#ga5">00142</a> u16 <a class="code" href="group__timer.html#ga5">timer0GetPrescaler</a>(<span class="keywordtype">void</span>)00143 {00144     <span class="comment">// get the current prescaler setting</span>00145     <span class="keywordflow">return</span> (pgm_read_word(TimerPrescaleFactor+(TCCR0B &amp; <a class="code" href="group__timer.html#ga23">TIMER_PRESCALE_MASK</a>)));00146 }00147 <a name="l00148"></a><a class="code" href="group__timer.html#ga7">00148</a> u16 <a class="code" href="group__timer.html#ga7">timer1GetPrescaler</a>(<span class="keywordtype">void</span>)00149 {00150     <span class="comment">// get the current prescaler setting</span>00151     <span class="keywordflow">return</span> (pgm_read_word(TimerPrescaleFactor+(TCCR1B &amp; <a class="code" href="group__timer.html#ga23">TIMER_PRESCALE_MASK</a>)));00152 }00153 00154 <span class="preprocessor">#ifdef TCNT2    // support timer2 only if it exists</span>00155 <span class="preprocessor"></span>u16 <a class="code" href="group__timer128.html#ga12">timer2GetPrescaler</a>(<span class="keywordtype">void</span>)

⌨️ 快捷键说明

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