Line No. | Rev | Author | Line |
---|---|---|---|
1 | 6 | kaklik | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
2 | <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> |
||
3 | <title>Procyon AVRlib: timerx8.c Source File</title> |
||
4 | <link href="dox.css" rel="stylesheet" type="text/css"> |
||
5 | </head><body> |
||
6 | <!-- Generated by Doxygen 1.4.2 --> |
||
7 | <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> |
||
8 | <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> |
||
9 | 00002 <span class="comment">//*****************************************************************************</span> |
||
10 | 00003 <span class="comment">//</span> |
||
11 | 00004 <span class="comment">// File Name : 'timerx8.c'</span> |
||
12 | 00005 <span class="comment">// Title : Timer function library for ATmegaXX8 Processors</span> |
||
13 | 00006 <span class="comment">// Author : Pascal Stang - Copyright (C) 2000-2005</span> |
||
14 | 00007 <span class="comment">// Created : 11/22/2000</span> |
||
15 | 00008 <span class="comment">// Revised : 06/15/2005</span> |
||
16 | 00009 <span class="comment">// Version : 1.0</span> |
||
17 | 00010 <span class="comment">// Target MCU : Atmel AVR Series</span> |
||
18 | 00011 <span class="comment">// Editor Tabs : 4</span> |
||
19 | 00012 <span class="comment">//</span> |
||
20 | 00013 <span class="comment">// This code is distributed under the GNU Public License</span> |
||
21 | 00014 <span class="comment">// which can be found at http://www.gnu.org/licenses/gpl.txt</span> |
||
22 | 00015 <span class="comment">//</span> |
||
23 | 00016 <span class="comment">//*****************************************************************************</span> |
||
24 | 00017 |
||
25 | 00018 <span class="preprocessor">#include <avr/io.h></span> |
||
26 | 00019 <span class="preprocessor">#include <avr/interrupt.h></span> |
||
27 | 00020 <span class="preprocessor">#include <avr/pgmspace.h></span> |
||
28 | 00021 <span class="preprocessor">#include <avr/sleep.h></span> |
||
29 | 00022 |
||
30 | 00023 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span> |
||
31 | 00024 <span class="preprocessor">#include "<a class="code" href="timerx8_8h.html">timerx8.h</a>"</span> |
||
32 | 00025 |
||
33 | 00026 <span class="comment">// Program ROM constants</span> |
||
34 | 00027 <span class="comment">// the prescale division values stored in order of timer control register index</span> |
||
35 | 00028 <span class="comment">// STOP, CLK, CLK/8, CLK/64, CLK/256, CLK/1024</span> |
||
36 | 00029 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> __attribute__ ((progmem)) TimerPrescaleFactor[] = {0,1,8,64,256,1024}; |
||
37 | 00030 <span class="comment">// the prescale division values stored in order of timer control register index</span> |
||
38 | 00031 <span class="comment">// STOP, CLK, CLK/8, CLK/32, CLK/64, CLK/128, CLK/256, CLK/1024</span> |
||
39 | 00032 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> __attribute__ ((progmem)) TimerRTCPrescaleFactor[] = {0,1,8,32,64,128,256,1024}; |
||
40 | 00033 |
||
41 | 00034 <span class="comment">// Global variables</span> |
||
42 | 00035 <span class="comment">// time registers</span> |
||
43 | 00036 <span class="keyword">volatile</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> TimerPauseReg; |
||
44 | 00037 <span class="keyword">volatile</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> Timer0Reg0; |
||
45 | 00038 <span class="keyword">volatile</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> Timer2Reg0; |
||
46 | 00039 |
||
47 | 00040 <span class="keyword">typedef</span> void (*voidFuncPtr)(void); |
||
48 | 00041 <span class="keyword">volatile</span> <span class="keyword">static</span> voidFuncPtr TimerIntFunc[TIMER_NUM_INTERRUPTS]; |
||
49 | 00042 |
||
50 | 00043 <span class="comment">// delay for a minimum of <us> microseconds </span> |
||
51 | 00044 <span class="comment">// the time resolution is dependent on the time the loop takes </span> |
||
52 | 00045 <span class="comment">// e.g. with 4Mhz and 5 cycles per loop, the resolution is 1.25 us </span> |
||
53 | 00046 <span class="keywordtype">void</span> delay_us(<span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> time_us) |
||
54 | 00047 { |
||
55 | 00048 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> delay_loops; |
||
56 | 00049 <span class="keyword">register</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> i; |
||
57 | 00050 |
||
58 | 00051 delay_loops = (time_us+3)/5*CYCLES_PER_US; <span class="comment">// +3 for rounding up (dirty) </span> |
||
59 | 00052 |
||
60 | 00053 <span class="comment">// one loop takes 5 cpu cycles </span> |
||
61 | 00054 <span class="keywordflow">for</span> (i=0; i < delay_loops; i++) {}; |
||
62 | 00055 } |
||
63 | 00056 <span class="comment">/*</span> |
||
64 | 00057 <span class="comment">void delay_ms(unsigned char time_ms)</span> |
||
65 | 00058 <span class="comment">{</span> |
||
66 | 00059 <span class="comment"> unsigned short delay_count = F_CPU / 4000;</span> |
||
67 | 00060 <span class="comment"></span> |
||
68 | 00061 <span class="comment"> unsigned short cnt;</span> |
||
69 | 00062 <span class="comment"> asm volatile ("\n"</span> |
||
70 | 00063 <span class="comment"> "L_dl1%=:\n\t"</span> |
||
71 | 00064 <span class="comment"> "mov %A0, %A2\n\t"</span> |
||
72 | 00065 <span class="comment"> "mov %B0, %B2\n"</span> |
||
73 | 00066 <span class="comment"> "L_dl2%=:\n\t"</span> |
||
74 | 00067 <span class="comment"> "sbiw %A0, 1\n\t"</span> |
||
75 | 00068 <span class="comment"> "brne L_dl2%=\n\t"</span> |
||
76 | 00069 <span class="comment"> "dec %1\n\t" "brne L_dl1%=\n\t":"=&w" (cnt)</span> |
||
77 | 00070 <span class="comment"> :"r"(time_ms), "r"((unsigned short) (delay_count))</span> |
||
78 | 00071 <span class="comment"> );</span> |
||
79 | 00072 <span class="comment">}</span> |
||
80 | 00073 <span class="comment">*/</span> |
||
81 | <a name="l00074"></a><a class="code" href="group__timer.html#ga1">00074</a> <span class="keywordtype">void</span> <a class="code" href="group__timer.html#ga1">timerInit</a>(<span class="keywordtype">void</span>) |
||
82 | 00075 { |
||
83 | 00076 u08 intNum; |
||
84 | 00077 <span class="comment">// detach all user functions from interrupts</span> |
||
85 | 00078 <span class="keywordflow">for</span>(intNum=0; intNum<TIMER_NUM_INTERRUPTS; intNum++) |
||
86 | 00079 <a class="code" href="group__timer.html#ga9">timerDetach</a>(intNum); |
||
87 | 00080 |
||
88 | 00081 <span class="comment">// initialize all timers</span> |
||
89 | 00082 <a class="code" href="group__timer.html#ga2">timer0Init</a>(); |
||
90 | 00083 <a class="code" href="group__timer.html#ga3">timer1Init</a>(); |
||
91 | 00084 <span class="preprocessor"> #ifdef TCNT2 // support timer2 only if it exists</span> |
||
92 | 00085 <span class="preprocessor"></span> timer2Init(); |
||
93 | 00086 <span class="preprocessor"> #endif</span> |
||
94 | 00087 <span class="preprocessor"></span> <span class="comment">// enable interrupts</span> |
||
95 | 00088 sei(); |
||
96 | 00089 } |
||
97 | 00090 |
||
98 | <a name="l00091"></a><a class="code" href="group__timer.html#ga2">00091</a> <span class="keywordtype">void</span> <a class="code" href="group__timer.html#ga2">timer0Init</a>() |
||
99 | 00092 { |
||
100 | 00093 <span class="comment">// initialize timer 0</span> |
||
101 | 00094 <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> |
||
102 | 00095 TCNT0 = 0; <span class="comment">// reset TCNT0</span> |
||
103 | 00096 sbi(TIMSK0, TOIE0); <span class="comment">// enable TCNT0 overflow interrupt</span> |
||
104 | 00097 |
||
105 | 00098 <a class="code" href="group__timer.html#ga11">timer0ClearOverflowCount</a>(); <span class="comment">// initialize time registers</span> |
||
106 | 00099 } |
||
107 | 00100 |
||
108 | <a name="l00101"></a><a class="code" href="group__timer.html#ga3">00101</a> <span class="keywordtype">void</span> <a class="code" href="group__timer.html#ga3">timer1Init</a>(<span class="keywordtype">void</span>) |
||
109 | 00102 { |
||
110 | 00103 <span class="comment">// initialize timer 1</span> |
||
111 | 00104 <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> |
||
112 | 00105 TCNT1 = 0; <span class="comment">// reset TCNT1</span> |
||
113 | 00106 sbi(TIMSK1, TOIE1); <span class="comment">// enable TCNT1 overflow</span> |
||
114 | 00107 } |
||
115 | 00108 |
||
116 | 00109 <span class="preprocessor">#ifdef TCNT2 // support timer2 only if it exists</span> |
||
117 | 00110 <span class="preprocessor"></span><span class="keywordtype">void</span> timer2Init(<span class="keywordtype">void</span>) |
||
118 | 00111 { |
||
119 | 00112 <span class="comment">// initialize timer 2</span> |
||
120 | 00113 <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> |
||
121 | 00114 TCNT2 = 0; <span class="comment">// reset TCNT2</span> |
||
122 | 00115 sbi(TIMSK2, TOIE2); <span class="comment">// enable TCNT2 overflow</span> |
||
123 | 00116 |
||
124 | 00117 timer2ClearOverflowCount(); <span class="comment">// initialize time registers</span> |
||
125 | 00118 } |
||
126 | 00119 <span class="preprocessor">#endif</span> |
||
127 | 00120 <span class="preprocessor"></span> |
||
128 | <a name="l00121"></a><a class="code" href="group__timer.html#ga4">00121</a> <span class="keywordtype">void</span> <a class="code" href="group__timer.html#ga4">timer0SetPrescaler</a>(u08 prescale) |
||
129 | 00122 { |
||
130 | 00123 <span class="comment">// set prescaler on timer 0</span> |
||
131 | 00124 TCCR0B = ((TCCR0B & ~<a class="code" href="group__timer.html#ga23">TIMER_PRESCALE_MASK</a>) | prescale); |
||
132 | 00125 } |
||
133 | 00126 |
||
134 | <a name="l00127"></a><a class="code" href="group__timer.html#ga6">00127</a> <span class="keywordtype">void</span> <a class="code" href="group__timer.html#ga6">timer1SetPrescaler</a>(u08 prescale) |
||
135 | 00128 { |
||
136 | 00129 <span class="comment">// set prescaler on timer 1</span> |
||
137 | 00130 TCCR1B = ((TCCR1B & ~<a class="code" href="group__timer.html#ga23">TIMER_PRESCALE_MASK</a>) | prescale); |
||
138 | 00131 } |
||
139 | 00132 |
||
140 | 00133 <span class="preprocessor">#ifdef TCNT2 // support timer2 only if it exists</span> |
||
141 | 00134 <span class="preprocessor"></span><span class="keywordtype">void</span> <a class="code" href="group__timer128.html#ga8">timer2SetPrescaler</a>(u08 prescale) |
||
142 | 00135 { |
||
143 | 00136 <span class="comment">// set prescaler on timer 2</span> |
||
144 | 00137 TCCR2B = ((TCCR2B & ~<a class="code" href="group__timer.html#ga23">TIMER_PRESCALE_MASK</a>) | prescale); |
||
145 | 00138 } |
||
146 | 00139 <span class="preprocessor">#endif</span> |
||
147 | 00140 <span class="preprocessor"></span> |
||
148 | <a name="l00141"></a><a class="code" href="group__timer.html#ga5">00141</a> u16 <a class="code" href="group__timer.html#ga5">timer0GetPrescaler</a>(<span class="keywordtype">void</span>) |
||
149 | 00142 { |
||
150 | 00143 <span class="comment">// get the current prescaler setting</span> |
||
151 | 00144 <span class="keywordflow">return</span> (pgm_read_word(TimerPrescaleFactor+(TCCR0B & <a class="code" href="group__timer.html#ga23">TIMER_PRESCALE_MASK</a>))); |
||
152 | 00145 } |
||
153 | 00146 |
||
154 | <a name="l00147"></a><a class="code" href="group__timer.html#ga7">00147</a> u16 <a class="code" href="group__timer.html#ga7">timer1GetPrescaler</a>(<span class="keywordtype">void</span>) |
||
155 | 00148 { |
||
156 | 00149 <span class="comment">// get the current prescaler setting</span> |
||
157 | 00150 <span class="keywordflow">return</span> (pgm_read_word(TimerPrescaleFactor+(TCCR1B & <a class="code" href="group__timer.html#ga23">TIMER_PRESCALE_MASK</a>))); |
||
158 | 00151 } |
||
159 | 00152 |
||
160 | 00153 <span class="preprocessor">#ifdef TCNT2 // support timer2 only if it exists</span> |
||
161 | 00154 <span class="preprocessor"></span>u16 <a class="code" href="group__timer128.html#ga12">timer2GetPrescaler</a>(<span class="keywordtype">void</span>) |
||
162 | 00155 { |
||
163 | 00156 <span class="comment">//TODO: can we assume for all 3-timer AVR processors,</span> |
||
164 | 00157 <span class="comment">// that timer2 is the RTC timer?</span> |
||
165 | 00158 |
||
166 | 00159 <span class="comment">// get the current prescaler setting</span> |
||
167 | 00160 <span class="keywordflow">return</span> (pgm_read_word(TimerRTCPrescaleFactor+(TCCR2B & <a class="code" href="group__timer.html#ga23">TIMER_PRESCALE_MASK</a>))); |
||
168 | 00161 } |
||
169 | 00162 <span class="preprocessor">#endif</span> |
||
170 | 00163 <span class="preprocessor"></span> |
||
171 | <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>) ) |
||
172 | 00165 { |
||
173 | 00166 <span class="comment">// make sure the interrupt number is within bounds</span> |
||
174 | 00167 <span class="keywordflow">if</span>(interruptNum < TIMER_NUM_INTERRUPTS) |
||
175 | 00168 { |
||
176 | 00169 <span class="comment">// set the interrupt function to run</span> |
||
177 | 00170 <span class="comment">// the supplied user's function</span> |
||
178 | 00171 TimerIntFunc[interruptNum] = userFunc; |
||
179 | 00172 } |
||
180 | 00173 } |
||
181 | 00174 |
||
182 | <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) |
||
183 | 00176 { |
||
184 | 00177 <span class="comment">// make sure the interrupt number is within bounds</span> |
||
185 | 00178 <span class="keywordflow">if</span>(interruptNum < TIMER_NUM_INTERRUPTS) |
||
186 | 00179 { |
||
187 | 00180 <span class="comment">// set the interrupt function to run nothing</span> |
||
188 | 00181 TimerIntFunc[interruptNum] = 0; |
||
189 | 00182 } |
||
190 | 00183 } |
||
191 | 00184 <span class="comment">/*</span> |
||
192 | 00185 <span class="comment">u32 timerMsToTics(u16 ms)</span> |
||
193 | 00186 <span class="comment">{</span> |
||
194 | 00187 <span class="comment"> // calculate the prescaler division rate</span> |
||
195 | 00188 <span class="comment"> u16 prescaleDiv = 1<<(pgm_read_byte(TimerPrescaleFactor+inb(TCCR0)));</span> |
||
196 | 00189 <span class="comment"> // calculate the number of timer tics in x milliseconds</span> |
||
197 | 00190 <span class="comment"> return (ms*(F_CPU/(prescaleDiv*256)))/1000;</span> |
||
198 | 00191 <span class="comment">}</span> |
||
199 | 00192 <span class="comment"></span> |
||
200 | 00193 <span class="comment">u16 timerTicsToMs(u32 tics)</span> |
||
201 | 00194 <span class="comment">{</span> |
||
202 | 00195 <span class="comment"> // calculate the prescaler division rate</span> |
||
203 | 00196 <span class="comment"> u16 prescaleDiv = 1<<(pgm_read_byte(TimerPrescaleFactor+inb(TCCR0)));</span> |
||
204 | 00197 <span class="comment"> // calculate the number of milliseconds in x timer tics</span> |
||
205 | 00198 <span class="comment"> return (tics*1000*(prescaleDiv*256))/F_CPU;</span> |
||
206 | 00199 <span class="comment">}</span> |
||
207 | 00200 <span class="comment">*/</span> |
||
208 | <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) |
||
209 | 00202 { |
||
210 | 00203 <span class="comment">// pauses for exactly <pause_ms> number of milliseconds</span> |
||
211 | 00204 u08 timerThres; |
||
212 | 00205 u32 ticRateHz; |
||
213 | 00206 u32 pause; |
||
214 | 00207 |
||
215 | 00208 <span class="comment">// capture current pause timer value</span> |
||
216 | 00209 timerThres = TCNT0; |
||
217 | 00210 <span class="comment">// reset pause timer overflow count</span> |
||
218 | 00211 TimerPauseReg = 0; |
||
219 | 00212 <span class="comment">// calculate delay for [pause_ms] milliseconds</span> |
||
220 | 00213 <span class="comment">// prescaler division = 1<<(pgm_read_byte(TimerPrescaleFactor+inb(TCCR0)))</span> |
||
221 | 00214 ticRateHz = F_CPU/<a class="code" href="group__timer.html#ga5">timer0GetPrescaler</a>(); |
||
222 | 00215 <span class="comment">// precision management</span> |
||
223 | 00216 <span class="comment">// prevent overflow and precision underflow</span> |
||
224 | 00217 <span class="comment">// -could add more conditions to improve accuracy</span> |
||
225 | 00218 <span class="keywordflow">if</span>( ((ticRateHz < 429497) && (pause_ms <= 10000)) ) |
||
226 | 00219 pause = (pause_ms*ticRateHz)/1000; |
||
227 | 00220 <span class="keywordflow">else</span> |
||
228 | 00221 pause = pause_ms*(ticRateHz/1000); |
||
229 | 00222 |
||
230 | 00223 <span class="comment">// loop until time expires</span> |
||
231 | 00224 <span class="keywordflow">while</span>( ((TimerPauseReg<<8) | (TCNT0)) < (pause+timerThres) ) |
||
232 | 00225 { |
||
233 | 00226 <span class="keywordflow">if</span>( TimerPauseReg < (pause>>8)); |
||
234 | 00227 { |
||
235 | 00228 <span class="comment">// save power by idling the processor</span> |
||
236 | 00229 set_sleep_mode(SLEEP_MODE_IDLE); |
||
237 | 00230 sleep_mode(); |
||
238 | 00231 } |
||
239 | 00232 } |
||
240 | 00233 |
||
241 | 00234 <span class="comment">/* old inaccurate code, for reference</span> |
||
242 | 00235 <span class="comment"> </span> |
||
243 | 00236 <span class="comment"> // calculate delay for [pause_ms] milliseconds</span> |
||
244 | 00237 <span class="comment"> u16 prescaleDiv = 1<<(pgm_read_byte(TimerPrescaleFactor+inb(TCCR0)));</span> |
||
245 | 00238 <span class="comment"> u32 pause = (pause_ms*(F_CPU/(prescaleDiv*256)))/1000;</span> |
||
246 | 00239 <span class="comment"> </span> |
||
247 | 00240 <span class="comment"> TimerPauseReg = 0;</span> |
||
248 | 00241 <span class="comment"> while(TimerPauseReg < pause);</span> |
||
249 | 00242 <span class="comment"></span> |
||
250 | 00243 <span class="comment"> */</span> |
||
251 | 00244 } |
||
252 | 00245 |
||
253 | <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>) |
||
254 | 00247 { |
||
255 | 00248 <span class="comment">// clear the timer overflow counter registers</span> |
||
256 | 00249 Timer0Reg0 = 0; <span class="comment">// initialize time registers</span> |
||
257 | 00250 } |
||
258 | 00251 |
||
259 | <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>) |
||
260 | 00253 { |
||
261 | 00254 <span class="comment">// return the current timer overflow count</span> |
||
262 | 00255 <span class="comment">// (this is since the last timer0ClearOverflowCount() command was called)</span> |
||
263 | 00256 <span class="keywordflow">return</span> Timer0Reg0; |
||
264 | 00257 } |
||
265 | 00258 |
||
266 | 00259 <span class="preprocessor">#ifdef TCNT2 // support timer2 only if it exists</span> |
||
267 | 00260 <span class="preprocessor"></span><span class="keywordtype">void</span> timer2ClearOverflowCount(<span class="keywordtype">void</span>) |
||
268 | 00261 { |
||
269 | 00262 <span class="comment">// clear the timer overflow counter registers</span> |
||
270 | 00263 Timer2Reg0 = 0; <span class="comment">// initialize time registers</span> |
||
271 | 00264 } |
||
272 | 00265 |
||
273 | 00266 <span class="keywordtype">long</span> timer2GetOverflowCount(<span class="keywordtype">void</span>) |
||
274 | 00267 { |
||
275 | 00268 <span class="comment">// return the current timer overflow count</span> |
||
276 | 00269 <span class="comment">// (this is since the last timer2ClearOverflowCount() command was called)</span> |
||
277 | 00270 <span class="keywordflow">return</span> Timer2Reg0; |
||
278 | 00271 } |
||
279 | 00272 <span class="preprocessor">#endif</span> |
||
280 | 00273 <span class="preprocessor"></span> |
||
281 | <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) |
||
282 | 00275 { |
||
283 | 00276 <span class="comment">// configures timer1 for use with PWM output</span> |
||
284 | 00277 <span class="comment">// on OC1A and OC1B pins</span> |
||
285 | 00278 |
||
286 | 00279 <span class="comment">// enable timer1 as 8,9,10bit PWM</span> |
||
287 | 00280 <span class="keywordflow">if</span>(bitRes == 9) |
||
288 | 00281 { <span class="comment">// 9bit mode</span> |
||
289 | 00282 sbi(TCCR1A,PWM11); |
||
290 | 00283 cbi(TCCR1A,PWM10); |
||
291 | 00284 } |
||
292 | 00285 <span class="keywordflow">else</span> <span class="keywordflow">if</span>( bitRes == 10 ) |
||
293 | 00286 { <span class="comment">// 10bit mode</span> |
||
294 | 00287 sbi(TCCR1A,PWM11); |
||
295 | 00288 sbi(TCCR1A,PWM10); |
||
296 | 00289 } |
||
297 | 00290 <span class="keywordflow">else</span> |
||
298 | 00291 { <span class="comment">// default 8bit mode</span> |
||
299 | 00292 cbi(TCCR1A,PWM11); |
||
300 | 00293 sbi(TCCR1A,PWM10); |
||
301 | 00294 } |
||
302 | 00295 |
||
303 | 00296 <span class="comment">// clear output compare value A</span> |
||
304 | 00297 OCR1A = 0; |
||
305 | 00298 <span class="comment">// clear output compare value B</span> |
||
306 | 00299 OCR1B = 0; |
||
307 | 00300 } |
||
308 | 00301 |
||
309 | 00302 <span class="preprocessor">#ifdef WGM10</span> |
||
310 | 00303 <span class="preprocessor"></span><span class="comment">// include support for arbitrary top-count PWM</span> |
||
311 | 00304 <span class="comment">// on new AVR processors that support it</span> |
||
312 | 00305 <span class="keywordtype">void</span> <a class="code" href="group__timerpwm.html#ga1">timer1PWMInitICR</a>(u16 topcount) |
||
313 | 00306 { |
||
314 | 00307 <span class="comment">// set PWM mode with ICR top-count</span> |
||
315 | 00308 cbi(TCCR1A,WGM10); |
||
316 | 00309 sbi(TCCR1A,WGM11); |
||
317 | 00310 sbi(TCCR1B,WGM12); |
||
318 | 00311 sbi(TCCR1B,WGM13); |
||
319 | 00312 |
||
320 | 00313 <span class="comment">// set top count value</span> |
||
321 | 00314 ICR1 = topcount; |
||
322 | 00315 |
||
323 | 00316 <span class="comment">// clear output compare value A</span> |
||
324 | 00317 OCR1A = 0; |
||
325 | 00318 <span class="comment">// clear output compare value B</span> |
||
326 | 00319 OCR1B = 0; |
||
327 | 00320 |
||
328 | 00321 } |
||
329 | 00322 <span class="preprocessor">#endif</span> |
||
330 | 00323 <span class="preprocessor"></span> |
||
331 | <a name="l00324"></a><a class="code" href="group__timerpwm.html#ga2">00324</a> <span class="keywordtype">void</span> <a class="code" href="group__timerpwm.html#ga2">timer1PWMOff</a>(<span class="keywordtype">void</span>) |
||
332 | 00325 { |
||
333 | 00326 <span class="comment">// turn off timer1 PWM mode</span> |
||
334 | 00327 cbi(TCCR1A,PWM11); |
||
335 | 00328 cbi(TCCR1A,PWM10); |
||
336 | 00329 <span class="comment">// set PWM1A/B (OutputCompare action) to none</span> |
||
337 | 00330 <a class="code" href="group__timerpwm.html#ga5">timer1PWMAOff</a>(); |
||
338 | 00331 <a class="code" href="group__timerpwm.html#ga6">timer1PWMBOff</a>(); |
||
339 | 00332 } |
||
340 | 00333 |
||
341 | <a name="l00334"></a><a class="code" href="group__timerpwm.html#ga3">00334</a> <span class="keywordtype">void</span> <a class="code" href="group__timerpwm.html#ga3">timer1PWMAOn</a>(<span class="keywordtype">void</span>) |
||
342 | 00335 { |
||
343 | 00336 <span class="comment">// turn on channel A (OC1A) PWM output</span> |
||
344 | 00337 <span class="comment">// set OC1A as non-inverted PWM</span> |
||
345 | 00338 sbi(TCCR1A,COM1A1); |
||
346 | 00339 cbi(TCCR1A,COM1A0); |
||
347 | 00340 } |
||
348 | 00341 |
||
349 | <a name="l00342"></a><a class="code" href="group__timerpwm.html#ga4">00342</a> <span class="keywordtype">void</span> <a class="code" href="group__timerpwm.html#ga4">timer1PWMBOn</a>(<span class="keywordtype">void</span>) |
||
350 | 00343 { |
||
351 | 00344 <span class="comment">// turn on channel B (OC1B) PWM output</span> |
||
352 | 00345 <span class="comment">// set OC1B as non-inverted PWM</span> |
||
353 | 00346 sbi(TCCR1A,COM1B1); |
||
354 | 00347 cbi(TCCR1A,COM1B0); |
||
355 | 00348 } |
||
356 | 00349 |
||
357 | <a name="l00350"></a><a class="code" href="group__timerpwm.html#ga5">00350</a> <span class="keywordtype">void</span> <a class="code" href="group__timerpwm.html#ga5">timer1PWMAOff</a>(<span class="keywordtype">void</span>) |
||
358 | 00351 { |
||
359 | 00352 <span class="comment">// turn off channel A (OC1A) PWM output</span> |
||
360 | 00353 <span class="comment">// set OC1A (OutputCompare action) to none</span> |
||
361 | 00354 cbi(TCCR1A,COM1A1); |
||
362 | 00355 cbi(TCCR1A,COM1A0); |
||
363 | 00356 } |
||
364 | 00357 |
||
365 | <a name="l00358"></a><a class="code" href="group__timerpwm.html#ga6">00358</a> <span class="keywordtype">void</span> <a class="code" href="group__timerpwm.html#ga6">timer1PWMBOff</a>(<span class="keywordtype">void</span>) |
||
366 | 00359 { |
||
367 | 00360 <span class="comment">// turn off channel B (OC1B) PWM output</span> |
||
368 | 00361 <span class="comment">// set OC1B (OutputCompare action) to none</span> |
||
369 | 00362 cbi(TCCR1A,COM1B1); |
||
370 | 00363 cbi(TCCR1A,COM1B0); |
||
371 | 00364 } |
||
372 | 00365 |
||
373 | <a name="l00366"></a><a class="code" href="group__timerpwm.html#ga7">00366</a> <span class="keywordtype">void</span> <a class="code" href="group__timerpwm.html#ga7">timer1PWMASet</a>(u16 pwmDuty) |
||
374 | 00367 { |
||
375 | 00368 <span class="comment">// set PWM (output compare) duty for channel A</span> |
||
376 | 00369 <span class="comment">// this PWM output is generated on OC1A pin</span> |
||
377 | 00370 <span class="comment">// NOTE: pwmDuty should be in the range 0-255 for 8bit PWM</span> |
||
378 | 00371 <span class="comment">// pwmDuty should be in the range 0-511 for 9bit PWM</span> |
||
379 | 00372 <span class="comment">// pwmDuty should be in the range 0-1023 for 10bit PWM</span> |
||
380 | 00373 <span class="comment">//outp( (pwmDuty>>8), OCR1AH); // set the high 8bits of OCR1A</span> |
||
381 | 00374 <span class="comment">//outp( (pwmDuty&0x00FF), OCR1AL); // set the low 8bits of OCR1A</span> |
||
382 | 00375 OCR1A = pwmDuty; |
||
383 | 00376 } |
||
384 | 00377 |
||
385 | <a name="l00378"></a><a class="code" href="group__timerpwm.html#ga8">00378</a> <span class="keywordtype">void</span> <a class="code" href="group__timerpwm.html#ga8">timer1PWMBSet</a>(u16 pwmDuty) |
||
386 | 00379 { |
||
387 | 00380 <span class="comment">// set PWM (output compare) duty for channel B</span> |
||
388 | 00381 <span class="comment">// this PWM output is generated on OC1B pin</span> |
||
389 | 00382 <span class="comment">// NOTE: pwmDuty should be in the range 0-255 for 8bit PWM</span> |
||
390 | 00383 <span class="comment">// pwmDuty should be in the range 0-511 for 9bit PWM</span> |
||
391 | 00384 <span class="comment">// pwmDuty should be in the range 0-1023 for 10bit PWM</span> |
||
392 | 00385 <span class="comment">//outp( (pwmDuty>>8), OCR1BH); // set the high 8bits of OCR1B</span> |
||
393 | 00386 <span class="comment">//outp( (pwmDuty&0x00FF), OCR1BL); // set the low 8bits of OCR1B</span> |
||
394 | 00387 OCR1B = pwmDuty; |
||
395 | 00388 } |
||
396 | 00389 <span class="comment"></span> |
||
397 | 00390 <span class="comment">//! Interrupt handler for tcnt0 overflow interrupt</span> |
||
398 | <a name="l00391"></a><a class="code" href="timerx8_8c.html#a27">00391</a> <span class="comment"></span>TIMER_INTERRUPT_HANDLER(SIG_OVERFLOW0) |
||
399 | 00392 { |
||
400 | 00393 Timer0Reg0++; <span class="comment">// increment low-order counter</span> |
||
401 | 00394 |
||
402 | 00395 <span class="comment">// increment pause counter</span> |
||
403 | 00396 TimerPauseReg++; |
||
404 | 00397 |
||
405 | 00398 <span class="comment">// if a user function is defined, execute it too</span> |
||
406 | 00399 <span class="keywordflow">if</span>(TimerIntFunc[TIMER0OVERFLOW_INT]) |
||
407 | 00400 TimerIntFunc[TIMER0OVERFLOW_INT](); |
||
408 | 00401 } |
||
409 | 00402 <span class="comment"></span> |
||
410 | 00403 <span class="comment">//! Interrupt handler for tcnt1 overflow interrupt</span> |
||
411 | <a name="l00404"></a><a class="code" href="timerx8_8c.html#a28">00404</a> <span class="comment"></span>TIMER_INTERRUPT_HANDLER(SIG_OVERFLOW1) |
||
412 | 00405 { |
||
413 | 00406 <span class="comment">// if a user function is defined, execute it</span> |
||
414 | 00407 <span class="keywordflow">if</span>(TimerIntFunc[TIMER1OVERFLOW_INT]) |
||
415 | 00408 TimerIntFunc[TIMER1OVERFLOW_INT](); |
||
416 | 00409 } |
||
417 | 00410 |
||
418 | 00411 <span class="preprocessor">#ifdef TCNT2 // support timer2 only if it exists</span> |
||
419 | 00412 <span class="preprocessor"></span><span class="comment">//! Interrupt handler for tcnt2 overflow interrupt</span> |
||
420 | 00413 <span class="comment"></span>TIMER_INTERRUPT_HANDLER(SIG_OVERFLOW2) |
||
421 | 00414 { |
||
422 | 00415 Timer2Reg0++; <span class="comment">// increment low-order counter</span> |
||
423 | 00416 |
||
424 | 00417 <span class="comment">// if a user function is defined, execute it</span> |
||
425 | 00418 <span class="keywordflow">if</span>(TimerIntFunc[TIMER2OVERFLOW_INT]) |
||
426 | 00419 TimerIntFunc[TIMER2OVERFLOW_INT](); |
||
427 | 00420 } |
||
428 | 00421 <span class="preprocessor">#endif</span> |
||
429 | 00422 <span class="preprocessor"></span> |
||
430 | 00423 <span class="preprocessor">#ifdef OCR0</span> |
||
431 | 00424 <span class="preprocessor"></span><span class="comment">// include support for Output Compare 0 for new AVR processors that support it</span><span class="comment"></span> |
||
432 | 00425 <span class="comment">//! Interrupt handler for OutputCompare0 match (OC0) interrupt</span> |
||
433 | 00426 <span class="comment"></span>TIMER_INTERRUPT_HANDLER(SIG_OUTPUT_COMPARE0) |
||
434 | 00427 { |
||
435 | 00428 <span class="comment">// if a user function is defined, execute it</span> |
||
436 | 00429 <span class="keywordflow">if</span>(TimerIntFunc[TIMER0OUTCOMPARE_INT]) |
||
437 | 00430 TimerIntFunc[TIMER0OUTCOMPARE_INT](); |
||
438 | 00431 } |
||
439 | 00432 <span class="preprocessor">#endif</span> |
||
440 | 00433 <span class="preprocessor"></span><span class="comment"></span> |
||
441 | 00434 <span class="comment">//! Interrupt handler for CutputCompare1A match (OC1A) interrupt</span> |
||
442 | <a name="l00435"></a><a class="code" href="timerx8_8c.html#a29">00435</a> <span class="comment"></span>TIMER_INTERRUPT_HANDLER(SIG_OUTPUT_COMPARE1A) |
||
443 | 00436 { |
||
444 | 00437 <span class="comment">// if a user function is defined, execute it</span> |
||
445 | 00438 <span class="keywordflow">if</span>(TimerIntFunc[TIMER1OUTCOMPAREA_INT]) |
||
446 | 00439 TimerIntFunc[TIMER1OUTCOMPAREA_INT](); |
||
447 | 00440 } |
||
448 | 00441 <span class="comment"></span> |
||
449 | 00442 <span class="comment">//! Interrupt handler for OutputCompare1B match (OC1B) interrupt</span> |
||
450 | <a name="l00443"></a><a class="code" href="timerx8_8c.html#a30">00443</a> <span class="comment"></span>TIMER_INTERRUPT_HANDLER(SIG_OUTPUT_COMPARE1B) |
||
451 | 00444 { |
||
452 | 00445 <span class="comment">// if a user function is defined, execute it</span> |
||
453 | 00446 <span class="keywordflow">if</span>(TimerIntFunc[TIMER1OUTCOMPAREB_INT]) |
||
454 | 00447 TimerIntFunc[TIMER1OUTCOMPAREB_INT](); |
||
455 | 00448 } |
||
456 | 00449 <span class="comment"></span> |
||
457 | 00450 <span class="comment">//! Interrupt handler for InputCapture1 (IC1) interrupt</span> |
||
458 | <a name="l00451"></a><a class="code" href="timerx8_8c.html#a31">00451</a> <span class="comment"></span>TIMER_INTERRUPT_HANDLER(SIG_INPUT_CAPTURE1) |
||
459 | 00452 { |
||
460 | 00453 <span class="comment">// if a user function is defined, execute it</span> |
||
461 | 00454 <span class="keywordflow">if</span>(TimerIntFunc[TIMER1INPUTCAPTURE_INT]) |
||
462 | 00455 TimerIntFunc[TIMER1INPUTCAPTURE_INT](); |
||
463 | 00456 } |
||
464 | 00457 <span class="comment"></span> |
||
465 | 00458 <span class="comment">//! Interrupt handler for OutputCompare2A match (OC2A) interrupt</span> |
||
466 | <a name="l00459"></a><a class="code" href="timerx8_8c.html#a32">00459</a> <span class="comment"></span>TIMER_INTERRUPT_HANDLER(SIG_OUTPUT_COMPARE2A) |
||
467 | 00460 { |
||
468 | 00461 <span class="comment">// if a user function is defined, execute it</span> |
||
469 | 00462 <span class="keywordflow">if</span>(TimerIntFunc[TIMER2OUTCOMPARE_INT]) |
||
470 | 00463 TimerIntFunc[TIMER2OUTCOMPARE_INT](); |
||
471 | 00464 } |
||
472 | 00465 <span class="comment"></span> |
||
473 | 00466 <span class="comment">//! Interrupt handler for OutputCompare2B match (OC2B) interrupt</span> |
||
474 | <a name="l00467"></a><a class="code" href="timerx8_8c.html#a33">00467</a> <span class="comment"></span>TIMER_INTERRUPT_HANDLER(SIG_OUTPUT_COMPARE2B) |
||
475 | 00468 { |
||
476 | 00469 <span class="comment">// if a user function is defined, execute it</span> |
||
477 | 00470 <span class="keywordflow">if</span>(TimerIntFunc[TIMER2OUTCOMPARE_INT]) |
||
478 | 00471 TimerIntFunc[TIMER2OUTCOMPARE_INT](); |
||
479 | 00472 } |
||
480 | </pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Oct 29 03:41:07 2006 for Procyon AVRlib by |
||
481 | <a href="http://www.doxygen.org/index.html"> |
||
482 | <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.2 </small></address> |
||
483 | </body> |
||
484 | </html> |
Powered by WebSVN v2.8.3