?? encoder_8h-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: encoder.h Source File</title><link href="dox.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.3.6 --><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="files.html">File List</a> | <a class="qindex" href="functions.html">Data Fields</a> | <a class="qindex" href="globals.html">Globals</a></div><h1>encoder.h</h1><a href="encoder_8h.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001 <span class="comment">/*! \file encoder.h \brief Quadrature Encoder reader/driver. */</span>00002 <span class="comment">//*****************************************************************************</span>00003 <span class="comment">//</span>00004 <span class="comment">// File Name : 'encoder.h'</span>00005 <span class="comment">// Title : Quadrature Encoder reader/driver</span>00006 <span class="comment">// Author : Pascal Stang - Copyright (C) 2003-2004</span>00007 <span class="comment">// Created : 2003.01.26</span>00008 <span class="comment">// Revised : 2004.06.25</span>00009 <span class="comment">// Version : 0.3</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">// Description : This library allows easy interfacing of quadrature encoders</span>00014 <span class="comment">// to the Atmel AVR-series processors.</span>00015 <span class="comment">//</span>00016 <span class="comment">// Quadrature encoders have two digital outputs usually called PhaseA and</span>00017 <span class="comment">// PhaseB. When the encoder rotates, PhaseA and PhaseB produce square wave</span>00018 <span class="comment">// pulses where each pulse represents a fraction of a turn of the encoder</span>00019 <span class="comment">// shaft. Encoders are rated for a certain number of pulses (or counts) per</span>00020 <span class="comment">// complete revolution of the shaft. Common counts/revolution specs are 50,</span>00021 <span class="comment">// 100,128,200,250,256,500,etc. By counting the number of pulses output on</span>00022 <span class="comment">// one of the phases starting from time0, you can calculate the total</span>00023 <span class="comment">// rotational distance the encoder has traveled.</span>00024 <span class="comment">// </span>00025 <span class="comment">// Often, however, we want current position not just total distance traveled.</span>00026 <span class="comment">// For this it is necessary to know not only how far the encoder has traveled,</span>00027 <span class="comment">// but also which direction it was going at each step of the way. To do this</span>00028 <span class="comment">// we need to use both outputs (or phases) of the quadrature encoder.</span>00029 <span class="comment">//</span>00030 <span class="comment">// The pulses from PhaseA and PhaseB on quadrature encoders are always aligned</span>00031 <span class="comment">// 90 degrees out-of-phase (otherwise said: 1/4 wavelength apart). This</span>00032 <span class="comment">// special phase relationship lets us extract both the distance and direction</span>00033 <span class="comment">// the encoder has rotated from the outputs.</span>00034 <span class="comment">//</span>00035 <span class="comment">// To do this, consider Phase A to be the distance counter. On each rising</span>00036 <span class="comment">// edge of PhaseA we will count 1 "tic" of distance, but we need to know the</span>00037 <span class="comment">// direction. Look at the quadrature waveform plot below. Notice that when</span>00038 <span class="comment">// we travel forward in time (left->right), PhaseB is always low (logic 0) at</span>00039 <span class="comment">// the rising edge of PhaseA. When we travel backwards in time (right->left),</span>00040 <span class="comment">// PhaseB is always high (logic 1) at the rising edge of PhaseA. Note that</span>00041 <span class="comment">// traveling forward or backwards in time is the same thing as rotating</span>00042 <span class="comment">// forwards or bardwards. Thus, if PhaseA is our counter, PhaseB indicates</span>00043 <span class="comment">// direction.</span>00044 <span class="comment">//</span>00045 <span class="comment">// Here is an example waveform from a quadrature encoder:</span>00046 <span class="comment">/*</span>00047 <span class="comment">// /---\ /---\ /---\ /---\ /---\ /---\</span>00048 <span class="comment">// Phase A: | | | | | | | | | | | |</span>00049 <span class="comment">// ---/ \---/ \---/ \---/ \---/ \---/ \-</span>00050 <span class="comment">// -\ /---\ /---\ /---\ /---\ /---\ /---</span>00051 <span class="comment">// Phase B: | | | | | | | | | | | |</span>00052 <span class="comment">// \---/ \---/ \---/ \---/ \---/ \---/</span>00053 <span class="comment">// Time: <---------------------------------------------------></span>00054 <span class="comment">// Rotate FWD: >----------------------------------------------> </span>00055 <span class="comment">// Rotate REV: <----------------------------------------------<</span>00056 <span class="comment">*/</span>00057 <span class="comment">// To keep track of the encoder position in software, we connect PhaseA to an</span>00058 <span class="comment">// external processor interrupt line, and PhaseB to any I/O pin. We set up</span>00059 <span class="comment">// the external interrupt to trigger whenever PhaseA produces a rising edge.</span>00060 <span class="comment">// When a rising edge is detected, our interrupt handler function is executed.</span>00061 <span class="comment">// Inside the handler function, we quickly check the PhaseB line to see if it</span>00062 <span class="comment">// is high or low. If it is high, we increment the encoder's position</span>00063 <span class="comment">// counter, otherwise we decrement it. The encoder position counter can be</span>00064 <span class="comment">// read at any time to find out the current position.</span>00065 <span class="comment">//</span>00066 <span class="comment">//</span>00067 <span class="comment">// NOTE: This code is currently below version 1.0, and therefore is considered</span>00068 <span class="comment">// to be lacking in some functionality or documentation, or may not be fully</span>00069 <span class="comment">// tested. Nonetheless, you can expect most functions to work.</span>00070 <span class="comment">//</span>00071 <span class="comment">// This code is distributed under the GNU Public License</span>00072 <span class="comment">// which can be found at http://www.gnu.org/licenses/gpl.txt</span>00073 <span class="comment">//</span>00074 <span class="comment">//*****************************************************************************</span>00075 00076 <span class="preprocessor">#ifndef ENCODER_H</span>00077 <span class="preprocessor"></span><span class="preprocessor">#define ENCODER_H</span>00078 <span class="preprocessor"></span>00079 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span>00080 00081 <span class="comment">// include encoder configuration file</span>00082 <span class="preprocessor">#include "<a class="code" href="encoderconf_8h.html">encoderconf.h</a>"</span>00083 00084 <span class="comment">// constants/macros/typdefs</span>00085 00086 <span class="comment">// defines for processor compatibility</span>00087 <span class="comment">// chose proper Interrupt Mask (IMSK)</span>00088 <span class="preprocessor">#ifdef EIMSK</span>00089 <span class="preprocessor"></span><span class="preprocessor"> #define IMSK EIMSK // for processors mega128, mega64</span>00090 <span class="preprocessor"></span><span class="preprocessor">#else</span>00091 <span class="preprocessor"></span><span class="preprocessor"> #define IMSK GIMSK // for other processors 90s8515, mega163, etc</span>00092 <span class="preprocessor"></span><span class="preprocessor">#endif</span>00093 <span class="preprocessor"></span>00094 <span class="comment"></span>00095 <span class="comment">//! Encoder state structure</span>00096 <span class="comment"></span><span class="comment">// stores the position and other information from each encoder</span><a name="l00097"></a><a class="code" href="structstruct__EncoderState.html">00097</a> <span class="keyword">typedef</span> <span class="keyword">struct </span><a class="code" href="structstruct__EncoderState.html">struct_EncoderState</a>00098 { <a name="l00099"></a><a class="code" href="structstruct__EncoderState.html#o0">00099</a> s32 <a class="code" href="structstruct__EncoderState.html#o0">position</a>; <span class="comment">///< position</span>00100 <span class="comment"></span><span class="comment">// s32 velocity; ///< velocity</span>00101 } <a class="code" href="structstruct__EncoderState.html">EncoderStateType</a>;00102 00103 00104 <span class="comment">// functions</span>00105 <span class="comment"></span>00106 <span class="comment">//! encoderInit() initializes hardware and encoder position readings</span>00107 <span class="comment"></span><span class="comment">// Run this init routine once before using any other encoder function.</span>00108 <span class="keywordtype">void</span> <a class="code" href="encoder_8c.html#a1">encoderInit</a>(<span class="keywordtype">void</span>);00109 <span class="comment"></span>00110 <span class="comment">//! encoderOff() disables hardware and stops encoder position updates</span>00111 <span class="comment"></span><span class="keywordtype">void</span> <a class="code" href="encoder_8c.html#a2">encoderOff</a>(<span class="keywordtype">void</span>);00112 <span class="comment"></span>00113 <span class="comment">//! encoderGetPosition() reads the current position of the encoder </span>00114 <span class="comment"></span>s32 <a class="code" href="encoder_8h.html#a4">encoderGetPosition</a>(u08 encoderNum);00115 <span class="comment"></span>00116 <span class="comment">//! encoderSetPosition() sets the current position of the encoder</span>00117 <span class="comment"></span><span class="keywordtype">void</span> <a class="code" href="encoder_8h.html#a5">encoderSetPosition</a>(u08 encoderNum, s32 position);00118 00119 <span class="preprocessor">#endif</span></pre></div><hr size="1"><address style="align: right;"><small>Generated on Fri Oct 15 03:50:21 2004 for Procyon AVRlib by<a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border=0 > </a>1.3.6 </small></address></body></html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -