⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 speed__cntr_8c-source.html

📁 使用AVR单片机控制步进电机的软件代码
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!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>AVR446 - Linear speed control of stepper motor: speed_cntr.c Source File</title><link href="doxygen.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.3.7 --><div class="qindex"><a class="qindex" href="main.html">Main&nbsp;Page</a> | <a class="qindex" href="annotated.html">Data&nbsp;Structures</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><h1>speed_cntr.c</h1><a href="speed__cntr_8c.html">Go to the documentation of this file.</a><pre class="fragment"><div>00001 <span class="comment">/*This file has been prepared for Doxygen automatic documentation generation.*/</span>00024 <span class="preprocessor">#include &lt;ioavr.h&gt;</span>00025 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span>00026 <span class="preprocessor">#include "<a class="code" href="sm__driver_8h.html">sm_driver.h</a>"</span>00027 <span class="preprocessor">#include "<a class="code" href="speed__cntr_8h.html">speed_cntr.h</a>"</span>00028 <span class="preprocessor">#include "<a class="code" href="uart_8h.html">uart.h</a>"</span>00029 <a name="l00031"></a><a class="code" href="speed__cntr_8c.html#a0">00031</a> <a class="code" href="structspeedRampData.html">speedRampData</a> <a class="code" href="speed__cntr_8c.html#a0">srd</a>;00032 <a name="l00046"></a><a class="code" href="speed__cntr_8h.html#a12">00046</a> <span class="keywordtype">void</span> <a class="code" href="speed__cntr_8h.html#a12">speed_cntr_Move</a>(<span class="keywordtype">signed</span> <span class="keywordtype">int</span> step, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> accel, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> decel, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> speed)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 &lt; 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&lt;&lt;CS12)|(1&lt;&lt;CS11)|(0&lt;&lt;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 class="code" href="speed__cntr_8h.html#a5">A_SQ</a> / 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 &lt;= 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> &lt;= <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&lt;&lt;CS12)|(1&lt;&lt;CS11)|(0&lt;&lt;CS10));00135   }00136 }00137 <a name="l00143"></a><a class="code" href="speed__cntr_8h.html#a13">00143</a> <span class="keywordtype">void</span> <a class="code" href="speed__cntr_8c.html#a2">speed_cntr_Init_Timer1</a>(<span class="keywordtype">void</span>)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&lt;&lt;WGM12);00149   <span class="comment">// Timer/Counter 1 Output Compare A Match Interrupt enable.</span>00150   TIMSK1 = (1&lt;&lt;OCIE1A);00151 }00152 00163 <span class="preprocessor">#pragma vector=TIMER1_COMPA_vect</span>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -