Go Down

### Topic: Need Help 4-bit Binary Counter With Parallel Load (Read 5020 times)previous topic - next topic

#### iwanna4get

##### Feb 05, 2013, 04:52 pmLast Edit: Feb 05, 2013, 10:42 pm by iwanna4get Reason: 1
So i got the binary counter working. Now I just have to take and input (Load = HIGH) and if i turn on count it will continue counting from there..,

So i was thinking that i was thinking that I would take the 4 bit input then turn it to decimal and give the value to count.. is there anyway to do this?

I was thinking putting the HIGH or LOW values in an array or concatenate each input value and convert it to decimal.

If so how to convert from int to string, string to bit and then from string to int.

#### marco_c

#1
##### Feb 06, 2013, 03:14 am
Maybe it is me, but I have no idea what you are talking about.

Can you post your circuit and any code you may have so we can see what you are trying to do. I have a feeling that you are overcomplicating something here.
Arduino Libraries https://github.com/MajicDesigns?tab=Repositories
Parola for Arduino https://github.com/MajicDesigns/Parola
Arduino++ blog https://arduinoplusplus.wordpress.com

#### Grumpy_Mike

#2
##### Feb 06, 2013, 09:21 am
No it is not just you, no idea what the problem is.
I do know you should not be using a string.
I suspect it might be a four digit counter and not a four bit one.

#### iwanna4get

#3
##### Feb 06, 2013, 11:51 amLast Edit: Feb 06, 2013, 11:56 am by iwanna4get Reason: 1
Ok first I got a binary counter program in arduino (with the help of some arduino topics),
The Code(I'm pretty sure your familiar with this):
Code: [Select]
`const int led[4] = {2,3,4,5};int pin;int counting;void setup(){   for (pin =0; pin < 4; pin++ )   {     pinMode (led[pin], OUTPUT);   }   }void loop(){ for ( counting = 0; counting < 16; counting++) {    LEDview();    delay (1000); // The One second delay }}void LEDview(){  digitalWrite (led[3], boolean ((counting & 8)>>3));  digitalWrite (led[2], boolean ((counting & 4)>>2));  digitalWrite (led[1], boolean ((counting & 2)>>1));  digitalWrite (led[0], boolean (counting & 1));}`

Ok now i will add 7 inputs :

bit0
bit1
bit2
bit3

bits 0-3 will represent a four binary bit input

Count
CLR

if load is high digital read the values of bit0-3 and output them on output (which is the led[4] in the program)
Now if we turn load to low , maintain the value last known.
if count is high, count from the last value (example: last input 5(decimal) - 0101(binary) and then i turn count on it will continue binary counting from 5(0101))
if clear is high all output is low

Oh and this is the circuit in which I am basing the program on.

http://www.expertcore.org/viewtopic.php?style=4&f=27&t=783

#### Grumpy_Mike

#4
##### Feb 06, 2013, 07:03 pm
Quote
Oh and this is the circuit in which I am basing the program on.

So it is not a real circuit but an FPGA.

Quote
Now if we turn load to low , maintain the value last known.
if count is high, count from the last value (example: last input 5(decimal) - 0101(binary) and then i turn count on it will continue binary counting from 5(0101))
if clear is high all output is low

There is nothing in that code that does anything with the circuit. All it does is output your count in binary to the 4 outputs.

I am still confused as to why you need an external 4 bit counter at all. Simply output the count to LEDs attached to the pin outputs.

If you want to clock that into the counter then pulse the load line at the end of the LEDview() function.

#### iwanna4get

#5
##### Feb 07, 2013, 03:16 pm
This is what I got. I haven't tested it out cause it's late and I don't have enough switches at the moment

Code: [Select]
`int in1 = 2;int in2 = 3;int in3 = 4;int in4 = 5;int LD = 6;int CNT = 7;int CLR = 8;int out1 = 9;int out2 = 10;int out3 = 11;int out4 = 12;int result = 0;int counting = 0;int counting2 = 0;void setup(){  pinMode (in1, INPUT);  pinMode (in2, INPUT);  pinMode (in3, INPUT);  pinMode (in4, INPUT);  pinMode (LD, INPUT);  pinMode (CNT, INPUT);  pinMode (CLR, INPUT);  pinMode (out1, OUTPUT);  pinMode (out2, OUTPUT);  pinMode (out3, OUTPUT);  pinMode (out4, OUTPUT);}void loop(){     while(CLR = 0){      if(LD = 1){         int j = digitalRead(in1);         int k = digitalRead(in2);         int l = digitalRead(in3);         int m = digitalRead(in4);         digitalWrite (out1, j);         digitalWrite (out2, k);         digitalWrite (out3, l);         digitalWrite (out4, m);         counting2 = LOAD(j, k, l, m);      }else if(CNT = 1){         COUNT();      }else{         digitalWrite (out1, LOW);         digitalWrite (out2, HIGH);         digitalWrite (out3, HIGH);         digitalWrite (out4, LOW);      }  }}int LOAD (int a, int b, int c, int d){  int sum = 0;  if(a = 1){    sum = sum + 1;  }else{    sum = sum;  }  if(b = 1){    sum = sum + 2;  }else{    sum = sum;  }  if(c = 1){    sum = sum + 4;  }else{    sum = sum;  }  if(d = 1){    sum = sum + 8;  }else{    sum = sum;  }  return sum;}void COUNT(){  for ( counting = counting2; counting < 16; counting++) {    LEDview();    delay (1000); // The One second delay }}void LEDview(){  digitalWrite (out4, boolean ((counting & 8)>>3));  digitalWrite (out3, boolean ((counting & 4)>>2));  digitalWrite (out2, boolean ((counting & 2)>>1));  digitalWrite (out1, boolean (counting & 1));}void CLEAR(){  digitalWrite(out4, LOW);  digitalWrite(out3, LOW);  digitalWrite(out2, LOW);  digitalWrite(out1, LOW);}`

#### Grumpy_Mike

#6
##### Feb 07, 2013, 03:47 pm
Well I can't follow what you are trying to do but one thing to watch is all those if statements with only a single equals sign in then are wrong. You need a double equals if you are going to compare things.
Rather than just presenting the code it also helps if you say what it is trying to accomplish.

#### iwanna4get

#7
##### Feb 08, 2013, 12:55 am
I was sure those single = signs were double (==) yesterday ._. must have been more tired than I thought
Code: [Select]
`/* set the pins */int in1 = 2;int in2 = 3;int in3 = 4;int in4 = 5;int LD = 6;int CNT = 7;int CLR = 8;int out1 = 9;int out2 = 10;int out3 = 11;int out4 = 12;int result = 0;int counting = 0;int counting2 = 0;void setup(){  pinMode (in1, INPUT);  pinMode (in2, INPUT);  pinMode (in3, INPUT);  pinMode (in4, INPUT);  pinMode (LD, INPUT);  pinMode (CNT, INPUT);  pinMode (CLR, INPUT);  pinMode (out1, OUTPUT);  pinMode (out2, OUTPUT);  pinMode (out3, OUTPUT);  pinMode (out4, OUTPUT);}void loop(){     while(CLR == 0){ /* while CLR is LOW do this */     /* if LOAD is HIGH read the inputs from in1-4 and give it to out1-4*/      if(LD == 1){          int j = digitalRead(in1);         int k = digitalRead(in2);         int l = digitalRead(in3);         int m = digitalRead(in4);         digitalWrite (out1, j);         digitalWrite (out2, k);         digitalWrite (out3, l);         digitalWrite (out4, m);         /*Get the value for count*/         counting2 = LOAD(j, k, l, m);      }else if(CNT == 1){ /* if count is HIGH do COUNT()*/         COUNT();      }else{ /*for error checking */         digitalWrite (out1, LOW);         digitalWrite (out2, HIGH);         digitalWrite (out3, HIGH);         digitalWrite (out4, LOW);      }  }  /*when CLR is HIGH*/  digitalWrite(out4, LOW);  digitalWrite(out3, LOW);  digitalWrite(out2, LOW);  digitalWrite(out1, LOW);}/* this function will compute for the decimal value of the 4 bit inputand give it to sum*/int LOAD (int a, int b, int c, int d){  int sum = 0;  if(a = 1){    sum = sum + 1;  }else{    sum = sum;  }  if(b = 1){    sum = sum + 2;  }else{    sum = sum;  }  if(c = 1){    sum = sum + 4;  }else{    sum = sum;  }  if(d = 1){    sum = sum + 8;  }else{    sum = sum;  }  return sum;}void COUNT(){  /*this is the binary counter program.counting is set to be = to counting2 since we would like to start from the last know data*/  for ( counting = counting2; counting < 16; counting++) {    LEDview();    delay (1000); // The One second delay }}void LEDview(){  digitalWrite (out4, boolean ((counting & 8)>>3));  digitalWrite (out3, boolean ((counting & 4)>>2));  digitalWrite (out2, boolean ((counting & 2)>>1));  digitalWrite (out1, boolean (counting & 1));}`

#### Grumpy_Mike

#8
##### Feb 08, 2013, 09:13 pm
You are still using single = in that last code you posted
Code: [Select]
`/* this function will compute for the decimal value of the 4 bit inputand give it to sum*/int LOAD (int a, int b, int c, int d){  int sum = 0;  if(a = 1){    sum = sum + 1;  }else{    sum = sum;  }  if(b = 1){    sum = sum + 2;  }else{    sum = sum;  }  if(c = 1){    sum = sum + 4;  }else{    sum = sum;  }  if(d = 1){    sum = sum + 8;  }else{    sum = sum;  }  return sum;}`

However that code is rubbish anyway, try
Code: [Select]
`/* this function will compute for the decimal value of the 4 bit inputand give it to sum*/int LOAD (int a, int b, int c, int d){  int sum = d<<3 | c<<2 | b<<1 | a; return sum;`

Or if you don't understand that try
Code: [Select]
`/* this function will compute for the decimal value of the 4 bit inputand give it to sum*/int LOAD (int a, int b, int c, int d){  int sum = d*8 + c*4 + b*2 + a;  return sum;`
Hint:- anything multiplied by zero is zero, no IFs are needed

I am still waiting for an explanation about what you think the code is doing.

#### iwanna4get

#9
##### Feb 09, 2013, 02:29 amLast Edit: Feb 09, 2013, 02:32 am by iwanna4get Reason: 1
I have this circuit and gonna turn it to code

Quote

Code: [Select]
`/* this function will compute for the decimal value of the 4 bit inputand give it to sum*/int LOAD (int a, int b, int c, int d){  int sum = d*8 + c*4 + b*2 + a;  return sum;`
Hint:- anything multiplied by zero is zero, no IFs are needed

And Thanks Grumpy Mike, you are so right about the LOAD function

#### iwanna4get

#10
##### Feb 09, 2013, 06:14 amLast Edit: Feb 09, 2013, 06:28 am by iwanna4get Reason: 1
Finally got it to work and do what I wanted it to do.
Here's the code. (It's too long I know there is probably a way to make this shorter)
Code: [Select]
`int LD = 6;int swtch = 7;int CLR = 8;const int led[4] = {9,10,11,12};const int in[4] = {2,3,4,5};int counting, counting2, pin, x, y, z, h, j, k, l ;void setup(){  for (pin =0; pin < 4; pin++ )   {     pinMode (led[pin], OUTPUT);   }   for (pin =0; pin < 4; pin++ )   {     pinMode (in[pin], INPUT);   }  pinMode(swtch, INPUT);  pinMode(CLR,INPUT);  pinMode(LD, INPUT);}void loop(){  x = digitalRead(swtch);  y = digitalRead(CLR);  z = digitalRead(LD);  h = digitalRead(in[0]);  j = digitalRead(in[1]);  k = digitalRead(in[2]);  l = digitalRead(in[3]);  while(x==1){     for ( counting = counting2; counting < 16; counting++)         {            x = digitalRead(swtch);           if(x==1){            LEDview();            delay(1000);           }else{           break;           }          if (counting2 == 15){                counting2 = 0;             }           else{            counting2++;          }         }  }    if(y == 1){    CLEAR();    counting2 = 0;  }else if(z == 1){    digitalWrite(led[0], h);    digitalWrite(led[1], j);    digitalWrite(led[2], k);    digitalWrite(led[3], l);    counting2 = LOAD(h,j,k,l);  }            }void LEDview(){  digitalWrite (led[3], boolean ((counting & 8)>>3));  digitalWrite (led[2], boolean ((counting & 4)>>2));  digitalWrite (led[1], boolean ((counting & 2)>>1));  digitalWrite (led[0], boolean (counting & 1));}int LOAD (int a, int b, int c, int d){  int sum = a*1 + b*2 + c*4 + d*8;  return sum;}void CLEAR() {  for (pin =0; pin < 4; pin++ )   {     digitalWrite(led[pin], LOW);   }}`

#### pwillard

#11
##### Feb 12, 2013, 12:43 pm
And... isn't that FPGA design damn close to a BCD preset 74HC192 counter?

#### Docedison

#12
##### Feb 12, 2013, 03:46 pm
@pwillard.. You got a new crystal ball?
--> WA7EMS <--
"The solution of every problem is another problem." -Johann Wolfgang von Goethe
I do answer technical questions PM'd to me with whatever is in my clipboard

Go Up