This Verilog HDL module is designed to meet the needs of many university students in their electronic instrumentation laboratories, where they often need to measure the angle, velocity, or position of a DC motor using an encoder.
Description of inputs and outputs of the HDL module:
Inputs:
- A: First input from the encoder.
- B: Second input from the encoder.
- Reset: Resets the pulse count and all outputs of the HDL module (active low).
Outputs:
- Encoder_pos: A 9-bit output that, depending on the number of pulses given by the encoder and its direction, marks a count from 0 to the maximum number of encoder pulses. Users can change the number of pulses by modifying the NUM_OF_PULSES parameter in the Verilog module. By default, the module was designed for use with encoders of 500 pulses.
- Dir_out: A 2-bit output that indicates the direction of rotation based on how the A and B inputs are connected. It will display '01' in one direction and '10' in the opposite direction. Upon system startup and after a reset, it is set to '00' (initialization condition).
- Zero: A 1-bit output that marks a pulse each time the encoder returns to the initial reference point of the internal count, indicating that it has reached the starting position
again after a reset.
Verilog instantiation:
encoder encoder_inst (
.A(A_sig) , // input A_sig .B(B_sig) , // input B_sig
.reset(reset_sig) , // input reset_sig
.encoder_pos(encoder_pos_sig) , // output [8:0] encoder_pos_sig .dir_out(dir_out_sig) , // output [1:0] dir_out_sig
.zero(zero_sig) // output zero_sig
);
Hardware connection:
Module(Motor_encoder.v):
//////////////////////////////////////////////////////////////////////////////////
// Company: Universidad Pontificia Bolivariana Seccional Bucaramanga, Semillero ADT
// Engineer: Holguer A Becerra
//
// Create Date: 18:07:43 03/11/2010
// Design Name:
// Module Name: Encoder
// Project Name:
// Target Devices:
// Tool versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module encoder(A,B,reset,encoder_pos,dir_out,zero);
// numero de pulsos del encoder
parameter NUM_OF_PULSES=9'd500;
input A;// Entrada A del Encoder
input B;// Entrada B del Encoder
input reset;
output [8:0]encoder_pos;// salida del encoder respecto al punto de iniciacion
output [1:0]dir_out;// direccion para donde va el rotor del encoder, dependiendo de la conexion A y B, cuando se inicia es 0, y 2 o 1 de acuerdo al sentido de giro
output zero;// Dependiendo del punto de iniciacion, esta salida marca un pulso si pasa nuevamente por la posicion inicial
reg [8:0]counter=9'd0;
reg [1:0]dir_out_temp=2'd0;
assign dir_out[1:0]=dir_out_temp[1:0];
assign encoder_pos[8:0]=counter[8:0];
// paso por zero
assign zero=(counter==9'd0);
// conteo de pulsos
always@(posedge A,negedge reset)
begin
if(!reset)
begin
counter<=9'd0;
end
else
begin
if(B)
begin
if(counter==9'd0)counter<=NUM_OF_PULSES-1'b1;
else counter<=counter-9'd1;
end
else
begin
if(counter==(NUM_OF_PULSES-1'b1))counter<=9'd0;
else counter<=counter+9'd1;
end
end
end
// sentido de giro
always@(posedge A, negedge reset)
begin
if(!reset)
begin
dir_out_temp<=2'd0;
end
else
begin
if(B)
begin
dir_out_temp<=2'b01;
end
else
begin
dir_out_temp<=2'b10;
end
end
end
endmodule