Realization of FPGA Rotary ENCODER

Source: Internet
Author: User

Module Pmodenc (
Class
Rst_n,
A
B
BTN,
A_debounce,
B_debounce,
Btn_debounce,
Rotary_right,
Rotary_left
Led
);

Input wire clk,rst_n;
Input wire a,b,btn;
Output wire a_debounce,b_debounce,btn_debounce;
Output wire rotary_right,rotary_left;
Output wire [5:0]led;

Reg CLK_10MS;
reg [31:0]count;

Reg A_REG,A_REG0;
Reg B_REG,B_REG0;
Reg BTN_REG,BTN_REG0;
Wire a_debounce;
Wire b_debounce;
Wire btn_debounce;

Reg A_debounce_reg;
Reg B_debounce_reg;
Wire A_pos,a_neg;
Wire B_pos,b_neg;

Reg Rotary_right;
Reg Rotary_left;

Reg Rotary_right_reg,rotary_left_reg;
Wire Rotary_right_pos,rotary_left_pos;
Wire rotary_event;

reg [5:0]shift_d;

[Email protected] (Posedge Clk,negedge rst_n) begin
if (!rst_n) begin
Count <= 0;
clk_10ms <= 1 ' b0;
End
ELSE begin
if (Count < d24_999) begin//10ms Shake, 25MCLK
Count <= count + 1 ' B1;
clk_10ms <= 1 ' b0;
End
ELSE begin
Count <= 0;
clk_10ms <= 1 ' B1;
End
End
End

[Email protected] (Posedge Clk,negedge rst_n) begin
if (!rst_n) begin
A_reg <= 1 ' B1;
A_reg0 <= 1 ' B1;
B_reg <= 1 ' B1;
B_reg0 <= 1 ' B1;
Btn_reg <= 1 ' b0;
Btn_reg0 <= 1 ' b0;
End
ELSE begin
if (clk_10ms) begin
A_reg <= A;
A_reg0 <= A_reg;
B_reg <= B;
B_reg0 <= B_reg;
Btn_reg <= BTN;
Btn_reg0 <= Btn_reg;
End
End
End

Assign a_debounce = a_reg0 && a_reg && A;
Assign b_debounce = b_reg0 && b_reg && B;
Assign btn_debounce = btn_reg0 && btn_reg && btn;//make pulse rising edge after shaking

[Email protected] (Posedge Clk,negedge rst_n) begin
if (!rst_n) begin
A_debounce_reg <= 1 ' B1;
B_debounce_reg <= 1 ' B1;
End
ELSE begin
A_debounce_reg <= a_debounce;
B_debounce_reg <= b_debounce;
End
End

Assign A_pos =! A_debounce_reg && a_debounce;
Assign B_pos =! B_debounce_reg && b_debounce;

Assign A_neg = A_debounce_reg &&! A_debounce;
Assign B_neg = B_debounce_reg &&! B_debounce;

[Email protected] (Posedge Clk,negedge rst_n) begin
if (!rst_n) begin
Rotary_right <= 1 ' B1;
Rotary_left <= 1 ' B1;
End
ELSE begin
if (A_pos &&!) B_debounce) begin//a If B is low, the rotary encoder is turned to the right
Rotary_right <= 1 ' B1;
End

if (A_pos && b_debounce) begin//a rising edge when B is low, the rotary encoder turns left
Rotary_left <= 1 ' B1;
End

if (A_neg && b_debounce) begin//a The falling Edge B is high, turn right to end
Rotary_right <= 1 ' b0;
End

if (A_neg &&!) b_debounce) begin//a The falling edge of B is low, then turn to the left end
Rotary_left <= 1 ' b0;
End
End
End

Assign rotary_left = Rotary_left;
Assign rotary_right = Rotary_right;
[Email protected] (Posedge Clk,negedge rst_n) begin
if (!rst_n) begin
Rotary_right_reg <= 1 ' B1;
Rotary_left_reg <= 1 ' B1;
End
ELSE begin
Rotary_right_reg <= rotary_right;
Rotary_left_reg <= Rotary_left;
End
End

Assign Rotary_right_pos =!rotary_right_reg && rotary_right;
Assign Rotary_left_pos =!rotary_left_reg && rotary_left;//Shake

Assign rotary_event = Rotary_right_pos | | rotary_left_pos;//rotation flag bit

[Email protected] (Posedge Clk,negedge rst_n) begin
if (!rst_n)
Shift_d <= 6 ' b111_110;
else if (rotary_event) begin
if (Rotary_right_pos)
Shift_d <= {shift_d[0],shift_d[5:1]};
if (Rotary_left_pos)
Shift_d <= {shift_d[4:0],shift_d[5]};
End
End

Assign LED = left-to-right test module for shift_d;//lamp

Endmodule

Principle, you can refer to the http://www.eeboard.com/bbs/forum.php?mod=viewthread&tid=45184&highlight=%E5%B0%8F%E8%84 of this great God. %9a%e4%b8%ab

Realization of FPGA Rotary ENCODER

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.