|
本帖最后由 HDL 于 2023-10-4 11:03 编辑
- module ec11( //增量式360度旋转编码器驱动模块
- input clk, //1Khz 时钟输入
- input A, //相位A输入 (CLK)
- input B, //相位B输入 (DT)
- output reg pos, //顺时针旋转 发出上升沿脉冲
- output reg neg //逆时针旋转 发出上升沿脉冲
- );
- reg a,b;
- always @(posedge clk) begin
- case({b,a,B,A})
- 4'b0001:pos <= 1'b1;
- 4'b0111:pos <= 1'b1;
- 4'b1110:pos <= 1'b1;
- 4'b1000:pos <= 1'b1;
- 4'b0010:neg <= 1'b1;
- 4'b1011:neg <= 1'b1;
- 4'b1101:neg <= 1'b1;
- 4'b0100:neg <= 1'b1;
- 4'b1111:{pos,neg} <= 2'b00;
- 4'b0000:{pos,neg} <= 2'b00;
- endcase
- a <= A;
- b <= B;
- end
- endmodule
- module main(
- input clk, //50Mhz时钟输入 Pin17
- //input配置上拉电阻模式,否则可能会受到干扰
- input A, //A相 (CLK) Pin40
- input B, //B相 (DT) Pin42
- input SW, //编码器按键 (SW) Pin44(低电平按下)
-
- output led1, //LED1(低电平点亮)Pin3
- output led2, //LED2 Pin7
- output led3 //LED2 Pin9
- );
- //50Mhz分频1Khz
- reg [14:0] i = 15'd0; //15位宽
- reg clk_1khz;
- always @(posedge clk) begin
- i = i + 15'd1;
- if(i >= 15'd25000) begin
- i = 15'd0;
- clk_1khz <= !clk_1khz;
- end
- end
- wire pos;
- wire neg;
- ec11 ec11(
- .clk(clk_1khz),
- .A(A),
- .B(B),
- .pos(pos),
- .neg(neg)
- );
- reg [2:0] count = 3'd0;
- assign {led3,led2,led1} = ~count;
- always @(posedge (pos|neg) or negedge SW) begin
- if(!SW) begin
- count <= 3'd0;
- end else begin
- if(pos) begin
- count <= count + 3'd1;
- end else if(neg) begin
- count <= count - 3'd1;
- end
- end
- end
- endmodule
复制代码
|
|