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

📄 main_8c-source.html

📁 ATtiny261 461 861 这份资料介绍了执行Attiny261 461 861微控制器系列正弦波驱动三相无刷直流电动机霍尔传感器。
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<a name="l00859"></a>00859   temp = <a class="code" href="PMSM_8h.html#a83">SineTableSmallGetValue</a>(tempIndex);<a name="l00860"></a>00860   <span class="keywordflow">if</span> (temp != 0)<a name="l00861"></a>00861   {<a name="l00862"></a>00862     temp = <a class="code" href="main_8c.html#a36">MultiplyUS15x8</a>(<a class="code" href="main_8c.html#a4">amplitude</a>, temp);<a name="l00863"></a>00863   }<a name="l00864"></a>00864   <span class="keywordflow">if</span> (<a class="code" href="main_8c.html#a19">GetDesiredDirection</a>() == <a class="code" href="PMSM_8h.html#a27">DIRECTION_FORWARD</a>)<a name="l00865"></a>00865   {<a name="l00866"></a>00866     <a class="code" href="TinyX61__macros_8h.html#a12">TC1_WRITE_10_BIT_REGISTER</a>(<a class="code" href="PMSM_8h.html#a24">COMPARE_REGISTER_PHASE_V</a>, temp);<a name="l00867"></a>00867   }<a name="l00868"></a>00868   <span class="keywordflow">else</span><a name="l00869"></a>00869   {<a name="l00870"></a>00870     <a class="code" href="TinyX61__macros_8h.html#a12">TC1_WRITE_10_BIT_REGISTER</a>(<a class="code" href="PMSM_8h.html#a25">COMPARE_REGISTER_PHASE_W</a>, temp);<a name="l00871"></a>00871   }<a name="l00872"></a>00872 }<a name="l00873"></a>00873 <span class="preprocessor">#endif</span><a name="l00874"></a>00874 <span class="preprocessor"></span><a name="l00875"></a>00875 <a name="l00887"></a>00887 <span class="preprocessor">#pragma vector = PCINT0_vect</span><a name="l00888"></a><a class="code" href="main_8c.html#a33">00888</a> <span class="preprocessor"></span>__interrupt <span class="keywordtype">void</span> <a class="code" href="main_8c.html#a33">HallChangeISR</a>()<a name="l00889"></a>00889 {<a name="l00890"></a>00890   <span class="keyword">static</span> <a class="code" href="stdint_8h.html#a56">uint8_t</a> lastHall = 0xff;<a name="l00891"></a>00891   <a class="code" href="stdint_8h.html#a56">uint8_t</a> hall;<a name="l00892"></a>00892 <a name="l00893"></a>00893   hall = <a class="code" href="main_8c.html#a21">GetHall</a>();<a name="l00894"></a>00894 <a name="l00895"></a>00895   <span class="comment">//Make sure that the hall sensors really changed.</span><a name="l00896"></a>00896   <span class="keywordflow">if</span> (hall == lastHall)<a name="l00897"></a>00897   {<a name="l00898"></a>00898     <span class="keywordflow">return</span>;<a name="l00899"></a>00899   }<a name="l00900"></a>00900 <a name="l00901"></a>00901   <a class="code" href="main_8c.html#a30">MotorSynchronizedUpdate</a>();<a name="l00902"></a>00902   <a class="code" href="stdint_8h.html#a56">uint8_t</a> synch = <a class="code" href="main_8c.html#a31">IsMotorSynchronized</a>();<a name="l00903"></a>00903   <span class="keywordflow">if</span> ((fastFlags.driveWaveform != <a class="code" href="PMSM_8h.html#a39">WAVEFORM_SINUSOIDAL</a>) &amp;&amp; (synch))<a name="l00904"></a>00904   {<a name="l00905"></a>00905     <a class="code" href="main_8c.html#a15">TimerSetModeSinusoidal</a>();<a name="l00906"></a>00906   }<a name="l00907"></a>00907 <a name="l00908"></a>00908   <span class="comment">//If sinusoidal driving is used, synchronize sine wave generation to the</span><a name="l00909"></a>00909   <span class="comment">//current hall sensor value. Advance commutation (lead angle) is also</span><a name="l00910"></a>00910   <span class="comment">//added in the process.</span><a name="l00911"></a>00911   <span class="keywordflow">if</span> (fastFlags.driveWaveform == <a class="code" href="PMSM_8h.html#a39">WAVEFORM_SINUSOIDAL</a>)<a name="l00912"></a>00912   {<a name="l00913"></a>00913     <a class="code" href="stdint_8h.html#a58">uint16_t</a> tempIndex;<a name="l00914"></a>00914     <span class="keywordflow">if</span> (<a class="code" href="main_8c.html#a19">GetDesiredDirection</a>() == <a class="code" href="PMSM_8h.html#a27">DIRECTION_FORWARD</a>)<a name="l00915"></a>00915     {<a name="l00916"></a>00916       tempIndex = (<a class="code" href="PMSM__tables_8h.html#a5">CSOffsetsForward</a>[hall] + <a class="code" href="main_8c.html#a5">advanceCommutationSteps</a>) &lt;&lt; 8;<a name="l00917"></a>00917     }<a name="l00918"></a>00918     <span class="keywordflow">else</span><a name="l00919"></a>00919     {<a name="l00920"></a>00920       tempIndex = (<a class="code" href="PMSM__tables_8h.html#a6">CSOffsetsReverse</a>[hall] + <a class="code" href="main_8c.html#a5">advanceCommutationSteps</a>) &lt;&lt; 8;<a name="l00921"></a>00921     }<a name="l00922"></a>00922     <a class="code" href="main_8c.html#a2">sineTableIndex</a> = tempIndex;<a name="l00923"></a>00923 <a name="l00924"></a>00924     <span class="comment">//Adjust next sector start index. It might be set to a value larger than</span><a name="l00925"></a>00925     <span class="comment">//SINE_TABLE_LENGTH at this point. This is adjusted in AdjustSineTableIndex</span><a name="l00926"></a>00926     <span class="comment">//and should not be done here, as it will cause problems when advance</span><a name="l00927"></a>00927     <span class="comment">//commutation is used.</span><a name="l00928"></a>00928     <a class="code" href="main_8c.html#a6">sineTableNextSectorStart</a> = (tempIndex &gt;&gt; 8) + <a class="code" href="PMSM_8h.html#a34">TABLE_ELEMENTS_PER_COMMUTATION_SECTOR</a>;<a name="l00929"></a>00929 <a name="l00930"></a>00930   }<a name="l00931"></a>00931   <span class="comment">//If block commutation is used. Commutate according to hall signal.</span><a name="l00932"></a>00932   <span class="keywordflow">else</span> <span class="keywordflow">if</span> (fastFlags.driveWaveform == <a class="code" href="PMSM_8h.html#a38">WAVEFORM_BLOCK_COMMUTATION</a>)<a name="l00933"></a>00933   {<a name="l00934"></a>00934     <a class="code" href="main_8c.html#a20">BlockCommutate</a>(<a class="code" href="main_8c.html#a19">GetDesiredDirection</a>(), hall);<a name="l00935"></a>00935   }<a name="l00936"></a>00936 <a name="l00937"></a>00937   <span class="comment">//Update the actual direction flag.</span><a name="l00938"></a>00938   <a class="code" href="main_8c.html#a23">ActualDirectionUpdate</a>(lastHall, hall);<a name="l00939"></a>00939 <a name="l00940"></a>00940   lastHall = hall;<a name="l00941"></a>00941 <a name="l00942"></a>00942 <a name="l00943"></a>00943   <span class="comment">//Calculate new step size for sine wave generation and reset commutation</span><a name="l00944"></a>00944   <span class="comment">//timer.</span><a name="l00945"></a>00945   <a class="code" href="main_8c.html#a1">sineTableIncrement</a> = <a class="code" href="main_8c.html#a24">SineTableIncrementCalculate</a>(<a class="code" href="main_8c.html#a3">commutationTicks</a>);<a name="l00946"></a>00946   <a class="code" href="main_8c.html#a3">commutationTicks</a> = 0;<a name="l00947"></a>00947 <a name="l00948"></a>00948   <span class="comment">//Since the hall sensors are changing, the motor can not be stopped.</span><a name="l00949"></a>00949   fastFlags.motorStopped = <a class="code" href="pid_8h.html#a4">FALSE</a>;<a name="l00950"></a>00950 }<a name="l00951"></a>00951 <a name="l00952"></a>00952 <a name="l00964"></a>00964 <span class="preprocessor">#pragma vector = TIM1_OVF_vect</span><a name="l00965"></a><a class="code" href="main_8c.html#a34">00965</a> <span class="preprocessor"></span>__interrupt <span class="keywordtype">void</span> <a class="code" href="main_8c.html#a34">Timer1OverflowISR</a>()<a name="l00966"></a>00966 {<a name="l00967"></a>00967   PORTA |= (1 &lt;&lt; PA4);<a name="l00968"></a>00968   <span class="keywordflow">if</span> (fastFlags.driveWaveform == <a class="code" href="PMSM_8h.html#a39">WAVEFORM_SINUSOIDAL</a>)<a name="l00969"></a>00969   {<a name="l00970"></a>00970     <a class="code" href="main_8c.html#a25">AdjustSineTableIndex</a>(<a class="code" href="main_8c.html#a1">sineTableIncrement</a>);<a name="l00971"></a>00971     <a class="code" href="main_8c.html#a32">SineOutputUpdate</a>();<a name="l00972"></a>00972   }<a name="l00973"></a>00973   <span class="keywordflow">else</span> <span class="keywordflow">if</span> (fastFlags.driveWaveform == <a class="code" href="PMSM_8h.html#a38">WAVEFORM_BLOCK_COMMUTATION</a>)<a name="l00974"></a>00974   {<a name="l00975"></a>00975     <a class="code" href="stdint_8h.html#a58">uint16_t</a> blockCommutationDuty = <a class="code" href="main_8c.html#a4">amplitude</a> * <a class="code" href="PMSM_8h.html#a42">BLOCK_COMMUTATION_DUTY_MULTIPLIER</a>;<a name="l00976"></a>00976 <a name="l00977"></a>00977     <span class="keywordflow">if</span> (blockCommutationDuty &gt; <a class="code" href="PMSM_8h.html#a35">PWM_TOP_VALUE</a>)<a name="l00978"></a>00978     {<a name="l00979"></a>00979       blockCommutationDuty = <a class="code" href="PMSM_8h.html#a35">PWM_TOP_VALUE</a>;<a name="l00980"></a>00980     }<a name="l00981"></a>00981     <a class="code" href="main_8c.html#a18">BlockCommutationSetDuty</a>(blockCommutationDuty);<a name="l00982"></a>00982   }<a name="l00983"></a>00983 <a name="l00984"></a>00984   <span class="comment">//Update desired direction flag.</span><a name="l00985"></a>00985   <a class="code" href="main_8c.html#a22">DesiredDirectionUpdate</a>();<a name="l00986"></a>00986 <a name="l00987"></a>00987   <span class="keyword">static</span> <a class="code" href="stdint_8h.html#a56">uint8_t</a> lastDesiredDirection = 0xff;<a name="l00988"></a>00988   <span class="keywordflow">if</span> ( (fastFlags.desiredDirection != lastDesiredDirection))<a name="l00989"></a>00989   {<a name="l00990"></a>00990 <span class="preprocessor">#if (TURN_MODE == TURN_MODE_COAST)</span><a name="l00991"></a>00991 <span class="preprocessor"></span>    <span class="comment">//Disable driver signals to let motor coast. The motor will automatically</span><a name="l00992"></a>00992     <span class="comment">//start once it is synchronized or stopped.</span><a name="l00993"></a>00993     <a class="code" href="main_8c.html#a28">DisablePWMOutputs</a>();<a name="l00994"></a>00994     fastFlags.motorSynchronized = <a class="code" href="pid_8h.html#a4">FALSE</a>;<a name="l00995"></a>00995     fastFlags.driveWaveform = <a class="code" href="PMSM_8h.html#a41">WAVEFORM_UNDEFINED</a>;<a name="l00996"></a>00996 <span class="preprocessor">#endif</span><a name="l00997"></a>00997 <span class="preprocessor"></span><a name="l00998"></a>00998 <span class="preprocessor">#if (TURN_MODE == TURN_MODE_BRAKE)</span><a name="l00999"></a>00999 <span class="preprocessor"></span>    <span class="comment">//Set motor in brake mode. The motor will automatically start once it is</span><a name="l01000"></a>01000     <span class="comment">//synchronized or stopped.</span><a name="l01001"></a>01001       fastFlags.motorSynchronized = <a class="code" href="pid_8h.html#a4">FALSE</a>;<a name="l01002"></a>01002     <span class="keywordflow">if</span> (fastFlags.actualDirection != <a class="code" href="PMSM_8h.html#a29">DIRECTION_UNKNOWN</a>)<a name="l01003"></a>01003     {<a name="l01004"></a>01004       <a class="code" href="main_8c.html#a17">TimerSetModeBrake</a>(); <span class="comment">// Automatically sets driveWaveform.</span><a name="l01005"></a>01005     }<a name="l01006"></a>01006 <span class="preprocessor">#endif</span><a name="l01007"></a>01007 <span class="preprocessor"></span><a name="l01008"></a>01008     lastDesiredDirection = fastFlags.desiredDirection;<a name="l01009"></a>01009   }<a name="l01010"></a>01010 <a name="l01011"></a>01011   <a class="code" href="main_8c.html#a29">CommutationTicksUpdate</a>();<a name="l01012"></a>01012 <a name="l01013"></a>01013   <span class="keywordflow">if</span> (<a class="code" href="main_8c.html#a7">speedControllerTimer</a> &gt; 0)<a name="l01014"></a>01014   {<a name="l01015"></a>01015     <a class="code" href="main_8c.html#a7">speedControllerTimer</a>--;<a name="l01016"></a>01016   }<a name="l01017"></a>01017   PORTA &amp;= ~(1 &lt;&lt; PA4);<a name="l01018"></a>01018 }<a name="l01019"></a>01019 <a name="l01020"></a>01020 <a name="l01028"></a>01028 <span class="preprocessor">#pragma vector = FAULT_PROTECTION</span><a name="l01029"></a><a class="code" href="main_8c.html#a35">01029</a> <span class="preprocessor"></span>__interrupt <span class="keywordtype">void</span> <a class="code" href="main_8c.html#a35">FaultProtectionISR</a>()<a name="l01030"></a>01030 {<a name="l01031"></a>01031   __delay_cycles(10000000);<a name="l01032"></a>01032   TCCR1D |= (1 &lt;&lt; FPEN1);<a name="l01033"></a>01033 <span class="preprocessor">#if (SPEED_CONTROL_METHOD == SPEED_CONTROL_CLOSED_LOOP)</span><a name="l01034"></a>01034 <span class="preprocessor"></span>  <a class="code" href="pid_8c.html#a2"

⌨️ 快捷键说明

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