# want to design a digital circuit of FOR loop

#### kalyanim

Joined Aug 28, 2018
8
I have to read data from memory and perform some operations for N number of times. I wrote a for loop in software for that but I don't understand how to design that in hardware.

I want to know how the counter circuit should be to count 1 2 3 2 1 in 5 cycles
can you help me in solving this
Code:
count=0;
for i=1:5
if i<=3
count =count+1;
else
count=count-1;
end
end
please help me to get hardware for this in form of flipflops or something

Mod edit: added code tags

#### MrChips

Joined Oct 2, 2009
19,774
Is this homework?

#### kalyanim

Joined Aug 28, 2018
8
Is this homework?
no part of my project. i need to know this so that i can move to design the circuit in the loop

#### MrChips

Joined Oct 2, 2009
19,774
Is the project part of a course assignment?

#### kalyanim

Joined Aug 28, 2018
8

#### MrChips

Joined Oct 2, 2009
19,774
That is still classified as homework.

#### kalyanim

Joined Aug 28, 2018
8
That is still classified as homework.
so what do you have an answer this is not class assignment this is my project in which I have a question on how to design a circuit

#### MrChips

Joined Oct 2, 2009
19,774
Does it always have to count in 1, 2, 3, 2, 1, and no other sequence?

#### kalyanim

Joined Aug 28, 2018
8
Does it always have to count in 1, 2, 3, 2, 1, and no other sequence?
it has to count 1 2 3 2 1, for now, it depends on my N value, for now, I am considering N=3 if I know how to do this I will consider for N

#### MrChips

Joined Oct 2, 2009
19,774
You have to lay down your specifications up front.

Ok, if N = 3 maybe we can do it one way.
What happens if N = 1000? Can we do it the same way?

What is the range of N?

#### kalyanim

Joined Aug 28, 2018
8
You have to lay down your specifications up front.

Ok, if N = 3 maybe we can do it one way.
What happens if N = 1000? Can we do it the same way?

What is the range of N?
I just want to know the circuit using flipflops on how to do that for N=3 so that i get a hint on how to do count in hardware
in my project N can be 512 1024 2048 depending on my input

#### -live wire-

Joined Dec 22, 2017
901
I have to read data from memory and perform some operations for N number of times. I wrote a for loop in software for that but I don't understand how to design that in hardware.

I want to know how the counter circuit should be to count 1 2 3 2 1 in 5 cycles
can you help me in solving this
Code:
count=0;
for i=1:5
if i<=3
count =count+1;
else
count=count-1;
end
end
please help me to get hardware for this in form of flipflops or something

Mod edit: added code tags
What language is that? If it's supposed to be some form of C, even if it's more like pseudocode, the syntax makes it very hard to understand. So what is the 5 in the for loop? Less than? Less than or = to? != to?

#### kalyanim

Joined Aug 28, 2018
8
What language is that? If it's supposed to be some form of C, even if it's more like pseudocode, the syntax makes it very hard to understand. So what is the 5 in the for loop? Less than? Less than or = to? != to?
that is code in Matlab
5 is just number of time loop will run. so I want the count to be 1 2 3 2 1
in hardware, it should be 2-bit count value 01 10 11 10 01

Joined Mar 10, 2018
3,725
Do you know Verilog ? Thats a way of producing a HW realization out
of code.

There is another method, use of a GP logic engine. Here is an example
of that -

http://www.cypress.com/video-library/PSoC/psoc-creator-tutorial-using-udb-editor-part-1/107946

Watch the 6 videos, ignore you have no background just watch. This is called UDB editor and
datapath wizard. Videos are short, 10 - 15 min each.

PSOC can also allow you to do this from scratch using schematic capture and
basic logic elements and counter/timer/pwm/comparator......

Regard, Dana,

#### kalyanim

Joined Aug 28, 2018
8

#### -live wire-

Joined Dec 22, 2017
901
that is code in Matlab
5 is just number of time loop will run. so I want the count to be 1 2 3 2 1
in hardware, it should be 2-bit count value 01 10 11 10 01
Oh.

#### WBahn

Joined Mar 31, 2012
24,981
so what do you have an answer this is not class assignment this is my project in which I have a question on how to design a circuit
If it is part of anything that is course related, then the principle goal is for you to learn something as opposed to getting something that works. Therefore the help that we provide is aimed primarily at helping you learn how to solve the problem, not just spoon-feeding you a solution. The best way to help you learn something is for you to show your best attempt, however far it gets, so that we can see what you are doing right, what you are doing wrong, and where your thinking appears to be headed. Then we can help you find your mistake or see the next step along the path.

#### WBahn

Joined Mar 31, 2012
24,981
that is code in Matlab
5 is just number of time loop will run. so I want the count to be 1 2 3 2 1
in hardware, it should be 2-bit count value 01 10 11 10 01
Now you have conflicting specs. You previously said that in your project N could be values like 512 or even higher. But now you are saying in hardware it should be a 2-bit count.

Your solution in code is also not tied to the value N. So we don't know how N relates to that code. You have a hardcoded 5 and a hardcoded 3. Is the 5 really the value of N? If so, if N is 512 is the value 3 still 3?

We are not mind readers. You need to fully specify your problem. That's your first step (and often the hardest step) in being able to solve it.

#### WBahn

Joined Mar 31, 2012
24,981
When you say "in hardware", what do you mean? Are you going to build this up with in physical hardware using discrete logic chips? Is this in simulation only? In and FPGA or some other form of programmable logic part?