📄 timer128_8c-source.html
字号:
<!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: timer128.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 Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data Structures</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Data Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="pages.html">Related Pages</a></div><h1>timer128.c</h1><a href="timer128_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*! \file timer128.c \brief System Timer function library for Mega128. */</span>00002 <span class="comment">//*****************************************************************************</span>00003 <span class="comment">//</span>00004 <span class="comment">// File Name : 'timer128.c'</span>00005 <span class="comment">// Title : System Timer function library for Mega128</span>00006 <span class="comment">// Author : Pascal Stang - Copyright (C) 2000-2003</span>00007 <span class="comment">// Created : 11/22/2000</span>00008 <span class="comment">// Revised : 02/24/2003</span>00009 <span class="comment">// Version : 1.2</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">#ifndef WIN32</span>00019 <span class="preprocessor"></span><span class="preprocessor"> #include <avr/io.h></span>00020 <span class="preprocessor"> #include <avr/signal.h></span>00021 <span class="preprocessor"> #include <avr/interrupt.h></span>00022 <span class="preprocessor"> #include <avr/pgmspace.h></span>00023 <span class="preprocessor"> #include <avr/sleep.h></span>00024 <span class="preprocessor">#endif</span>00025 <span class="preprocessor"></span>00026 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span>00027 <span class="preprocessor">#include "<a class="code" href="timer128_8h.html">timer128.h</a>"</span>00028 00029 <span class="comment">// Program ROM constants</span>00030 <span class="comment">// the prescale division values stored in order of timer control register index</span>00031 <span class="comment">// STOP, CLK, CLK/8, CLK/64, CLK/256, CLK/1024</span>00032 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> __attribute__ ((progmem)) TimerPrescaleFactor[] = {0,1,8,64,256,1024};00033 <span class="comment">// the prescale division values stored in order of timer control register index</span>00034 <span class="comment">// STOP, CLK, CLK/8, CLK/32, CLK/64, CLK/128, CLK/256, CLK/1024</span>00035 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> __attribute__ ((progmem)) TimerRTCPrescaleFactor[] = {0,1,8,32,64,128,256,1024};00036 00037 <span class="comment">// Global variables</span>00038 <span class="comment">// time registers</span>00039 <span class="keyword">volatile</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> TimerPauseReg;00040 <span class="keyword">volatile</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> Timer0Reg0;00041 <span class="keyword">volatile</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> Timer0Reg1;00042 <span class="keyword">volatile</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> Timer2Reg0;00043 <span class="keyword">volatile</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> Timer2Reg1;00044 00045 <span class="keyword">typedef</span> void (*voidFuncPtr)(void);00046 <span class="keyword">volatile</span> <span class="keyword">static</span> voidFuncPtr TimerIntFunc[TIMER_NUM_INTERRUPTS];00047 00048 <span class="comment">// delay for a minimum of <us> microseconds </span>00049 <span class="comment">// the time resolution is dependent on the time the loop takes </span>00050 <span class="comment">// e.g. with 4Mhz and 5 cycles per loop, the resolution is 1.25 us </span>00051 <span class="keywordtype">void</span> delay_us(<span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> time_us) 00052 {00053 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> delay_loops;00054 <span class="keyword">register</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> i;00055 00056 delay_loops = (time_us+3)/5*CYCLES_PER_US; <span class="comment">// +3 for rounding up (dirty) </span>00057 00058 <span class="comment">// one loop takes 5 cpu cycles </span>00059 <span class="keywordflow">for</span> (i=0; i < delay_loops; i++) {};00060 }00061 <span class="comment">/*</span>00062 <span class="comment">void delay_ms(unsigned char time_ms)</span>00063 <span class="comment">{</span>00064 <span class="comment"> unsigned short delay_count = F_CPU / 4000;</span>00065 <span class="comment"></span>00066 <span class="comment"> unsigned short cnt;</span>00067 <span class="comment"> asm volatile ("\n"</span>00068 <span class="comment"> "L_dl1%=:\n\t"</span>00069 <span class="comment"> "mov %A0, %A2\n\t"</span>00070 <span class="comment"> "mov %B0, %B2\n"</span>00071 <span class="comment"> "L_dl2%=:\n\t"</span>00072 <span class="comment"> "sbiw %A0, 1\n\t"</span>00073 <span class="comment"> "brne L_dl2%=\n\t"</span>00074 <span class="comment"> "dec %1\n\t" "brne L_dl1%=\n\t":"=&w" (cnt)</span>00075 <span class="comment"> :"r"(time_ms), "r"((unsigned short) (delay_count))</span>00076 <span class="comment"> );</span>00077 <span class="comment">}</span>00078 <span class="comment">*/</span><a name="l00079"></a><a class="code" href="group__timer.html#ga1">00079</a> <span class="keywordtype">void</span> <a class="code" href="group__timer.html#ga1">timerInit</a>(<span class="keywordtype">void</span>)00080 {00081 u08 intNum;00082 <span class="comment">// detach all user functions from interrupts</span>00083 <span class="keywordflow">for</span>(intNum=0; intNum<TIMER_NUM_INTERRUPTS; intNum++)00084 <a class="code" href="group__timer.html#ga9">timerDetach</a>(intNum);00085 00086 <span class="comment">// initialize all timers</span>00087 <a class="code" href="group__timer.html#ga2">timer0Init</a>();00088 <a class="code" href="group__timer.html#ga3">timer1Init</a>();00089 timer2Init();00090 timer3Init();00091 <span class="comment">// enable interrupts</span>00092 sei();00093 }00094 <a name="l00095"></a><a class="code" href="group__timer.html#ga2">00095</a> <span class="keywordtype">void</span> <a class="code" href="group__timer.html#ga2">timer0Init</a>()00096 {00097 <span class="comment">// initialize timer 0</span>00098 <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>00099 outb(TCNT0, 0); <span class="comment">// reset TCNT0</span>00100 sbi(TIMSK, TOIE0); <span class="comment">// enable TCNT0 overflow interrupt</span>00101 00102 <a class="code" href="group__timer.html#ga11">timer0ClearOverflowCount</a>(); <span class="comment">// initialize time registers</span>00103 }00104 <a name="l00105"></a><a class="code" href="group__timer.html#ga3">00105</a> <span class="keywordtype">void</span> <a class="code" href="group__timer.html#ga3">timer1Init</a>(<span class="keywordtype">void</span>)00106 {00107 <span class="comment">// initialize timer 1</span>00108 <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>00109 outb(TCNT1H, 0); <span class="comment">// reset TCNT1</span>00110 outb(TCNT1L, 0);00111 sbi(TIMSK, TOIE1); <span class="comment">// enable TCNT1 overflow</span>00112 }00113 00114 <span class="keywordtype">void</span> timer2Init(<span class="keywordtype">void</span>)00115 {00116 <span class="comment">// initialize timer 2</span>00117 <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>00118 outb(TCNT2, 0); <span class="comment">// reset TCNT2</span>00119 sbi(TIMSK, TOIE2); <span class="comment">// enable TCNT2 overflow</span>00120 00121 timer2ClearOverflowCount(); <span class="comment">// initialize time registers</span>00122 }00123 00124 <span class="keywordtype">void</span> timer3Init(<span class="keywordtype">void</span>)00125 {00126 <span class="comment">// initialize timer 3</span>00127 <a class="code" href="group__timer128.html#ga9">timer3SetPrescaler</a>( <a class="code" href="group__timer128.html#ga66">TIMER3PRESCALE</a> ); <span class="comment">// set prescaler</span>00128 outb(TCNT3H, 0); <span class="comment">// reset TCNT3</span>00129 outb(TCNT3L, 0);00130 sbi(ETIMSK, TOIE3); <span class="comment">// enable TCNT3 overflow</span>00131 }00132 <a name="l00133"></a><a class="code" href="group__timer.html#ga4">00133</a> <span class="keywordtype">void</span> <a class="code" href="group__timer.html#ga4">timer0SetPrescaler</a>(u08 prescale)00134 {00135 <span class="comment">// set prescaler on timer 0</span>00136 outb(TCCR0, (inb(TCCR0) & ~<a class="code" href="group__timer.html#ga23">TIMER_PRESCALE_MASK</a>) | prescale);00137 }00138 <a name="l00139"></a><a class="code" href="group__timer.html#ga6">00139</a> <span class="keywordtype">void</span> <a class="code" href="group__timer.html#ga6">timer1SetPrescaler</a>(u08 prescale)00140 {00141 <span class="comment">// set prescaler on timer 1</span>00142 outb(TCCR1B, (inb(TCCR1B) & ~<a class="code" href="group__timer.html#ga23">TIMER_PRESCALE_MASK</a>) | prescale);00143 }00144 <a name="l00145"></a><a class="code" href="group__timer128.html#ga8">00145</a> <span class="keywordtype">void</span> <a class="code" href="group__timer128.html#ga8">timer2SetPrescaler</a>(u08 prescale)00146 {00147 <span class="comment">// set prescaler on timer 2</span>00148 outb(TCCR2, (inb(TCCR2) & ~<a class="code" href="group__timer.html#ga23">TIMER_PRESCALE_MASK</a>) | prescale);00149 }00150 <a name="l00151"></a><a class="code" href="group__timer128.html#ga9">00151</a> <span class="keywordtype">void</span> <a class="code" href="group__timer128.html#ga9">timer3SetPrescaler</a>(u08 prescale)00152 {00153 <span class="comment">// set prescaler on timer 2</span>00154 outb(TCCR3B, (inb(TCCR3B) & ~<a class="code" href="group__timer.html#ga23">TIMER_PRESCALE_MASK</a>) | prescale);00155 }00156 <a name="l00157"></a><a class="code" href="group__timer.html#ga5">00157</a> u16 <a class="code" href="group__timer.html#ga5">timer0GetPrescaler</a>(<span class="keywordtype">void</span>)00158 {00159 <span class="comment">// get the current prescaler setting</span>00160 <span class="keywordflow">return</span> (pgm_read_word(TimerPrescaleFactor+(inb(TCCR0) & <a class="code" href="group__timer.html#ga23">TIMER_PRESCALE_MASK</a>)));00161 }00162 <a name="l00163"></a><a class="code" href="group__timer.html#ga7">00163</a> u16 <a class="code" href="group__timer.html#ga7">timer1GetPrescaler</a>(<span class="keywordtype">void</span>)00164 {00165 <span class="comment">// get the current prescaler setting</span>00166 <span class="keywordflow">return</span> (pgm_read_word(TimerPrescaleFactor+(inb(TCCR1B) & <a class="code" href="group__timer.html#ga23">TIMER_PRESCALE_MASK</a>)));00167 }00168 <a name="l00169"></a><a class="code" href="group__timer128.html#ga12">00169</a> u16 <a class="code" href="group__timer128.html#ga12">timer2GetPrescaler</a>(<span class="keywordtype">void</span>)00170 {00171 <span class="comment">// get the current prescaler setting</span>00172 <span class="keywordflow">return</span> (pgm_read_word(TimerPrescaleFactor+(inb(TCCR2) & <a class="code" href="group__timer.html#ga23">TIMER_PRESCALE_MASK</a>)));00173 }00174 <a name="l00175"></a><a class="code" href="group__timer128.html#ga13">00175</a> u16 <a class="code" href="group__timer128.html#ga13">timer3GetPrescaler</a>(<span class="keywordtype">void</span>)00176 {00177 <span class="comment">// get the current prescaler setting</span>00178 <span class="keywordflow">return</span> (pgm_read_word(TimerPrescaleFactor+(inb(TCCR3B) & <a class="code" href="group__timer.html#ga23">TIMER_PRESCALE_MASK</a>)));00179 }00180 <a name="l00181"></a><a class="code" href="group__timer.html#ga8">00181</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>) )00182 {00183 <span class="comment">// make sure the interrupt number is within bounds</span>00184 <span class="keywordflow">if</span>(interruptNum < TIMER_NUM_INTERRUPTS)00185 {00186 <span class="comment">// set the interrupt function to run</span>00187 <span class="comment">// the supplied user's function</span>00188 TimerIntFunc[interruptNum] = userFunc;00189 }00190 }00191 <a name="l00192"></a><a class="code" href="group__timer.html#ga9">00192</a> <span class="keywordtype">void</span> <a class="code" href="group__timer.html#ga9">timerDetach</a>(u08 interruptNum)00193 {00194 <span class="comment">// make sure the interrupt number is within bounds</span>00195 <span class="keywordflow">if</span>(interruptNum < TIMER_NUM_INTERRUPTS)00196 {00197 <span class="comment">// set the interrupt function to run nothing</span>00198 TimerIntFunc[interruptNum] = 0;00199 }00200 }00201 <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 <pause_ms> 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 = inb(TCNT2);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<<(pgm_read_byte(TimerPrescaleFactor+inb(TCCR2)))</span>00215 ticRateHz = F_CPU/<a class="code" href="group__timer128.html#ga12">timer2GetPrescaler</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 < 429497) && (pause_ms <= 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<<8) | inb(TCNT2)) < (pause+timerThres) )00226 {00227 <span class="keywordflow">if</span>( TimerPauseReg < (pause>>8));00228 {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -