| 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: fixedpt.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>fixedpt.c</h1><a href="fixedpt_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*! \file fixedpt.c \brief Fixed-point math function library. */</span> |
||
| 9 | 00002 <span class="comment">//*****************************************************************************</span> |
||
| 10 | 00003 <span class="comment">//</span> |
||
| 11 | 00004 <span class="comment">// File Name : 'fixedpt.c'</span> |
||
| 12 | 00005 <span class="comment">// Title : Fixed-point math function library</span> |
||
| 13 | 00006 <span class="comment">// Author : Pascal Stang - Copyright (C) 2003</span> |
||
| 14 | 00007 <span class="comment">// Created : 2003.01.26</span> |
||
| 15 | 00008 <span class="comment">// Revised : 2003.02.02</span> |
||
| 16 | 00009 <span class="comment">// Version : 0.1</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 |
||
| 30 | 00023 <span class="preprocessor">#include "<a class="code" href="fixedpt_8h.html">fixedpt.h</a>"</span> |
||
| 31 | 00024 |
||
| 32 | 00025 <span class="comment">// Program ROM constants</span> |
||
| 33 | 00026 |
||
| 34 | 00027 <span class="comment">// Global variables</span> |
||
| 35 | 00028 u08 FixedPtBits; |
||
| 36 | 00029 |
||
| 37 | 00030 <span class="comment">// Functions</span> |
||
| 38 | 00031 |
||
| 39 | 00032 <span class="comment">// fixedptInit() initializes fixed-point math function library</span> |
||
| 40 | <a name="l00033"></a><a class="code" href="fixedpt_8h.html#a0">00033</a> <span class="keywordtype">void</span> <a class="code" href="fixedpt_8c.html#a1">fixedptInit</a>(u08 fixedPtBits) |
||
| 41 | 00034 { |
||
| 42 | 00035 <span class="comment">// set the number of bits to use behind the point</span> |
||
| 43 | 00036 FixedPtBits = fixedPtBits; |
||
| 44 | 00037 } |
||
| 45 | 00038 |
||
| 46 | <a name="l00039"></a><a class="code" href="fixedpt_8h.html#a1">00039</a> s32 <a class="code" href="fixedpt_8c.html#a2">fixedptConvertFromInt</a>(s32 int_number) |
||
| 47 | 00040 { |
||
| 48 | 00041 <span class="comment">// convert integer to fixed-point number</span> |
||
| 49 | 00042 <span class="keywordflow">return</span> (int_number<<FixedPtBits); |
||
| 50 | 00043 } |
||
| 51 | 00044 |
||
| 52 | <a name="l00045"></a><a class="code" href="fixedpt_8h.html#a2">00045</a> s32 <a class="code" href="fixedpt_8c.html#a3">fixedptConvertToInt</a>(s32 fp_number) |
||
| 53 | 00046 { |
||
| 54 | 00047 <span class="comment">// convert fixed-point number to integer</span> |
||
| 55 | 00048 <span class="comment">// do rounding</span> |
||
| 56 | 00049 <span class="keywordflow">if</span>( fp_number & 1<<(FixedPtBits-1) ) |
||
| 57 | 00050 { |
||
| 58 | 00051 <span class="comment">// bit behind the point was a '1'</span> |
||
| 59 | 00052 <span class="comment">// round up to next higher integer</span> |
||
| 60 | 00053 <span class="keywordflow">return</span> (fp_number>>FixedPtBits)+1; |
||
| 61 | 00054 } |
||
| 62 | 00055 <span class="keywordflow">else</span> |
||
| 63 | 00056 { |
||
| 64 | 00057 <span class="comment">// bit behind the point was a '0'</span> |
||
| 65 | 00058 <span class="comment">// round down (truncate) to next lower integer</span> |
||
| 66 | 00059 <span class="keywordflow">return</span> (fp_number>>FixedPtBits); |
||
| 67 | 00060 } |
||
| 68 | 00061 } |
||
| 69 | 00062 |
||
| 70 | <a name="l00063"></a><a class="code" href="fixedpt_8h.html#a3">00063</a> s32 <a class="code" href="fixedpt_8c.html#a4">fixedptAdd</a>(s32 a, s32 b) |
||
| 71 | 00064 { |
||
| 72 | 00065 <span class="comment">// add a and b (a+b) with fixed-point math</span> |
||
| 73 | 00066 <span class="keywordflow">return</span> a+b; |
||
| 74 | 00067 } |
||
| 75 | 00068 |
||
| 76 | <a name="l00069"></a><a class="code" href="fixedpt_8h.html#a4">00069</a> s32 <a class="code" href="fixedpt_8c.html#a5">fixedptSubtract</a>(s32 a, s32 b) |
||
| 77 | 00070 { |
||
| 78 | 00071 <span class="comment">// subtract a and b (a-b) with fixed-point math</span> |
||
| 79 | 00072 <span class="keywordflow">return</span> a-b; |
||
| 80 | 00073 } |
||
| 81 | 00074 |
||
| 82 | <a name="l00075"></a><a class="code" href="fixedpt_8h.html#a5">00075</a> s32 <a class="code" href="fixedpt_8c.html#a6">fixedptMultiply</a>(s32 a, s32 b) |
||
| 83 | 00076 { |
||
| 84 | 00077 <span class="comment">// multiply a and b (a*b) with fixed-point math</span> |
||
| 85 | 00078 <span class="keywordflow">return</span> (a*b)>>FixedPtBits; |
||
| 86 | 00079 } |
||
| 87 | 00080 |
||
| 88 | <a name="l00081"></a><a class="code" href="fixedpt_8h.html#a6">00081</a> s32 <a class="code" href="fixedpt_8c.html#a7">fixedptDivide</a>(s32 numer, s32 denom) |
||
| 89 | 00082 { |
||
| 90 | 00083 <span class="comment">// divide numer by denom (numer/denom) with fixed-point math</span> |
||
| 91 | 00084 <span class="keywordflow">return</span> (numer<<FixedPtBits)/denom; |
||
| 92 | 00085 } |
||
| 93 | </pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Oct 29 03:41:06 2006 for Procyon AVRlib by |
||
| 94 | <a href="http://www.doxygen.org/index.html"> |
||
| 95 | <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.2 </small></address> |
||
| 96 | </body> |
||
| 97 | </html> |
Powered by WebSVN v2.8.3