| 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: pulse.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>pulse.c</h1><a href="pulse_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*! \file pulse.c \brief Pulse/frequency generation function library. */</span> |
||
| 9 | 00002 <span class="comment">//*****************************************************************************</span> |
||
| 10 | 00003 <span class="comment">//</span> |
||
| 11 | 00004 <span class="comment">// File Name : 'pulse.c'</span> |
||
| 12 | 00005 <span class="comment">// Title : Pulse/frequency generation function library</span> |
||
| 13 | 00006 <span class="comment">// Author : Pascal Stang - Copyright (C) 2000-2002</span> |
||
| 14 | 00007 <span class="comment">// Created : 2002-08-19</span> |
||
| 15 | 00008 <span class="comment">// Revised : 2003-05-29</span> |
||
| 16 | 00009 <span class="comment">// Version : 0.7</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 |
||
| 29 | 00022 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span> |
||
| 30 | 00023 <span class="preprocessor">#include "<a class="code" href="timer_8h.html">timer.h</a>"</span> |
||
| 31 | 00024 <span class="preprocessor">#include "<a class="code" href="pulse_8h.html">pulse.h</a>"</span> |
||
| 32 | 00025 |
||
| 33 | 00026 <span class="comment">// Global variables</span> |
||
| 34 | 00027 <span class="comment">// pulse generation registers</span> |
||
| 35 | 00028 <span class="keyword">volatile</span> <span class="keyword">static</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> PulseT1AMode; |
||
| 36 | 00029 <span class="keyword">volatile</span> <span class="keyword">static</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> PulseT1ACount; |
||
| 37 | 00030 <span class="keyword">volatile</span> <span class="keyword">static</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> PulseT1APeriodTics; |
||
| 38 | 00031 <span class="keyword">volatile</span> <span class="keyword">static</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> PulseT1BMode; |
||
| 39 | 00032 <span class="keyword">volatile</span> <span class="keyword">static</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> PulseT1BCount; |
||
| 40 | 00033 <span class="keyword">volatile</span> <span class="keyword">static</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> PulseT1BPeriodTics; |
||
| 41 | 00034 |
||
| 42 | 00035 <span class="comment">// pulse mode bit definitions</span> |
||
| 43 | 00036 <span class="comment">// PULSE_MODE_COUNTED</span> |
||
| 44 | 00037 <span class="comment">// if true, the requested number of pulses are output, then output is turned off</span> |
||
| 45 | 00038 <span class="comment">// if false, pulses are output continuously</span> |
||
| 46 | 00039 <span class="preprocessor">#define PULSE_MODE_CONTINUOUS 0x00</span> |
||
| 47 | 00040 <span class="preprocessor"></span><span class="preprocessor">#define PULSE_MODE_COUNTED 0x01</span> |
||
| 48 | 00041 <span class="preprocessor"></span> |
||
| 49 | 00042 <span class="comment">// functions</span> |
||
| 50 | 00043 |
||
| 51 | 00044 <span class="keywordtype">void</span> pulseInit(<span class="keywordtype">void</span>) |
||
| 52 | 00045 { |
||
| 53 | 00046 <span class="comment">// initialize timer1 for pulse operation</span> |
||
| 54 | 00047 pulseT1Init(); |
||
| 55 | 00048 } |
||
| 56 | 00049 |
||
| 57 | 00050 <span class="keywordtype">void</span> pulseT1Init(<span class="keywordtype">void</span>) |
||
| 58 | 00051 { |
||
| 59 | 00052 <span class="comment">// try to make sure that timer1 is in "normal" mode</span> |
||
| 60 | 00053 <span class="comment">// most importantly, turn off PWM mode</span> |
||
| 61 | 00054 <a class="code" href="group__timerpwm.html#ga2">timer1PWMOff</a>(); |
||
| 62 | 00055 |
||
| 63 | 00056 <span class="comment">// set some reasonable initial values</span> |
||
| 64 | 00057 <span class="comment">// in case the user forgets to</span> |
||
| 65 | 00058 PulseT1AMode = 0; |
||
| 66 | 00059 PulseT1BMode = 0; |
||
| 67 | 00060 PulseT1ACount = 0; |
||
| 68 | 00061 PulseT1BCount = 0; |
||
| 69 | 00062 PulseT1APeriodTics = 0x8000; |
||
| 70 | 00063 PulseT1BPeriodTics = 0x8000; |
||
| 71 | 00064 |
||
| 72 | 00065 <span class="comment">// attach the pulse service routines to</span> |
||
| 73 | 00066 <span class="comment">// the timer 1 output compare A and B interrupts</span> |
||
| 74 | 00067 <a class="code" href="group__timer.html#ga8">timerAttach</a>(TIMER1OUTCOMPAREA_INT,pulseT1AService); |
||
| 75 | 00068 <a class="code" href="group__timer.html#ga8">timerAttach</a>(TIMER1OUTCOMPAREB_INT,pulseT1BService); |
||
| 76 | 00069 } |
||
| 77 | 00070 |
||
| 78 | 00071 <span class="keywordtype">void</span> pulseT1Off(<span class="keywordtype">void</span>) |
||
| 79 | 00072 { |
||
| 80 | 00073 <span class="comment">// turns pulse outputs off immediately</span> |
||
| 81 | 00074 |
||
| 82 | 00075 <span class="comment">// set pulse counters to zero (finished)</span> |
||
| 83 | 00076 PulseT1ACount = 0; |
||
| 84 | 00077 PulseT1BCount = 0; |
||
| 85 | 00078 <span class="comment">// disconnect OutputCompare action from OC1A pin</span> |
||
| 86 | 00079 cbi(TCCR1A,COM1A1); |
||
| 87 | 00080 cbi(TCCR1A,COM1A0); |
||
| 88 | 00081 <span class="comment">// disconnect OutputCompare action from OC1B pin</span> |
||
| 89 | 00082 cbi(TCCR1A,COM1B1); |
||
| 90 | 00083 cbi(TCCR1A,COM1B0); |
||
| 91 | 00084 <span class="comment">// detach the pulse service routines</span> |
||
| 92 | 00085 <a class="code" href="group__timer.html#ga9">timerDetach</a>(TIMER1OUTCOMPAREA_INT); |
||
| 93 | 00086 <a class="code" href="group__timer.html#ga9">timerDetach</a>(TIMER1OUTCOMPAREB_INT); |
||
| 94 | 00087 } |
||
| 95 | 00088 |
||
| 96 | 00089 <span class="keywordtype">void</span> pulseT1ASetFreq(u16 freqHz) |
||
| 97 | 00090 { |
||
| 98 | 00091 <span class="comment">// set the frequency of the pulse output</span> |
||
| 99 | 00092 <span class="comment">// we need to find the requested period/2 (in timer tics)</span> |
||
| 100 | 00093 <span class="comment">// from the frequency (in hertz)</span> |
||
| 101 | 00094 |
||
| 102 | 00095 <span class="comment">// calculate how many tics in period/2</span> |
||
| 103 | 00096 <span class="comment">// this is the (timer tic rate)/(2*requested freq)</span> |
||
| 104 | 00097 PulseT1APeriodTics = ((u32)F_CPU/((u32)<a class="code" href="group__timer.html#ga7">timer1GetPrescaler</a>()*2*freqHz)); |
||
| 105 | 00098 } |
||
| 106 | 00099 |
||
| 107 | 00100 <span class="keywordtype">void</span> pulseT1BSetFreq(u16 freqHz) |
||
| 108 | 00101 { |
||
| 109 | 00102 <span class="comment">// set the frequency of the pulse output</span> |
||
| 110 | 00103 <span class="comment">// we need to find the requested period/2 (in timer tics)</span> |
||
| 111 | 00104 <span class="comment">// from the frequency (in hertz)</span> |
||
| 112 | 00105 |
||
| 113 | 00106 <span class="comment">// calculate how many tics in period/2</span> |
||
| 114 | 00107 <span class="comment">// this is the (timer tic rate)/(2*requested freq)</span> |
||
| 115 | 00108 PulseT1BPeriodTics = ((u32)F_CPU/((u32)<a class="code" href="group__timer.html#ga7">timer1GetPrescaler</a>()*2*freqHz)); |
||
| 116 | 00109 } |
||
| 117 | 00110 |
||
| 118 | 00111 <span class="keywordtype">void</span> pulseT1ARun(u16 nPulses) |
||
| 119 | 00112 { |
||
| 120 | 00113 <span class="comment">// set the number of pulses we want and the mode</span> |
||
| 121 | 00114 <span class="keywordflow">if</span>(nPulses) |
||
| 122 | 00115 { |
||
| 123 | 00116 <span class="comment">// if the nPulses is non-zero, use "counted" mode</span> |
||
| 124 | 00117 PulseT1AMode |= PULSE_MODE_COUNTED; |
||
| 125 | 00118 PulseT1ACount = nPulses<<1; |
||
| 126 | 00119 } |
||
| 127 | 00120 <span class="keywordflow">else</span> |
||
| 128 | 00121 { |
||
| 129 | 00122 <span class="comment">// if nPulses is zero, run forever</span> |
||
| 130 | 00123 PulseT1AMode &= ~PULSE_MODE_COUNTED; |
||
| 131 | 00124 PulseT1ACount = 1<<1; |
||
| 132 | 00125 } |
||
| 133 | 00126 <span class="comment">// set OutputCompare action to toggle OC1A pin</span> |
||
| 134 | 00127 cbi(TCCR1A,COM1A1); |
||
| 135 | 00128 sbi(TCCR1A,COM1A0); |
||
| 136 | 00129 |
||
| 137 | 00130 <span class="comment">// now the "enabling" stuff</span> |
||
| 138 | 00131 |
||
| 139 | 00132 <span class="comment">// set the output compare one pulse cycle ahead of current timer position </span> |
||
| 140 | 00133 <span class="comment">// to make sure we don't have to wait until the timer overflows and comes</span> |
||
| 141 | 00134 <span class="comment">// back to the current value</span> |
||
| 142 | 00135 <span class="comment">// set future output compare time to TCNT1 + PulseT1APeriodTics</span> |
||
| 143 | 00136 <span class="comment">//outw(OCR1A, inw(TCNT1) + PulseT1APeriodTics);</span> |
||
| 144 | 00137 OCR1A += PulseT1APeriodTics; |
||
| 145 | 00138 |
||
| 146 | 00139 <span class="comment">// enable OutputCompare interrupt</span> |
||
| 147 | 00140 sbi(TIMSK, OCIE1A); |
||
| 148 | 00141 } |
||
| 149 | 00142 |
||
| 150 | 00143 <span class="keywordtype">void</span> pulseT1BRun(u16 nPulses) |
||
| 151 | 00144 { |
||
| 152 | 00145 <span class="comment">// set the number of pulses we want and the mode</span> |
||
| 153 | 00146 <span class="keywordflow">if</span>(nPulses) |
||
| 154 | 00147 { |
||
| 155 | 00148 <span class="comment">// if the nPulses is non-zero, use "counted" mode</span> |
||
| 156 | 00149 PulseT1BMode |= PULSE_MODE_COUNTED; |
||
| 157 | 00150 PulseT1BCount = nPulses<<1; |
||
| 158 | 00151 } |
||
| 159 | 00152 <span class="keywordflow">else</span> |
||
| 160 | 00153 { |
||
| 161 | 00154 <span class="comment">// if nPulses is zero, run forever</span> |
||
| 162 | 00155 PulseT1BMode &= ~PULSE_MODE_COUNTED; |
||
| 163 | 00156 PulseT1BCount = 1<<1; |
||
| 164 | 00157 } |
||
| 165 | 00158 <span class="comment">// set OutputCompare action to toggle OC1B pin</span> |
||
| 166 | 00159 <span class="comment">// (note: with all the A's and B's flying around, TCCR1A is not a bug)</span> |
||
| 167 | 00160 cbi(TCCR1A,COM1B1); |
||
| 168 | 00161 sbi(TCCR1A,COM1B0); |
||
| 169 | 00162 |
||
| 170 | 00163 <span class="comment">// now the "enabling" stuff</span> |
||
| 171 | 00164 |
||
| 172 | 00165 <span class="comment">// set the output compare one pulse cycle ahead of current timer position </span> |
||
| 173 | 00166 <span class="comment">// to make sure we don't have to wait until the timer overflows and comes</span> |
||
| 174 | 00167 <span class="comment">// back to the current value</span> |
||
| 175 | 00168 <span class="comment">// set future output compare time to TCNT1 + PulseT1APeriodTics</span> |
||
| 176 | 00169 <span class="comment">//outw(OCR1B, inw(TCNT1) + PulseT1BPeriodTics);</span> |
||
| 177 | 00170 OCR1B += PulseT1BPeriodTics; |
||
| 178 | 00171 |
||
| 179 | 00172 <span class="comment">// enable OutputCompare interrupt</span> |
||
| 180 | 00173 sbi(TIMSK, OCIE1B); |
||
| 181 | 00174 } |
||
| 182 | 00175 |
||
| 183 | 00176 <span class="keywordtype">void</span> pulseT1AStop(<span class="keywordtype">void</span>) |
||
| 184 | 00177 { |
||
| 185 | 00178 <span class="comment">// stop output regardless of remaining pulses or mode</span> |
||
| 186 | 00179 <span class="comment">// go to "counted" mode</span> |
||
| 187 | 00180 PulseT1AMode |= PULSE_MODE_COUNTED; |
||
| 188 | 00181 <span class="comment">// set pulses to zero</span> |
||
| 189 | 00182 PulseT1ACount = 0; |
||
| 190 | 00183 } |
||
| 191 | 00184 |
||
| 192 | 00185 <span class="keywordtype">void</span> pulseT1BStop(<span class="keywordtype">void</span>) |
||
| 193 | 00186 { |
||
| 194 | 00187 <span class="comment">// stop output regardless of remaining pulses or mode</span> |
||
| 195 | 00188 <span class="comment">// go to "counted" mode</span> |
||
| 196 | 00189 PulseT1BMode |= PULSE_MODE_COUNTED; |
||
| 197 | 00190 <span class="comment">// set pulses to zero</span> |
||
| 198 | 00191 PulseT1BCount = 0; |
||
| 199 | 00192 } |
||
| 200 | 00193 |
||
| 201 | 00194 u16 pulseT1ARemaining(<span class="keywordtype">void</span>) |
||
| 202 | 00195 { |
||
| 203 | 00196 <span class="comment">// return the number of pulses remaining for channel A</span> |
||
| 204 | 00197 <span class="comment">// add 1 to make sure we round up, >>1 equivalent to /2</span> |
||
| 205 | 00198 <span class="keywordflow">return</span> (PulseT1ACount+1)>>1; |
||
| 206 | 00199 } |
||
| 207 | 00200 |
||
| 208 | 00201 u16 pulseT1BRemaining(<span class="keywordtype">void</span>) |
||
| 209 | 00202 { |
||
| 210 | 00203 <span class="comment">// return the number of pulses remaining for channel A</span> |
||
| 211 | 00204 <span class="comment">// add 1 to make sure we round up, >>1 equivalent to /2</span> |
||
| 212 | 00205 <span class="keywordflow">return</span> (PulseT1BCount+1)>>1; |
||
| 213 | 00206 } |
||
| 214 | 00207 |
||
| 215 | 00208 <span class="keywordtype">void</span> pulseT1AService(<span class="keywordtype">void</span>) |
||
| 216 | 00209 { |
||
| 217 | 00210 <span class="comment">// check if TimerPulseACount is non-zero</span> |
||
| 218 | 00211 <span class="comment">// (i.e. pulses are still requested)</span> |
||
| 219 | 00212 <span class="keywordflow">if</span>(PulseT1ACount) |
||
| 220 | 00213 { |
||
| 221 | 00214 <span class="comment">//u16 OCValue;</span> |
||
| 222 | 00215 <span class="comment">// read in current value of output compare register OCR1A</span> |
||
| 223 | 00216 <span class="comment">//OCValue = inp(OCR1AL); // read low byte of OCR1A</span> |
||
| 224 | 00217 <span class="comment">//OCValue += inp(OCR1AH)<<8; // read high byte of OCR1A</span> |
||
| 225 | 00218 <span class="comment">// increment OCR1A value by PulseT1APeriodTics</span> |
||
| 226 | 00219 <span class="comment">//OCValue += PulseT1APeriodTics;</span> |
||
| 227 | 00220 <span class="comment">// set future output compare time to this new value</span> |
||
| 228 | 00221 <span class="comment">//outp((OCValue>>8), OCR1AH); // write high byte</span> |
||
| 229 | 00222 <span class="comment">//outp((OCValue & 0x00FF),OCR1AL); // write low byte</span> |
||
| 230 | 00223 |
||
| 231 | 00224 <span class="comment">// the following line should be identical in operation</span> |
||
| 232 | 00225 <span class="comment">// to the lines above, but for the moment, I'm not convinced</span> |
||
| 233 | 00226 <span class="comment">// this method is bug-free. At least it's simpler!</span> |
||
| 234 | 00227 <span class="comment">//outw(OCR1A, inw(OCR1A) + PulseT1APeriodTics);</span> |
||
| 235 | 00228 <span class="comment">// change again</span> |
||
| 236 | 00229 OCR1A += PulseT1APeriodTics; |
||
| 237 | 00230 |
||
| 238 | 00231 <span class="comment">// decrement the number of pulses executed</span> |
||
| 239 | 00232 <span class="keywordflow">if</span>(PulseT1AMode & PULSE_MODE_COUNTED) |
||
| 240 | 00233 PulseT1ACount--; |
||
| 241 | 00234 } |
||
| 242 | 00235 <span class="keywordflow">else</span> |
||
| 243 | 00236 { |
||
| 244 | 00237 <span class="comment">// pulse count has reached zero</span> |
||
| 245 | 00238 <span class="comment">// disable the output compare's action on OC1A pin</span> |
||
| 246 | 00239 cbi(TCCR1A,COM1A1); |
||
| 247 | 00240 cbi(TCCR1A,COM1A0); |
||
| 248 | 00241 <span class="comment">// and disable the output compare's interrupt to stop pulsing</span> |
||
| 249 | 00242 cbi(TIMSK, OCIE1A); |
||
| 250 | 00243 } |
||
| 251 | 00244 } |
||
| 252 | 00245 |
||
| 253 | 00246 <span class="keywordtype">void</span> pulseT1BService(<span class="keywordtype">void</span>) |
||
| 254 | 00247 { |
||
| 255 | 00248 <span class="comment">// check if TimerPulseACount is non-zero</span> |
||
| 256 | 00249 <span class="comment">// (i.e. pulses are still requested)</span> |
||
| 257 | 00250 <span class="keywordflow">if</span>(PulseT1BCount) |
||
| 258 | 00251 { |
||
| 259 | 00252 <span class="comment">//u16 OCValue;</span> |
||
| 260 | 00253 <span class="comment">// read in current value of output compare register OCR1B</span> |
||
| 261 | 00254 <span class="comment">//OCValue = inp(OCR1BL); // read low byte of OCR1B</span> |
||
| 262 | 00255 <span class="comment">//OCValue += inp(OCR1BH)<<8; // read high byte of OCR1B</span> |
||
| 263 | 00256 <span class="comment">// increment OCR1B value by PulseT1BPeriodTics</span> |
||
| 264 | 00257 <span class="comment">//OCValue += PulseT1BPeriodTics; </span> |
||
| 265 | 00258 <span class="comment">// set future output compare time to this new value</span> |
||
| 266 | 00259 <span class="comment">//outp((OCValue>>8), OCR1BH); // write high byte</span> |
||
| 267 | 00260 <span class="comment">//outp((OCValue & 0x00FF),OCR1BL); // write low byte</span> |
||
| 268 | 00261 |
||
| 269 | 00262 <span class="comment">// the following line should be identical in operation</span> |
||
| 270 | 00263 <span class="comment">// to the lines above, but for the moment, I'm not convinced</span> |
||
| 271 | 00264 <span class="comment">// this method is bug-free. At least it's simpler!</span> |
||
| 272 | 00265 <span class="comment">//outw(OCR1B, inw(OCR1B) + PulseT1BPeriodTics);</span> |
||
| 273 | 00266 <span class="comment">// change again</span> |
||
| 274 | 00267 OCR1B += PulseT1BPeriodTics; |
||
| 275 | 00268 |
||
| 276 | 00269 |
||
| 277 | 00270 <span class="comment">// decrement the number of pulses executed</span> |
||
| 278 | 00271 <span class="keywordflow">if</span>(PulseT1BMode & PULSE_MODE_COUNTED) |
||
| 279 | 00272 PulseT1BCount--; |
||
| 280 | 00273 } |
||
| 281 | 00274 <span class="keywordflow">else</span> |
||
| 282 | 00275 { |
||
| 283 | 00276 <span class="comment">// pulse count has reached zero</span> |
||
| 284 | 00277 <span class="comment">// disable the output compare's action on OC1B pin</span> |
||
| 285 | 00278 cbi(TCCR1A,COM1B1); |
||
| 286 | 00279 cbi(TCCR1A,COM1B0); |
||
| 287 | 00280 <span class="comment">// and disable the output compare's interrupt to stop pulsing</span> |
||
| 288 | 00281 cbi(TIMSK, OCIE1B); |
||
| 289 | 00282 } |
||
| 290 | 00283 } |
||
| 291 | </pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Oct 29 03:41:07 2006 for Procyon AVRlib by |
||
| 292 | <a href="http://www.doxygen.org/index.html"> |
||
| 293 | <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.2 </small></address> |
||
| 294 | </body> |
||
| 295 | </html> |
Powered by WebSVN v2.8.3