📄 speed__cntr_8h.html
字号:
<td> </td> <td><p>Init of Timer/Counter1. <p>Set up Timer/Counter1 to use mode 1 CTC and enable Output Compare A Match Interrupt. <p>Definition at line <a class="el" href="speed__cntr_8c-source.html#l00143">143</a> of file <a class="el" href="speed__cntr_8c-source.html">speed_cntr.c</a>.<p>References <a class="el" href="speed__cntr_8h-source.html#l00032">speedRampData::run_state</a>, <a class="el" href="speed__cntr_8c-source.html#l00031">srd</a>, and <a class="el" href="speed__cntr_8h-source.html#l00080">STOP</a>.<p>Referenced by <a class="el" href="main_8c-source.html#l00042">Init()</a>.<p><pre class="fragment"><div>00144 {00145 <span class="comment">// Tells what part of speed ramp we are in.</span>00146 <a class="code" href="speed__cntr_8c.html#a0">srd</a>.<a class="code" href="structspeedRampData.html#o5">run_state</a> = <a class="code" href="speed__cntr_8h.html#a7">STOP</a>;00147 <span class="comment">// Timer/Counter 1 in mode 4 CTC (Not running).</span>00148 TCCR1B = (1<<WGM12);00149 <span class="comment">// Timer/Counter 1 Output Compare A Match Interrupt enable.</span>00150 TIMSK1 = (1<<OCIE1A);00151 }</div></pre> </td> </tr></table><a class="anchor" name="a12" doxytag="speed_cntr.h::speed_cntr_Move" ></a><p><table class="mdTable" width="100%" cellpadding="2" cellspacing="0"> <tr> <td class="mdRow"> <table cellpadding="0" cellspacing="0" border="0"> <tr> <td class="md" nowrap valign="top"> void speed_cntr_Move </td> <td class="md" valign="top">( </td> <td class="md" nowrap valign="top">signed int </td> <td class="mdname" nowrap> <em>step</em>, </td> </tr> <tr> <td class="md" nowrap align="right"></td> <td></td> <td class="md" nowrap>unsigned int </td> <td class="mdname" nowrap> <em>accel</em>, </td> </tr> <tr> <td class="md" nowrap align="right"></td> <td></td> <td class="md" nowrap>unsigned int </td> <td class="mdname" nowrap> <em>decel</em>, </td> </tr> <tr> <td class="md" nowrap align="right"></td> <td></td> <td class="md" nowrap>unsigned int </td> <td class="mdname" nowrap> <em>speed</em></td> </tr> <tr> <td></td> <td class="md">) </td> <td class="md" colspan="2"></td> </tr> </table> </td> </tr></table><table cellspacing=5 cellpadding=0 border=0> <tr> <td> </td> <td><p>Move the stepper motor a given number of steps. <p>Makes the stepper motor move the given number of steps. It accelrate with given accelration up to maximum speed and decelerate with given deceleration so it stops at the given step. If accel/decel is to small and steps to move is to few, speed might not reach the max speed limit before deceleration starts.<p><dl compact><dt><b>Parameters:</b></dt><dd> <table border="0" cellspacing="2" cellpadding="0"> <tr><td></td><td valign=top><em>step</em> </td><td>Number of steps to move (pos - CW, neg - CCW). </td></tr> <tr><td></td><td valign=top><em>accel</em> </td><td>Accelration to use, in 0.01*rad/sec^2. </td></tr> <tr><td></td><td valign=top><em>decel</em> </td><td>Decelration to use, in 0.01*rad/sec^2. </td></tr> <tr><td></td><td valign=top><em>speed</em> </td><td>Max speed, in 0.01*rad/sec.</td></tr> </table></dl><p>Number of steps before we hit max speed.<p>Number of steps before we must start deceleration (if accel does not hit max speed). <p>Definition at line <a class="el" href="speed__cntr_8c-source.html#l00046">46</a> of file <a class="el" href="speed__cntr_8c-source.html">speed_cntr.c</a>.<p>References <a class="el" href="speed__cntr_8h-source.html#l00076">A_SQ</a>, <a class="el" href="speed__cntr_8h-source.html#l00074">A_T_x100</a>, <a class="el" href="speed__cntr_8h-source.html#l00077">A_x20000</a>, <a class="el" href="speed__cntr_8h-source.html#l00081">ACCEL</a>, <a class="el" href="speed__cntr_8h-source.html#l00044">speedRampData::accel_count</a>, <a class="el" href="sm__driver_8h-source.html#l00026">CCW</a>, <a class="el" href="sm__driver_8h-source.html#l00025">CW</a>, <a class="el" href="speed__cntr_8h-source.html#l00082">DECEL</a>, <a class="el" href="speed__cntr_8h-source.html#l00038">speedRampData::decel_start</a>, <a class="el" href="speed__cntr_8h-source.html#l00040">speedRampData::decel_val</a>, <a class="el" href="speed__cntr_8h-source.html#l00034">speedRampData::dir</a>, <a class="el" href="speed__cntr_8h-source.html#l00042">speedRampData::min_delay</a>, <a class="el" href="speed__cntr_8h-source.html#l00083">RUN</a>, <a class="el" href="speed__cntr_8h-source.html#l00032">speedRampData::run_state</a>, <a class="el" href="global_8h-source.html#l00031">GLOBAL_FLAGS::running</a>, <a class="el" href="speed__cntr_8c-source.html#l00031">srd</a>, <a class="el" href="main_8c-source.html#l00033">status</a>, <a class="el" href="speed__cntr_8h-source.html#l00036">speedRampData::step_delay</a>, <a class="el" href="speed__cntr_8h-source.html#l00075">T1_FREQ_148</a>, and <a class="el" href="global_8h-source.html#l00024">TRUE</a>.<p>Referenced by <a class="el" href="main_8c-source.html#l00062">main()</a>.<p><pre class="fragment"><div>00047 {00049 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> max_s_lim;00051 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> accel_lim;00052 00053 <span class="comment">// Set direction from sign on step value.</span>00054 <span class="keywordflow">if</span>(step < 0){00055 <a class="code" href="speed__cntr_8c.html#a0">srd</a>.<a class="code" href="structspeedRampData.html#o3">dir</a> = <a class="code" href="sm__driver_8h.html#a1">CCW</a>;00056 step = -step;00057 }00058 <span class="keywordflow">else</span>{00059 <a class="code" href="speed__cntr_8c.html#a0">srd</a>.<a class="code" href="structspeedRampData.html#o3">dir</a> = <a class="code" href="sm__driver_8h.html#a0">CW</a>;00060 }00061 00062 <span class="comment">// If moving only 1 step.</span>00063 <span class="keywordflow">if</span>(step == 1){00064 <span class="comment">// Move one step...</span>00065 <a class="code" href="speed__cntr_8c.html#a0">srd</a>.<a class="code" href="structspeedRampData.html#o0">accel_count</a> = -1;00066 <span class="comment">// ...in DECEL state.</span>00067 <a class="code" href="speed__cntr_8c.html#a0">srd</a>.<a class="code" href="structspeedRampData.html#o5">run_state</a> = <a class="code" href="speed__cntr_8h.html#a9">DECEL</a>;00068 <span class="comment">// Just a short delay so main() can act on 'running'.</span>00069 <a class="code" href="speed__cntr_8c.html#a0">srd</a>.<a class="code" href="structspeedRampData.html#o6">step_delay</a> = 1000;00070 <a class="code" href="main_8c.html#a0">status</a>.<a class="code" href="structGLOBAL__FLAGS.html#o2">running</a> = <a class="code" href="global_8h.html#a0">TRUE</a>;00071 OCR1A = 10;00072 <span class="comment">// Run Timer/Counter 1 with prescaler = 8.</span>00073 TCCR1B |= ((0<<CS12)|(1<<CS11)|(0<<CS10));00074 }00075 <span class="comment">// Only move if number of steps to move is not zero.</span>00076 <span class="keywordflow">else</span> <span class="keywordflow">if</span>(step != 0){00077 <span class="comment">// Refer to documentation for detailed information about these calculations.</span>00078 00079 <span class="comment">// Set max speed limit, by calc min_delay to use in timer.</span>00080 <span class="comment">// min_delay = (alpha / tt)/ w</span>00081 <a class="code" href="speed__cntr_8c.html#a0">srd</a>.<a class="code" href="structspeedRampData.html#o4">min_delay</a> = <a class="code" href="speed__cntr_8h.html#a3">A_T_x100</a> / speed;00082 00083 <span class="comment">// Set accelration by calc the first (c0) step delay .</span>00084 <span class="comment">// step_delay = 1/tt * sqrt(2*alpha/accel)</span>00085 <span class="comment">// step_delay = ( tfreq*0.676/100 )*100 * sqrt( (2*alpha*10000000000) / (accel*100) )/10000</span>00086 <a class="code" href="speed__cntr_8c.html#a0">srd</a>.<a class="code" href="structspeedRampData.html#o6">step_delay</a> = (<a class="code" href="speed__cntr_8h.html#a4">T1_FREQ_148</a> * sqrt(A_SQ / accel))/100;00087 00088 <span class="comment">// Find out after how many steps does the speed hit the max speed limit.</span>00089 <span class="comment">// max_s_lim = speed^2 / (2*alpha*accel)</span>00090 max_s_lim = (<span class="keywordtype">long</span>)speed*speed/(<span class="keywordtype">long</span>)(((<span class="keywordtype">long</span>)<a class="code" href="speed__cntr_8h.html#a6">A_x20000</a>*accel)/100);00091 <span class="comment">// If we hit max speed limit before 0,5 step it will round to 0.</span>00092 <span class="comment">// But in practice we need to move atleast 1 step to get any speed at all.</span>00093 <span class="keywordflow">if</span>(max_s_lim == 0){00094 max_s_lim = 1;00095 }00096 00097 <span class="comment">// Find out after how many steps we must start deceleration.</span>00098 <span class="comment">// n1 = (n1+n2)decel / (accel + decel)</span>00099 accel_lim = ((<span class="keywordtype">long</span>)step*decel) / (accel+decel);00100 <span class="comment">// We must accelrate at least 1 step before we can start deceleration.</span>00101 <span class="keywordflow">if</span>(accel_lim == 0){00102 accel_lim = 1;00103 }00104 00105 <span class="comment">// Use the limit we hit first to calc decel.</span>00106 <span class="keywordflow">if</span>(accel_lim <= max_s_lim){00107 <a class="code" href="speed__cntr_8c.html#a0">srd</a>.<a class="code" href="structspeedRampData.html#o2">decel_val</a> = accel_lim - step;00108 }00109 <span class="keywordflow">else</span>{00110 <a class="code" href="speed__cntr_8c.html#a0">srd</a>.<a class="code" href="structspeedRampData.html#o2">decel_val</a> = -((<span class="keywordtype">long</span>)max_s_lim*accel)/decel;00111 }00112 <span class="comment">// We must decelrate at least 1 step to stop.</span>00113 <span class="keywordflow">if</span>(<a class="code" href="speed__cntr_8c.html#a0">srd</a>.<a class="code" href="structspeedRampData.html#o2">decel_val</a> == 0){00114 <a class="code" href="speed__cntr_8c.html#a0">srd</a>.<a class="code" href="structspeedRampData.html#o2">decel_val</a> = -1;00115 }00116 00117 <span class="comment">// Find step to start decleration.</span>00118 <a class="code" href="speed__cntr_8c.html#a0">srd</a>.<a class="code" href="structspeedRampData.html#o1">decel_start</a> = step + <a class="code" href="speed__cntr_8c.html#a0">srd</a>.<a class="code" href="structspeedRampData.html#o2">decel_val</a>;00119 00120 <span class="comment">// If the maximum speed is so low that we dont need to go via accelration state.</span>00121 <span class="keywordflow">if</span>(<a class="code" href="speed__cntr_8c.html#a0">srd</a>.<a class="code" href="structspeedRampData.html#o6">step_delay</a> <= <a class="code" href="speed__cntr_8c.html#a0">srd</a>.<a class="code" href="structspeedRampData.html#o4">min_delay</a>){00122 <a class="code" href="speed__cntr_8c.html#a0">srd</a>.<a class="code" href="structspeedRampData.html#o6">step_delay</a> = <a class="code" href="speed__cntr_8c.html#a0">srd</a>.<a class="code" href="structspeedRampData.html#o4">min_delay</a>;00123 <a class="code" href="speed__cntr_8c.html#a0">srd</a>.<a class="code" href="structspeedRampData.html#o5">run_state</a> = <a class="code" href="speed__cntr_8h.html#a10">RUN</a>;00124 }00125 <span class="keywordflow">else</span>{00126 <a class="code" href="speed__cntr_8c.html#a0">srd</a>.<a class="code" href="structspeedRampData.html#o5">run_state</a> = <a class="code" href="speed__cntr_8h.html#a8">ACCEL</a>;00127 }00128 00129 <span class="comment">// Reset counter.</span>00130 <a class="code" href="speed__cntr_8c.html#a0">srd</a>.<a class="code" href="structspeedRampData.html#o0">accel_count</a> = 0;00131 <a class="code" href="main_8c.html#a0">status</a>.<a class="code" href="structGLOBAL__FLAGS.html#o2">running</a> = <a class="code" href="global_8h.html#a0">TRUE</a>;00132 OCR1A = 10;00133 <span class="comment">// Set Timer/Counter to divide clock by 8</span>00134 TCCR1B |= ((0<<CS12)|(1<<CS11)|(0<<CS10));00135 }00136 }</div></pre> </td> </tr></table><hr><h2>Variable Documentation</h2><a class="anchor" name="a11" doxytag="speed_cntr.h::status" ></a><p><table class="mdTable" width="100%" cellpadding="2" cellspacing="0"> <tr> <td class="mdRow"> <table cellpadding="0" cellspacing="0" border="0"> <tr> <td class="md" nowrap valign="top"> struct <a class="el" href="structGLOBAL__FLAGS.html">GLOBAL_FLAGS</a> <a class="el" href="speed__cntr_8h.html#a11">status</a> </td> </tr> </table> </td> </tr></table><table cellspacing=5 cellpadding=0 border=0> <tr> <td> </td> <td><p>Global status flags. <p><p>Definition at line <a class="el" href="speed__cntr_8h-source.html#l00091">91</a> of file <a class="el" href="speed__cntr_8h-source.html">speed_cntr.h</a>.<p>Referenced by <a class="el" href="main_8c-source.html#l00062">main()</a>, <a class="el" href="speed__cntr_8c-source.html#l00046">speed_cntr_Move()</a>, <a class="el" href="speed__cntr_8c-source.html#l00164">speed_cntr_TIMER1_COMPA_interrupt()</a>, and <a class="el" href="uart_8c-source.html#l00146">UART_RX_interrupt()</a>. </td> </tr></table><hr size="1"><address style="align: right;"><small>Generated on Mon May 8 15:05:04 2006 for AVR446 - Linear speed control of stepper motor by<a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border=0 ></a> 1.3.7 </small></address></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -