OpenCores
URL https://opencores.org/ocsvn/q_rotary_enc/q_rotary_enc/trunk

Subversion Repositories q_rotary_enc

[/] [q_rotary_enc/] [rot_enc_flt.sv] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 Papayaved
/*
2
        Counter for quadrature rotary encoder with low-pass filter
3
*/
4
 
5
`ifndef _rot_enc_flt_
6
`define _rot_enc_flt_
7
`include "lpf_cap.sv"
8
`include "q_rotary_enc.sv"
9
 
10
module rot_enc_flt(
11
        input clock,
12
        input sclr,
13
        input dir, // main direction
14
        input A, B,
15
        output signed [31:0] bidir_counter,
16
        output error,
17
        output ready // counter enabled
18
);
19
        reg A_reg = 1'b0, B_reg = 1'b0;
20
        wire A_flt, B_flt;
21
        reg [1:0] dir_reg = '0;
22
        wire dir_changed;
23
        wire [1:0] init;
24
 
25
        // to trigger A, B signals on input pins
26
        always_ff @(posedge clock) begin
27
                A_reg <= A;
28
                B_reg <= B;
29
                dir_reg <= {dir_reg[0], dir};
30
        end
31
 
32
        assign dir_changed = dir_reg[1] ^ dir_reg[0];
33
 
34
        // debouncing
35
        lpf_cap #(7) // 2^7 - it's about 1/8 of min encoder period
36
                f0(.clock, .sclr(dir_changed), .in(A_reg), .out(A_flt), .init(init[0])),
37
                f1(.clock, .sclr(dir_changed), .in(B_reg), .out(B_flt), .init(init[1]));
38
 
39
        reg ready_reg = 1'b0;
40
        always_ff @(posedge clock)
41
                ready_reg <= (dir_changed) ? 1'b0 : init == '0;
42
 
43
        assign ready = ready_reg;
44
 
45
        q_rotary_enc enc_inst(
46
                .clock, .sclr(sclr || dir_changed), .ena(init == '0), .dir(dir_reg[0]),
47
                .A(A_flt), .B(B_flt),
48
                .bidir_counter,
49
                .error
50
        );
51
 
52
endmodule :rot_enc_flt
53
 
54
`endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.