STM32定时器---正交编码器模式详解

<div id="article_content" class="article_content clearfix csdn-tracking-statistics" data-pid="blog" data-mod="popu_307" data-dsm="post">
                    <div class="markdown_views">
                <p>编码器分类: <br>
按工作原理:光电式、磁电式和触点电刷式 <br>
按码盘的刻孔方式:增量式和绝对式两类 <br>
由于博主接触面还不是很广,一共就用过两个种类的编码器,都是属于光电的  <br>
差分编码器:一般由8根线连接   信号线分别为  A+  A-   B+   B-  Z+  Z-  以及VCC和GND <br>
                  这里有一种不需要Z信号的,6线差分A+  A-  B+  B-  VCC 和GND <br>
正交编码器:一般是5根线连接,信号线分别为A  B   Z  VCC和GND</p>


<p>编码器线数:  就是旋转一圈你的A(B)会输出多少个脉冲 ,这里的A  B就是上面的输出脉冲信号线,它们转一圈发出的脉冲数一样的,不过存在90°相位差   通常都是360线的   线数越高代表编码器能够反应的位置精度越高</p>


<p><img src="https://img-blog.csdn.net/20160125151941378" alt="这里写图片描述" title=""></p>


<p>相位差为90° 通过判断哪个信号在前  哪个信号在后 来决定TIM-&gt;COUNT是++  还是 – <br>
360线  AB一圈各为360个,Z信号为一圈一个</p>


<p>编码器信号: <br>
A       脉冲输出 <br>
B       脉冲输出 <br>
Z       零点信号  当编码器旋转到零点时,Z信号会发出一个脉冲表示现在是零位置  这个零点位置是固定,厂商指定的 <br>
VCC     电源通常分为24V的和5V的 <br>
GND    地线</p>


<p>这里需要注意: <br>
1.这里的正交编码器是如果是24V的工作电压还需要用光耦隔离,24V转为3V3在接到STM32的定时器两个通道上 <br>
2.脉冲输出是OC门输出,需要上拉电阻 <br>
3.Z信号接到STM32的外部中断口上,很容易受到干扰 ,通常需要接一个电容到GND</p>


<p>这里给出一个24V转3.3V的隔离电路,用到的是6N136光耦</p>


<p><img src="https://img-blog.csdn.net/20160125150859954" alt="这里写图片描述" title=""></p>


<p>硬件连接(这里使用的STM32F103ZET6的TIM4的CH1和CH2): <br>
 PB6–A <br>
 PB7–B <br>
 PA1–Z</p>


<p><img src="https://img-blog.csdn.net/20160125153115086" alt="这里写图片描述" title=""></p>


<p>代码详解: <br>
TIM4初始化代码如下:</p>






<pre class="prettyprint" name="code"><code class="hljs fsharp has-numbering">#include <span class="hljs-string">"stm32f10x.h"</span>
#include <span class="hljs-string">"encode.h"</span>
#include <span class="hljs-string">"misc.h"</span>
#include <span class="hljs-string">"nvic.h"</span>
#include <span class="hljs-string">"sys.h"</span> 
#include <span class="hljs-string">"delay.h"</span>


<span class="hljs-keyword">void</span> TIM4_Mode_Config(<span class="hljs-keyword">void</span>)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_ICInitTypeDef TIM_ICInitStructure;      


    <span class="hljs-comment">//PB6 ch1  A,PB7 ch2 </span>
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);<span class="hljs-comment">//使能TIM4时钟  </span>
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);<span class="hljs-comment">//使能GPIOA时钟</span>


    GPIO_StructInit(&amp;GPIO_InitStructure);<span class="hljs-comment">//将GPIO_InitStruct中的参数按缺省值输入</span>
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;         
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;<span class="hljs-comment">//PA6 PA7浮空输入  </span>
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &amp;GPIO_InitStructure);                           


    NVIC_Config(<span class="hljs-number">2</span>);


    TIM_DeInit(TIM4);
    TIM_TimeBaseStructInit(&amp;TIM_TimeBaseStructure);
    TIM_TimeBaseStructure.TIM_Period = <span class="hljs-number">359</span>*<span class="hljs-number">4</span>;  <span class="hljs-comment">//设定计数器重装值   TIMx_ARR = 359*4</span>
    TIM_TimeBaseStructure.TIM_Prescaler = <span class="hljs-number">0</span>; <span class="hljs-comment">//TIM3时钟预分频值</span>
    TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1 ;<span class="hljs-comment">//设置时钟分割 T_dts = T_ck_int    </span>
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; <span class="hljs-comment">//TIM向上计数 </span>
    TIM_TimeBaseInit(TIM4, &amp;TIM_TimeBaseStructure);              


    TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_BothEdge ,TIM_ICPolarity_BothEdge);<span class="hljs-comment">//使用编码器模式3,上升下降都计数</span>
    TIM_ICStructInit(&amp;TIM_ICInitStructure);<span class="hljs-comment">//将结构体中的内容缺省输入</span>
    TIM_ICInitStructure.TIM_ICFilter = <span class="hljs-number">6</span>;  <span class="hljs-comment">//选择输入比较滤波器 </span>
    TIM_ICInit(TIM4, &amp;TIM_ICInitStructure);<span class="hljs-comment">//将TIM_ICInitStructure中的指定参数初始化TIM3</span>


<span class="hljs-comment">//  TIM_ARRPreloadConfig(TIM4, ENABLE);//使能预装载</span>
    TIM_ClearFlag(TIM4, TIM_FLAG_Update);<span class="hljs-comment">//清除TIM3的更新标志位</span>
    TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);<span class="hljs-comment">//运行更新中断</span>
    <span class="hljs-comment">//Reset counter</span>
    TIM4-&gt;CNT = <span class="hljs-number">0</span>;<span class="hljs-comment">//</span>


    TIM_Cmd(TIM4, ENABLE);   <span class="hljs-comment">//启动TIM4定时器</span>


}
/*  
<span class="hljs-keyword">void</span> TIM3_Mode_Config(<span class="hljs-keyword">void</span>)
{
    <span class="hljs-comment">///TIM3 clock source enable </span>
    RCC-&gt;APB1ENR|=<span class="hljs-number">1</span>&lt;&lt;<span class="hljs-number">1</span>;       <span class="hljs-comment">//TIM3时钟使能</span>
    <span class="hljs-comment">// Enable 1GPIOA, clock </span>
    RCC-&gt;APB2ENR|=<span class="hljs-number">1</span>&lt;&lt;<span class="hljs-number">2</span>;    <span class="hljs-comment">//使能PORTA时钟</span>


    <span class="hljs-comment">// Configure PA.06,07 as encoder input </span>
    GPIOA-&gt;CRL&amp;=<span class="hljs-number">0XF0FFFFFF</span>;<span class="hljs-comment">//PA6</span>
    GPIOA-&gt;CRL|=<span class="hljs-number">0X04000000</span>;<span class="hljs-comment">//浮空输入</span>
    GPIOA-&gt;CRL&amp;=<span class="hljs-number">0X0FFFFFFF</span>;<span class="hljs-comment">//PA7</span>
    GPIOA-&gt;CRL|=<span class="hljs-number">0X40000000</span>;<span class="hljs-comment">//浮空输入</span>


    <span class="hljs-comment">// Enable the TIM3 Update Interrupt </span>
    <span class="hljs-comment">//这两个东东要同时设置才可以使用中断</span>
    TIM3-&gt;DIER|=<span class="hljs-number">1</span>&lt;&lt;<span class="hljs-number">0</span>;   <span class="hljs-comment">//允许更新中断                </span>
    TIM3-&gt;DIER|=<span class="hljs-number">1</span>&lt;&lt;<span class="hljs-number">6</span>;   <span class="hljs-comment">//允许触发中断</span>


    TIM3_NVIC_Config();




    <span class="hljs-comment">//Timer configuration in Encoder mode </span>
    TIM3-&gt;PSC = <span class="hljs-number">0x0</span>;<span class="hljs-comment">//预分频器</span>
    TIM3-&gt;ARR = <span class="hljs-number">15</span>-<span class="hljs-number">1</span>;<span class="hljs-comment">//设定计数器自动重装值 </span>
    TIM3-&gt;CR1 &amp;=~(<span class="hljs-number">3</span>&lt;&lt;<span class="hljs-number">8</span>);<span class="hljs-comment">// 选择时钟分频:不分频</span>
    TIM3-&gt;CR1 &amp;=~(<span class="hljs-number">3</span>&lt;&lt;<span class="hljs-number">5</span>);<span class="hljs-comment">// 选择计数模式:边沿对齐模式</span>


    TIM3-&gt;CCMR1 |= <span class="hljs-number">1</span>&lt;&lt;<span class="hljs-number">0</span>; <span class="hljs-comment">//CC1S='01' IC1FP1映射到TI1</span>
    TIM3-&gt;CCMR1 |= <span class="hljs-number">1</span>&lt;&lt;<span class="hljs-number">8</span>; <span class="hljs-comment">//CC2S='01' IC2FP2映射到TI2</span>
    TIM3-&gt;CCER &amp;= ~(<span class="hljs-number">1</span>&lt;&lt;<span class="hljs-number">1</span>);  <span class="hljs-comment">//CC1P='0'  IC1FP1不反相,IC1FP1=TI1</span>
    TIM3-&gt;CCER &amp;= ~(<span class="hljs-number">1</span>&lt;&lt;<span class="hljs-number">5</span>);  <span class="hljs-comment">//CC2P='0'  IC2FP2不反相,IC2FP2=TI2</span>
    TIM3-&gt;CCMR1 |= <span class="hljs-number">3</span>&lt;&lt;<span class="hljs-number">4</span>; <span class="hljs-comment">// IC1F='1000' 输入捕获1滤波器</span>
    TIM3-&gt;SMCR |= <span class="hljs-number">3</span>&lt;&lt;<span class="hljs-number">0</span>;  <span class="hljs-comment">//SMS='011' 所有的输入均在上升沿和下降沿有效</span>
    TIM3-&gt;CNT = <span class="hljs-number">0</span>;
    TIM3-&gt;CR1 |= <span class="hljs-number">0x01</span>;    <span class="hljs-comment">//CEN=1,使能定时器</span>


}*/


<span class="hljs-keyword">void</span> TIM4_Init(<span class="hljs-keyword">void</span>)
{
  TIM4_Mode_Config();
}
</code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li><li style="color: rgb(153, 153, 153);">20</li><li style="color: rgb(153, 153, 153);">21</li><li style="color: rgb(153, 153, 153);">22</li><li style="color: rgb(153, 153, 153);">23</li><li style="color: rgb(153, 153, 153);">24</li><li style="color: rgb(153, 153, 153);">25</li><li style="color: rgb(153, 153, 153);">26</li><li style="color: rgb(153, 153, 153);">27</li><li style="color: rgb(153, 153, 153);">28</li><li style="color: rgb(153, 153, 153);">29</li><li style="color: rgb(153, 153, 153);">30</li><li style="color: rgb(153, 153, 153);">31</li><li style="color: rgb(153, 153, 153);">32</li><li style="color: rgb(153, 153, 153);">33</li><li style="color: rgb(153, 153, 153);">34</li><li style="color: rgb(153, 153, 153);">35</li><li style="color: rgb(153, 153, 153);">36</li><li style="color: rgb(153, 153, 153);">37</li><li style="color: rgb(153, 153, 153);">38</li><li style="color: rgb(153, 153, 153);">39</li><li style="color: rgb(153, 153, 153);">40</li><li style="color: rgb(153, 153, 153);">41</li><li style="color: rgb(153, 153, 153);">42</li><li style="color: rgb(153, 153, 153);">43</li><li style="color: rgb(153, 153, 153);">44</li><li style="color: rgb(153, 153, 153);">45</li><li style="color: rgb(153, 153, 153);">46</li><li style="color: rgb(153, 153, 153);">47</li><li style="color: rgb(153, 153, 153);">48</li><li style="color: rgb(153, 153, 153);">49</li><li style="color: rgb(153, 153, 153);">50</li><li style="color: rgb(153, 153, 153);">51</li><li style="color: rgb(153, 153, 153);">52</li><li style="color: rgb(153, 153, 153);">53</li><li style="color: rgb(153, 153, 153);">54</li><li style="color: rgb(153, 153, 153);">55</li><li style="color: rgb(153, 153, 153);">56</li><li style="color: rgb(153, 153, 153);">57</li><li style="color: rgb(153, 153, 153);">58</li><li style="color: rgb(153, 153, 153);">59</li><li style="color: rgb(153, 153, 153);">60</li><li style="color: rgb(153, 153, 153);">61</li><li style="color: rgb(153, 153, 153);">62</li><li style="color: rgb(153, 153, 153);">63</li><li style="color: rgb(153, 153, 153);">64</li><li style="color: rgb(153, 153, 153);">65</li><li style="color: rgb(153, 153, 153);">66</li><li style="color: rgb(153, 153, 153);">67</li><li style="color: rgb(153, 153, 153);">68</li><li style="color: rgb(153, 153, 153);">69</li><li style="color: rgb(153, 153, 153);">70</li><li style="color: rgb(153, 153, 153);">71</li><li style="color: rgb(153, 153, 153);">72</li><li style="color: rgb(153, 153, 153);">73</li><li style="color: rgb(153, 153, 153);">74</li><li style="color: rgb(153, 153, 153);">75</li><li style="color: rgb(153, 153, 153);">76</li><li style="color: rgb(153, 153, 153);">77</li><li style="color: rgb(153, 153, 153);">78</li><li style="color: rgb(153, 153, 153);">79</li><li style="color: rgb(153, 153, 153);">80</li><li style="color: rgb(153, 153, 153);">81</li><li style="color: rgb(153, 153, 153);">82</li><li style="color: rgb(153, 153, 153);">83</li><li style="color: rgb(153, 153, 153);">84</li><li style="color: rgb(153, 153, 153);">85</li><li style="color: rgb(153, 153, 153);">86</li><li style="color: rgb(153, 153, 153);">87</li><li style="color: rgb(153, 153, 153);">88</li><li style="color: rgb(153, 153, 153);">89</li><li style="color: rgb(153, 153, 153);">90</li><li style="color: rgb(153, 153, 153);">91</li></ul></pre>


<p>这里的NVIC_Config(2)是我个人写的一种多种中断配置的方法单独放在nvic.c中需要了解的可以自己看看工程</p>


<p>这里通常要问的是两点 <br>
1.TIM_TimeBaseStructure.TIM_Period = 359*4 <br>
2.TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_BothEdge ,TIM_ICPolarity_BothEdge);//使用编码器模式3,上升下降都计数</p>


<p>很多人不理解为要360线的编码器为什么这里的重装值乘以4  读出来的为什么又要/4,其实这两个要结合起来解释 <br>
首先看看这个函数TIM_EncoderInterfaceConfig,它有4个参数 <br>
1.选择哪个定时器  即TIM4 <br>
2.编码器模式有三种 见下图 <br>
3.TIM_IC1的极性 <br>
4.TIM_IC2的极性 <br>
这里设置的是编码器模式3,且TI1和TI2都是双边沿触发–即上下边沿都计数</p>


<p><img src="https://img-blog.csdn.net/20160125152225895" alt="这里写图片描述" title=""></p>


<p>还有一个大家不是很懂的图,我来分析一下</p>


<p><img src="https://img-blog.csdn.net/20160125154325419" alt="这里写图片描述" title=""></p>


<p>1.有效边沿  其实就是对应上面设置的编码器的三种模式 <br>
2.相对信号的电平,这里没有理解手册意思,我把它理解为于它的高低电平意味着将PB6和PB7接口对换,PB7接A  PB6接B   这样一来就意味着原来的正转变成反转  计数上升变为下降</p>


<p><img src="https://img-blog.csdn.net/20160125171327688" alt="这里写图片描述" title=""></p>


<p><img src="https://img-blog.csdn.net/20160125163354985" alt="这里写图片描述" title=""></p>


<p>TIx  就相当于输入信号的 TIM4-&gt;CH1  TIM4-&gt;CH2 <br>
TIxF    滤波后信号 <br>
TIxFPx经过带极性选择的边缘检测器过后的产生的信号</p>


<p>3.至于TI1FP1和TI2FP2信号在上身沿计数还是下降沿计数受两点影响  极性(是否反向)  边缘检测(上升沿还是下降沿) <br>
我们这里设置的是不反向  在双边沿计数,即在A上升下降 B的上身下降都计数</p>


<p>而计数为什么是x4倍 ,下图结合上面的配置详细说明了</p>


<p><img src="https://img-blog.csdn.net/20160125164936544" alt="这里写图片描述" title=""></p>


<p>由此完成了编码器的配置</p>


<p>至于读取编码器角度的时间,要根据实际需要来设置</p>


<p>编码器线数为   w线/圈 <br>
转速为  V  圈/min <br>
读取间隔时间 t(线间隔时间)</p>


<p>t &lt;= 60/WV   单位为秒</p>


<p>还有Z信号归零,在遇到Z信号的时候,将定时器的CNT=0,这样就能保证位置与CNT实际对应上了 <br>
中断代码如下</p>






<pre class="prettyprint" name="code"><code class="hljs lasso has-numbering"><span class="hljs-comment">//外部中断1,编码器Z相归零  优先级--①  0 0</span>
<span class="hljs-literal">void</span> EXTI1_IRQHandler(<span class="hljs-literal">void</span>)
{


    TIM4<span class="hljs-subst">-&gt;</span>CNT <span class="hljs-subst">=</span> <span class="hljs-number">0</span>;<span class="hljs-comment">//每次遇到相对零(Z信号)就将计数归0</span>
    TIM_Cmd(TIM4, ENABLE);
    EXTI_ClearITPendingBit(EXTI_Line1);


}




<span class="hljs-comment">//编码器接口模式     优先级--2   1  1</span>
<span class="hljs-literal">void</span> TIM4_IRQHandler(<span class="hljs-literal">void</span>)
{   
    <span class="hljs-keyword">if</span>(TIM4<span class="hljs-subst">-&gt;</span>SR<span class="hljs-subst">&amp;</span><span class="hljs-number">0x0001</span>)<span class="hljs-comment">//溢出中断</span>
    {
        ;
    }   
    TIM4<span class="hljs-subst">-&gt;</span>SR<span class="hljs-subst">&amp;=</span>~(<span class="hljs-number">1</span><span class="hljs-subst">&lt;&lt;</span><span class="hljs-number">0</span>);<span class="hljs-comment">//清除中断标志位    </span>
}</code><ul class="pre-numbering"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li><li style="color: rgb(153, 153, 153);">20</li></ul></pre>


<p>最后附上工程代码百度云盘跟CSDN下载地址: <br>
百度云盘:<a href="http://pan.baidu.com/s/1bowhzDP" rel="nofollow" target="_blank">http://pan.baidu.com/s/1bowhzDP</a> <br>
CSDN:<a href="http://download.csdn.net/detail/wang328452854/9417395" rel="nofollow" target="_blank">http://download.csdn.net/detail/wang328452854/9417395</a></p>            </div>
            <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/markdown_views-ea0013b516.css">
                </div>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值