📄 analog_8c-source.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>AVR453 Smart Battery Reference Design: analog.c Source File</title><link href="doxygen.css" rel="stylesheet" type="text/css"><link href="tabs.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.5.3 --><div class="tabs"> <ul> <li><a href="main.html"><span>Main Page</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> <li><a href="pages.html"><span>Related Pages</span></a></li> </ul></div><h1>analog.c</h1><a href="analog_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/* This file has been prepared for Doxygen automatic documentation generation.*/</span><a name="l00024"></a>00024 <span class="comment">/* Validation Test for Current Measurement:</span><a name="l00025"></a>00025 <span class="comment">Using the I-SIM circuit, apply 20.0mV to the ATmega406 CCADC input.</span><a name="l00026"></a>00026 <span class="comment">Read back the variable LatestCCI. Multiply this value by 53.7uV/count.</span><a name="l00027"></a>00027 <span class="comment">You should get approximately 20,000uV, or 20mV. The variable LatestCCI</span><a name="l00028"></a>00028 <span class="comment">is intended to be used for cell impedance measurements and is not filtered</span><a name="l00029"></a>00029 <span class="comment">or scaled in any way, but is a copy of the raw result of the instantaneous</span><a name="l00030"></a>00030 <span class="comment">CC reading.</span><a name="l00031"></a>00031 <span class="comment"></span><a name="l00032"></a>00032 <span class="comment">The variable CCarray contains averaged 1-second Current readings based on the</span><a name="l00033"></a>00033 <span class="comment">results of the Instantaneous reading of the CCADC. These calculations</span><a name="l00034"></a>00034 <span class="comment">assume a 5mOhm sense resistor. To verify these readings, pick one</span><a name="l00035"></a>00035 <span class="comment">sample out of the CCarray and convert to decimal. The value should be read</span><a name="l00036"></a>00036 <span class="comment">directly in mA. If using 20mV as noted above, this would correspond to</span><a name="l00037"></a>00037 <span class="comment">a value of 20mV/5mOhm = 4000mA, therefore all non-zero elements in the array</span><a name="l00038"></a>00038 <span class="comment">should read approx. 4000 (decimal).</span><a name="l00039"></a>00039 <span class="comment"></span><a name="l00040"></a>00040 <span class="comment">The Accumulator should also be tested, since its reading must also be</span><a name="l00041"></a>00041 <span class="comment">scaled in order to be read as mA. Assuming 20mV applied to the input,</span><a name="l00042"></a>00042 <span class="comment">and each bit corresponding to 1.678uV, then each 1-second reading of the</span><a name="l00043"></a>00043 <span class="comment">CCADC accumulated result should be approx. 20mV/1.678uV = 0x2EF8.</span><a name="l00044"></a>00044 <span class="comment">This number must be multiplied by 3600 to convert from seconds to hours.</span><a name="l00045"></a>00045 <span class="comment">Dividing this result by 10727 will yield the current in mAHr, in this</span><a name="l00046"></a>00046 <span class="comment">case approximately 4000mAHr.</span><a name="l00047"></a>00047 <span class="comment"></span><a name="l00048"></a>00048 <span class="comment"></span><a name="l00049"></a>00049 <span class="comment">*/</span><a name="l00050"></a>00050 <a name="l00051"></a>00051 <a name="l00052"></a>00052 <a name="l00053"></a>00053 <a name="l00054"></a>00054 <a name="l00055"></a>00055 <span class="comment">//#include "iom406_320.h"</span><a name="l00056"></a>00056 <span class="preprocessor">#include <iom406.h></span> <span class="comment">// IAR headerfile for Mega406 (EW 410)</span><a name="l00057"></a>00057 <span class="preprocessor">#include <inavr.h></span><a name="l00058"></a>00058 <a name="l00059"></a>00059 <span class="preprocessor">#include "<a class="code" href="main_8h.html" title="Headerfile for main.c.">main.h</a>"</span><a name="l00060"></a>00060 <span class="preprocessor">#include "<a class="code" href="pack_8h.html" title="Battery pack definitions.">pack.h</a>"</span><a name="l00061"></a>00061 <a name="l00062"></a><a class="code" href="analog_8c.html#cbfbdeddc7732ff2b930d3ea9a301f25">00062</a> <span class="preprocessor">#define MODULE_ANALOG</span><a name="l00063"></a>00063 <span class="preprocessor"></span><span class="preprocessor">#include "<a class="code" href="analog_8h.html" title="Headerfile for analog.c.">analog.h</a>"</span><a name="l00064"></a>00064 <a name="l00065"></a><a class="code" href="analog_8c.html#653ae0a06c24dd46dc525e57868537a6">00065</a> <span class="preprocessor">#define MODULE_CALIBRATION</span><a name="l00066"></a>00066 <span class="preprocessor"></span><span class="preprocessor">#include "<a class="code" href="calibration_8h.html" title="Defines and variables for calibration.">calibration.h</a>"</span><a name="l00067"></a>00067 <a name="l00068"></a>00068 <a name="l00069"></a>00069 <span class="preprocessor">#include "<a class="code" href="smbus_8h.html" title="Headerfile for smbus.c.">smbus.h</a>"</span><a name="l00070"></a>00070 <span class="preprocessor">#include "<a class="code" href="timer_8h.html" title="Headerfile for timer.c.">timer.h</a>"</span> <span class="comment">// (for access to the Timer32KHz variable)</span><a name="l00071"></a>00071 <span class="preprocessor">#include "<a class="code" href="pwrmgmt_8h.html" title="Headerfile for pwrmgmt.c.">pwrmgmt.h</a>"</span><a name="l00072"></a>00072 <span class="preprocessor">#include "<a class="code" href="ee_8h.html" title="Allocation of EEPROM storage.">ee.h</a>"</span><a name="l00073"></a>00073 <a name="l00074"></a>00074 <a name="l00075"></a>00075 <span class="comment">//Local prototypes (not exposed in analog.h)</span><a name="l00076"></a>00076 <span class="keywordtype">void</span> <a class="code" href="analog_8c.html#8d495c946874544c00e4293dc361a7d1">CCarray_Init</a>(<span class="keywordtype">void</span>);<a name="l00077"></a>00077 <a name="l00078"></a>00078 <a name="l00079"></a>00079 <a name="l00080"></a>00080 <a name="l00081"></a>00081 <span class="comment">//Local variables (not exposed in analog.h)</span><a name="l00082"></a>00082 <a name="l00083"></a>00083 <span class="comment">//signed long RunningAcc = 0; //this must be externally visible for Hibernate mode estimated power loss</span><a name="l00084"></a><a class="code" href="analog_8c.html#e4d5cdf56dbdf521edbff27852086419">00084</a> <span class="keywordtype">signed</span> <span class="keywordtype">long</span> <a class="code" href="analog_8c.html#e4d5cdf56dbdf521edbff27852086419">MaxTopAcc</a> = 0;<a name="l00085"></a><a class="code" href="analog_8c.html#c4c4253a5691b9958b247ac9284735e7">00085</a> <span class="keywordtype">signed</span> <span class="keywordtype">long</span> <a class="code" href="analog_8c.html#c4c4253a5691b9958b247ac9284735e7">MaxBottomAcc</a> = 0;<a name="l00086"></a>00086 <a name="l00087"></a><a class="code" href="analog_8c.html#57b4e44e09b1974747efce34305a6797">00087</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> <a class="code" href="analog_8c.html#57b4e44e09b1974747efce34305a6797">CC_delay_acc</a> = 0;<a name="l00088"></a><a class="code" href="analog_8c.html#8148013e5a4258d8676280733f28e522">00088</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> <a class="code" href="analog_8c.html#8148013e5a4258d8676280733f28e522">CC_delay_inst</a> = 0;<a name="l00089"></a>00089 <a name="l00090"></a>00090 <span class="comment">//Calculated, calibrated results</span><a name="l00091"></a><a class="code" href="analog_8c.html#fcfe4bb2b98f8e2315f0efe6b8dc22e1">00091</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="analog_8c.html#fcfe4bb2b98f8e2315f0efe6b8dc22e1">CellV</a>[4] = {0};<a name="l00092"></a><a class="code" href="analog_8c.html#206e8b6459b9cade97fe6cf03cebb4c0">00092</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="analog_8c.html#206e8b6459b9cade97fe6cf03cebb4c0">OnChipTemp</a> = 0;<a name="l00093"></a>00093 <a name="l00094"></a>00094 <span class="comment">//Intermediate results</span><a name="l00095"></a><a class="code" href="analog_8c.html#872a0509432a2aec4963a1580467fd5c">00095</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="analog_8c.html#872a0509432a2aec4963a1580467fd5c">Thermistor</a>[4] = {0}; <span class="comment">// this hold an Ohmic value, scaled per "FixedThermistorPullup"</span><a name="l00096"></a><a class="code" href="analog_8c.html#820364efa27ed882c60768c141469648">00096</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="analog_8c.html#820364efa27ed882c60768c141469648">VPA4</a> = 0; <span class="comment">// this is V(ADC4) * 5</span><a name="l00097"></a>00097 <a name="l00098"></a>00098 <a name="l00099"></a>00099 <span class="comment">// The following variables are used for the 1-minute (approx) average of current flow.</span><a name="l00100"></a>00100 <span class="comment">// We treat CCarray as a circular buffer, containing the 64 most recent samples available.</span><a name="l00101"></a>00101 <span class="comment">// Each sample is properly scaled (mA) but does not include temperature or offset effects.</span><a name="l00102"></a>00102 <span class="comment">// These functions are kept local to this file and are not made public.</span><a name="l00103"></a><a class="code" href="analog_8c.html#65095ca83e17d375199b626682b24c81">00103</a> <span class="keywordtype">signed</span> <span class="keywordtype">int</span> <a class="code" href="analog_8c.html#65095ca83e17d375199b626682b24c81">CCarray</a>[64]; <span class="comment">//positive (charging) or negative (discharging).</span><a name="l00104"></a><a class="code" href="analog_8c.html#41315964a907e00db068ee8b6abc4719">00104</a> <span class="keywordtype">char</span> <a class="code" href="analog_8c.html#41315964a907e00db068ee8b6abc4719">CCindex</a> = 0;<a name="l00105"></a><a class="code" href="analog_8c.html#a7de899149fc0394dd361887d2ab4d6b">00105</a> <span class="keywordtype">char</span> <a class="code" href="analog_8c.html#a7de899149fc0394dd361887d2ab4d6b">CCvalidsamples</a> = 0;<a name="l00106"></a>00106 <a name="l00107"></a>00107 <a name="l00108"></a>00108 <a name="l00109"></a>00109 <a name="l00110"></a>00110 <span class="comment">//Support variables</span><a name="l00111"></a><a class="code" href="analog_8c.html#a34ab127785f8cbaf48d0f3d92638415">00111</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="analog_8c.html#a34ab127785f8cbaf48d0f3d92638415">ADCbuffer</a>[10]; <span class="comment">//raw converted results</span><a name="l00112"></a><a class="code" href="analog_8c.html#92c13181f40eb6acea2626b50d3f94fe">00112</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="analog_8c.html#92c13181f40eb6acea2626b50d3f94fe">ADCgain</a>[4]; <span class="comment">//gain values for Cell1-4</span><a name="l00113"></a><a class="code" href="analog_8c.html#3dd9f0a8cb62e461777529d933b8bb86">00113</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="analog_8c.html#3dd9f0a8cb62e461777529d933b8bb86">VTgain</a>; <span class="comment">//gain for on-chip temperature sensor</span><a name="l00114"></a><a class="code" href="analog_8c.html#06a9a2ac08c2fafbb0fa31e0c8a7bb45">00114</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> <a class="code" href="analog_8c.html#06a9a2ac08c2fafbb0fa31e0c8a7bb45">ADCchannel</a>; <span class="comment">//visible so we can force the start channel</span><a name="l00115"></a>00115 <a name="l00116"></a>00116 <a name="l00117"></a><a class="code" href="analog_8c.html#96970f4bb8d74ce105640d620d3e65d8">00117</a> <span class="preprocessor">#define FixedThermistorPullup 1000 </span><span class="comment">/* ohmic value */</span><a name="l00118"></a>00118 <a name="l00119"></a>00119 <span class="comment">/* =====================================================================================</span><a name="l00120"></a>00120 <span class="comment"> =====================================================================================</span><a name="l00121"></a>00121 <span class="comment"> ===================================================================================== */</span><a name="l00122"></a>00122 <a name="l00123"></a>00123 <a name="l00124"></a>00124 <span class="comment">//Public functions (exposed in analog.h)</span><a name="l00125"></a>00125 <a name="l00126"></a><a class="code" href="analog_8h.html#edb3a12a3f949ade91da1a3d8d483a46">00126</a> <span class="keywordtype">void</span> <a class="code" href="analog_8c.html#edb3a12a3f949ade91da1a3d8d483a46">SetMaxTopAcc</a>(<span class="keywordtype">long</span> value)<a name="l00127"></a>00127 {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -