3 bit up counter verilog code

Thread Starter

vead

Joined Nov 24, 2011
629
someone help me to write verilog code for 3 bit up counter
N | A B C |next state
0 | 0 0 0 | 001
1 | 0 0 0 | 001
2 | 0 1 0 | 011
3 | 0 1 1 | 100
4 | 1 0 0 | 101
5 | 1 0 1 | 110
6 | 1 1 0 | 111
7 | 1 1 1 | 000

module up_counter (A,B,C,clk A' B' C')
input clk
output
reg 3:0
 
Last edited:

t06afre

Joined May 11, 2009
5,934
Vead this site has a lot of working examples. I suggest you take a look at it. http://www.csit-sun.pub.ro/courses/Masterat/Xilinx%20Synthesis%20Technology/toolbox.xilinx.com/docsan/xilinx4/data/docs/xst/hdlcode.html
If you still are stuck you are more than welcome to ask for help here. But at least try using google/youtube before asking for help in this forum. That is the purpose of this forum. It is a help to helping oneself. Not a private personal teacher
It is a lot good material on web regarding VHDL coding. Then it comes to programming you can learn a lot by looking at working code examples and understanding them. After that try to do some experiments with code.
 

Thread Starter

vead

Joined Nov 24, 2011
629
Vead this site has a lot of working examples. I suggest you take a look at it. http://www.csit-sun.pub.ro/courses/Masterat/Xilinx%20Synthesis%20Technology/toolbox.xilinx.com/docsan/xilinx4/data/docs/xst/hdlcode.html
If you still are stuck you are more than welcome to ask for help here. But at least try using google/youtube before asking for help in this forum. That is the purpose of this forum. It is a help to helping oneself. Not a private personal teacher
It is a lot good material on web regarding VHDL coding. Then it comes to programming you can learn a lot by looking at working code examples and understanding them. After that try to do some experiments with code.
thanks for reply I don't want to complete someone whole code. before posting on forum I googled for up counter verilog code and I saw lot of example because of my previous knowledge I know the basic table I have read ,before going to design hardware in verilog we need to know the function table I confused here how to write assignment statement for this counter
 

tshuck

Joined Oct 18, 2012
3,534
First, let's figure out the answer to my question in your other thread: is there a specific modelling technique you are required to use? From your recent string of threads, I would assume you are required to use structural, but confirmation would allow us to move along.
 

WBahn

Joined Mar 31, 2012
30,071
thanks for reply I don't want to complete someone whole code. before posting on forum I googled for up counter verilog code and I saw lot of example because of my previous knowledge I know the basic table I have read ,before going to design hardware in verilog we need to know the function table I confused here how to write assignment statement for this counter
Then perhaps a better place for you to start is to understand assignment statements in general.

Regardless, we need to see YOUR best attempt at a solution.
 

WBahn

Joined Mar 31, 2012
30,071
someone help me to write verilog code for 3 bit up counter
N | A B C |next state
0 | 0 0 0 | 001
1 | 0 0 0 | 001
2 | 0 1 0 | 011
3 | 0 1 1 | 100
4 | 1 0 0 | 101
5 | 1 0 1 | 110
6 | 1 1 0 | 111
7 | 1 1 1 | 000

module up_counter (A,B,C,clk A' B' C')
input clk
output
reg 3:0
That's a rather strange up counter, don't you think. What happens if it is in state 001?
 

Thread Starter

vead

Joined Nov 24, 2011
629
That's a rather strange up counter, don't you think. What happens if it is in state 001?
sorry that was wrong table It was my mistake
table for counter

N | A B C(current state) clk |next state
0 | 0 0 0 | ↑ 001
1 | 0 0 1 | ↑ 010
2 | 0 1 0 | ↑ 011
3 | 0 1 1 | ↑ 100
4 | 1 0 0 | ↑ 101
5 | 1 0 1 | ↑ 110
6 | 1 1 0 | ↑ 111
7 | 1 1 1 | ↑ 000
I try to write code but not sure that code is correct
Rich (BB code):
module up_counter(current state, next state ,clk)
          input current state ;
          input clk;
          output next state;
          reg 3:0
          always @ (posedge clk);
          begin 
          next state <= current state +1 ;
          end
         endmodule
 
Last edited:

WBahn

Joined Mar 31, 2012
30,071
Think about this code. Is that how a counter chip works? Do you have a bunch of input pins on which you have to provide a "current state" value and then on the rising clock edge the counter chip outputs what is on the input incremented by 1? And what does it put out on the output pins before the clock goes HI?

Remember, your HDL code has to describe the complete behavior of a piece of hardware. Don't make or expect the tool to guess what you want.
 

Thread Starter

vead

Joined Nov 24, 2011
629
Think about this code. Is that how a counter chip works? Do you have a bunch of input pins on which you have to provide a "current state" value and then on the rising clock edge the counter chip outputs what is on the input incremented by 1? And what does it put out on the output pins before the clock goes HI?

Remember, your HDL code has to describe the complete behavior of a piece of hardware. Don't make or expect the tool to guess what you want.
there is bunch of input and output so i confused how to declare the input and output port can you tell me how to declare
 

Brownout

Joined Jan 10, 2012
2,390
sorry that was wrong table It was my mistake
table for counter

N | A B C(current state) clk |next state
0 | 0 0 0 | ↑ 001
1 | 0 0 1 | ↑ 010
2 | 0 1 0 | ↑ 011
3 | 0 1 1 | ↑ 100
4 | 1 0 0 | ↑ 101
5 | 1 0 1 | ↑ 110
6 | 1 1 0 | ↑ 111
7 | 1 1 1 | ↑ 000
I try to write code but not sure that code is correct
Rich (BB code):
module up_counter(current state, next state ,clk)
          input current state ;
          input clk;
          output next state;
          reg 3:0
          always @ (posedge clk);
          begin 
          next state <= current state +1 ;
          end
         endmodule
That's pretty how it works. I will only say we typically don't use "state" as a variable when coding counters. We usually reserve those vairable names for actual state machines. You can make the counter a state machine, but what you have here is the way it's almost always done, and the way it should be done. Also, you want to just increment the value, you don't need the extra vairable you have defined as "next." So, use "inout" for the port direction for the variable.

As you progress in digital design, you'll make state machines, where the state/next state idea from your charts will be used directly in your code.

You have a couple syntax errors, you'll find them when you attempt to compile your code.
 
Last edited:

WBahn

Joined Mar 31, 2012
30,071
That's pretty how it works. I will only say we typically don't use "state" as a variable when coding counters. We usually reserve those vairable names for actual state machines. You can make the counter a state machine, but what you have here is the way it's almost always done, and the way it should be done. Also, you want to just increment the value, you don't need the extra vairable you have defined as "next." So, use "inout" for the port direction for the variable.

As you progress in digital design, you'll make state machines, where the state/next state idea from your charts will be used directly in your code.

You have a couple syntax errors, you'll find them when you attempt to compile your code.
I'm pretty sure he has just three signals, current_state, next_state, and clk.

I don't follow your assertion that this is how a counter should be done. When you use a counter, don't you expect the counter to keep track of what the count is? And shouldn't the value of the output be defined for the time before the clock's rising edge?
 

Brownout

Joined Jan 10, 2012
2,390
Not sure what you're refering to. Here's what I'm talking about.


Rich (BB code):
module counter( input clk, inout reg [2:0] count)
 
...
 
count <= count + 1;
 
...
 
endmodule
 

Thread Starter

vead

Joined Nov 24, 2011
629
If we know the function table of any logic circuit then we can write code in verilog but here is I am totally confused I don't know how to declare port and how to make loop
 

WBahn

Joined Mar 31, 2012
30,071
Why do you need a loop? You functional description says, "Upon seeing a rising edge on the clock, if in this state go to that state." No loop.
 

Thread Starter

vead

Joined Nov 24, 2011
629
hello again my aim is to find out input and output for counter so that I can write the verilog code Now I made the present state and next state table than What i do next i think I need to drive the K map for present state help me please
 

tshuck

Joined Oct 18, 2012
3,534
hello again my aim is to find out input and output for counter so that I can write the verilog code Now I made the present state and next state table than What i do next i think I need to drive the K map for present state help me please
Vead,
Please take some time and look at a generic counter circuit.

How many inputs to the counter do you see? And how many outputs?

The flip flops are a part of the counter, they aren't inputs and their inputs are not necessarily the inputs to the counter.
 
Top