Following code may be useful for students or enthusiasts who are trying to design a voting machine. It has been successfully simulated as well as implemented on Spartan 3 xc3s400 FPGA. If you are having any doubts regarding following code then post a comment. I will try my best to answer the questions.
`timescale 1ns / 1ps ////////////////////////////////////////////////////////////////////////////////// // Engineer: Chaitannya Supe // Create Date: 09:31:19 03/08/2015 // Design Name: Electronic Voting Machine // Module Name: evm // Target Devices: Xilinx Spartan 3 XCS400-PQ208 // Tool versions: Xilinx ISE 12.2, 14.7 // Description: This is simple Voting machine code in which total 3 partys were considered. // A voter can vote to any of the parties only one at a time. If a voter votes two parties // at a time then that vote will be invalid. This code has been successfully simulated as well as // implemented on Spartan 3 xc3s400-4pq208 device. // // Additional Comments: To register a vote, operate a voter_switch and then press push button to register // a vote. This operation was chosen to simplify a coding and get better understanding // of a voting machine on FPGA. ////////////////////////////////////////////////////////////////////////////////// module evm(clk,voter_switch,PB,voting_en,opled,invalid,dout); input voting_en,PB,clk;//voting process will start when vote_en is on input [2:0]voter_switch; output [6:0]dout;//Max no. of votes = 127 output reg [2:0]opled;//opled[0]=party1 led, opled[1]=party2 led, opled[2]=party3 led output reg invalid;//invalid vote indicator led //counters to count each party votes reg [6:0]cnt_reg1=0;//party1 reg [6:0]cnt_nxt1=0;//party1 reg [6:0]cnt_reg2=0;//party2 reg [6:0]cnt_nxt2=0;//party2 reg [6:0]cnt_reg3=0;//party3 reg [6:0]cnt_nxt3=0;//party3 reg PB_reg1; reg PB_reg2; reg [15:0] PB_cnt; reg PB_state; //debounce circuit to detect only one rising edge of push button PB(Refer http://www.fpga4fun.com/Debouncer2.html for more information on debounce circuit) always @(posedge clk) PB_reg1 <= PB; always @(posedge clk) PB_reg2 <= PB_reg1; wire PB_idle = (PB_state==PB_reg2); wire PB_cnt_max = &PB_cnt; // true when all bits of PB_cnt are 1's always @(posedge clk) if(PB_idle) PB_cnt <= 0; // idle state i.e. PB_cnt will not increment else begin PB_cnt <= PB_cnt + 16'd1; if(PB_cnt_max) PB_state <= ~PB_state; // if the counter is maximum, PB changes end assign PB_down = ~PB_idle & PB_cnt_max & ~PB_state; //counter for party1 votes always@(posedge PB_down) if(voter_switch == 3'b001 && voting_en == 1'b1) begin cnt_reg1 <= cnt_nxt1; end always@(*) begin cnt_nxt1 = cnt_reg1 + 1; end //Counter for party2 votes always@(posedge PB_down) if(voter_switch == 3'b010 && voting_en == 1'b1) begin cnt_reg2 <= cnt_nxt2; end always@(*) begin cnt_nxt2 = cnt_reg2 + 1; end //Counter for party3 votes always@(posedge PB_down) if(voter_switch == 3'b100 && voting_en == 1'b1) begin cnt_reg3 <= cnt_nxt3; end always@(*) begin cnt_nxt3 = cnt_reg3 + 1; end //Final count i.e. total number of votes assign dout = cnt_reg1 + cnt_reg2 + cnt_reg3; //relation of "voter_switch" with "opled" & "invalid" always@(*) if(voting_en) case(voter_switch) 3'b100 : begin opled = 3'b100; invalid = 1'b0; end 3'b010 : begin opled = 3'b010; invalid = 1'b0; end 3'b001 : begin opled = 3'b001; invalid = 1'b0; end 3'b011 : begin opled = 3'b000; invalid = 1'b1; end 3'b110 : begin opled = 3'b000; invalid = 1'b1; end 3'b101 : begin opled = 3'b000; invalid = 1'b1; end 3'b111 : begin opled = 3'b000; invalid = 1'b1; end 3'b000 : begin opled = 3'b000; invalid = 1'b0; end default : begin opled = 3'b000; invalid = 1'b0; end endcase endmodule
good job..
ReplyDeleteThanks
ReplyDeletenice job
ReplyDeleteGood explanation, helpful for beginners to verilog and FPGA
ReplyDeleteGood explanation, helpful for beginners to verilog and FPGA
ReplyDeletecode is working perfectly. can u provide testbench for it
ReplyDeleteThanks..Currently i am not having testbench code but i will post it sooner with proper explanation.
ReplyDeletecan you provide test bench please
DeleteYes please provide testbench sir
Deletecan you please explain the whole code?
ReplyDeleteHi, its already explained in the above code. However you can tell me which point you didn't understood.
Deletecan we run this code on any spartan kit or only the one you have mentioned?
ReplyDeleteYou can run it on any Xilinx FPGA as Verilog is common language for all. However for virtex and zynq series, behaviour may differ slightly because of different LUT structure they have. Personally i think it will support all other families(Virtex, Zynq etc)
DeletePB_cnt <= PB_cnt + 16'd1
ReplyDeletewhat does this do?
It is a part of a debounce circuit to detect only one high to low edge of push button. To know what debounce circuit is visit http://www.fpga4fun.com/Debouncer2.html.
Deletecnt_nxt3 = cnt_reg3 + 1
ReplyDeleteand this?
This is how we form a small state machine.
Deletehey when you prepared the counters for parties 1,2,3 why did you take 3'b100 for party 3 and not 3'b011
ReplyDelete3'b100 corresponds to three push buttons( for 3 parties). Note for rest of the cases votes will not be counted anymore.
Deleteand can you explain the case statement?
Deleteassign PB_down = ~PB_idle & PB_cnt_max & ~PB_state
Deleteand this indicates?
@JENNY: Even if i explain, you will not understand it unless you do it yourself. I suggest you to go through the basics (Verilog coding, state machines etc) and then post comments. If you need materials for basics i can provide you that. Contact me on email for that.
ReplyDeleteHi chaitannaya
ReplyDeleteDo u have state diagram for the above code..
Thank you
Unfortunately i don't have it. I prepared this code without any state diagrams just by thinking..
Deletei think on line 41 instead of PB_reg1 <= PB; there should be PB_reg1 <= ~PB; can you check it,because on the website that u have mentioned http://www.fpga4fun.com/Debouncer2.html it has PB_reg1 <=~PB; written there.
ReplyDeleteIt is because my FPGA board was having push to ON tactile switch rather than push to OFF. Hope it clears your doubt.
Deletecould you please explain me from line 34 to line 58 i am not able to understand the significance of these lines
ReplyDeleteThose lines of code belong to debounce circuit which is needed when you use tactile switches.
DeleteRefer http://www.fpga4fun.com/Debouncer2.html for more information on debounce circuit.
please provide me ur email id
ReplyDeletechetansupe@gmail.com
ReplyDeleteSir,for testbench dout is not giving an output wave. What we should give to PB as it depends on PB, others all are giving right output.The total debounce program based on dout right it is not working
ReplyDeletePlease reply for this if anyone know the solution
Hi, for testbench you can follow folling procedure:
Delete1. Remove debounce circuit code.
2. Change PB_down to PB.
3. Now provide pulses to PB as per your convenience.
Hope it helps. Let me know if this is the solution for your question.
Hi GOOD Work
ReplyDeleteCan I know
How to get the votes polled to the individual candidates who are contesting
You can have 3 dout instead of one.
DeleteSir, what is dout here?
ReplyDeleteDout gives total number of votes for all 3 parties.
DeleteDoes it run for quartus vlsi
ReplyDeleteThis code will run for quartus tool
ReplyDeleteCan u provide testbench for this program
ReplyDeleteyou have rtl schematic?
ReplyDeleteHi, you can generate the RTL schematic in the synthesis tool of Xilinx ISE directly.
DeleteCan u provide testbench plzz..
ReplyDeletecan u provide text bench also
ReplyDeletehow can we simulate the source code without test bench ?
ReplyDeleteif you have test bench for it please provide it.
You can simulate the code directly on FPGA board. Please read the description of the code for using it on board.
DeleteIf any one have testbench for this electronic voting machine using Verizon hdl please send to my mail
ReplyDeleteUsing verilog hdl
Deletedid you get the test bench?
Deleteif yes send it to me please
i also need the test bench please
DeleteI need the flow diagram and testbench of above code.. Can anyone help me with that?
DeleteWill you please provide fpga design flow for this
ReplyDeletecanu u provide ucf for this program
ReplyDeleteCan I use without any boarf
ReplyDeleteKindly provide your mail I'd have doubts.
ReplyDeleteThanks for a wonderful share. Your article has proved your hard work and experience you have got in this field. Brilliant .i love it reading.
ReplyDeleteelectronics
Thanks for posting this info. I just want to let you know that I just check out your site and I find it very interesting and informative. I can't wait to read lots of your posts.
ReplyDeleteonline electronic world
I want to run this on CPLD board, what modifications do i need to do
ReplyDelete