?lang_form? ?lang_select? ?lang_submit? ?lang_endform?
{HEADER END}
{BLAME START}

library

?curdirlinks? -

Blame information for rev 6

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: encoder.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&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> | <a class="qindex" href="pages.html">Related&nbsp;Pages</a></div>
8 <h1>encoder.c</h1><a href="encoder_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*! \file encoder.c \brief Quadrature Encoder reader/driver. */</span>
9 00002 <span class="comment">//*****************************************************************************</span>
10 00003 <span class="comment">//</span>
11 00004 <span class="comment">// File Name : 'encoder.c'</span>
12 00005 <span class="comment">// Title : Quadrature Encoder reader/driver</span>
13 00006 <span class="comment">// Author : Pascal Stang - Copyright (C) 2003-2004</span>
14 00007 <span class="comment">// Created : 2003.01.26</span>
15 00008 <span class="comment">// Revised : 2004.06.25</span>
16 00009 <span class="comment">// Version : 0.3</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">// NOTE: This code is currently below version 1.0, and therefore is considered</span>
21 00014 <span class="comment">// to be lacking in some functionality or documentation, or may not be fully</span>
22 00015 <span class="comment">// tested. Nonetheless, you can expect most functions to work.</span>
23 00016 <span class="comment">//</span>
24 00017 <span class="comment">// This code is distributed under the GNU Public License</span>
25 00018 <span class="comment">// which can be found at http://www.gnu.org/licenses/gpl.txt</span>
26 00019 <span class="comment">//</span>
27 00020 <span class="comment">//*****************************************************************************</span>
28 00021
29 00022 <span class="preprocessor">#include &lt;avr/io.h&gt;</span>
30 00023 <span class="preprocessor">#include &lt;avr/interrupt.h&gt;</span>
31 00024
32 00025 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span>
33 00026 <span class="preprocessor">#include "<a class="code" href="encoder_8h.html">encoder.h</a>"</span>
34 00027
35 00028 <span class="comment">// Program ROM constants</span>
36 00029
37 00030 <span class="comment">// Global variables</span>
38 00031 <span class="keyword">volatile</span> <a class="code" href="structstruct__EncoderState.html">EncoderStateType</a> EncoderState[NUM_ENCODERS];
39 00032
40 00033 <span class="comment">// Functions</span>
41 00034
42 00035 <span class="comment">// encoderInit() initializes hardware and encoder position readings</span>
43 00036 <span class="comment">// Run this init routine once before using any other encoder functions.</span>
44 <a name="l00037"></a><a class="code" href="encoder_8h.html#a2">00037</a> <span class="keywordtype">void</span> <a class="code" href="encoder_8c.html#a1">encoderInit</a>(<span class="keywordtype">void</span>)
45 00038 {
46 00039 u08 i;
47 00040
48 00041 <span class="comment">// initialize/clear encoder data</span>
49 00042 <span class="keywordflow">for</span>(i=0; i&lt;NUM_ENCODERS; i++)
50 00043 {
51 00044 EncoderState[i].<a class="code" href="structstruct__EncoderState.html#o0">position</a> = 0;
52 00045 <span class="comment">//EncoderState[i].velocity = 0; // NOT CURRENTLY USED</span>
53 00046 }
54 00047
55 00048 <span class="comment">// configure direction and interrupt I/O pins:</span>
56 00049 <span class="comment">// - for input</span>
57 00050 <span class="comment">// - apply pullup resistors</span>
58 00051 <span class="comment">// - any-edge interrupt triggering</span>
59 00052 <span class="comment">// - enable interrupt</span>
60 00053
61 00054 <span class="preprocessor"> #ifdef ENC0_SIGNAL</span>
62 00055 <span class="preprocessor"></span> <span class="comment">// set interrupt pins to input and apply pullup resistor</span>
63 00056 cbi(ENC0_PHASEA_DDR, ENC0_PHASEA_PIN);
64 00057 sbi(ENC0_PHASEA_PORT, ENC0_PHASEA_PIN);
65 00058 <span class="comment">// set encoder direction pin for input and apply pullup resistor</span>
66 00059 cbi(ENC0_PHASEB_DDR, ENC0_PHASEB_PIN);
67 00060 sbi(ENC0_PHASEB_PORT, ENC0_PHASEB_PIN);
68 00061 <span class="comment">// configure interrupts for any-edge triggering</span>
69 00062 sbi(ENC0_ICR, ENC0_ISCX0);
70 00063 cbi(ENC0_ICR, ENC0_ISCX1);
71 00064 <span class="comment">// enable interrupts</span>
72 00065 sbi(<a class="code" href="encoder_8h.html#a0">IMSK</a>, ENC0_INT); <span class="comment">// ISMK is auto-defined in encoder.h</span>
73 00066 <span class="preprocessor"> #endif</span>
74 00067 <span class="preprocessor"></span><span class="preprocessor"> #ifdef ENC1_SIGNAL</span>
75 00068 <span class="preprocessor"></span> <span class="comment">// set interrupt pins to input and apply pullup resistor</span>
76 00069 cbi(ENC1_PHASEA_DDR, ENC1_PHASEA_PIN);
77 00070 sbi(ENC1_PHASEA_PORT, ENC1_PHASEA_PIN);
78 00071 <span class="comment">// set encoder direction pin for input and apply pullup resistor</span>
79 00072 cbi(ENC1_PHASEB_DDR, ENC1_PHASEB_PIN);
80 00073 sbi(ENC1_PHASEB_PORT, ENC1_PHASEB_PIN);
81 00074 <span class="comment">// configure interrupts for any-edge triggering</span>
82 00075 sbi(ENC1_ICR, ENC1_ISCX0);
83 00076 cbi(ENC1_ICR, ENC1_ISCX1);
84 00077 <span class="comment">// enable interrupts</span>
85 00078 sbi(<a class="code" href="encoder_8h.html#a0">IMSK</a>, ENC1_INT); <span class="comment">// ISMK is auto-defined in encoder.h</span>
86 00079 <span class="preprocessor"> #endif</span>
87 00080 <span class="preprocessor"></span><span class="preprocessor"> #ifdef ENC2_SIGNAL</span>
88 00081 <span class="preprocessor"></span> <span class="comment">// set interrupt pins to input and apply pullup resistor</span>
89 00082 cbi(ENC2_PHASEA_DDR, ENC2_PHASEA_PIN);
90 00083 sbi(ENC2_PHASEA_PORT, ENC2_PHASEA_PIN);
91 00084 <span class="comment">// set encoder direction pin for input and apply pullup resistor</span>
92 00085 cbi(ENC2_PHASEB_DDR, ENC2_PHASEB_PIN);
93 00086 sbi(ENC2_PHASEB_PORT, ENC2_PHASEB_PIN);
94 00087 <span class="comment">// configure interrupts for any-edge triggering</span>
95 00088 sbi(ENC2_ICR, ENC2_ISCX0);
96 00089 cbi(ENC2_ICR, ENC2_ISCX1);
97 00090 <span class="comment">// enable interrupts</span>
98 00091 sbi(<a class="code" href="encoder_8h.html#a0">IMSK</a>, ENC2_INT); <span class="comment">// ISMK is auto-defined in encoder.h</span>
99 00092 <span class="preprocessor"> #endif</span>
100 00093 <span class="preprocessor"></span><span class="preprocessor"> #ifdef ENC3_SIGNAL</span>
101 00094 <span class="preprocessor"></span> <span class="comment">// set interrupt pins to input and apply pullup resistor</span>
102 00095 cbi(ENC3_PHASEA_DDR, ENC3_PHASEA_PIN);
103 00096 sbi(ENC3_PHASEA_PORT, ENC3_PHASEA_PIN);
104 00097 <span class="comment">// set encoder direction pin for input and apply pullup resistor</span>
105 00098 cbi(ENC3_PHASEB_DDR, ENC3_PHASEB_PIN);
106 00099 sbi(ENC3_PHASEB_PORT, ENC3_PHASEB_PIN);
107 00100 <span class="comment">// configure interrupts for any-edge triggering</span>
108 00101 sbi(ENC3_ICR, ENC3_ISCX0);
109 00102 cbi(ENC3_ICR, ENC3_ISCX1);
110 00103 <span class="comment">// enable interrupts</span>
111 00104 sbi(<a class="code" href="encoder_8h.html#a0">IMSK</a>, ENC3_INT); <span class="comment">// ISMK is auto-defined in encoder.h</span>
112 00105 <span class="preprocessor"> #endif</span>
113 00106 <span class="preprocessor"></span>
114 00107 <span class="comment">// enable global interrupts</span>
115 00108 sei();
116 00109 }
117 00110
118 00111 <span class="comment">// encoderOff() disables hardware and stops encoder position updates</span>
119 <a name="l00112"></a><a class="code" href="encoder_8h.html#a3">00112</a> <span class="keywordtype">void</span> <a class="code" href="encoder_8c.html#a2">encoderOff</a>(<span class="keywordtype">void</span>)
120 00113 {
121 00114 <span class="comment">// disable encoder interrupts</span>
122 00115 <span class="preprocessor"> #ifdef ENC0_SIGNAL</span>
123 00116 <span class="preprocessor"></span> <span class="comment">// disable interrupts</span>
124 00117 sbi(<a class="code" href="encoder_8h.html#a0">IMSK</a>, INT0); <span class="comment">// ISMK is auto-defined in encoder.h</span>
125 00118 <span class="preprocessor"> #endif</span>
126 00119 <span class="preprocessor"></span><span class="preprocessor"> #ifdef ENC1_SIGNAL</span>
127 00120 <span class="preprocessor"></span> <span class="comment">// disable interrupts</span>
128 00121 sbi(<a class="code" href="encoder_8h.html#a0">IMSK</a>, INT1); <span class="comment">// ISMK is auto-defined in encoder.h</span>
129 00122 <span class="preprocessor"> #endif</span>
130 00123 <span class="preprocessor"></span><span class="preprocessor"> #ifdef ENC2_SIGNAL</span>
131 00124 <span class="preprocessor"></span> <span class="comment">// disable interrupts</span>
132 00125 sbi(<a class="code" href="encoder_8h.html#a0">IMSK</a>, INT2); <span class="comment">// ISMK is auto-defined in encoder.h</span>
133 00126 <span class="preprocessor"> #endif</span>
134 00127 <span class="preprocessor"></span><span class="preprocessor"> #ifdef ENC3_SIGNAL</span>
135 00128 <span class="preprocessor"></span> <span class="comment">// disable interrupts</span>
136 00129 sbi(<a class="code" href="encoder_8h.html#a0">IMSK</a>, INT3); <span class="comment">// ISMK is auto-defined in encoder.h</span>
137 00130 <span class="preprocessor"> #endif</span>
138 00131 <span class="preprocessor"></span>}
139 00132
140 00133 <span class="comment">// encoderGetPosition() reads the current position of the encoder </span>
141 <a name="l00134"></a><a class="code" href="encoder_8h.html#a4">00134</a> s32 <a class="code" href="encoder_8c.html#a3">encoderGetPosition</a>(u08 encoderNum)
142 00135 {
143 00136 <span class="comment">// sanity check</span>
144 00137 <span class="keywordflow">if</span>(encoderNum &lt; NUM_ENCODERS)
145 00138 <span class="keywordflow">return</span> EncoderState[encoderNum].<a class="code" href="structstruct__EncoderState.html#o0">position</a>;
146 00139 <span class="keywordflow">else</span>
147 00140 <span class="keywordflow">return</span> 0;
148 00141 }
149 00142
150 00143 <span class="comment">// encoderSetPosition() sets the current position of the encoder</span>
151 <a name="l00144"></a><a class="code" href="encoder_8h.html#a5">00144</a> <span class="keywordtype">void</span> <a class="code" href="encoder_8c.html#a4">encoderSetPosition</a>(u08 encoderNum, s32 position)
152 00145 {
153 00146 <span class="comment">// sanity check</span>
154 00147 <span class="keywordflow">if</span>(encoderNum &lt; NUM_ENCODERS)
155 00148 EncoderState[encoderNum].<a class="code" href="structstruct__EncoderState.html#o0">position</a> = position;
156 00149 <span class="comment">// else do nothing</span>
157 00150 }
158 00151
159 00152 <span class="preprocessor">#ifdef ENC0_SIGNAL</span>
160 00153 <span class="preprocessor"></span><span class="comment">//! Encoder 0 interrupt handler</span>
161 00154 <span class="comment"></span><a class="code" href="a2d_8c.html#a10">SIGNAL</a>(ENC0_SIGNAL)
162 00155 {
163 00156 <span class="comment">// encoder has generated a pulse</span>
164 00157 <span class="comment">// check the relative phase of the input channels</span>
165 00158 <span class="comment">// and update position accordingly</span>
166 00159 <span class="keywordflow">if</span>( ((inb(ENC0_PHASEA_PORTIN) &amp; (1&lt;&lt;ENC0_PHASEA_PIN)) == 0) ^
167 00160 ((inb(ENC0_PHASEB_PORTIN) &amp; (1&lt;&lt;ENC0_PHASEB_PIN)) == 0) )
168 00161 {
169 00162 EncoderState[0].<a class="code" href="structstruct__EncoderState.html#o0">position</a>++;
170 00163 }
171 00164 <span class="keywordflow">else</span>
172 00165 {
173 00166 EncoderState[0].<a class="code" href="structstruct__EncoderState.html#o0">position</a>--;
174 00167 }
175 00168 }
176 00169 <span class="preprocessor">#endif</span>
177 00170 <span class="preprocessor"></span>
178 00171 <span class="preprocessor">#ifdef ENC1_SIGNAL</span>
179 00172 <span class="preprocessor"></span><span class="comment">//! Encoder 1 interrupt handler</span>
180 00173 <span class="comment"></span><a class="code" href="a2d_8c.html#a10">SIGNAL</a>(ENC1_SIGNAL)
181 00174 {
182 00175 <span class="comment">// encoder has generated a pulse</span>
183 00176 <span class="comment">// check the relative phase of the input channels</span>
184 00177 <span class="comment">// and update position accordingly</span>
185 00178 <span class="keywordflow">if</span>( ((inb(ENC1_PHASEA_PORTIN) &amp; (1&lt;&lt;ENC1_PHASEA_PIN)) == 0) ^
186 00179 ((inb(ENC1_PHASEB_PORTIN) &amp; (1&lt;&lt;ENC1_PHASEB_PIN)) == 0) )
187 00180 {
188 00181 EncoderState[1].<a class="code" href="structstruct__EncoderState.html#o0">position</a>++;
189 00182 }
190 00183 <span class="keywordflow">else</span>
191 00184 {
192 00185 EncoderState[1].<a class="code" href="structstruct__EncoderState.html#o0">position</a>--;
193 00186 }
194 00187 }
195 00188 <span class="preprocessor">#endif</span>
196 00189 <span class="preprocessor"></span>
197 00190 <span class="preprocessor">#ifdef ENC2_SIGNAL</span>
198 00191 <span class="preprocessor"></span><span class="comment">//! Encoder 2 interrupt handler</span>
199 00192 <span class="comment"></span><a class="code" href="a2d_8c.html#a10">SIGNAL</a>(ENC2_SIGNAL)
200 00193 {
201 00194 <span class="comment">// encoder has generated a pulse</span>
202 00195 <span class="comment">// check the relative phase of the input channels</span>
203 00196 <span class="comment">// and update position accordingly</span>
204 00197 <span class="keywordflow">if</span>( ((inb(ENC2_PHASEA_PORTIN) &amp; (1&lt;&lt;ENC2_PHASEA_PIN)) == 0) ^
205 00198 ((inb(ENC2_PHASEB_PORTIN) &amp; (1&lt;&lt;ENC2_PHASEB_PIN)) == 0) )
206 00199 {
207 00200 EncoderState[2].<a class="code" href="structstruct__EncoderState.html#o0">position</a>++;
208 00201 }
209 00202 <span class="keywordflow">else</span>
210 00203 {
211 00204 EncoderState[2].<a class="code" href="structstruct__EncoderState.html#o0">position</a>--;
212 00205 }
213 00206 }
214 00207 <span class="preprocessor">#endif</span>
215 00208 <span class="preprocessor"></span>
216 00209 <span class="preprocessor">#ifdef ENC3_SIGNAL</span>
217 00210 <span class="preprocessor"></span><span class="comment">//! Encoder 3 interrupt handler</span>
218 00211 <span class="comment"></span><a class="code" href="a2d_8c.html#a10">SIGNAL</a>(ENC3_SIGNAL)
219 00212 {
220 00213 <span class="comment">// encoder has generated a pulse</span>
221 00214 <span class="comment">// check the relative phase of the input channels</span>
222 00215 <span class="comment">// and update position accordingly</span>
223 00216 <span class="keywordflow">if</span>( ((inb(ENC3_PHASEA_PORTIN) &amp; (1&lt;&lt;ENC3_PHASEA_PIN)) == 0) ^
224 00217 ((inb(ENC3_PHASEB_PORTIN) &amp; (1&lt;&lt;ENC3_PHASEB_PIN)) == 0) )
225 00218 {
226 00219 EncoderState[3].<a class="code" href="structstruct__EncoderState.html#o0">position</a>++;
227 00220 }
228 00221 <span class="keywordflow">else</span>
229 00222 {
230 00223 EncoderState[3].<a class="code" href="structstruct__EncoderState.html#o0">position</a>--;
231 00224 }
232 00225 }
233 00226 <span class="preprocessor">#endif</span>
234 </pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Oct 29 03:41:06 2006 for Procyon AVRlib by&nbsp;
235 <a href="http://www.doxygen.org/index.html">
236 <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.2 </small></address>
237 </body>
238 </html>
{BLAME END}
{FOOTER START}

Powered by WebSVN v2.8.3