设为首页收藏本站淘宝杂货铺

从F到0 - From F to 0

 找回密码
 注册已关闭
搜索
查看: 235|回复: 0
收起左侧

Quartus Verilog HDL/FPGA 实现驱动 EC11 增量式360度旋转编码器 并演示3位二进制加减

[复制链接]
发表于 2023-10-4 10:51:03 | 显示全部楼层 |阅读模式
本帖最后由 HDL 于 2023-10-4 11:03 编辑

main.png
  1. module ec11(                                //增量式360度旋转编码器驱动模块
  2.         input clk,                                //1Khz 时钟输入
  3.         input A,                                        //相位A输入 (CLK)
  4.         input B,                                        //相位B输入 (DT)
  5.         output reg pos,                //顺时针旋转 发出上升沿脉冲
  6.         output reg neg                        //逆时针旋转 发出上升沿脉冲
  7. );
  8. reg a,b;
  9. always @(posedge clk) begin
  10.         case({b,a,B,A})
  11.         4'b0001:pos <= 1'b1;
  12.         4'b0111:pos <= 1'b1;
  13.         4'b1110:pos <= 1'b1;
  14.         4'b1000:pos <= 1'b1;
  15.         4'b0010:neg <= 1'b1;
  16.         4'b1011:neg <= 1'b1;
  17.         4'b1101:neg <= 1'b1;
  18.         4'b0100:neg <= 1'b1;
  19.         4'b1111:{pos,neg} <= 2'b00;
  20.         4'b0000:{pos,neg} <= 2'b00;
  21.         endcase
  22.         a <= A;
  23.         b <= B;
  24. end
  25. endmodule

  26. module main(
  27.         input clk,                //50Mhz时钟输入 Pin17
  28.         //input配置上拉电阻模式,否则可能会受到干扰
  29.         input A,                        //A相 (CLK) Pin40
  30.         input B,                        //B相 (DT)        Pin42
  31.         input SW,                //编码器按键 (SW) Pin44(低电平按下)
  32.         
  33.         output led1,        //LED1(低电平点亮)Pin3
  34.         output led2,        //LED2 Pin7
  35.         output led3                //LED2 Pin9
  36. );

  37. //50Mhz分频1Khz
  38. reg [14:0] i = 15'd0;                //15位宽
  39. reg clk_1khz;
  40. always @(posedge clk) begin
  41.         i = i + 15'd1;
  42.         if(i >= 15'd25000) begin
  43.          i = 15'd0;
  44.          clk_1khz <= !clk_1khz;
  45.         end
  46. end

  47. wire pos;
  48. wire neg;

  49. ec11 ec11(
  50.         .clk(clk_1khz),
  51.         .A(A),
  52.         .B(B),
  53.         .pos(pos),
  54.         .neg(neg)
  55. );

  56. reg [2:0] count = 3'd0;
  57. assign {led3,led2,led1} = ~count;
  58. always @(posedge (pos|neg) or negedge SW) begin
  59.         if(!SW) begin
  60.                 count <= 3'd0;
  61.         end else begin
  62.                 if(pos) begin
  63.                         count <= count + 3'd1;
  64.                 end else if(neg) begin
  65.                         count <= count - 3'd1;               
  66.                 end
  67.         end
  68. end
  69. endmodule
复制代码

相关帖子

您需要登录后才可以回帖 登录 | 注册已关闭

本版积分规则

QQ|手机版|Archiver|从F到0 ( 蒙ICP备17002595号-1 )
蒙公网安备15010402000325号

腾讯云安全认证

GMT+8, 2024-4-27 14:17 , Processed in 1.308074 second(s), 22 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表