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

📄 main_8c-source.html

📁 ATtiny261 461 861 这份资料介绍了执行Attiny261 461 861微控制器系列正弦波驱动三相无刷直流电动机霍尔传感器。
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<!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>AVR449: main.c Source File</title><link href="doxygen.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.4.4 --><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>main.c</h1><a href="main_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="l00028"></a>00028 <span class="preprocessor">#include &lt;ioavr.h&gt;</span><a name="l00029"></a>00029 <span class="preprocessor">#include &lt;inavr.h&gt;</span><a name="l00030"></a>00030 <span class="preprocessor">#include "<a class="code" href="stdint_8h.html">stdint.h</a>"</span><a name="l00031"></a>00031 <span class="preprocessor">#include "<a class="code" href="PMSM_8h.html">PMSM.h</a>"</span><a name="l00032"></a>00032 <span class="preprocessor">#include "<a class="code" href="PMSM__tables_8h.html">PMSM_tables.h</a>"</span><a name="l00033"></a>00033 <span class="preprocessor">#include "<a class="code" href="TinyX61__macros_8h.html">TinyX61_macros.h</a>"</span><a name="l00034"></a>00034 <a name="l00035"></a>00035 <a name="l00036"></a>00036 <span class="preprocessor">#if (SPEED_CONTROL_METHOD == SPEED_CONTROL_CLOSED_LOOP)</span><a name="l00037"></a>00037 <span class="preprocessor"></span><span class="preprocessor">#include "<a class="code" href="pid_8h.html">pid.h</a>"</span><a name="l00038"></a>00038 <span class="preprocessor">#endif</span><a name="l00039"></a>00039 <span class="preprocessor"></span><a name="l00040"></a>00040 <a name="l00047"></a><a class="code" href="main_8c.html#a0">00047</a> <span class="keyword">volatile</span> __io <a class="code" href="structPMSMflags.html">PMSMflags_t</a> fastFlags                         @0x0a;<a name="l00048"></a>00048 <a name="l00049"></a>00049 <a name="l00055"></a><a class="code" href="main_8c.html#a1">00055</a> __no_init __regvar <span class="keyword">volatile</span> <a class="code" href="stdint_8h.html#a58">uint16_t</a> <a class="code" href="main_8c.html#a1">sineTableIncrement</a>     @14;<a name="l00056"></a>00056 <a name="l00057"></a>00057 <a name="l00064"></a><a class="code" href="main_8c.html#a2">00064</a> __no_init __regvar <span class="keyword">volatile</span> <a class="code" href="stdint_8h.html#a58">uint16_t</a> <a class="code" href="main_8c.html#a2">sineTableIndex</a>         @12;<a name="l00065"></a>00065 <a name="l00066"></a>00066 <a name="l00074"></a><a class="code" href="main_8c.html#a3">00074</a> __no_init __regvar <span class="keyword">volatile</span> <a class="code" href="stdint_8h.html#a58">uint16_t</a> <a class="code" href="main_8c.html#a3">commutationTicks</a>       @10;<a name="l00075"></a>00075 <a name="l00076"></a>00076 <a name="l00082"></a><a class="code" href="main_8c.html#a4">00082</a> __no_init __regvar <span class="keyword">volatile</span> <a class="code" href="stdint_8h.html#a58">uint16_t</a> <a class="code" href="main_8c.html#a4">amplitude</a>               @8;<a name="l00083"></a>00083 <a name="l00084"></a>00084 <a name="l00092"></a><a class="code" href="main_8c.html#a5">00092</a> __no_init __regvar <span class="keyword">volatile</span> <a class="code" href="stdint_8h.html#a56">uint8_t</a> <a class="code" href="main_8c.html#a5">advanceCommutationSteps</a>      @7;<a name="l00093"></a>00093 <a name="l00094"></a>00094 <a name="l00103"></a><a class="code" href="main_8c.html#a6">00103</a> __no_init __regvar <span class="keyword">volatile</span> <a class="code" href="stdint_8h.html#a56">uint8_t</a> <a class="code" href="main_8c.html#a6">sineTableNextSectorStart</a>     @6;<a name="l00104"></a>00104 <a name="l00105"></a>00105 <a name="l00111"></a><a class="code" href="main_8c.html#a7">00111</a> __no_init __regvar <span class="keyword">volatile</span> <a class="code" href="stdint_8h.html#a56">uint8_t</a> <a class="code" href="main_8c.html#a7">speedControllerTimer</a> @5;<a name="l00112"></a>00112 <a name="l00113"></a>00113 <a name="l00114"></a>00114 <span class="preprocessor">#if (SPEED_CONTROL_METHOD == SPEED_CONTROL_CLOSED_LOOP)</span><a name="l00115"></a>00115 <span class="preprocessor"></span><a name="l00116"></a>00116 <a class="code" href="structPID__DATA.html">pidData_t</a> pidParameters;<a name="l00117"></a>00117 <span class="preprocessor">#endif</span><a name="l00118"></a>00118 <span class="preprocessor"></span><a name="l00119"></a>00119 <a name="l00120"></a><a class="code" href="main_8c.html#a8">00120</a> <span class="keywordtype">void</span> <a class="code" href="main_8c.html#a8">main</a>(<span class="keywordtype">void</span>)<a name="l00121"></a>00121 {<a name="l00122"></a>00122   <a class="code" href="main_8c.html#a9">PortsInit</a>();<a name="l00123"></a>00123   <a class="code" href="main_8c.html#a10">PLLInit</a>();<a name="l00124"></a>00124   <a class="code" href="main_8c.html#a11">PWMInit</a>();<a name="l00125"></a>00125   <a class="code" href="main_8c.html#a13">PinChangeInit</a>();<a name="l00126"></a>00126   <a class="code" href="main_8c.html#a12">ADCInit</a>();<a name="l00127"></a>00127 <a name="l00128"></a>00128 <span class="preprocessor">#if (SPEED_CONTROL_METHOD == SPEED_CONTROL_CLOSED_LOOP)</span><a name="l00129"></a>00129 <span class="preprocessor"></span>  <a class="code" href="pid_8c.html#a0">pid_Init</a>(<a class="code" href="PMSM_8h.html#a50">PID_K_P</a>, <a class="code" href="PMSM_8h.html#a51">PID_K_I</a>, <a class="code" href="PMSM_8h.html#a52">PID_K_D</a>, &amp;pidParameters);<a name="l00130"></a>00130 <span class="preprocessor">#endif</span><a name="l00131"></a>00131 <span class="preprocessor"></span><a name="l00132"></a>00132   <a class="code" href="main_8c.html#a7">speedControllerTimer</a>= <a class="code" href="PMSM_8h.html#a49">SPEED_CONTROLLER_TIME_BASE</a>;<a name="l00133"></a>00133   <a class="code" href="main_8c.html#a26">SetAdvanceCommutation</a>(0);<a name="l00134"></a>00134 <a name="l00135"></a>00135   <span class="comment">//Initialize fastflags. Use temporary variable to avoid several accesses to</span><a name="l00136"></a>00136   <span class="comment">//volatile variable.</span><a name="l00137"></a>00137   {<a name="l00138"></a>00138     <a class="code" href="structPMSMflags.html">PMSMflags_t</a> fastFlagsInitial;<a name="l00139"></a>00139 <a name="l00140"></a>00140     <span class="comment">// Set motorStopped to FALSE at startup. This will make sure that the motor</span><a name="l00141"></a>00141     <span class="comment">// is not started if it is not really stopped. If it is stopped, this variable</span><a name="l00142"></a>00142     <span class="comment">// will quickly be updated.</span><a name="l00143"></a>00143     fastFlagsInitial.<a class="code" href="structPMSMflags.html#o3">motorStopped</a> = <a class="code" href="pid_8h.html#a4">FALSE</a>;<a name="l00144"></a>00144 <a name="l00145"></a>00145     <span class="comment">// Set motorSyncronized to FALSE at startup. This will prevent the motor from being</span><a name="l00146"></a>00146     <span class="comment">// driven until the motor is in synch or stopped.</span><a name="l00147"></a>00147     fastFlagsInitial.<a class="code" href="structPMSMflags.html#o4">motorSynchronized</a> = <a class="code" href="pid_8h.html#a4">FALSE</a>;<a name="l00148"></a>00148     fastFlagsInitial.<a class="code" href="structPMSMflags.html#o0">actualDirection</a> = <a class="code" href="PMSM_8h.html#a29">DIRECTION_UNKNOWN</a>;<a name="l00149"></a>00149     fastFlagsInitial.<a class="code" href="structPMSMflags.html#o1">desiredDirection</a> = 0;<a name="l00150"></a>00150     fastFlagsInitial.<a class="code" href="structPMSMflags.html#o2">driveWaveform</a> = <a class="code" href="PMSM_8h.html#a41">WAVEFORM_UNDEFINED</a>;<a name="l00151"></a>00151 <a name="l00152"></a>00152     fastFlags = fastFlagsInitial;<a name="l00153"></a>00153   }<a name="l00154"></a>00154 <a name="l00155"></a>00155   <a class="code" href="main_8c.html#a22">DesiredDirectionUpdate</a>();<a name="l00156"></a>00156 <a name="l00157"></a>00157   <span class="comment">//Enable Timer/counter1 overflow.</span><a name="l00158"></a>00158   TIMSK |= (1 &lt;&lt; TOV1);<a name="l00159"></a>00159 <a name="l00160"></a>00160   <span class="comment">//Enable interrupts globally and let motor driver take over.</span><a name="l00161"></a>00161   __enable_interrupt();<a name="l00162"></a>00162 <a name="l00163"></a>00163   <span class="keywordflow">for</span> (;;)<a name="l00164"></a>00164   {<a name="l00165"></a>00165     <a class="code" href="stdint_8h.html#a58">uint16_t</a> speedReference;<a name="l00166"></a>00166 <a name="l00167"></a>00167     <span class="keywordflow">if</span> ( !(ADCSRA &amp; (1 &lt;&lt; ADSC)) )<a name="l00168"></a>00168     {<a name="l00169"></a>00169       speedReference = ADC;<a name="l00170"></a>00170       ADCSRA |= (1 &lt;&lt; ADSC);<a name="l00171"></a>00171     }<a name="l00172"></a>00172 <a name="l00173"></a>00173     <span class="keywordflow">if</span> (<a class="code" href="main_8c.html#a7">speedControllerTimer</a> == 0)<a name="l00174"></a>00174     {<a name="l00175"></a>00175       <a class="code" href="main_8c.html#a7">speedControllerTimer</a> = <a class="code" href="PMSM_8h.html#a49">SPEED_CONTROLLER_TIME_BASE</a>;<a name="l00176"></a>00176       <a class="code" href="main_8c.html#a14">SpeedController</a>(speedReference);<a name="l00177"></a>00177     }<a name="l00178"></a>00178   }<a name="l00179"></a>00179 }<a name="l00180"></a>00180 <a name="l00181"></a>00181 <a name="l00182"></a>00182 <a name="l00187"></a><a class="code" href="main_8c.html#a9">00187</a> <span class="keyword">static</span> <span class="keywordtype">void</span> <a class="code" href="main_8c.html#a9">PortsInit</a>(<span class="keywordtype">void</span>)<a name="l00188"></a>00188 {<a name="l00189"></a>00189 <span class="preprocessor">#if (HALL_PULL_UP_ENABLE)</span><a name="l00190"></a>00190 <span class="preprocessor"></span>  PORTA = (1 &lt;&lt; <a class="code" href="PMSM_8h.html#a12">H1_PIN</a>) | (1 &lt;&lt; <a class="code" href="PMSM_8h.html#a13">H2_PIN</a>) | (1 &lt;&lt; <a class="code" href="PMSM_8h.html#a14">H3_PIN</a>);<a name="l00191"></a>00191 <span class="preprocessor">#endif</span><a name="l00192"></a>00192 <span class="preprocessor"></span><a name="l00193"></a>00193   DDRA = (1 &lt;&lt; PA4);<a name="l00194"></a>00194 }<a name="l00195"></a>00195 <a name="l00196"></a>00196 <a name="l00203"></a><a class="code" href="main_8c.html#a10">00203</a> <span class="keyword">static</span> <span class="keywordtype">void</span> <a class="code" href="main_8c.html#a10">PLLInit</a>(<span class="keywordtype">void</span>)<a name="l00204"></a>00204 {<a name="l00205"></a>00205   <span class="comment">//Wait for PLL lock</span><a name="l00206"></a>00206   <span class="keywordflow">while</span> ( !(PLLCSR &amp; (1 &lt;&lt; PLOCK)) )<a name="l00207"></a>00207   {<a name="l00208"></a>00208 <a name="l00209"></a>00209   }<a name="l00210"></a>00210   PLLCSR = (1 &lt;&lt; PCKE);<a name="l00211"></a>00211 }<a name="l00212"></a>00212 <a name="l00213"></a>00213 <a name="l00218"></a><a class="code" href="main_8c.html#a11">00218</a> <span class="keyword">static</span> <span class="keywordtype">void</span> <a class="code" href="main_8c.html#a11">PWMInit</a>(<span class="keywordtype">void</span>)<a name="l00219"></a>00219 {<a name="l00220"></a>00220   <span class="comment">//Start TCNT1 with prescaler 1.</span><a name="l00221"></a>00221   TCCR1B = (<a class="code" href="PMSM_8h.html#a26">PWM_INVERT_OUTPUT</a> &lt;&lt; <a class="code" href="PMSM_8h.html#a4">PWM1X</a>) | (1 &lt;&lt; CS10);<a name="l00222"></a>00222 <a name="l00223"></a>00223   <span class="comment">//Enable fault protection input on INT0 pin, falling edge.</span><a name="l00224"></a>00224   TCCR1D = (1 &lt;&lt; FPIE1) | (1 &lt;&lt; FPEN1) | (0 &lt;&lt; FPES1);<a name="l00225"></a>00225 <a name="l00226"></a>00226   <span class="comment">//Set dead-time.</span><a name="l00227"></a>00227   DT1 = (<a class="code" href="PMSM_8h.html#a3">DEAD_TIME</a> &lt;&lt; 4) | (<a class="code" href="PMSM_8h.html#a3">DEAD_TIME</a>);<a name="l00228"></a>00228 <a name="l00229"></a>00229   <span class="comment">//Set PWM top value.</span><a name="l00230"></a>00230   <a class="code" href="TinyX61__macros_8h.html#a12">TC1_WRITE_10_BIT_REGISTER</a>(OCR1C, <a class="code" href="PMSM_8h.html#a35">PWM_TOP_VALUE</a>);<a name="l00231"></a>00231 }<a name="l00232"></a>00232 

⌨️ 快捷键说明

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